From b34353101f545b24f6212382822ec2cc3680f6b2 Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sat, 4 Feb 2023 22:42:15 +0100 Subject: [PATCH 1/9] Updated to work with zig 0.11.0-dev.1570+693b12f8e --- src/main.zig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.zig b/src/main.zig index 289502f85..c750edad9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -57,7 +57,7 @@ pub const EmbeddedExecutable = struct { } pub fn setBuildMode(exe: *EmbeddedExecutable, mode: std.builtin.Mode) void { - exe.inner.setBuildMode(mode); + exe.inner.optimize = mode; } pub fn install(exe: *EmbeddedExecutable) void { @@ -177,7 +177,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" } }), .app_packages = std.ArrayList(Pkg).init(builder.allocator), }; @@ -186,7 +186,7 @@ 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); + exe.inner.target = chip.cpu.target; const linkerscript = LinkerScriptStep.create(builder, chip) catch unreachable; exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file }); From 17bb6ba83398b8d07ce9149e74e63dae20e5b38b Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sat, 4 Feb 2023 23:03:08 +0100 Subject: [PATCH 2/9] Updated build.zig to zig 0.11.0-dev.1570+693b12f8e --- build.zig | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/build.zig b/build.zig index 86b4f4855..c19e35668 100644 --- a/build.zig +++ b/build.zig @@ -10,7 +10,7 @@ const chips = microzig.chips; const Backing = microzig.Backing; pub fn build(b: *std.build.Builder) !void { - const mode = b.standardReleaseOptions(); + const optimize = b.standardOptimizeOption(.{}); const test_step = b.step("test", "Builds and runs the library test suite"); @@ -56,7 +56,7 @@ pub fn build(b: *std.build.Builder) !void { ); if (filter == null or exe.inner.target.cpu_arch.? == filter.?) { - exe.inner.setBuildMode(mode); + exe.inner.optimize = optimize; exe.inner.install(); test_step.dependOn(&exe.inner.step); From 07c8bc83fb44fba6920f1085e3c65cb830b4893c Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sat, 4 Feb 2023 23:12:16 +0100 Subject: [PATCH 3/9] Update build command --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 4ddce886a..1026e820d 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 From be84b39e57840d53f877181fbd3fdf0692fef051 Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sun, 5 Feb 2023 19:31:25 +0100 Subject: [PATCH 4/9] Add optimize mode as build option --- src/main.zig | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main.zig b/src/main.zig index c750edad9..c3edf860e 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 { From 3606341d4c163d47a8e185318f9493747c8b51f4 Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sun, 5 Feb 2023 19:37:28 +0100 Subject: [PATCH 5/9] Move target to invocation of addExecutable() --- src/main.zig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index c3edf860e..a1167fd8d 100644 --- a/src/main.zig +++ b/src/main.zig @@ -178,7 +178,7 @@ pub fn addEmbeddedExecutable( }; var exe = EmbeddedExecutable{ - .inner = builder.addExecutable(.{ .name = name, .root_source_file = .{ .path = root_path ++ "core/microzig.zig" } }), + .inner = builder.addExecutable(.{ .name = name, .root_source_file = .{ .path = root_path ++ "core/microzig.zig" }, .target = chip.cpu.target }), .app_packages = std.ArrayList(Pkg).init(builder.allocator), }; @@ -187,7 +187,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.target = chip.cpu.target; const linkerscript = LinkerScriptStep.create(builder, chip) catch unreachable; exe.inner.setLinkerScriptPath(.{ .generated = &linkerscript.generated_file }); From 00636501eb84d8a3f5235188847c1fbc834f389f Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sun, 5 Feb 2023 19:50:40 +0100 Subject: [PATCH 6/9] Removed code to set optimize, as its now a option --- build.zig | 3 --- 1 file changed, 3 deletions(-) diff --git a/build.zig b/build.zig index c19e35668..e06cc70e8 100644 --- a/build.zig +++ b/build.zig @@ -10,8 +10,6 @@ const chips = microzig.chips; const Backing = microzig.Backing; pub fn build(b: *std.build.Builder) !void { - const optimize = b.standardOptimizeOption(.{}); - const test_step = b.step("test", "Builds and runs the library test suite"); const BuildConfig = struct { name: []const u8, backing: Backing, supports_uart_test: bool = true }; @@ -56,7 +54,6 @@ pub fn build(b: *std.build.Builder) !void { ); if (filter == null or exe.inner.target.cpu_arch.? == filter.?) { - exe.inner.optimize = optimize; exe.inner.install(); test_step.dependOn(&exe.inner.step); From bf2d0ec5c58d4f1926301b9cf12405dc0d0aa48c Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Sun, 5 Feb 2023 19:55:31 +0100 Subject: [PATCH 7/9] Apply build/optimize mode --- src/main.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.zig b/src/main.zig index a1167fd8d..e12c53cd8 100644 --- a/src/main.zig +++ b/src/main.zig @@ -178,7 +178,7 @@ pub fn addEmbeddedExecutable( }; var exe = EmbeddedExecutable{ - .inner = builder.addExecutable(.{ .name = name, .root_source_file = .{ .path = root_path ++ "core/microzig.zig" }, .target = chip.cpu.target }), + .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), }; From 58c7f5dffa2a5b2a9ff1d1af4bb3e10587493872 Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Mon, 6 Feb 2023 12:08:31 +0100 Subject: [PATCH 8/9] Removed function to set build mode, as it's now an option --- src/main.zig | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main.zig b/src/main.zig index e12c53cd8..ff4c6f927 100644 --- a/src/main.zig +++ b/src/main.zig @@ -57,10 +57,6 @@ pub const EmbeddedExecutable = struct { }); } - pub fn setBuildMode(exe: *EmbeddedExecutable, mode: std.builtin.Mode) void { - exe.inner.optimize = mode; - } - pub fn install(exe: *EmbeddedExecutable) void { exe.inner.install(); } From 53930b0b764f5b33b2adb30cc97f406885b73661 Mon Sep 17 00:00:00 2001 From: Philipp Wendel Date: Mon, 6 Feb 2023 12:28:35 +0100 Subject: [PATCH 9/9] Add support for compiler flag `-Doptimize=` --- build.zig | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/build.zig b/build.zig index e06cc70e8..304c3d7f8 100644 --- a/build.zig +++ b/build.zig @@ -10,6 +10,11 @@ const chips = microzig.chips; const Backing = microzig.Backing; pub fn build(b: *std.build.Builder) !void { + // 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"); const BuildConfig = struct { name: []const u8, backing: Backing, supports_uart_test: bool = true }; @@ -50,7 +55,7 @@ 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.?) {