Skip to content

Commit be48c95

Browse files
committed
init template: comment and migrate from deprecated std.Build API
Restructurized "build.zig" and added more info about modules, since they are now exposed here. Signed-off-by: Eric Joldasov <[email protected]>
1 parent cf0c7db commit be48c95

File tree

1 file changed

+69
-23
lines changed

1 file changed

+69
-23
lines changed

lib/init/build.zig

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,59 @@ pub fn build(b: *std.Build) void {
1515
// set a preferred release mode, allowing the user to decide how to optimize.
1616
const optimize = b.standardOptimizeOption(.{});
1717

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(.{
2035
// In this case the main source file is merely a path, however, in more
2136
// 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"),
2338
.target = target,
2439
.optimize = optimize,
2540
});
2641

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.
3166

32-
const exe = b.addExecutable(.{
67+
// After finishing module, we are building executable out of it.
68+
const exe = b.addExecutable2(.{
3369
.name = "$",
34-
.root_source_file = b.path("src/main.zig"),
35-
.target = target,
36-
.optimize = optimize,
70+
.root_module = main_mod,
3771
});
3872

3973
// This declares intent for the executable to be installed into the
@@ -66,26 +100,38 @@ pub fn build(b: *std.Build) void {
66100

67101
// Creates a step for unit testing. This only builds the test executable
68102
// 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(.{
70117
.root_source_file = b.path("src/root.zig"),
71118
.target = target,
72119
.optimize = optimize,
73120
});
74121

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+
});
76127

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,
81132
});
82133

83-
const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
134+
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
84135

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");
89136
test_step.dependOn(&run_lib_unit_tests.step);
90-
test_step.dependOn(&run_exe_unit_tests.step);
91137
}

0 commit comments

Comments
 (0)