-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbuild.zig
More file actions
128 lines (112 loc) · 4.61 KB
/
build.zig
File metadata and controls
128 lines (112 loc) · 4.61 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
const std = @import("std");
const cross_targets: []const std.Target.Query = &.{
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .gnu },
.{ .cpu_arch = .aarch64, .os_tag = .linux, .abi = .musl },
.{ .cpu_arch = .x86_64, .os_tag = .macos },
.{ .cpu_arch = .aarch64, .os_tag = .macos },
.{ .cpu_arch = .x86_64, .os_tag = .windows },
.{ .cpu_arch = .aarch64, .os_tag = .windows },
};
pub fn build(b: *std.Build) void {
b.install_path = "prebuilds";
const optimize: std.builtin.OptimizeMode = b.option(std.builtin.OptimizeMode, "optimize", "Optimization mode") orelse .ReleaseSmall;
const target_query = b.standardTargetOptionsQueryOnly(.{});
// --- Zig package module (pure library, no NAPI) ---
_ = b.addModule("zigpty", .{
.root_source_file = b.path("zig/lib.zig"),
.target = b.resolveTargetQuery(target_query),
.optimize = optimize,
.link_libc = true,
});
// --- Zig tests ---
const test_mod = b.createModule(.{
.root_source_file = b.path("zig/lib.zig"),
.target = b.resolveTargetQuery(target_query),
.optimize = optimize,
.link_libc = true,
});
if (b.resolveTargetQuery(target_query).result.os.tag == .linux) {
test_mod.linkSystemLibrary("util", .{});
}
const lib_tests = b.addTest(.{ .root_module = test_mod });
const run_tests = b.addRunArtifact(lib_tests);
const test_step = b.step("test", "Run Zig unit tests");
test_step.dependOn(&run_tests.step);
// --- NAPI shared library builds ---
const clean = &b.addRemoveDirTree(.{ .cwd_relative = "prebuilds" }).step;
if (target_query.isNative()) {
addTarget(b, clean, b.resolveTargetQuery(target_query), optimize);
for (cross_targets) |ct| {
addTarget(b, clean, b.resolveTargetQuery(ct), optimize);
}
} else {
addTarget(b, clean, b.resolveTargetQuery(target_query), optimize);
}
}
fn addTarget(b: *std.Build, clean: *std.Build.Step, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) void {
const is_windows = target.result.os.tag == .windows;
const is_musl = target.result.abi == .musl or target.result.abi == .musleabi;
const mod = b.createModule(.{
.root_source_file = b.path("zig/root.zig"),
.target = target,
.optimize = optimize,
.link_libc = !is_windows,
});
// Musl builds: add errno shim for Android/Bionic compat
// (weak symbols — overridden by musl's libc.so on real Linux, active on Android)
if (is_musl) {
mod.addCSourceFile(.{ .file = b.path("zig/errno_shim.c") });
}
// Link libutil for forkpty/openpty (glibc needs it; musl has it in libc)
if (target.result.os.tag == .linux and !is_musl) {
mod.linkSystemLibrary("util", .{});
}
// On Windows, generate NAPI import lib from .def file via dlltool
if (is_windows) {
const machine: []const u8 = switch (target.result.cpu.arch) {
.x86_64 => "i386:x86-64",
.aarch64 => "arm64",
else => unreachable,
};
const dlltool = b.addSystemCommand(&.{
"zig", "dlltool",
"-d", b.pathFromRoot("zig/win/node_api.def"),
"-m", machine,
"-D", "node.exe",
});
const import_lib = dlltool.addPrefixedOutputFileArg("-l", "node_api.lib");
mod.addObjectFile(import_lib);
}
const lib = b.addLibrary(.{
.name = "zigpty",
.root_module = mod,
.linkage = .dynamic,
});
// Allow undefined symbols (resolved by Node.js at runtime)
lib.linker_allow_shlib_undefined = true;
// Build platform-specific output name: zigpty.<os>-<arch>[-musl].node
const os_name: []const u8 = switch (target.result.os.tag) {
.linux => "linux",
.macos => "darwin",
.windows => "win32",
else => @tagName(target.result.os.tag),
};
const arch_name: []const u8 = switch (target.result.cpu.arch) {
.x86_64 => "x64",
.aarch64 => "arm64",
else => @tagName(target.result.cpu.arch),
};
const abi_suffix: []const u8 = switch (target.result.abi) {
.musl, .musleabi => "-musl",
else => "",
};
const dest_name = b.fmt("zigpty.{s}-{s}{s}.node", .{ os_name, arch_name, abi_suffix });
const install = b.addInstallArtifact(lib, .{
.dest_dir = .{ .override = .{ .custom = "" } },
.dest_sub_path = dest_name,
});
install.step.dependOn(clean);
b.getInstallStep().dependOn(&install.step);
}