A Zig build system uses build.zig to organize compilation, linking, dependencies, and other build steps. Separating modules lets a project build only the parts it needs. section 1.2.3 section 6.3.1
A build script can:
- Separate a virtual machine, runtime, and tests into modules with distinct dependencies. section 6.3.1
- Create build steps that compile and run tests. section 6.3.1
Listing 6.2 A more modular build script
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{}); #1
const optimize = b.standardOptimizeOption(.{}); #1
const vm_mod = b.createModule(.{ #2
.root_source_file = b.path("src/vm.zig"),
.target = target,
.optimize = optimize,
});
const test_mod = b.createModule(.{
.root_source_file = b.path("src/tests.zig"),
.target = target,
.optimize = optimize,
});
test_mod.addImport("vm", vm_mod); #3
}
A modular build script creates VM and test modules, setting target and optimization options and importing the VM into the tests. section 6.3.1
Listing 6.3 Making new steps
pub fn build(b: *std.Build) void {
// ... module code ...
const tests = b.addTest(.{ #1
.root_module = test_mod,
});
const run_tests = b.addRunArtifact(tests); #2
const test_cmd = b.step("test", "Run the vm tests"); #3
test_cmd.dependOn(&run_tests.step); #4
}
This snippet adds a test artifact and a named test step that runs it. section 6.3.1




