-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
88 lines (74 loc) · 3.31 KB
/
build.zig
File metadata and controls
88 lines (74 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const node_api = b.addModule("node-api", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
const node_api_headers = b.dependency("node_api_headers", .{
.optimize = optimize,
.target = target,
});
node_api.addSystemIncludePath(node_api_headers.path("include"));
const is_root_package = b.pkg_hash.len == 0;
if (is_root_package) {
// zig build (test modules)
const build_mods_step = b.step("test-modules", "build native zig modules");
{
var dir = std.fs.cwd().openDir("tests/zig_modules", .{ .iterate = true }) catch unreachable;
defer dir.close();
var it = dir.iterate();
while (it.next() catch unreachable) |entry| {
switch (entry.kind) {
.directory => {
std.log.info("tests/zig_modules/{s}/src/root.zig", .{entry.name});
const lib = b.addLibrary(.{
// imprtant, works without on MacOS, but not on Linux
.use_llvm = true,
.linkage = .dynamic,
.name = entry.name,
.root_module = b.createModule(.{
.root_source_file = b.path(std.fmt.allocPrint(b.allocator, "tests/zig_modules/{s}/src/root.zig", .{entry.name}) catch unreachable),
.optimize = optimize,
.target = target,
}),
});
lib.root_module.addImport("node-api", node_api);
// important
lib.linker_allow_shlib_undefined = true;
lib.linkLibC();
const sub_path = std.fmt.allocPrint(b.allocator, "{s}.node", .{entry.name}) catch |err| {
std.log.info("{s}", .{@errorName(err)});
unreachable;
};
std.log.info("sub path: {s}", .{sub_path});
build_mods_step.dependOn(
&b.addInstallArtifact(
lib,
.{
// custom dir is relative to ./zig-out
.dest_dir = .{ .override = .{
.custom = "../tests/zig_modules",
} },
.dest_sub_path = sub_path,
},
).step,
);
},
else => {},
}
}
}
// `zig build test`
const test_step = b.step("test", "test bindings");
{
const run_cmd = b.addSystemCommand(&.{"bun"});
run_cmd.addArg("test");
run_cmd.cwd = .{ .cwd_relative = "tests" };
run_cmd.step.dependOn(build_mods_step);
test_step.dependOn(&run_cmd.step);
}
}
}