diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4ddce88..1026e82 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -29,4 +29,4 @@ jobs: version: master - name: Build tests - run: zig build -Drelease-small + run: zig build -Doptimize=ReleaseSmall diff --git a/build.zig b/build.zig index 86b4f48..304c3d7 100644 --- a/build.zig +++ b/build.zig @@ -10,7 +10,10 @@ const chips = microzig.chips; const Backing = microzig.Backing; pub fn build(b: *std.build.Builder) !void { - const mode = b.standardReleaseOptions(); + // Standard optimization options allow the person running `zig build -Doptimize=...` to select + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not + // set a preferred release mode, allowing the user to decide how to optimize. + const optimize = b.standardOptimizeOption(.{}); const test_step = b.step("test", "Builds and runs the library test suite"); @@ -52,11 +55,10 @@ pub fn build(b: *std.build.Builder) !void { b.fmt("test-{s}-{s}.elf", .{ tst.name, cfg.name }), tst.source, cfg.backing, - .{}, + .{.optimize = optimize }, ); if (filter == null or exe.inner.target.cpu_arch.? == filter.?) { - exe.inner.setBuildMode(mode); exe.inner.install(); test_step.dependOn(&exe.inner.step); diff --git a/src/main.zig b/src/main.zig index 289502f..ff4c6f9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -32,6 +32,7 @@ pub const BuildOptions = struct { // a hal package is a package with ergonomic wrappers for registers for a // given mcu, it's only dependency can be microzig hal_package_path: ?std.build.FileSource = null, + optimize: std.builtin.OptimizeMode = std.builtin.OptimizeMode.Debug, }; pub const EmbeddedExecutable = struct { @@ -56,10 +57,6 @@ pub const EmbeddedExecutable = struct { }); } - pub fn setBuildMode(exe: *EmbeddedExecutable, mode: std.builtin.Mode) void { - exe.inner.setBuildMode(mode); - } - pub fn install(exe: *EmbeddedExecutable) void { exe.inner.install(); } @@ -177,7 +174,7 @@ pub fn addEmbeddedExecutable( }; var exe = EmbeddedExecutable{ - .inner = builder.addExecutable(name, root_path ++ "core/microzig.zig"), + .inner = builder.addExecutable(.{ .name = name, .root_source_file = .{ .path = root_path ++ "core/microzig.zig" }, .target = chip.cpu.target, .optimize = options.optimize }), .app_packages = std.ArrayList(Pkg).init(builder.allocator), }; @@ -186,7 +183,6 @@ pub fn addEmbeddedExecutable( // might not be true for all machines (Pi Pico), but // for the HAL it's true (it doesn't know the concept of threading) exe.inner.single_threaded = true; - exe.inner.setTarget(chip.cpu.target); const linkerscript = LinkerScriptStep.create(builder, chip) catch unreachable; exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file });