-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbuild.zig
More file actions
410 lines (359 loc) · 11.4 KB
/
Copy pathbuild.zig
File metadata and controls
410 lines (359 loc) · 11.4 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
pub fn build(b: *std.Build) void {
comptime checkVersion();
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Top-level steps you can invoke on the command line.
const build_steps = .{
.run = b.step("run", "Run a Tardy Program/Example"),
.static = b.step("static", "Build tardy as a static lib"),
.@"test" = b.step("test", "Run all tests"),
.test_unit = b.step("test_unit", "Run general unit tests"),
.test_fmt = b.step("test_fmt", "Run formmatter tests"),
.test_e2e = b.step("test_e2e", "Run e2e tests"),
};
// Build options passed with `-D` flags.
const build_options = .{
.example = b.option(Example, "example", "example name") orelse .none,
.async_backend = b.option(AsyncKind, "async", "async backend to use") orelse .auto,
};
// create a public tardy module
const tardy = b.addModule("tardy", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
// currently not supported on aarch64
.error_tracing = target.result.cpu.arch != .aarch64,
.pic = true,
});
const options: BuildOptions = .{
.async_backend = build_options.async_backend,
.tardy_mod = tardy,
.optimize = optimize,
.target = target,
};
// build and run examples
// usage: zig [build/build run] -Dasync=[async_backend] -Dexample[example_name]
build_examples(b, .{
.run = build_steps.run,
.install = b.getInstallStep(),
}, .{
.async_backend = build_options.async_backend,
.tardy_mod = tardy,
.example = build_options.example,
.optimize = optimize,
.target = target,
.skip_run_step_steup = undefined,
});
// build tardy as a static lib
// usage: zig build static
build_static_lib(
b,
.{ .static = build_steps.static },
options,
);
// build and run tests
// usage: refer to function declaration
build_test(b, .{
.test_unit = build_steps.test_unit,
.test_fmt = build_steps.test_fmt,
.@"test" = build_steps.@"test",
}, options);
// build and run e2e test
// usage: zig build test_e2e -Dasync=[async_backend] -- [u64 num]
build_test_e2e(
b,
.{ .test_e2e = build_steps.test_e2e },
options,
);
}
// used for building and running examples
fn build_examples(
b: *std.Build,
steps: struct {
run: *std.Build.Step,
install: *std.Build.Step,
},
options: ExampleOptions,
) void {
// build/run specific example
switch (options.example) {
.none => return,
// build .all example
// run wont work for .all example
.all => {
log.info("zig build run -Dexample=all will only build examples and will not run them", .{});
inline for (@typeInfo(Example).@"enum".field_values) |value| {
// convert captured field value to field enum
const field: Example = @fromBackingInt(@intCast(value));
// skip .none and .all for building step
if (field == .none or field == .all) continue;
build_example_exe(
b,
.{
.run = steps.run,
.install = steps.install,
},
.{
.async_backend = options.async_backend,
.tardy_mod = options.tardy_mod,
.example = field,
.optimize = options.optimize,
.target = options.target,
.skip_run_step_steup = true,
},
);
}
},
else => {
return build_example_exe(
b,
.{
.run = steps.run,
.install = steps.install,
},
.{
.async_backend = options.async_backend,
.tardy_mod = options.tardy_mod,
.example = options.example,
.optimize = options.optimize,
.target = options.target,
.skip_run_step_steup = false,
},
);
},
}
}
fn build_example_module(
b: *std.Build,
options: ExampleOptions,
) *std.Build.Module {
debug.assert(options.example != .none);
debug.assert(options.example != .all);
// create a private example module
const example_mod = b.createModule(.{
.root_source_file = b.path(
b.fmt("examples/{t}/main.zig", .{options.example}),
),
.target = options.target,
.optimize = options.optimize,
});
example_mod.addImport("tardy", options.tardy_mod);
const example_options = b.addOptions();
example_options.addOption(
AsyncKind,
"async_backend",
options.async_backend,
);
example_mod.addOptions("options", example_options);
return example_mod;
}
// build/run a specific example
fn build_example_exe(
b: *std.Build,
steps: struct {
run: *std.Build.Step,
install: *std.Build.Step,
},
options: ExampleOptions,
) void {
debug.assert(options.example != .none);
debug.assert(options.example != .all);
const example_mod = build_example_module(b, options);
// windows: errors with `unknow size: 0xx(%%rsp)` without llvm
// aarch64: use_new_linker panics and codegen deadlocks without llvm
const use_llvm = (options.target.result.os.tag == .windows) or
(options.target.result.cpu.arch == .aarch64);
const example_exe = b.addExecutable(.{
.name = @tagName(options.example),
.root_module = example_mod,
.use_llvm = use_llvm,
});
example_exe.use_new_linker = !use_llvm;
const install_artifact = b.addInstallArtifact(
example_exe,
.{},
);
// depend on build/install step
steps.install.dependOn(&install_artifact.step);
// Should not run all examples at the same time
if (options.skip_run_step_steup) return;
const run_artifact = b.addRunArtifact(example_exe);
run_artifact.step.dependOn(&install_artifact.step);
// pass args to examples (.ie cat, rmdir, shove, stat)
run_artifact.addPassthruArgs();
steps.run.dependOn(&install_artifact.step);
steps.run.dependOn(&run_artifact.step);
}
fn build_static_lib(
b: *std.Build,
steps: struct {
static: *std.Build.Step,
},
options: BuildOptions,
) void {
const tardy_lib = b.addLibrary(.{
.linkage = .static,
.name = "tardy",
.root_module = options.tardy_mod,
});
const check_tardy = b.addLibrary(.{
.linkage = .static,
.name = "tardy",
.root_module = options.tardy_mod,
});
const check_step = b.step(
"check",
"check compile errors in tardy",
);
check_step.dependOn(&check_tardy.step);
// depend on static step
const install_artifact = b.addInstallArtifact(
tardy_lib,
.{},
);
steps.static.dependOn(&install_artifact.step);
}
fn build_test(
b: *std.Build,
steps: struct {
test_unit: *std.Build.Step,
test_fmt: *std.Build.Step,
@"test": *std.Build.Step,
},
options: BuildOptions,
) void {
// Run general unit tests
// usage: zig build test_unit
const unit_tests = b.addTest(.{
.name = "general unit tests",
.root_module = b.createModule(.{
.root_source_file = b.path("./src/tests.zig"),
.optimize = options.optimize,
.target = options.target,
}),
});
const run_unit_tests = b.addRunArtifact(unit_tests);
run_unit_tests.step.dependOn(&unit_tests.step);
steps.test_unit.dependOn(&run_unit_tests.step);
// Check formatting
// usage: zig build fmt
const run_fmt = b.addFmt(.{
.paths = &.{b.path(".")},
.check = true,
});
steps.test_fmt.dependOn(&run_fmt.step);
// Run all tests
// usage: zig build test
steps.@"test".dependOn(&run_unit_tests.step);
steps.@"test".dependOn(steps.test_fmt);
}
fn build_test_e2e(
b: *std.Build,
steps: struct {
test_e2e: *std.Build.Step,
},
options: BuildOptions,
) void {
// create a private example module
const e2e_mod = b.createModule(.{
.root_source_file = b.path("test/e2e/main.zig"),
.target = options.target,
.optimize = options.optimize,
.strip = false,
.imports = &.{
.{
.name = "tardy",
.module = options.tardy_mod,
},
},
});
// add needed options
const test_options = b.addOptions();
test_options.addOption(
AsyncKind,
"async_backend",
options.async_backend,
);
e2e_mod.addOptions("options", test_options);
// windows: errors with `unknow size: 0xx(%%rsp)` without llvm
// aarch64: use_new_linker panics and codegen deadlocks without llvm
const use_llvm = (options.target.result.os.tag == .windows) or
(options.target.result.cpu.arch == .aarch64);
const exe = b.addExecutable(.{
.name = "e2e",
.root_module = e2e_mod,
.use_llvm = use_llvm,
});
exe.use_new_linker = !use_llvm;
// build/install e2e test
const install_artifact = b.addInstallArtifact(exe, .{});
steps.test_e2e.dependOn(&install_artifact.step);
// run e2e test
const run_artifact = b.addRunArtifact(exe);
run_artifact.step.dependOn(&install_artifact.step);
// pass a u64 as an arg
run_artifact.addPassthruArgs();
steps.test_e2e.dependOn(&install_artifact.step);
steps.test_e2e.dependOn(&run_artifact.step);
}
// ensures the currently in-use zig version is at least the minimum required
fn checkVersion() void {
const minimum_zig_version: []const u8 = @import("build.zig.zon").minimum_zig_version;
const supported_version = std.SemanticVersion.parse(
minimum_zig_version,
) catch unreachable;
const current_version = builtin.zig_version;
// Compare versions while allowing different pre/patch metadata.
const order = current_version.order(supported_version);
switch (order) {
.lt => {
const message = std.fmt.comptimePrint(
\\Your Zig version ({0s}) is less than the
\\minimum supported by Tardy ({1s}).
\\
\\Update your Zig toolchain to `{1s}`
\\
, .{ builtin.zig_version_string, minimum_zig_version });
@compileError(message);
},
else => {},
}
}
const ExampleOptions = struct {
async_backend: AsyncKind,
tardy_mod: *std.Build.Module,
example: Example,
target: std.Build.ResolvedTarget,
optimize: std.lang.Optimize,
skip_run_step_steup: bool,
};
const BuildOptions = struct {
async_backend: AsyncKind,
tardy_mod: *std.Build.Module,
target: std.Build.ResolvedTarget,
optimize: std.lang.Optimize,
};
const Example = enum {
none,
all,
basic,
cat,
channel,
echo,
http,
rmdir,
shove,
stat,
stream,
};
pub const AsyncKind = enum {
auto,
io_uring,
epoll,
kqueue,
poll,
};
const log = std.log.scoped(.@"build.zig");
const std = @import("std");
const debug = std.debug;
const builtin = @import("builtin");