From 488b340cefa2b0bf00e8a0b3b82e8105600eabfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20=27vesim=27=20Kuli=C5=84ski?= Date: Fri, 26 Apr 2024 20:48:49 +0200 Subject: [PATCH] core: use special union for linker_script --- bsp/raspberrypi/rp2040/build.zig | 2 +- build/build.zig | 54 +++++++++++++++++++------------- 2 files changed, 33 insertions(+), 23 deletions(-) diff --git a/bsp/raspberrypi/rp2040/build.zig b/bsp/raspberrypi/rp2040/build.zig index 7b2e3c14..67a38c9b 100644 --- a/bsp/raspberrypi/rp2040/build.zig +++ b/bsp/raspberrypi/rp2040/build.zig @@ -119,7 +119,7 @@ pub const BootROM = union(enum) { }; const linker_script = .{ - .path = build_root ++ "/rp2040.ld", + .explicit = .{ .path = build_root ++ "/rp2040.ld" }, }; const hal = .{ diff --git a/build/build.zig b/build/build.zig index 526343ad..20f9e021 100644 --- a/build/build.zig +++ b/build/build.zig @@ -204,29 +204,33 @@ pub fn add_firmware( fw.artifact.bundle_compiler_rt = options.bundle_compiler_rt orelse fw.target.bundle_compiler_rt; - if (linker_script) |ls| { - fw.artifact.setLinkerScriptPath(ls); - } else { - const target = mz.host_build.resolveTargetQuery(chip.cpu.target); - const generate_linkerscript_args = GenerateLinkerscriptArgs{ - .cpu_name = target.result.cpu.model.name, - .cpu_arch = target.result.cpu.arch, - .chip_name = chip.name, - .memory_regions = chip.memory_regions, - }; + switch (linker_script) { + .explicit => |ls| { + fw.artifact.setLinkerScriptPath(ls); + }, + .generated => { + const target = mz.host_build.resolveTargetQuery(chip.cpu.target); + const generate_linkerscript_args = GenerateLinkerscriptArgs{ + .cpu_name = target.result.cpu.model.name, + .cpu_arch = target.result.cpu.arch, + .chip_name = chip.name, + .memory_regions = chip.memory_regions, + }; - const args_str = std.json.stringifyAlloc( - mz.microzig_core.builder.allocator, - generate_linkerscript_args, - .{}, - ) catch @panic("OOM"); + const args_str = std.json.stringifyAlloc( + mz.microzig_core.builder.allocator, + generate_linkerscript_args, + .{}, + ) catch @panic("OOM"); - const generate_linkerscript_run = mz.microzig_core.builder.addRunArtifact(mz.generate_linkerscript); - generate_linkerscript_run.addArg(args_str); - const linkerscript = generate_linkerscript_run.addOutputFileArg("linker.ld"); + const generate_linkerscript_run = mz.microzig_core.builder.addRunArtifact(mz.generate_linkerscript); + generate_linkerscript_run.addArg(args_str); + const linkerscript = generate_linkerscript_run.addOutputFileArg("linker.ld"); - // If not specified then generate the linker script - fw.artifact.setLinkerScript(linkerscript); + // If not specified then generate the linker script + fw.artifact.setLinkerScript(linkerscript); + }, + .none => {}, } if (options.target.configure) |configure| { @@ -317,7 +321,7 @@ pub const Target = struct { board: ?BoardDefinition = null, /// (optional) Provide a custom linker script for the hardware or define a custom generation. - linker_script: ?LazyPath = null, + linker_script: LinkerScript = .{ .generated = {} }, /// (optional) Further configures the created firmware depending on the chip and/or board settings. /// This can be used to set/change additional properties on the created `*Firmware` object. @@ -327,6 +331,12 @@ pub const Target = struct { binary_post_process: ?*const fn (host_build: *std.Build, LazyPath) std.Build.LazyPath = null, }; +pub const LinkerScript = union(enum) { + generated: void, + explicit: LazyPath, + none: void, +}; + /// Options to the `add_firmware` function. pub const FirmwareOptions = struct { /// The name of the firmware file. @@ -357,7 +367,7 @@ pub const FirmwareOptions = struct { board: ?BoardDefinition = null, /// If set, overrides the `linker_script` property of the target. - linker_script: ?LazyPath = null, + linker_script: ?LinkerScript = null, }; /// Configuration options for firmware installation.