Skip to content

Commit 72fbd3c

Browse files
committed
build: only build test modules when node-api is root package
1 parent ba6c54f commit 72fbd3c

File tree

1 file changed

+57
-53
lines changed

1 file changed

+57
-53
lines changed

build.zig

Lines changed: 57 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,72 @@ pub fn build(b: *std.Build) void {
1717

1818
node_api.addSystemIncludePath(node_api_headers.path("include"));
1919

20-
// zig build (test modules)
21-
const build_mods_step = b.step("test-modules", "build native zig modules");
22-
{
23-
var dir = std.fs.cwd().openDir("tests/zig_modules", .{ .iterate = true }) catch unreachable;
24-
defer dir.close();
20+
const is_root_package = b.pkg_hash.len == 0;
2521

26-
var it = dir.iterate();
27-
while (it.next() catch unreachable) |entry| {
28-
switch (entry.kind) {
29-
.directory => {
30-
std.log.info("tests/zig_modules/{s}/src/root.zig", .{entry.name});
22+
if (is_root_package) {
23+
// zig build (test modules)
24+
const build_mods_step = b.step("test-modules", "build native zig modules");
25+
{
26+
var dir = std.fs.cwd().openDir("tests/zig_modules", .{ .iterate = true }) catch unreachable;
27+
defer dir.close();
3128

32-
const mod = b.addLibrary(.{
33-
// imprtant, works without on MacOS, but not on Linux
34-
.use_llvm = true,
35-
.linkage = .dynamic,
36-
.name = entry.name,
37-
.root_module = b.createModule(.{
38-
.root_source_file = b.path(std.fmt.allocPrint(b.allocator, "tests/zig_modules/{s}/src/root.zig", .{entry.name}) catch unreachable),
39-
.optimize = optimize,
40-
.target = target,
41-
}),
42-
});
43-
mod.root_module.addImport("node-api", node_api);
29+
var it = dir.iterate();
30+
while (it.next() catch unreachable) |entry| {
31+
switch (entry.kind) {
32+
.directory => {
33+
std.log.info("tests/zig_modules/{s}/src/root.zig", .{entry.name});
4434

45-
// important
46-
mod.linker_allow_shlib_undefined = true;
47-
mod.linkLibC();
35+
const mod = b.addLibrary(.{
36+
// imprtant, works without on MacOS, but not on Linux
37+
.use_llvm = true,
38+
.linkage = .dynamic,
39+
.name = entry.name,
40+
.root_module = b.createModule(.{
41+
.root_source_file = b.path(std.fmt.allocPrint(b.allocator, "tests/zig_modules/{s}/src/root.zig", .{entry.name}) catch unreachable),
42+
.optimize = optimize,
43+
.target = target,
44+
}),
45+
});
46+
mod.root_module.addImport("node-api", node_api);
4847

49-
const sub_path = std.fmt.allocPrint(b.allocator, "{s}.node", .{entry.name}) catch |err| {
50-
std.log.info("{s}", .{@errorName(err)});
51-
unreachable;
52-
};
53-
std.log.info("sub path: {s}", .{sub_path});
48+
// important
49+
mod.linker_allow_shlib_undefined = true;
50+
mod.linkLibC();
5451

55-
build_mods_step.dependOn(
56-
&b.addInstallArtifact(
57-
mod,
58-
.{
59-
// custom dir is relative to ./zig-out
60-
.dest_dir = .{ .override = .{
61-
.custom = "../tests/zig_modules",
62-
} },
52+
const sub_path = std.fmt.allocPrint(b.allocator, "{s}.node", .{entry.name}) catch |err| {
53+
std.log.info("{s}", .{@errorName(err)});
54+
unreachable;
55+
};
56+
std.log.info("sub path: {s}", .{sub_path});
6357

64-
.dest_sub_path = sub_path,
65-
},
66-
).step,
67-
);
68-
},
69-
else => {},
58+
build_mods_step.dependOn(
59+
&b.addInstallArtifact(
60+
mod,
61+
.{
62+
// custom dir is relative to ./zig-out
63+
.dest_dir = .{ .override = .{
64+
.custom = "../tests/zig_modules",
65+
} },
66+
67+
.dest_sub_path = sub_path,
68+
},
69+
).step,
70+
);
71+
},
72+
else => {},
73+
}
7074
}
7175
}
72-
}
7376

74-
// `zig build test`
75-
const test_step = b.step("test", "test bindings");
76-
{
77-
const run_cmd = b.addSystemCommand(&.{"bun"});
78-
run_cmd.addArg("test");
79-
run_cmd.cwd = .{ .cwd_relative = "tests" };
80-
run_cmd.step.dependOn(build_mods_step);
77+
// `zig build test`
78+
const test_step = b.step("test", "test bindings");
79+
{
80+
const run_cmd = b.addSystemCommand(&.{"bun"});
81+
run_cmd.addArg("test");
82+
run_cmd.cwd = .{ .cwd_relative = "tests" };
83+
run_cmd.step.dependOn(build_mods_step);
8184

82-
test_step.dependOn(&run_cmd.step);
85+
test_step.dependOn(&run_cmd.step);
86+
}
8387
}
8488
}

0 commit comments

Comments
 (0)