Skip to content

Commit 78a55a7

Browse files
committed
make it build with zig 0.16
1 parent a2dfd38 commit 78a55a7

File tree

1 file changed

+123
-101
lines changed

1 file changed

+123
-101
lines changed

build.zig

Lines changed: 123 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,43 @@ pub fn build(b: *std.Build) void {
238222
"uv/sunos.h",
239223
);
240224
},
241-
else => @panic("Unsupported build target"),
225+
else => {
226+
// aix and solaris are 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 if (@hasDecl(std.Target.Os.Tag, "solaris") and tinfo.os.tag == .solaris) {
244+
root_module.addCMacro("__EXTENSIONS__", "");
245+
root_module.addCMacro("_XOPEN_SOURCE", "500");
246+
root_module.addCMacro("_REENTRANT", "");
247+
root_module.linkSystemLibrary("kstat", .{});
248+
root_module.linkSystemLibrary("nsl", .{});
249+
root_module.linkSystemLibrary("sendfile", .{});
250+
root_module.linkSystemLibrary("socket", .{});
251+
root_module.addCSourceFiles(.{
252+
.root = src_root,
253+
.files = solaris_sources,
254+
.flags = cflags,
255+
});
256+
lib.installHeader(
257+
include_root.path(b, "uv/sunos.h"),
258+
"uv/sunos.h",
259+
);
260+
} else @panic("Unsupported build target");
261+
},
242262
}
243263
},
244264
}
@@ -253,75 +273,77 @@ pub fn build(b: *std.Build) void {
253273
b.installArtifact(lib);
254274

255275
if (build_tests) {
276+
var tests_module = b.createModule(.{
277+
.target = target,
278+
.optimize = optimize,
279+
});
256280
const tests = b.addExecutable(.{
257281
.name = "uv_run_tests_a",
258-
.root_module = b.createModule(.{
259-
.target = target,
260-
.optimize = optimize,
261-
}),
282+
.root_module = tests_module,
262283
});
263-
tests.addCSourceFiles(.{
284+
tests_module.addCSourceFiles(.{
264285
.root = test_root,
265286
.files = test_sources,
266287
.flags = cflags,
267288
});
268289
if (tinfo.os.tag == .windows) {
269-
tests.addCSourceFiles(.{
290+
tests_module.addCSourceFiles(.{
270291
.root = test_root,
271292
.files = win_test_sources,
272293
.flags = cflags,
273294
});
274-
tests.addCSourceFile(.{
295+
tests_module.addCSourceFile(.{
275296
.file = src_root.path(b, "win/snprintf.c"),
276297
.flags = cflags,
277298
});
278299
} else {
279-
tests.addCSourceFiles(.{
300+
tests_module.addCSourceFiles(.{
280301
.root = test_root,
281302
.files = unix_test_sources,
282303
.flags = cflags,
283304
});
284305
}
285-
tests.addIncludePath(src_root);
286-
tests.addIncludePath(include_root);
287-
tests.linkLibrary(lib);
306+
tests_module.addIncludePath(src_root);
307+
tests_module.addIncludePath(include_root);
308+
tests_module.linkLibrary(lib);
288309
b.installArtifact(tests);
289310
}
290311

291312
if (build_benchmarks) {
313+
var benchmarks_module = b.createModule(.{
314+
.target = target,
315+
.optimize = optimize,
316+
});
292317
const benchmarks = b.addExecutable(.{
293318
.name = "uv_run_benchmarks_a",
294-
.root_module = b.createModule(.{
295-
.target = target,
296-
.optimize = optimize,
297-
}),
319+
.root_module = benchmarks_module,
298320
});
299321

300-
benchmarks.addCSourceFiles(.{
322+
benchmarks_module.addCSourceFiles(.{
301323
.root = test_root,
302324
.files = benchmark_sources,
303325
.flags = cflags,
304326
});
305327
if (tinfo.os.tag == .windows) {
306-
benchmarks.addCSourceFiles(.{
328+
benchmarks_module.addCSourceFiles(.{
307329
.root = test_root,
308330
.files = win_test_sources,
309331
.flags = cflags,
310332
});
311-
benchmarks.addCSourceFile(.{
333+
benchmarks_module.addCSourceFile(.{
312334
.file = src_root.path(b, "win/snprintf.c"),
313335
.flags = cflags,
314336
});
315337
} else {
316-
benchmarks.addCSourceFiles(.{
338+
benchmarks_module.addCSourceFiles(.{
317339
.root = test_root,
318340
.files = unix_test_sources,
319341
.flags = cflags,
320342
});
321343
}
322-
benchmarks.addIncludePath(src_root);
323-
benchmarks.addIncludePath(include_root);
324-
benchmarks.linkLibrary(lib);
344+
benchmarks_module.addIncludePath(src_root);
345+
benchmarks_module.addIncludePath(include_root);
346+
benchmarks_module.linkLibrary(lib);
325347
b.installArtifact(benchmarks);
326348
}
327349
}

0 commit comments

Comments
 (0)