Skip to content

Commit

Permalink
Use build.zig.zon to install node header dependency
Browse files Browse the repository at this point in the history
This uses a path because with the current version of zig there is an
issue initiating a tls connection with cloudflare which is where node
stores its releases.

Until that is fixed I am going to use a local path.

See ziglang/zig#21747
  • Loading branch information
zschreur committed Oct 30, 2024
1 parent d955934 commit f2bfb6a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 13 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
zig-out/
.zig-cache/
.cache/
21 changes: 9 additions & 12 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ pub fn build(b: *std.Build) void {
// set a preferred release mode, allowing the user to decide how to optimize.
const optimize = b.standardOptimizeOption(.{});

const node = b.dependency("node", .{
.target = target,
.optimize = optimize,
});
const node_headers = node.path("include/node");

const lib = b.addStaticLibrary(.{
.name = "node-zig",
// In this case the main source file is merely a path, however, in more
Expand All @@ -24,10 +30,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});

lib.addIncludePath(.{ .src_path = .{
.owner = b,
.sub_path = ".cache/node/node-v20.18.0/include/node",
} });
lib.addIncludePath(node_headers);

// This declares intent for the library to be installed into the standard
// location when the user invokes the "install" step (the default step when
Expand All @@ -42,10 +45,7 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});

lib_unit_tests.addIncludePath(.{ .src_path = .{
.owner = b,
.sub_path = ".cache/node/node-v20.18.0/include/node",
} });
lib_unit_tests.addIncludePath(node_headers);

const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);

Expand All @@ -64,10 +64,7 @@ pub fn build(b: *std.Build) void {
nodeAddon.linker_allow_shlib_undefined = true;

nodeAddon.linkLibrary(lib);
nodeAddon.addIncludePath(.{ .src_path = .{
.owner = b,
.sub_path = ".cache/node/node-v20.18.0/include/node",
} });
nodeAddon.addIncludePath(node_headers);
nodeAddon.linkLibC();
nodeAddon.addCSourceFile(.{
.file = .{ .src_path = .{
Expand Down
3 changes: 3 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
// Once all dependencies are fetched, `zig build` no longer requires
// internet connectivity.
.dependencies = .{
.node = .{
.path = "../../node-v22.11.0",
},
// See `zig fetch --save <url>` for a command-line interface for adding dependencies.
//.example = .{
// // When updating this field to a new URL, be sure to delete the corresponding
Expand Down

0 comments on commit f2bfb6a

Please sign in to comment.