-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbuild.zig
198 lines (180 loc) · 7.51 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
const builtin = @import("builtin");
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const sanitize = b.option(bool, "sanitize", "use sanitizers when running examples or tests") orelse false;
const single_threaded = b.option(bool, "single-threaded", "use only a single thread") orelse null;
var aio_opts = b.addOptions();
{
const debug = b.option(bool, "aio:debug", "enable debug prints") orelse false;
aio_opts.addOption(bool, "debug", debug);
const PosixMode = enum { auto, force, disable };
const posix = b.option(PosixMode, "aio:posix", "posix mode") orelse .auto;
aio_opts.addOption(PosixMode, "posix", posix);
const WasiMode = enum { wasi, wasix };
const wasi = b.option(WasiMode, "aio:wasi", "wasi mode") orelse .wasi;
aio_opts.addOption(WasiMode, "wasi", wasi);
}
var coro_opts = b.addOptions();
{
const debug = b.option(bool, "coro:debug", "enable debug prints") orelse false;
coro_opts.addOption(bool, "debug", debug);
}
const minilib = b.addModule("minilib", .{
.root_source_file = b.path("src/minilib.zig"),
.target = target,
.optimize = optimize,
});
const aio = b.addModule("aio", .{
.root_source_file = b.path("src/aio.zig"),
.target = target,
.optimize = optimize,
.link_libc = switch (target.query.os_tag orelse builtin.os.tag) {
.windows => true,
.freebsd, .openbsd, .dragonfly, .netbsd => true,
else => false,
},
});
aio.addImport("minilib", minilib);
aio.addImport("build_options", aio_opts.createModule());
if (target.query.os_tag orelse builtin.os.tag == .windows) {
if (b.lazyDependency("zigwin32", .{})) |zigwin32| {
aio.addImport("win32", zigwin32.module("win32"));
}
}
const coro = b.addModule("coro", .{
.root_source_file = b.path("src/coro.zig"),
.target = target,
.optimize = optimize,
});
coro.addImport("minilib", minilib);
coro.addImport("aio", aio);
coro.addImport("build_options", coro_opts.createModule());
const run_all = b.step("example", "Run all examples");
inline for (.{
.aio_dynamic,
.aio_immediate,
.coro,
.coro_wttr,
}) |example| {
const os = target.query.os_tag orelse builtin.os.tag;
const exe = b.addExecutable(.{
.name = @tagName(example),
.root_source_file = b.path("examples/" ++ @tagName(example) ++ ".zig"),
.target = target,
.optimize = optimize,
.sanitize_thread = sanitize,
.single_threaded = if (example == .coro_wttr and os != .wasi) false else single_threaded,
.strip = false,
});
exe.root_module.addImport("aio", aio);
exe.root_module.addImport("coro", coro);
const install = b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "example" } } });
b.getInstallStep().dependOn(&install.step);
var cmd = makeRunStep(b, target, exe, "example:" ++ @tagName(example), "Run " ++ @tagName(example) ++ " example", .{});
run_all.dependOn(&cmd.step);
}
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match any filter") orelse "";
const test_step = b.step("test", "Run unit tests");
inline for (.{ .minilib, .aio, .coro }) |mod| {
const tst = b.addTest(.{
.root_source_file = b.path("src/" ++ @tagName(mod) ++ ".zig"),
.target = target,
.optimize = optimize,
.filters = &.{test_filter},
.link_libc = aio.link_libc,
.sanitize_thread = sanitize,
.single_threaded = single_threaded,
.strip = false,
});
switch (mod) {
.minilib => addImportsFrom(tst.root_module, minilib),
.aio => addImportsFrom(tst.root_module, aio),
.coro => addImportsFrom(tst.root_module, coro),
else => unreachable,
}
var cmd = makeRunStep(b, target, tst, "test:" ++ @tagName(mod), "Run " ++ @tagName(mod) ++ " tests", .{});
test_step.dependOn(&cmd.step);
}
const bug_step = b.step("bug", "Run regression tests");
inline for (.{
.@"22",
.@"31",
.@"33",
.@"67",
.ticker,
.backend_override,
}) |bug| {
const exe = b.addExecutable(.{
.name = @tagName(bug),
.root_source_file = b.path("bugs/" ++ @tagName(bug) ++ ".zig"),
.target = target,
.optimize = switch (bug) {
// fails on io_uring if sanitize == true and optimize == debug, not sure why
.ticker => if (sanitize) .ReleaseFast else optimize,
else => optimize,
},
.sanitize_thread = sanitize,
.single_threaded = single_threaded,
.strip = false,
});
exe.root_module.addImport("aio", aio);
exe.root_module.addImport("coro", coro);
var cmd = makeRunStep(b, target, exe, "bug:" ++ @tagName(bug), "Check regression for #" ++ @tagName(bug), .{});
bug_step.dependOn(&cmd.step);
}
const bench_step = b.step("bench", "Run all benchmarks");
inline for (.{
.ping_pongs,
.ping_pongs_uring,
.flow,
.flow_uring,
.aio_nops,
.coro_nops,
.fs,
.spawn_managed,
.spawn_unmanaged,
}) |bench| {
const exe = b.addExecutable(.{
.name = @tagName(bench),
.root_source_file = b.path("bench/" ++ @tagName(bench) ++ ".zig"),
.target = target,
.optimize = .ReleaseFast,
.sanitize_thread = sanitize,
.single_threaded = single_threaded,
.strip = false,
});
exe.root_module.addImport("aio", aio);
exe.root_module.addImport("coro", coro);
const install = b.addInstallArtifact(exe, .{ .dest_dir = .{ .override = .{ .custom = "bench" } } });
b.getInstallStep().dependOn(&install.step);
var cmd = makeRunStep(b, target, exe, "bench:" ++ @tagName(bench), "Run " ++ @tagName(bench) ++ " benchmark", .{});
bench_step.dependOn(&cmd.step);
}
}
fn addImportsFrom(dst: *std.Build.Module, src: *std.Build.Module) void {
var iter = src.import_table.iterator();
while (iter.next()) |e| dst.addImport(e.key_ptr.*, e.value_ptr.*);
}
const RunStepOptions = struct {
wasm_max_memory: usize = 1e+9, // 1GiB
};
fn runArtifactForStep(b: *std.Build, target: std.Build.ResolvedTarget, step: *std.Build.Step.Compile, opts: RunStepOptions) *std.Build.Step.Run {
return switch (target.query.os_tag orelse builtin.os.tag) {
.wasi => blk: {
step.max_memory = std.mem.alignForward(usize, opts.wasm_max_memory, 65536);
const wasmtime = b.addSystemCommand(&.{ "wasmtime", "-W", "trap-on-grow-failure=y", "--dir", ".", "--" });
wasmtime.addArtifactArg(step);
break :blk wasmtime;
},
else => b.addRunArtifact(step),
};
}
fn makeRunStep(b: *std.Build, target: std.Build.ResolvedTarget, step: *std.Build.Step.Compile, name: []const u8, description: []const u8, opts: RunStepOptions) *std.Build.Step.Run {
const cmd = runArtifactForStep(b, target, step, opts);
if (b.args) |args| cmd.addArgs(args);
const run = b.step(name, description);
run.dependOn(&cmd.step);
return cmd;
}