@@ -15,25 +15,49 @@ pub fn build(b: *std.Build) void {
15
15
// set a preferred release mode, allowing the user to decide how to optimize.
16
16
const optimize = b .standardOptimizeOption (.{});
17
17
18
- const lib = b .addStaticLibrary (.{
19
- .name = "$" ,
18
+ // Here we are creating a module. Module is a set of files
19
+ // that you can use to build executable or library, or import into other module.
20
+ //
21
+ // Here we are creating a module, but not exposing it to package manager.
22
+ // To expose it to package manager, use function `addModule` and choose
23
+ // a name for it. Packages that want to import your module will need to use
24
+ // exactly this name in their build.zig:
25
+ //
26
+ // ```zig
27
+ // // In your build.zig
28
+ // const main_mod = b.addModule("main", .{ ... });
29
+ //
30
+ // // In their build.zig
31
+ // const some_dep = b.dependency("...", .{ ... });
32
+ // const some_mod = some_dep.module("main");
33
+ // ```
34
+ const main_mod = b .createModule (.{
20
35
// In this case the main source file is merely a path, however, in more
21
36
// complicated build scripts, this could be a generated file.
22
- .root_source_file = b .path ("src/root .zig" ),
37
+ .root_source_file = b .path ("src/main .zig" ),
23
38
.target = target ,
24
39
.optimize = optimize ,
25
40
});
26
41
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 );
42
+ // You can add to this module C/C++ source files, link libraries,
43
+ // or import other Zig modules. To import Zig modules, use
44
+ // `addImport` function:
45
+ //
46
+ // ```zig
47
+ // main_mod.addImport("some_mod", some_mod);
48
+ // ```
49
+ //
50
+ // Now you can leverage it in your source code by using `@import("some_mod")`
51
+ // syntax (get all definitions from root source file) or by using `@embedFile("some_mod")`
52
+ // (get literal content of the root source file at comptime).
53
+ //
54
+ // Note that Zig's "std" module (a.k.a. standard library)
55
+ // is always available, you don't need to import it manually.
31
56
32
- const exe = b .addExecutable (.{
57
+ // After finishing module, we are building executable out of it.
58
+ const exe = b .addExecutable2 (.{
33
59
.name = "$" ,
34
- .root_source_file = b .path ("src/main.zig" ),
35
- .target = target ,
36
- .optimize = optimize ,
60
+ .root_module = main_mod ,
37
61
});
38
62
39
63
// This declares intent for the executable to be installed into the
@@ -66,26 +90,38 @@ pub fn build(b: *std.Build) void {
66
90
67
91
// Creates a step for unit testing. This only builds the test executable
68
92
// but does not run it.
69
- const lib_unit_tests = b .addTest (.{
93
+ const exe_unit_tests = b .addTest2 (.{
94
+ .root_module = main_mod ,
95
+ });
96
+
97
+ const run_exe_unit_tests = b .addRunArtifact (exe_unit_tests );
98
+
99
+ // Similar to creating the run step earlier, this exposes a `test` step to
100
+ // the `zig build --help` menu, providing a way for the user to request
101
+ // running the unit tests.
102
+ const test_step = b .step ("test" , "Run unit tests" );
103
+ test_step .dependOn (& run_exe_unit_tests .step );
104
+
105
+ // Below we are doing same things, but for static library.
106
+ const another_mod = b .createModule (.{
70
107
.root_source_file = b .path ("src/root.zig" ),
71
108
.target = target ,
72
109
.optimize = optimize ,
73
110
});
74
111
75
- const run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
112
+ const lib = b .addLibrary (.{
113
+ .name = "$" ,
114
+ .root_module = another_mod ,
115
+ .linkage = .static ,
116
+ });
76
117
77
- const exe_unit_tests = b . addTest (.{
78
- . root_source_file = b . path ( "src/main.zig" ),
79
- . target = target ,
80
- .optimize = optimize ,
118
+ b . installArtifact ( lib );
119
+
120
+ const lib_unit_tests = b . addTest2 (.{
121
+ .root_module = another_mod ,
81
122
});
82
123
83
- const run_exe_unit_tests = b .addRunArtifact (exe_unit_tests );
124
+ const run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
84
125
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
126
test_step .dependOn (& run_lib_unit_tests .step );
90
- test_step .dependOn (& run_exe_unit_tests .step );
91
127
}
0 commit comments