I have been diving deeper into Zig build system, so I don’t have to use Makefiles anymore.
I have read the official guide and the system seems quite powerful, but I do have one question when it comes to multiple binaries and how to compile just one binary and not everything.
In my testing project, I have two programs (build.zig looks like this).
I can of course build both of them with zig build. What I am wondering is, what is the best way to compile just one of them. Something like zig build sandbox.
Is there a common way to achieve this, maybe a pattern, I don’t know.
It can be quite difficult to understand what’s happening under the hood. Try using the --summary all or new options to get an output of what’s actually been built versus retrieved from cache.
This usually means the artifacts were cached and their inputs were unchanged, so no rebuild happened. Use the --summary option to see what zig build actually did.
Note: even when I changed the source file, it was still treating it like nothing changed. When I provided a custom cache directory, it started working.
I guess because I did zig build before adding new executables, it somehow got confused. I am using version 0.14.
This is ok now, and I will continue providing --cache-dir just to be sure.
The issue with this version is that b.addExecutable only builds the executable (artifact), but doesn’t do anything with it. To get the executable to appear in zig-out/bin, you additionally need an install step:
This is actually what b.installArtifact(runtime) is doing under the hood for the default “install” step. The entire implementation of installArtifact is
That is, it’s just creating an install step for the artifact and adding it as a dependency of the “install” step. By creating your own install steps, you can add them selectively as dependencies of other steps, as you’re looking to do here.
Just to add on to this, you also no longer need the explicit runtime_step.dependOn(&runtime.step), since runtime is already a dependency of install_runtime (to install any artifact, it needs to be built first), and hence transitively of runtime_step (due to runtime_step.dependOn(&install_runtime.step)). The resulting build graph will end up looking like