@@ -15,25 +15,59 @@ 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 import to this module other Zig modules, add C/C++ source files, link libraries,
43
+ // and etc. To import Zig modules, use `addImport` function:
44
+ //
45
+ // ```zig
46
+ // main_mod.addImport("some_mod", some_mod);
47
+ // ```
48
+ //
49
+ // If they are added unconditionally, you can also import them during module creation instead:
50
+ // ```zig
51
+ // const main_mod = b.createModule(.{
52
+ // // ...
53
+ // .imports = &.{
54
+ // .{ .name = "some_mod", .module = some_mod },
55
+ // .{ .name = "another_mod", .module = another_mod },
56
+ // },
57
+ // });
58
+ // ```
59
+ //
60
+ // Now you can leverage it in your source code by using `@import("some_mod")`
61
+ // syntax (get all definitions from root source file) or by using `@embedFile("some_mod")`
62
+ // (get literal content of the root source file at comptime).
63
+ //
64
+ // Note that Zig's "std" module (a.k.a. standard library)
65
+ // is always available, you don't need to import it manually.
31
66
32
- const exe = b .addExecutable (.{
67
+ // After finishing module, we are building executable out of it.
68
+ const exe = b .addExecutable2 (.{
33
69
.name = "$" ,
34
- .root_source_file = b .path ("src/main.zig" ),
35
- .target = target ,
36
- .optimize = optimize ,
70
+ .root_module = main_mod ,
37
71
});
38
72
39
73
// This declares intent for the executable to be installed into the
@@ -66,26 +100,38 @@ pub fn build(b: *std.Build) void {
66
100
67
101
// Creates a step for unit testing. This only builds the test executable
68
102
// but does not run it.
69
- const lib_unit_tests = b .addTest (.{
103
+ const exe_unit_tests = b .addTest2 (.{
104
+ .root_module = main_mod ,
105
+ });
106
+
107
+ const run_exe_unit_tests = b .addRunArtifact (exe_unit_tests );
108
+
109
+ // Similar to creating the run step earlier, this exposes a `test` step to
110
+ // the `zig build --help` menu, providing a way for the user to request
111
+ // running the unit tests.
112
+ const test_step = b .step ("test" , "Run unit tests" );
113
+ test_step .dependOn (& run_exe_unit_tests .step );
114
+
115
+ // Below we are doing same things, but for static library.
116
+ const another_mod = b .createModule (.{
70
117
.root_source_file = b .path ("src/root.zig" ),
71
118
.target = target ,
72
119
.optimize = optimize ,
73
120
});
74
121
75
- const run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
122
+ const lib = b .addLibrary (.{
123
+ .name = "$" ,
124
+ .root_module = another_mod ,
125
+ .linkage = .static ,
126
+ });
76
127
77
- const exe_unit_tests = b . addTest (.{
78
- . root_source_file = b . path ( "src/main.zig" ),
79
- . target = target ,
80
- .optimize = optimize ,
128
+ b . installArtifact ( lib );
129
+
130
+ const lib_unit_tests = b . addTest2 (.{
131
+ .root_module = another_mod ,
81
132
});
82
133
83
- const run_exe_unit_tests = b .addRunArtifact (exe_unit_tests );
134
+ const run_lib_unit_tests = b .addRunArtifact (lib_unit_tests );
84
135
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
136
test_step .dependOn (& run_lib_unit_tests .step );
90
- test_step .dependOn (& run_exe_unit_tests .step );
91
137
}
0 commit comments