forked from mitchellh/zig-libuv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
202 lines (179 loc) · 5.8 KB
/
build.zig
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const std = @import("std");
/// Directories with our includes.
const root = thisDir() ++ "/vendor/libuv/";
const include_path = root ++ "include";
pub const pkg = .{
.name = "libuv",
.source = .{ .path = thisDir() ++ "/src/main.zig" },
};
fn thisDir() []const u8 {
return std.fs.path.dirname(@src().file) orelse ".";
}
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Create module
_ = b.addModule("libuv", .{
.root_source_file = .{ .cwd_relative = "src/main.zig" },
});
const tests = b.addTest(.{
.name = "pixman-test",
.root_source_file = .{ .cwd_relative = "src/main.zig" },
.target = target,
.optimize = optimize,
});
_ = try link(b, tests);
b.installArtifact(tests);
const test_step = b.step("test", "Run tests");
const tests_run = b.addRunArtifact(tests);
test_step.dependOn(&tests_run.step);
}
pub fn link(b: *std.Build, step: *std.Build.Step.Compile) !*std.Build.Step.Compile {
const libuv = try buildLibuv(b, step);
step.linkLibrary(libuv);
step.addIncludePath(.{ .cwd_relative = include_path });
return libuv;
}
pub fn buildLibuv(
b: *std.Build,
step: *std.Build.Step.Compile,
) !*std.Build.Step.Compile {
const target = step.root_module.resolved_target.?;
const lib = b.addStaticLibrary(.{
.name = "uv",
.target = target,
.optimize = step.root_module.optimize.?,
});
// Include dirs
lib.addIncludePath(.{ .cwd_relative = include_path });
lib.addIncludePath(.{ .cwd_relative = root ++ "src" });
// Links
if (target.result.os.tag == .windows) {
lib.linkSystemLibrary("psapi");
lib.linkSystemLibrary("user32");
lib.linkSystemLibrary("advapi32");
lib.linkSystemLibrary("iphlpapi");
lib.linkSystemLibrary("userenv");
lib.linkSystemLibrary("ws2_32");
}
if (target.result.os.tag == .linux) {
lib.linkSystemLibrary("pthread");
}
lib.linkLibC();
// Compilation
var flags = std.ArrayList([]const u8).init(b.allocator);
defer flags.deinit();
// try flags.appendSlice(&.{});
if (target.result.os.tag != .windows) {
try flags.appendSlice(&.{
"-D_FILE_OFFSET_BITS=64",
"-D_LARGEFILE_SOURCE",
});
}
if (target.result.os.tag == .linux) {
try flags.appendSlice(&.{
"-D_GNU_SOURCE",
"-D_POSIX_C_SOURCE=200112",
});
}
if (target.result.os.tag == .macos) {
try flags.appendSlice(&.{
"-D_DARWIN_UNLIMITED_SELECT=1",
"-D_DARWIN_USE_64_BIT_INODE=1",
});
}
// C files common to all platforms
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/fs-poll.c",
root ++ "src/idna.c",
root ++ "src/inet.c",
root ++ "src/random.c",
root ++ "src/strscpy.c",
root ++ "src/strtok.c",
root ++ "src/threadpool.c",
root ++ "src/timer.c",
root ++ "src/uv-common.c",
root ++ "src/uv-data-getter-setters.c",
root ++ "src/version.c",
},
.flags = flags.items,
});
if (target.result.os.tag != .windows) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/async.c",
root ++ "src/unix/core.c",
root ++ "src/unix/dl.c",
root ++ "src/unix/fs.c",
root ++ "src/unix/getaddrinfo.c",
root ++ "src/unix/getnameinfo.c",
root ++ "src/unix/loop-watcher.c",
root ++ "src/unix/loop.c",
root ++ "src/unix/pipe.c",
root ++ "src/unix/poll.c",
root ++ "src/unix/process.c",
root ++ "src/unix/random-devurandom.c",
root ++ "src/unix/signal.c",
root ++ "src/unix/stream.c",
root ++ "src/unix/tcp.c",
root ++ "src/unix/thread.c",
root ++ "src/unix/tty.c",
root ++ "src/unix/udp.c",
},
.flags = flags.items,
});
}
if (target.result.os.tag == .linux or target.result.os.tag == .macos) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/proctitle.c",
},
.flags = flags.items,
});
}
if (target.result.os.tag == .linux) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/linux.c",
root ++ "src/unix/procfs-exepath.c",
root ++ "src/unix/random-getrandom.c",
root ++ "src/unix/random-sysctl-linux.c",
},
.flags = flags.items,
});
}
if (target.result.os.tag == .macos or
target.result.os.tag == .openbsd or
target.result.os.tag == .netbsd or
target.result.os.tag == .freebsd or
target.result.os.tag == .dragonfly)
{
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/bsd-ifaddrs.c",
root ++ "src/unix/kqueue.c",
},
.flags = flags.items,
});
}
if (target.result.os.tag == .macos or target.result.os.tag == .openbsd) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/random-getentropy.c",
},
.flags = flags.items,
});
}
if (target.result.os.tag == .macos) {
lib.addCSourceFiles(.{
.files = &.{
root ++ "src/unix/darwin-proctitle.c",
root ++ "src/unix/darwin.c",
root ++ "src/unix/fsevents.c",
},
.flags = flags.items,
});
}
return lib;
}