|
1 | 1 | const std = @import("std"); |
2 | 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 | 3 | 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 | 4 | 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 | 5 | const optimize = b.standardOptimizeOption(.{}); |
17 | 6 |
|
| 7 | + const dep_opts = .{ .target = target, .optimize = optimize }; |
| 8 | + const ws_module = b.dependency("ws", dep_opts).module("ws"); |
| 9 | + |
18 | 10 | const exe = b.addExecutable(.{ |
19 | 11 | .name = "exe", |
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/main.zig" }, |
| 12 | + .root_source_file = b.path("src/main.zig"), |
23 | 13 | .target = target, |
24 | 14 | .optimize = optimize, |
25 | 15 | }); |
| 16 | + exe.root_module.addImport("ws", ws_module); |
26 | 17 |
|
27 | | - const ws = b.dependency("ws", .{}); |
28 | | - exe.linkLibrary(ws.artifact("ws")); |
29 | | - exe.addModule("ws", ws.module("ws")); |
30 | | - |
31 | | - // This declares intent for the executable to be installed into the |
32 | | - // standard location when the user invokes the "install" step (the default |
33 | | - // step when running `zig build`). |
34 | 18 | b.installArtifact(exe); |
35 | 19 |
|
36 | | - // This *creates* a Run step in the build graph, to be executed when another |
37 | | - // step is evaluated that depends on it. The next line below will establish |
38 | | - // such a dependency. |
39 | 20 | const run_cmd = b.addRunArtifact(exe); |
40 | | - |
41 | | - // By making the run step depend on the install step, it will be run from the |
42 | | - // installation directory rather than directly from within the cache directory. |
43 | | - // This is not necessary, however, if the application depends on other installed |
44 | | - // files, this ensures they will be present and in the expected location. |
45 | 21 | run_cmd.step.dependOn(b.getInstallStep()); |
46 | | - |
47 | | - // This allows the user to pass arguments to the application in the build |
48 | | - // command itself, like this: `zig build run -- arg1 arg2 etc` |
49 | | - if (b.args) |args| { |
50 | | - run_cmd.addArgs(args); |
51 | | - } |
52 | | - |
53 | | - // This creates a build step. It will be visible in the `zig build --help` menu, |
54 | | - // and can be selected like this: `zig build run` |
55 | | - // This will evaluate the `run` step rather than the default, which is "install". |
56 | 22 | const run_step = b.step("run", "Run the app"); |
57 | 23 | run_step.dependOn(&run_cmd.step); |
58 | 24 |
|
59 | | - // Creates a step for unit testing. This only builds the test executable |
60 | | - // but does not run it. |
61 | | - const unit_tests = b.addTest(.{ |
62 | | - .root_source_file = .{ .path = "src/main.zig" }, |
| 25 | + const exe_unit_tests = b.addTest(.{ |
| 26 | + .root_source_file = b.path("src/main.zig"), |
63 | 27 | .target = target, |
64 | 28 | .optimize = optimize, |
65 | 29 | }); |
66 | | - |
67 | | - const run_unit_tests = b.addRunArtifact(unit_tests); |
68 | | - |
69 | | - // Similar to creating the run step earlier, this exposes a `test` step to |
70 | | - // the `zig build --help` menu, providing a way for the user to request |
71 | | - // running the unit tests. |
| 30 | + const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests); |
72 | 31 | const test_step = b.step("test", "Run unit tests"); |
73 | | - test_step.dependOn(&run_unit_tests.step); |
| 32 | + test_step.dependOn(&run_exe_unit_tests.step); |
74 | 33 | } |
0 commit comments