make it build with zig 0.16

This commit is contained in:
Chinmay Dalal
2026-01-05 16:14:52 -05:00
committed by torque
parent 9c6f8f7e9a
commit ddf88a5238

205
build.zig
View File

@@ -14,13 +14,15 @@ pub fn build(b: *std.Build) void {
const upstream = b.dependency("libuv", .{}); const upstream = b.dependency("libuv", .{});
var root_module = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
const lib = b.addLibrary(.{ const lib = b.addLibrary(.{
.name = "uv", .name = "uv",
.linkage = .static, .linkage = .static,
.root_module = b.createModule(.{ .root_module = root_module,
.target = target,
.optimize = optimize,
}),
}); });
const cflags: []const []const u8 = &.{ const cflags: []const []const u8 = &.{
@@ -33,34 +35,33 @@ pub fn build(b: *std.Build) void {
const include_root = upstream.path("include"); const include_root = upstream.path("include");
const test_root = upstream.path("test"); const test_root = upstream.path("test");
lib.linkLibC(); root_module.addCSourceFiles(.{
lib.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = common_sources, .files = common_sources,
.flags = cflags, .flags = cflags,
}); });
lib.addIncludePath(src_root); root_module.addIncludePath(src_root);
lib.addIncludePath(include_root); root_module.addIncludePath(include_root);
const tinfo = target.result; const tinfo = target.result;
switch (tinfo.os.tag) { switch (tinfo.os.tag) {
.windows => { .windows => {
lib.root_module.addCMacro("_WIN32_WINNT", "0x0A00"); root_module.addCMacro("_WIN32_WINNT", "0x0A00");
lib.root_module.addCMacro("WIN32_LEAN_AND_MEAN", ""); root_module.addCMacro("WIN32_LEAN_AND_MEAN", "");
lib.root_module.addCMacro("_CRT_DECLARE_NONSTDC_NAMES", "0"); root_module.addCMacro("_CRT_DECLARE_NONSTDC_NAMES", "0");
lib.linkSystemLibrary("psapi"); root_module.linkSystemLibrary("psapi", .{});
lib.linkSystemLibrary("user32"); root_module.linkSystemLibrary("user32", .{});
lib.linkSystemLibrary("advapi32"); root_module.linkSystemLibrary("advapi32", .{});
lib.linkSystemLibrary("iphlpapi"); root_module.linkSystemLibrary("iphlpapi", .{});
lib.linkSystemLibrary("userenv"); root_module.linkSystemLibrary("userenv", .{});
lib.linkSystemLibrary("ws2_32"); root_module.linkSystemLibrary("ws2_32", .{});
lib.linkSystemLibrary("dbghelp"); root_module.linkSystemLibrary("dbghelp", .{});
lib.linkSystemLibrary("ole32"); root_module.linkSystemLibrary("ole32", .{});
lib.linkSystemLibrary("shell32"); root_module.linkSystemLibrary("shell32", .{});
if (optimize == .Debug) if (optimize == .Debug)
lib.linkSystemLibrary("ucrtbased"); root_module.linkSystemLibrary("ucrtbased", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = win_sources, .files = win_sources,
.flags = cflags, .flags = cflags,
@@ -75,9 +76,9 @@ pub fn build(b: *std.Build) void {
); );
}, },
else => { else => {
lib.root_module.addCMacro("_FILE_OFFSET_BITS", "64"); root_module.addCMacro("_FILE_OFFSET_BITS", "64");
lib.root_module.addCMacro("_LARGEFILE_SOURCE", ""); root_module.addCMacro("_LARGEFILE_SOURCE", "");
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = unix_sources, .files = unix_sources,
.flags = cflags, .flags = cflags,
@@ -87,12 +88,12 @@ pub fn build(b: *std.Build) void {
"uv/unix.h", "uv/unix.h",
); );
if (!tinfo.abi.isAndroid()) if (!tinfo.abi.isAndroid())
lib.linkSystemLibrary("pthread"); root_module.linkSystemLibrary("pthread", .{});
if (tinfo.os.tag.isDarwin()) { if (tinfo.os.tag.isDarwin()) {
lib.root_module.addCMacro("_DARWIN_UNLIMITED_SELECT", "1"); root_module.addCMacro("_DARWIN_UNLIMITED_SELECT", "1");
lib.root_module.addCMacro("_DARWIN_USE_64_BIT_INODE", "1"); root_module.addCMacro("_DARWIN_USE_64_BIT_INODE", "1");
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = darwin_sources, .files = darwin_sources,
.flags = cflags, .flags = cflags,
@@ -102,9 +103,9 @@ pub fn build(b: *std.Build) void {
"uv/darwin.h", "uv/darwin.h",
); );
} else if (tinfo.abi.isAndroid()) { } else if (tinfo.abi.isAndroid()) {
lib.root_module.addCMacro("_GNU_SOURCE", ""); root_module.addCMacro("_GNU_SOURCE", "");
lib.linkSystemLibrary("dl"); root_module.linkSystemLibrary("dl", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = android_sources, .files = android_sources,
.flags = cflags, .flags = cflags,
@@ -115,11 +116,11 @@ pub fn build(b: *std.Build) void {
); );
} else switch (tinfo.os.tag) { } else switch (tinfo.os.tag) {
.linux => { .linux => {
lib.root_module.addCMacro("_GNU_SOURCE", ""); root_module.addCMacro("_GNU_SOURCE", "");
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112"); root_module.addCMacro("_POSIX_C_SOURCE", "200112");
lib.linkSystemLibrary("dl"); root_module.linkSystemLibrary("dl", .{});
lib.linkSystemLibrary("rt"); root_module.linkSystemLibrary("rt", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = linux_sources, .files = linux_sources,
.flags = cflags, .flags = cflags,
@@ -129,28 +130,11 @@ pub fn build(b: *std.Build) void {
"uv/linux.h", "uv/linux.h",
); );
}, },
.aix => {
lib.root_module.addCMacro("_ALL_SOURCE", "");
lib.root_module.addCMacro("_LINUX_SOURCE_COMPAT", "");
lib.root_module.addCMacro("_THREAD_SAFE", "");
lib.root_module.addCMacro("_XOPEN_SOURCE", "500");
lib.root_module.addCMacro("HAVE_SYS_AHAFS_EVPRODS_H", "");
lib.linkSystemLibrary("perfstat");
lib.addCSourceFiles(.{
.root = src_root,
.files = aix_sources,
.flags = cflags,
});
lib.installHeader(
include_root.path(b, "uv/aix.h"),
"uv/aix.h",
);
},
.haiku => { .haiku => {
lib.root_module.addCMacro("_BSD_SOURCE", ""); root_module.addCMacro("_BSD_SOURCE", "");
lib.linkSystemLibrary("bsd"); root_module.linkSystemLibrary("bsd", .{});
lib.linkSystemLibrary("network"); root_module.linkSystemLibrary("network", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = haiku_sources, .files = haiku_sources,
.flags = cflags, .flags = cflags,
@@ -161,11 +145,11 @@ pub fn build(b: *std.Build) void {
); );
}, },
.hurd => { .hurd => {
lib.root_module.addCMacro("_GNU_SOURCE", ""); root_module.addCMacro("_GNU_SOURCE", "");
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112"); root_module.addCMacro("_POSIX_C_SOURCE", "200112");
lib.root_module.addCMacro("_XOPEN_SOURCE", "500"); root_module.addCMacro("_XOPEN_SOURCE", "500");
lib.linkSystemLibrary("dl"); root_module.linkSystemLibrary("dl", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = hurd_sources, .files = hurd_sources,
.flags = cflags, .flags = cflags,
@@ -176,7 +160,7 @@ pub fn build(b: *std.Build) void {
); );
}, },
.dragonfly => { .dragonfly => {
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = dragonfly_sources, .files = dragonfly_sources,
.flags = cflags, .flags = cflags,
@@ -187,7 +171,7 @@ pub fn build(b: *std.Build) void {
); );
}, },
.freebsd => { .freebsd => {
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = freebsd_sources, .files = freebsd_sources,
.flags = cflags, .flags = cflags,
@@ -198,8 +182,8 @@ pub fn build(b: *std.Build) void {
); );
}, },
.netbsd => { .netbsd => {
lib.linkSystemLibrary("kvm"); root_module.linkSystemLibrary("kvm", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = netbsd_sources, .files = netbsd_sources,
.flags = cflags, .flags = cflags,
@@ -210,7 +194,7 @@ pub fn build(b: *std.Build) void {
); );
}, },
.openbsd => { .openbsd => {
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = openbsd_sources, .files = openbsd_sources,
.flags = cflags, .flags = cflags,
@@ -220,15 +204,15 @@ pub fn build(b: *std.Build) void {
"uv/bsd.h", "uv/bsd.h",
); );
}, },
.illumos, .solaris => { .illumos => {
lib.root_module.addCMacro("__EXTENSIONS__", ""); root_module.addCMacro("__EXTENSIONS__", "");
lib.root_module.addCMacro("_XOPEN_SOURCE", "500"); root_module.addCMacro("_XOPEN_SOURCE", "500");
lib.root_module.addCMacro("_REENTRANT", ""); root_module.addCMacro("_REENTRANT", "");
lib.linkSystemLibrary("kstat"); root_module.linkSystemLibrary("kstat", .{});
lib.linkSystemLibrary("nsl"); root_module.linkSystemLibrary("nsl", .{});
lib.linkSystemLibrary("sendfile"); root_module.linkSystemLibrary("sendfile", .{});
lib.linkSystemLibrary("socket"); root_module.linkSystemLibrary("socket", .{});
lib.addCSourceFiles(.{ root_module.addCSourceFiles(.{
.root = src_root, .root = src_root,
.files = solaris_sources, .files = solaris_sources,
.flags = cflags, .flags = cflags,
@@ -238,7 +222,26 @@ pub fn build(b: *std.Build) void {
"uv/sunos.h", "uv/sunos.h",
); );
}, },
else => @panic("Unsupported build target"), else => {
// aix is removed in zig 0.16
if (@hasDecl(std.Target.Os.Tag, "aix") and tinfo.os.tag == .aix) {
root_module.addCMacro("_ALL_SOURCE", "");
root_module.addCMacro("_LINUX_SOURCE_COMPAT", "");
root_module.addCMacro("_THREAD_SAFE", "");
root_module.addCMacro("_XOPEN_SOURCE", "500");
root_module.addCMacro("HAVE_SYS_AHAFS_EVPRODS_H", "");
root_module.linkSystemLibrary("perfstat", .{});
root_module.addCSourceFiles(.{
.root = src_root,
.files = aix_sources,
.flags = cflags,
});
lib.installHeader(
include_root.path(b, "uv/aix.h"),
"uv/aix.h",
);
} else @panic("Unsupported build target");
},
} }
}, },
} }
@@ -253,75 +256,77 @@ pub fn build(b: *std.Build) void {
b.installArtifact(lib); b.installArtifact(lib);
if (build_tests) { if (build_tests) {
const tests = b.addExecutable(.{ var tests_module = b.createModule(.{
.name = "uv_run_tests_a",
.root_module = b.createModule(.{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}),
}); });
tests.addCSourceFiles(.{ const tests = b.addExecutable(.{
.name = "uv_run_tests_a",
.root_module = tests_module,
});
tests_module.addCSourceFiles(.{
.root = test_root, .root = test_root,
.files = test_sources, .files = test_sources,
.flags = cflags, .flags = cflags,
}); });
if (tinfo.os.tag == .windows) { if (tinfo.os.tag == .windows) {
tests.addCSourceFiles(.{ tests_module.addCSourceFiles(.{
.root = test_root, .root = test_root,
.files = win_test_sources, .files = win_test_sources,
.flags = cflags, .flags = cflags,
}); });
tests.addCSourceFile(.{ tests_module.addCSourceFile(.{
.file = src_root.path(b, "win/snprintf.c"), .file = src_root.path(b, "win/snprintf.c"),
.flags = cflags, .flags = cflags,
}); });
} else { } else {
tests.addCSourceFiles(.{ tests_module.addCSourceFiles(.{
.root = test_root, .root = test_root,
.files = unix_test_sources, .files = unix_test_sources,
.flags = cflags, .flags = cflags,
}); });
} }
tests.addIncludePath(src_root); tests_module.addIncludePath(src_root);
tests.addIncludePath(include_root); tests_module.addIncludePath(include_root);
tests.linkLibrary(lib); tests_module.linkLibrary(lib);
b.installArtifact(tests); b.installArtifact(tests);
} }
if (build_benchmarks) { if (build_benchmarks) {
const benchmarks = b.addExecutable(.{ var benchmarks_module = b.createModule(.{
.name = "uv_run_benchmarks_a",
.root_module = b.createModule(.{
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}), });
const benchmarks = b.addExecutable(.{
.name = "uv_run_benchmarks_a",
.root_module = benchmarks_module,
}); });
benchmarks.addCSourceFiles(.{ benchmarks_module.addCSourceFiles(.{
.root = test_root, .root = test_root,
.files = benchmark_sources, .files = benchmark_sources,
.flags = cflags, .flags = cflags,
}); });
if (tinfo.os.tag == .windows) { if (tinfo.os.tag == .windows) {
benchmarks.addCSourceFiles(.{ benchmarks_module.addCSourceFiles(.{
.root = test_root, .root = test_root,
.files = win_test_sources, .files = win_test_sources,
.flags = cflags, .flags = cflags,
}); });
benchmarks.addCSourceFile(.{ benchmarks_module.addCSourceFile(.{
.file = src_root.path(b, "win/snprintf.c"), .file = src_root.path(b, "win/snprintf.c"),
.flags = cflags, .flags = cflags,
}); });
} else { } else {
benchmarks.addCSourceFiles(.{ benchmarks_module.addCSourceFiles(.{
.root = test_root, .root = test_root,
.files = unix_test_sources, .files = unix_test_sources,
.flags = cflags, .flags = cflags,
}); });
} }
benchmarks.addIncludePath(src_root); benchmarks_module.addIncludePath(src_root);
benchmarks.addIncludePath(include_root); benchmarks_module.addIncludePath(include_root);
benchmarks.linkLibrary(lib); benchmarks_module.linkLibrary(lib);
b.installArtifact(benchmarks); b.installArtifact(benchmarks);
} }
} }