@@ -19,7 +19,7 @@ pub fn build(b: *std.Build) void {
19
19
.name = "zig-project-template" ,
20
20
// In this case the main source file is merely a path, however, in more
21
21
// complicated build scripts, this could be a generated file.
22
- .root_source_file = .{ .path = "src/lib .zig" },
22
+ .root_source_file = .{ .path = "src/root .zig" },
23
23
.target = target ,
24
24
.optimize = optimize ,
25
25
});
@@ -29,19 +29,63 @@ pub fn build(b: *std.Build) void {
29
29
// running `zig build`).
30
30
b .installArtifact (lib );
31
31
32
+ const exe = b .addExecutable (.{
33
+ .name = "zig-project-template" ,
34
+ .root_source_file = .{ .path = "src/main.zig" },
35
+ .target = target ,
36
+ .optimize = optimize ,
37
+ });
38
+
39
+ // This declares intent for the executable to be installed into the
40
+ // standard location when the user invokes the "install" step (the default
41
+ // step when running `zig build`).
42
+ b .installArtifact (exe );
43
+
44
+ // This *creates* a Run step in the build graph, to be executed when another
45
+ // step is evaluated that depends on it. The next line below will establish
46
+ // such a dependency.
47
+ const run_cmd = b .addRunArtifact (exe );
48
+
49
+ // By making the run step depend on the install step, it will be run from the
50
+ // installation directory rather than directly from within the cache directory.
51
+ // This is not necessary, however, if the application depends on other installed
52
+ // files, this ensures they will be present and in the expected location.
53
+ run_cmd .step .dependOn (b .getInstallStep ());
54
+
55
+ // This allows the user to pass arguments to the application in the build
56
+ // command itself, like this: `zig build run -- arg1 arg2 etc`
57
+ if (b .args ) | args | {
58
+ run_cmd .addArgs (args );
59
+ }
60
+
61
+ // This creates a build step. It will be visible in the `zig build --help` menu,
62
+ // and can be selected like this: `zig build run`
63
+ // This will evaluate the `run` step rather than the default, which is "install".
64
+ const run_step = b .step ("run" , "Run the app" );
65
+ run_step .dependOn (& run_cmd .step );
66
+
32
67
// Creates a step for unit testing. This only builds the test executable
33
68
// but does not run it.
34
- const main_tests = b .addTest (.{
35
- .root_source_file = .{ .path = "src/lib .zig" },
69
+ const lib_unit_tests = b .addTest (.{
70
+ .root_source_file = .{ .path = "src/root .zig" },
36
71
.target = target ,
37
72
.optimize = optimize ,
38
73
});
39
74
40
- const run_main_tests = b .addRunArtifact (main_tests );
75
+ const run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
41
76
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 );
77
+ const exe_unit_tests = b .addTest (.{
78
+ .root_source_file = .{ .path = "src/main.zig" },
79
+ .target = target ,
80
+ .optimize = optimize ,
81
+ });
82
+
83
+ const run_exe_unit_tests = b .addRunArtifact (exe_unit_tests );
84
+
85
+ // Similar to creating the run step earlier, this exposes a `test` step to
86
+ // the `zig build --help` menu, providing a way for the user to request
87
+ // running the unit tests.
88
+ const test_step = b .step ("test" , "Run unit tests" );
89
+ test_step .dependOn (& run_lib_unit_tests .step );
90
+ test_step .dependOn (& run_exe_unit_tests .step );
47
91
}
0 commit comments