0 |
const std = @import("std"); |
1 |
|
2 |
// Although this function looks imperative, note that its job is to |
3 |
// declaratively construct a build graph that will be executed by an external |
4 |
// runner. |
5 |
pub fn build(b: *std.Build) void { |
6 |
// Standard target options allows the person running `zig build` to choose |
7 |
// what target to build for. Here we do not override the defaults, which |
8 |
// means any target is allowed, and the default is native. Other options |
9 |
// for restricting supported target set are available. |
10 |
const target = b.standardTargetOptions(.{}); |
11 |
|
12 |
// Standard optimization options allow the person running `zig build` to select |
13 |
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not |
14 |
// set a preferred release mode, allowing the user to decide how to optimize. |
15 |
const optimize = b.standardOptimizeOption(.{}); |
16 |
|
17 |
const exe = b.addExecutable(.{ |
18 |
.name = "rig", |
19 |
.root_source_file = b.path("src/main.zig"), |
20 |
.target = target, |
21 |
.optimize = optimize, |
22 |
}); |
23 |
|
24 |
// This declares intent for the executable to be installed into the |
25 |
// standard location when the user invokes the "install" step (the default |
26 |
// step when running `zig build`). |
27 |
b.installArtifact(exe); |
28 |
|
29 |
// This *creates* a Run step in the build graph, to be executed when another |
30 |
// step is evaluated that depends on it. The next line below will establish |
31 |
// such a dependency. |
32 |
const run_cmd = b.addRunArtifact(exe); |
33 |
|
34 |
// By making the run step depend on the install step, it will be run from the |
35 |
// installation directory rather than directly from within the cache directory. |
36 |
// This is not necessary, however, if the application depends on other installed |
37 |
// files, this ensures they will be present and in the expected location. |
38 |
run_cmd.step.dependOn(b.getInstallStep()); |
39 |
|
40 |
// This allows the user to pass arguments to the application in the build |
41 |
// command itself, like this: `zig build run -- arg1 arg2 etc` |
42 |
if (b.args) |args| { |
43 |
run_cmd.addArgs(args); |
44 |
} |
45 |
|
46 |
// This creates a build step. It will be visible in the `zig build --help` menu, |
47 |
// and can be selected like this: `zig build run` |
48 |
// This will evaluate the `run` step rather than the default, which is "install". |
49 |
const run_step = b.step("run", "Run the app"); |
50 |
run_step.dependOn(&run_cmd.step); |
51 |
|
52 |
const exe_unit_tests = b.addTest(.{ |
53 |
.root_source_file = b.path("src/main.zig"), |
54 |
.target = target, |
55 |
.optimize = optimize, |
56 |
}); |
57 |
|
58 |
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); |
59 |
|
60 |
// Similar to creating the run step earlier, this exposes a `test` step to |
61 |
// the `zig build --help` menu, providing a way for the user to request |
62 |
// running the unit tests. |
63 |
const test_step = b.step("test", "Run unit tests"); |
64 |
test_step.dependOn(&run_exe_unit_tests.step); |
65 |
} |