Skip to content

Commit

Permalink
core: use special union for linker_script
Browse files Browse the repository at this point in the history
  • Loading branch information
vesim987 committed Apr 26, 2024
1 parent c1d82f7 commit 488b340
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion bsp/raspberrypi/rp2040/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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 = .{
Expand Down
54 changes: 32 additions & 22 deletions build/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 488b340

Please sign in to comment.