Skip to content

Commit aaebf26

Browse files
committed
make it build with zig 0.16
1 parent 9c6f8f7 commit aaebf26

File tree

1 file changed

+106
-101
lines changed

1 file changed

+106
-101
lines changed

build.zig

Lines changed: 106 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ pub fn build(b: *std.Build) void {
1414

1515
const upstream = b.dependency("libuv", .{});
1616

17+
var root_module = b.createModule(.{
18+
.target = target,
19+
.optimize = optimize,
20+
.link_libc = true,
21+
});
1722
const lib = b.addLibrary(.{
1823
.name = "uv",
1924
.linkage = .static,
20-
.root_module = b.createModule(.{
21-
.target = target,
22-
.optimize = optimize,
23-
}),
25+
.root_module = root_module,
2426
});
2527

2628
const cflags: []const []const u8 = &.{
@@ -33,34 +35,33 @@ pub fn build(b: *std.Build) void {
3335
const include_root = upstream.path("include");
3436
const test_root = upstream.path("test");
3537

36-
lib.linkLibC();
37-
lib.addCSourceFiles(.{
38+
root_module.addCSourceFiles(.{
3839
.root = src_root,
3940
.files = common_sources,
4041
.flags = cflags,
4142
});
42-
lib.addIncludePath(src_root);
43-
lib.addIncludePath(include_root);
43+
root_module.addIncludePath(src_root);
44+
root_module.addIncludePath(include_root);
4445

4546
const tinfo = target.result;
4647
switch (tinfo.os.tag) {
4748
.windows => {
48-
lib.root_module.addCMacro("_WIN32_WINNT", "0x0A00");
49-
lib.root_module.addCMacro("WIN32_LEAN_AND_MEAN", "");
50-
lib.root_module.addCMacro("_CRT_DECLARE_NONSTDC_NAMES", "0");
51-
52-
lib.linkSystemLibrary("psapi");
53-
lib.linkSystemLibrary("user32");
54-
lib.linkSystemLibrary("advapi32");
55-
lib.linkSystemLibrary("iphlpapi");
56-
lib.linkSystemLibrary("userenv");
57-
lib.linkSystemLibrary("ws2_32");
58-
lib.linkSystemLibrary("dbghelp");
59-
lib.linkSystemLibrary("ole32");
60-
lib.linkSystemLibrary("shell32");
49+
root_module.addCMacro("_WIN32_WINNT", "0x0A00");
50+
root_module.addCMacro("WIN32_LEAN_AND_MEAN", "");
51+
root_module.addCMacro("_CRT_DECLARE_NONSTDC_NAMES", "0");
52+
53+
root_module.linkSystemLibrary("psapi", .{});
54+
root_module.linkSystemLibrary("user32", .{});
55+
root_module.linkSystemLibrary("advapi32", .{});
56+
root_module.linkSystemLibrary("iphlpapi", .{});
57+
root_module.linkSystemLibrary("userenv", .{});
58+
root_module.linkSystemLibrary("ws2_32", .{});
59+
root_module.linkSystemLibrary("dbghelp", .{});
60+
root_module.linkSystemLibrary("ole32", .{});
61+
root_module.linkSystemLibrary("shell32", .{});
6162
if (optimize == .Debug)
62-
lib.linkSystemLibrary("ucrtbased");
63-
lib.addCSourceFiles(.{
63+
root_module.linkSystemLibrary("ucrtbased", .{});
64+
root_module.addCSourceFiles(.{
6465
.root = src_root,
6566
.files = win_sources,
6667
.flags = cflags,
@@ -75,9 +76,9 @@ pub fn build(b: *std.Build) void {
7576
);
7677
},
7778
else => {
78-
lib.root_module.addCMacro("_FILE_OFFSET_BITS", "64");
79-
lib.root_module.addCMacro("_LARGEFILE_SOURCE", "");
80-
lib.addCSourceFiles(.{
79+
root_module.addCMacro("_FILE_OFFSET_BITS", "64");
80+
root_module.addCMacro("_LARGEFILE_SOURCE", "");
81+
root_module.addCSourceFiles(.{
8182
.root = src_root,
8283
.files = unix_sources,
8384
.flags = cflags,
@@ -87,12 +88,12 @@ pub fn build(b: *std.Build) void {
8788
"uv/unix.h",
8889
);
8990
if (!tinfo.abi.isAndroid())
90-
lib.linkSystemLibrary("pthread");
91+
root_module.linkSystemLibrary("pthread", .{});
9192

9293
if (tinfo.os.tag.isDarwin()) {
93-
lib.root_module.addCMacro("_DARWIN_UNLIMITED_SELECT", "1");
94-
lib.root_module.addCMacro("_DARWIN_USE_64_BIT_INODE", "1");
95-
lib.addCSourceFiles(.{
94+
root_module.addCMacro("_DARWIN_UNLIMITED_SELECT", "1");
95+
root_module.addCMacro("_DARWIN_USE_64_BIT_INODE", "1");
96+
root_module.addCSourceFiles(.{
9697
.root = src_root,
9798
.files = darwin_sources,
9899
.flags = cflags,
@@ -102,9 +103,9 @@ pub fn build(b: *std.Build) void {
102103
"uv/darwin.h",
103104
);
104105
} else if (tinfo.abi.isAndroid()) {
105-
lib.root_module.addCMacro("_GNU_SOURCE", "");
106-
lib.linkSystemLibrary("dl");
107-
lib.addCSourceFiles(.{
106+
root_module.addCMacro("_GNU_SOURCE", "");
107+
root_module.linkSystemLibrary("dl", .{});
108+
root_module.addCSourceFiles(.{
108109
.root = src_root,
109110
.files = android_sources,
110111
.flags = cflags,
@@ -115,11 +116,11 @@ pub fn build(b: *std.Build) void {
115116
);
116117
} else switch (tinfo.os.tag) {
117118
.linux => {
118-
lib.root_module.addCMacro("_GNU_SOURCE", "");
119-
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112");
120-
lib.linkSystemLibrary("dl");
121-
lib.linkSystemLibrary("rt");
122-
lib.addCSourceFiles(.{
119+
root_module.addCMacro("_GNU_SOURCE", "");
120+
root_module.addCMacro("_POSIX_C_SOURCE", "200112");
121+
root_module.linkSystemLibrary("dl", .{});
122+
root_module.linkSystemLibrary("rt", .{});
123+
root_module.addCSourceFiles(.{
123124
.root = src_root,
124125
.files = linux_sources,
125126
.flags = cflags,
@@ -129,28 +130,11 @@ pub fn build(b: *std.Build) void {
129130
"uv/linux.h",
130131
);
131132
},
132-
.aix => {
133-
lib.root_module.addCMacro("_ALL_SOURCE", "");
134-
lib.root_module.addCMacro("_LINUX_SOURCE_COMPAT", "");
135-
lib.root_module.addCMacro("_THREAD_SAFE", "");
136-
lib.root_module.addCMacro("_XOPEN_SOURCE", "500");
137-
lib.root_module.addCMacro("HAVE_SYS_AHAFS_EVPRODS_H", "");
138-
lib.linkSystemLibrary("perfstat");
139-
lib.addCSourceFiles(.{
140-
.root = src_root,
141-
.files = aix_sources,
142-
.flags = cflags,
143-
});
144-
lib.installHeader(
145-
include_root.path(b, "uv/aix.h"),
146-
"uv/aix.h",
147-
);
148-
},
149133
.haiku => {
150-
lib.root_module.addCMacro("_BSD_SOURCE", "");
151-
lib.linkSystemLibrary("bsd");
152-
lib.linkSystemLibrary("network");
153-
lib.addCSourceFiles(.{
134+
root_module.addCMacro("_BSD_SOURCE", "");
135+
root_module.linkSystemLibrary("bsd", .{});
136+
root_module.linkSystemLibrary("network", .{});
137+
root_module.addCSourceFiles(.{
154138
.root = src_root,
155139
.files = haiku_sources,
156140
.flags = cflags,
@@ -161,11 +145,11 @@ pub fn build(b: *std.Build) void {
161145
);
162146
},
163147
.hurd => {
164-
lib.root_module.addCMacro("_GNU_SOURCE", "");
165-
lib.root_module.addCMacro("_POSIX_C_SOURCE", "200112");
166-
lib.root_module.addCMacro("_XOPEN_SOURCE", "500");
167-
lib.linkSystemLibrary("dl");
168-
lib.addCSourceFiles(.{
148+
root_module.addCMacro("_GNU_SOURCE", "");
149+
root_module.addCMacro("_POSIX_C_SOURCE", "200112");
150+
root_module.addCMacro("_XOPEN_SOURCE", "500");
151+
root_module.linkSystemLibrary("dl", .{});
152+
root_module.addCSourceFiles(.{
169153
.root = src_root,
170154
.files = hurd_sources,
171155
.flags = cflags,
@@ -176,7 +160,7 @@ pub fn build(b: *std.Build) void {
176160
);
177161
},
178162
.dragonfly => {
179-
lib.addCSourceFiles(.{
163+
root_module.addCSourceFiles(.{
180164
.root = src_root,
181165
.files = dragonfly_sources,
182166
.flags = cflags,
@@ -187,7 +171,7 @@ pub fn build(b: *std.Build) void {
187171
);
188172
},
189173
.freebsd => {
190-
lib.addCSourceFiles(.{
174+
root_module.addCSourceFiles(.{
191175
.root = src_root,
192176
.files = freebsd_sources,
193177
.flags = cflags,
@@ -198,8 +182,8 @@ pub fn build(b: *std.Build) void {
198182
);
199183
},
200184
.netbsd => {
201-
lib.linkSystemLibrary("kvm");
202-
lib.addCSourceFiles(.{
185+
root_module.linkSystemLibrary("kvm", .{});
186+
root_module.addCSourceFiles(.{
203187
.root = src_root,
204188
.files = netbsd_sources,
205189
.flags = cflags,
@@ -210,7 +194,7 @@ pub fn build(b: *std.Build) void {
210194
);
211195
},
212196
.openbsd => {
213-
lib.addCSourceFiles(.{
197+
root_module.addCSourceFiles(.{
214198
.root = src_root,
215199
.files = openbsd_sources,
216200
.flags = cflags,
@@ -220,15 +204,15 @@ pub fn build(b: *std.Build) void {
220204
"uv/bsd.h",
221205
);
222206
},
223-
.illumos, .solaris => {
224-
lib.root_module.addCMacro("__EXTENSIONS__", "");
225-
lib.root_module.addCMacro("_XOPEN_SOURCE", "500");
226-
lib.root_module.addCMacro("_REENTRANT", "");
227-
lib.linkSystemLibrary("kstat");
228-
lib.linkSystemLibrary("nsl");
229-
lib.linkSystemLibrary("sendfile");
230-
lib.linkSystemLibrary("socket");
231-
lib.addCSourceFiles(.{
207+
.illumos => {
208+
root_module.addCMacro("__EXTENSIONS__", "");
209+
root_module.addCMacro("_XOPEN_SOURCE", "500");
210+
root_module.addCMacro("_REENTRANT", "");
211+
root_module.linkSystemLibrary("kstat", .{});
212+
root_module.linkSystemLibrary("nsl", .{});
213+
root_module.linkSystemLibrary("sendfile", .{});
214+
root_module.linkSystemLibrary("socket", .{});
215+
root_module.addCSourceFiles(.{
232216
.root = src_root,
233217
.files = solaris_sources,
234218
.flags = cflags,
@@ -238,7 +222,26 @@ pub fn build(b: *std.Build) void {
238222
"uv/sunos.h",
239223
);
240224
},
241-
else => @panic("Unsupported build target"),
225+
else => {
226+
// aix is removed in zig 0.16
227+
if (@hasDecl(std.Target.Os.Tag, "aix") and tinfo.os.tag == .aix) {
228+
root_module.addCMacro("_ALL_SOURCE", "");
229+
root_module.addCMacro("_LINUX_SOURCE_COMPAT", "");
230+
root_module.addCMacro("_THREAD_SAFE", "");
231+
root_module.addCMacro("_XOPEN_SOURCE", "500");
232+
root_module.addCMacro("HAVE_SYS_AHAFS_EVPRODS_H", "");
233+
root_module.linkSystemLibrary("perfstat", .{});
234+
root_module.addCSourceFiles(.{
235+
.root = src_root,
236+
.files = aix_sources,
237+
.flags = cflags,
238+
});
239+
lib.installHeader(
240+
include_root.path(b, "uv/aix.h"),
241+
"uv/aix.h",
242+
);
243+
} else @panic("Unsupported build target");
244+
},
242245
}
243246
},
244247
}
@@ -253,75 +256,77 @@ pub fn build(b: *std.Build) void {
253256
b.installArtifact(lib);
254257

255258
if (build_tests) {
259+
var tests_module = b.createModule(.{
260+
.target = target,
261+
.optimize = optimize,
262+
});
256263
const tests = b.addExecutable(.{
257264
.name = "uv_run_tests_a",
258-
.root_module = b.createModule(.{
259-
.target = target,
260-
.optimize = optimize,
261-
}),
265+
.root_module = tests_module,
262266
});
263-
tests.addCSourceFiles(.{
267+
tests_module.addCSourceFiles(.{
264268
.root = test_root,
265269
.files = test_sources,
266270
.flags = cflags,
267271
});
268272
if (tinfo.os.tag == .windows) {
269-
tests.addCSourceFiles(.{
273+
tests_module.addCSourceFiles(.{
270274
.root = test_root,
271275
.files = win_test_sources,
272276
.flags = cflags,
273277
});
274-
tests.addCSourceFile(.{
278+
tests_module.addCSourceFile(.{
275279
.file = src_root.path(b, "win/snprintf.c"),
276280
.flags = cflags,
277281
});
278282
} else {
279-
tests.addCSourceFiles(.{
283+
tests_module.addCSourceFiles(.{
280284
.root = test_root,
281285
.files = unix_test_sources,
282286
.flags = cflags,
283287
});
284288
}
285-
tests.addIncludePath(src_root);
286-
tests.addIncludePath(include_root);
287-
tests.linkLibrary(lib);
289+
tests_module.addIncludePath(src_root);
290+
tests_module.addIncludePath(include_root);
291+
tests_module.linkLibrary(lib);
288292
b.installArtifact(tests);
289293
}
290294

291295
if (build_benchmarks) {
296+
var benchmarks_module = b.createModule(.{
297+
.target = target,
298+
.optimize = optimize,
299+
});
292300
const benchmarks = b.addExecutable(.{
293301
.name = "uv_run_benchmarks_a",
294-
.root_module = b.createModule(.{
295-
.target = target,
296-
.optimize = optimize,
297-
}),
302+
.root_module = benchmarks_module,
298303
});
299304

300-
benchmarks.addCSourceFiles(.{
305+
benchmarks_module.addCSourceFiles(.{
301306
.root = test_root,
302307
.files = benchmark_sources,
303308
.flags = cflags,
304309
});
305310
if (tinfo.os.tag == .windows) {
306-
benchmarks.addCSourceFiles(.{
311+
benchmarks_module.addCSourceFiles(.{
307312
.root = test_root,
308313
.files = win_test_sources,
309314
.flags = cflags,
310315
});
311-
benchmarks.addCSourceFile(.{
316+
benchmarks_module.addCSourceFile(.{
312317
.file = src_root.path(b, "win/snprintf.c"),
313318
.flags = cflags,
314319
});
315320
} else {
316-
benchmarks.addCSourceFiles(.{
321+
benchmarks_module.addCSourceFiles(.{
317322
.root = test_root,
318323
.files = unix_test_sources,
319324
.flags = cflags,
320325
});
321326
}
322-
benchmarks.addIncludePath(src_root);
323-
benchmarks.addIncludePath(include_root);
324-
benchmarks.linkLibrary(lib);
327+
benchmarks_module.addIncludePath(src_root);
328+
benchmarks_module.addIncludePath(include_root);
329+
benchmarks_module.linkLibrary(lib);
325330
b.installArtifact(benchmarks);
326331
}
327332
}

0 commit comments

Comments
 (0)