@@ -4,21 +4,23 @@ const std = @import("std");
44// declaratively construct a build graph that will be executed by an external
55// runner.
66pub 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.
711 const target = b .standardTargetOptions (.{});
8- const optimize = b .standardOptimizeOption (.{});
9-
10- const parser_module = b .addModule ("parser" , .{
11- .root_source_file = b .path ("httpfile/parser.zig" ),
12- });
1312
14- const http_client_module = b .addModule ("http_client" , .{
15- .root_source_file = b .path ("client/http_client.zig" ),
16- .imports = &.{
17- .{ .name = "parser" , .module = parser_module },
18- },
19- });
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 (.{});
2017
18+ // We will also create a module for our other entry point, 'main.zig'.
2119 const exe_mod = b .createModule (.{
20+ // `root_source_file` is the Zig "entry point" of the module. If a module
21+ // only contains e.g. external object files, you can make this `null`.
22+ // In this case the main source file is merely a path, however, in more
23+ // complicated build scripts, this could be a generated file.
2224 .root_source_file = b .path ("src/main.zig" ),
2325 .target = target ,
2426 .optimize = optimize ,
@@ -31,17 +33,31 @@ pub fn build(b: *std.Build) void {
3133 .root_module = exe_mod ,
3234 });
3335
34- exe . root_module . addImport ( "parser" , parser_module );
35- exe . root_module . addImport ( "http_client" , http_client_module );
36-
36+ // This declares intent for the executable to be installed into the
37+ // standard location when the user invokes the "install" step (the default
38+ // step when running `zig build`).
3739 b .installArtifact (exe );
3840
41+ // This *creates* a Run step in the build graph, to be executed when another
42+ // step is evaluated that depends on it. The next line below will establish
43+ // such a dependency.
3944 const run_cmd = b .addRunArtifact (exe );
45+
46+ // By making the run step depend on the install step, it will be run from the
47+ // installation directory rather than directly from within the cache directory.
48+ // This is not necessary, however, if the application depends on other installed
49+ // files, this ensures they will be present and in the expected location.
4050 run_cmd .step .dependOn (b .getInstallStep ());
51+
52+ // This allows the user to pass arguments to the application in the build
53+ // command itself, like this: `zig build run -- arg1 arg2 etc`
4154 if (b .args ) | args | {
4255 run_cmd .addArgs (args );
4356 }
4457
58+ // This creates a build step. It will be visible in the `zig build --help` menu,
59+ // and can be selected like this: `zig build run`
60+ // This will evaluate the `run` step rather than the default, which is "install".
4561 const run_step = b .step ("run" , "Run the app" );
4662 run_step .dependOn (& run_cmd .step );
4763
@@ -51,6 +67,9 @@ pub fn build(b: *std.Build) void {
5167
5268 const run_exe_unit_tests = b .addRunArtifact (exe_unit_tests );
5369
70+ // Similar to creating the run step earlier, this exposes a `test` step to
71+ // the `zig build --help` menu, providing a way for the user to request
72+ // running the unit tests.
5473 const test_step = b .step ("test" , "Run unit tests" );
5574 test_step .dependOn (& run_exe_unit_tests .step );
5675}
0 commit comments