1
1
const std = @import ("std" );
2
2
3
+ // Although this function looks imperative, note that its job is to
4
+ // declaratively construct a build graph that will be executed by an external
5
+ // runner.
3
6
pub 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.
4
11
const target = b .standardTargetOptions (.{});
12
+
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.
5
16
const optimize = b .standardOptimizeOption (.{});
6
17
7
- // Define dependencies.
8
- const zlib = b .dependency ("zlib" , .{});
18
+ // Zlib dependency
19
+ const dep_zlib = b .dependency ("zlib" , .{});
20
+
21
+ // Zlib headers path
22
+ // https://github.com/ziglang/zig/issues/14719
23
+ var gpa = std .heap .GeneralPurposeAllocator (.{}){};
24
+ const allocator = gpa .allocator ();
25
+ defer _ = gpa .deinit ();
26
+ const zlib_install_path = dep_zlib .module ("zlib" ).builder .install_path ;
27
+ const zlib_headers_rel = "/include/zlib" ;
28
+ const zlib_headers = allocator .alloc (u8 , zlib_install_path .len + zlib_headers_rel .len ) catch {
29
+ @panic ("out of memory" );
30
+ };
31
+ defer _ = allocator .free (zlib_headers );
32
+ std .mem .copy (u8 , zlib_headers [0.. ], zlib_install_path );
33
+ std .mem .copy (u8 , zlib_headers [zlib_install_path .len .. ], zlib_headers_rel );
34
+ std .debug .print ("zlib headers found at: {s}\n " , .{zlib_headers });
9
35
10
- // Define module
11
- const ws_module = b .addModule ("ws" , .{
36
+ // Declare module to expose to package manager to make it available to downstream
37
+ const mod = b .addModule ("ws" , .{
12
38
.source_file = .{ .path = "src/main.zig" },
13
- .dependencies = &[ _ ] std.Build.ModuleDependency {.{ .name = "zlib" , .module = zlib .module ("zlib" ) }},
39
+ .dependencies = &. {.{ .name = "zlib" , .module = dep_zlib .module ("zlib" ) }},
14
40
});
15
41
16
- // Build library.
17
- const ws_lib = b .addStaticLibrary (.{
42
+ const lib = b .addStaticLibrary (.{
18
43
.name = "ws" ,
19
- .root_source_file = .{ .path = "src/main.zig" },
44
+ // In this case the main source file is merely a path, however, in more
45
+ // complicated build scripts, this could be a generated file.
46
+ // .root_source_file = .{ .path = "src/main.zig" },
20
47
.target = target ,
21
48
.optimize = optimize ,
22
49
});
23
- // Link z library and zlib module.
24
- ws_lib .linkLibrary (b .dependency ("zlib" , .{
50
+
51
+ // Link zlib
52
+ lib .linkLibrary (b .dependency ("zlib" , .{
25
53
.target = target ,
26
54
.optimize = optimize ,
27
- }).artifact ("z" ));
28
- ws_lib .addModule ("zlib" , zlib .module ("zlib" ));
29
- b .installArtifact (ws_lib );
55
+ }).artifact ("zlib" ));
56
+ lib .addModule ("zlib" , dep_zlib .module ("zlib" ));
57
+
58
+ // This declares intent for the library to be installed into the standard
59
+ // location when the user invokes the "install" step (the default step when
60
+ // running `zig build`).
61
+ b .installArtifact (lib );
30
62
31
- // Build test.
32
- const test_compile = b .addTest (.{
63
+ // Creates a step for unit testing. This only builds the test executable
64
+ // but does not run it.
65
+ const main_tests = b .addTest (.{
33
66
.root_source_file = .{ .path = "src/main.zig" },
34
67
.target = target ,
35
68
.optimize = optimize ,
36
69
});
37
- test_compile .linkLibrary (b .dependency ("zlib" , .{
70
+
71
+ // Link zlib
72
+ main_tests .addIncludePath (.{ .path = zlib_headers });
73
+ main_tests .linkLibrary (b .dependency ("zlib" , .{
38
74
.target = target ,
39
75
.optimize = optimize ,
40
- }).artifact ("z" ));
41
- test_compile .addModule ("zlib" , zlib .module ("zlib" ));
42
- const test_step = b .step ("test" , "Run unit tests" );
43
- test_step .dependOn (& test_compile .step );
76
+ }).artifact ("zlib" ));
77
+ main_tests .addModule ("zlib" , dep_zlib .module ("zlib" ));
78
+
79
+ const run_main_tests = b .addRunArtifact (main_tests );
80
+
81
+ // This creates a build step. It will be visible in the `zig build --help` menu,
82
+ // and can be selected like this: `zig build test`
83
+ // This will evaluate the `test` step rather than the default, which is "install".
84
+ const test_step = b .step ("test" , "Run library tests" );
85
+ test_step .dependOn (& run_main_tests .step );
44
86
45
87
// Build examples.
46
88
const bin = b .addExecutable (.{
@@ -49,10 +91,8 @@ pub fn build(b: *std.Build) void {
49
91
.target = target ,
50
92
.optimize = optimize ,
51
93
});
52
- bin .linkLibrary (ws_lib );
53
- bin .addModule ("ws" , ws_module );
94
+ bin .addIncludePath (.{ .path = zlib_headers });
95
+ bin .linkLibrary (lib );
96
+ bin .addModule ("ws" , mod );
54
97
b .installArtifact (bin );
55
98
}
56
-
57
- // to test single file
58
- // $ zig test src/main.zig --deps zlib=zlib --mod zlib::zlib/src/main.zig -l z
0 commit comments