@@ -17,34 +17,60 @@ pub fn build(b: *std.Build) void {
1717
1818 node_api .addSystemIncludePath (node_api_headers .path ("include" ));
1919
20- const sample_addon = b .addLibrary (.{
21- .use_llvm = true ,
22- .linkage = .dynamic ,
23- .name = "sample-addon" ,
24- .root_module = b .createModule (.{
25- .root_source_file = b .path ("tests/zig/test-module.zig" ),
26- .optimize = optimize ,
27- .target = target ,
28- }),
29- });
30- sample_addon .root_module .addImport ("node-api" , node_api );
20+ const install_step = b .getInstallStep ();
3121
32- // important
33- sample_addon .linker_allow_shlib_undefined = true ;
34- sample_addon .linkLibC ();
22+ // zig build (test modules)
23+ {
24+ var dir = std .fs .cwd ().openDir ("tests/zig_modules" , .{ .iterate = true }) catch unreachable ;
25+ defer dir .close ();
3526
36- const install_step = b .getInstallStep ();
27+ var it = dir .iterate ();
28+ while (it .next () catch unreachable ) | entry | {
29+ switch (entry .kind ) {
30+ .directory = > {
31+ std .log .info ("tests/zig_modules/{s}/src/root.zig" , .{entry .name });
32+
33+ const mod = b .addLibrary (.{
34+ // imprtant, works without on MacOS, but not on Linux
35+ .use_llvm = true ,
36+ .linkage = .dynamic ,
37+ .name = entry .name ,
38+ .root_module = b .createModule (.{
39+ .root_source_file = b .path (std .fmt .allocPrint (b .allocator , "tests/zig_modules/{s}/src/root.zig" , .{entry .name }) catch unreachable ),
40+ .optimize = optimize ,
41+ .target = target ,
42+ }),
43+ });
44+ mod .root_module .addImport ("node-api" , node_api );
3745
38- install_step .dependOn (
39- & b .addInstallArtifact (
40- sample_addon ,
41- .{
42- // custom dir is relative to ./zig-out
43- .dest_dir = .{ .override = .{ .custom = "../tests/node_modules/node-api-test-module" } },
44- .dest_sub_path = "zig-module.node" ,
45- },
46- ).step ,
47- );
46+ // important
47+ mod .linker_allow_shlib_undefined = true ;
48+ mod .linkLibC ();
49+
50+ const sub_path = std .fmt .allocPrint (b .allocator , "{s}.node" , .{entry .name }) catch | err | {
51+ std .log .info ("{s}" , .{@errorName (err )});
52+ unreachable ;
53+ };
54+ std .log .info ("sub path: {s}" , .{sub_path });
55+
56+ install_step .dependOn (
57+ & b .addInstallArtifact (
58+ mod ,
59+ .{
60+ // custom dir is relative to ./zig-out
61+ .dest_dir = .{ .override = .{
62+ .custom = "../tests/zig_modules" ,
63+ } },
64+
65+ .dest_sub_path = sub_path ,
66+ },
67+ ).step ,
68+ );
69+ },
70+ else = > {},
71+ }
72+ }
73+ }
4874
4975 // `zig build test`
5076 {
0 commit comments