Hi,
I was trying to play around with optimizations done in different compilation optimization modes, like the fact that the compiler optimize away certain calls to std.debug.assert
.
But when I setup the project, I can’t find a way to use the command zig build -Doptimize=ReleaseFast run
with the run
step taking into account the executable generated with -Doptimize=ReleaseFast
.
I have a very little example to show my problem:
main.zig:
const std = @import("std");
pub fn main() !void {
positive(-1);
}
fn positive(i: i64) void {
std.debug.assert(i > 0);
}
build.zig
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const exe = b.addExecutable(.{
.name = "various",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
}
When I run zig build -Doptimize=ReleaseFast
and then manually executing it with zig-out/bin/various.exe
I have no error, showing that the call to assert
isn’t part of the exe.
But when I run zig build -Doptimize=ReleaseFast run
, I have the following:
run
└─ run various failure
error: the following command exited with error code 3:
\Zig\Tests\various\zig-out\bin\various.exe
Build Summary: 3/5 steps succeeded; 1 failed (disable with --summary none)
run transitive failure
└─ run various failure
To my understanding, the fact that the second one ends in an error at the run
step means that the executable used isn’t compiled with -Doptimze=ReleaseFast
, otherwise I should not see anything as the call to assert
should not part of the executable in this optimization mode.
What’s wrong with this simple setup? How can make the run
step to use the executable compiled with -Doptimize=ReleaseFast
?