|
| 1 | +const std = @import("std"); |
| 2 | + |
| 3 | +// Although this function looks imperative, note that its job is to |
| 4 | +// declaratively construct a build graph that will be executed by an external |
| 5 | +// runner. |
| 6 | +pub fn build(b: *std.Build) void { |
| 7 | + // Standard target options allows the person running `zig build` to choose |
| 8 | + // what target to build for. Here we do not override the defaults, which |
| 9 | + // means any target is allowed, and the default is native. Other options |
| 10 | + // for restricting supported target set are available. |
| 11 | + const target = b.standardTargetOptions(.{}); |
| 12 | + |
| 13 | + // Standard optimization options allow the person running `zig build` to select |
| 14 | + // between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not |
| 15 | + // set a preferred release mode, allowing the user to decide how to optimize. |
| 16 | + const optimize = b.standardOptimizeOption(.{}); |
| 17 | + |
| 18 | + const lib = b.addStaticLibrary(.{ |
| 19 | + .name = "zig-project-template", |
| 20 | + // In this case the main source file is merely a path, however, in more |
| 21 | + // complicated build scripts, this could be a generated file. |
| 22 | + .root_source_file = .{ .path = "src/lib.zig" }, |
| 23 | + .target = target, |
| 24 | + .optimize = optimize, |
| 25 | + }); |
| 26 | + |
| 27 | + // This declares intent for the library to be installed into the standard |
| 28 | + // location when the user invokes the "install" step (the default step when |
| 29 | + // running `zig build`). |
| 30 | + b.installArtifact(lib); |
| 31 | + |
| 32 | + // Creates a step for unit testing. This only builds the test executable |
| 33 | + // but does not run it. |
| 34 | + const main_tests = b.addTest(.{ |
| 35 | + .root_source_file = .{ .path = "src/lib.zig" }, |
| 36 | + .target = target, |
| 37 | + .optimize = optimize, |
| 38 | + }); |
| 39 | + |
| 40 | + const run_main_tests = b.addRunArtifact(main_tests); |
| 41 | + |
| 42 | + // This creates a build step. It will be visible in the `zig build --help` menu, |
| 43 | + // and can be selected like this: `zig build test` |
| 44 | + // This will evaluate the `test` step rather than the default, which is "install". |
| 45 | + const test_step = b.step("test", "Run library tests"); |
| 46 | + test_step.dependOn(&run_main_tests.step); |
| 47 | +} |
0 commit comments