Idiomatic way of using Zig build system with multiple binaries

You need to add two steps (build targets):

    const runtime_step = b.step("runtime", "Build runtime");
    const sandbox_step = b.step("sandbox", "Build sandbox");

These steps are listed, with their name and description, when running: zig build -l


EDIT: added calls to addInstallArtifact that copies the executable to zig-out/bin

const install_runtime = b.addInstallArtifact(runtime, .{});
const install_sandbox = b.addInstallArtifact(sandbox, .{});

Finally you need to add the step dependencies:

    runtime_step.dependOn(&install_runtime.step);
    sandbox_step.dependOn(&install_sandbox.step);

Then you can build:

  • runtime by running zig build runtime,
  • sandbox by running zig build sandbox,
  • both by running zig build runtime sandbox,
  • both (via the default install step) by running zig build.

See also: Build System Tricks

2 Likes