Skip to content

build.zig not smart enough about -fno-emit-bin #18877

@sm2n

Description

@sm2n

Zig Version

0.11.0

Steps to Reproduce and Observed Behavior

At tigerbeetle, we would like to have a way to check if code compiles without actually running codegen and waiting for llvm in order to have a faster feedback cycle.

Following 5c01818 it seems that the way to do that is to make sure that your build step does not have dependencies on it's generated binary output, and zig build will pick that up and pass -fno-emit-bin to the compiler.

In practice we have to duplicate our build logic: rather than the following diff to build.zig:

diff --git a/build.zig b/build.zig
index b25680cd..cb95a6bb 100644
--- a/build.zig
+++ b/build.zig
@@ -135,6 +155,8 @@ pub fn build(b: *std.Build) !void {
     }
     tigerbeetle.addModule("vsr", vsr_module);
     tigerbeetle.addModule("vsr_options", vsr_options_module);
+    const check = b.step("check", "Check if Tigerbeetle compiles");
+    check.dependOn(&tigerbeetle.step);
     b.installArtifact(tigerbeetle);
     // Ensure that we get stack traces even in release builds.
     tigerbeetle.omit_frame_pointer = false;

We need

diff --git a/build.zig b/build.zig
index b25680cd..cb95a6bb 100644
--- a/build.zig
+++ b/build.zig
@@ -121,6 +121,26 @@ pub fn build(b: *std.Build) !void {
         },
     });
 
+    {
+        const tigerbeetle = b.addExecutable(.{
+            .name = "tigerbeetle",
+            .root_source_file = .{ .path = "src/tigerbeetle/main.zig" },
+            .target = target,
+            .optimize = mode,
+        });
+        tigerbeetle.addModule("vsr", vsr_module);
+        tigerbeetle.addModule("vsr_options", vsr_options_module);
+
+        const check = b.step("check", "Check if Tigerbeetle compiles");
+        check.dependOn(&tigerbeetle.step);
+    }
+
     const tigerbeetle = b.addExecutable(.{
         .name = "tigerbeetle",
         .root_source_file = .{ .path = "src/tigerbeetle/main.zig" },

Presumably because the dependencies not actually used in the step we're executing cause a false positive for the detection.

Expected Behavior

If there are dependencies on a step's generated binaries, but they're not actually necessary at runtime, then -fno-emit-bin should be passed to the compiler.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorzig build systemstd.Build, the build runner, `zig build` subcommand, package management

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions