-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbuild.zig
More file actions
388 lines (358 loc) · 15.6 KB
/
build.zig
File metadata and controls
388 lines (358 loc) · 15.6 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
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// -Dlibusb=false disables libusb linkage for musl cross-compile
const use_libusb = b.option(bool, "libusb", "Link libusb-1.0 (default: true)") orelse true;
const use_wasm = b.option(bool, "wasm", "Link wasm3 runtime (default: true)") orelse true;
const coverage = b.option(bool, "test-coverage", "Run tests with kcov coverage") orelse false;
const test_filter = b.option([]const u8, "test-filter", "Substring filter passed to addTest.filters (local dev)");
const wasm3_c_flags: []const []const u8 = &.{ "-std=c99", "-DDEBUG=0", "-Dd_m3HasWASI=0" };
const build_opts = b.addOptions();
build_opts.addOption(bool, "use_wasm", use_wasm);
const toml_dep = b.dependency("toml", .{ .target = target, .optimize = optimize });
const toml_mod = toml_dep.module("toml");
// src module: shared by padctl, padctl-debug, and integration tests
const src_mod = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
src_mod.addImport("toml", toml_mod);
src_mod.addImport("build_options", build_opts.createModule());
if (use_wasm) addWasm3(b, src_mod, wasm3_c_flags);
const exe = b.addExecutable(.{ .name = "padctl", .root_module = src_mod });
if (use_libusb) {
exe.linkSystemLibrary("usb-1.0");
} else {
exe.addIncludePath(b.path("compat"));
}
exe.linkLibC();
b.installArtifact(exe);
const debug_mod = b.createModule(.{
.root_source_file = b.path("tools/padctl-debug.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
debug_mod.addImport("src", src_mod);
const debug_exe = b.addExecutable(.{ .name = "padctl-debug", .root_module = debug_mod });
if (use_libusb) {
debug_exe.linkSystemLibrary("usb-1.0");
} else {
debug_exe.addIncludePath(b.path("compat"));
}
debug_exe.linkLibC();
b.installArtifact(debug_exe);
const capture_analyse_mod = b.createModule(.{
.root_source_file = b.path("src/capture/analyse.zig"),
.target = target,
.optimize = optimize,
});
const capture_toml_gen_mod = b.createModule(.{
.root_source_file = b.path("src/capture/toml_gen.zig"),
.target = target,
.optimize = optimize,
});
capture_toml_gen_mod.addImport("analyse", capture_analyse_mod);
const io_hidraw_mod = b.createModule(.{
.root_source_file = b.path("src/io/hidraw.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
io_hidraw_mod.link_libc = true;
const capture_mod = b.createModule(.{
.root_source_file = b.path("tools/padctl-capture.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
capture_mod.addImport("analyse", capture_analyse_mod);
capture_mod.addImport("toml_gen", capture_toml_gen_mod);
capture_mod.addImport("hidraw_mod", io_hidraw_mod);
capture_mod.addImport("toml", toml_mod);
const capture_exe = b.addExecutable(.{ .name = "padctl-capture", .root_module = capture_mod });
capture_exe.linkLibC();
b.installArtifact(capture_exe);
// test: Layer 0 + Layer 1 (CI); refAllDecls in main.zig pulls in debug/render.zig tests
// test_root.zig sits at repo root so @embedFile("../../...") paths in src/ stay within package.
const unit_mod = b.createModule(.{
.root_source_file = b.path("test_root.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
unit_mod.addImport("toml", toml_mod);
unit_mod.addImport("analyse", capture_analyse_mod);
unit_mod.addImport("toml_gen", capture_toml_gen_mod);
unit_mod.addImport("build_options", build_opts.createModule());
if (use_wasm) addWasm3(b, unit_mod, wasm3_c_flags);
const unit_filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{};
const unit_tests = b.addTest(.{ .root_module = unit_mod, .filters = unit_filters });
if (use_libusb) {
unit_tests.linkSystemLibrary("usb-1.0");
} else {
unit_tests.addIncludePath(b.path("compat"));
}
unit_tests.linkLibC();
if (coverage) unit_tests.setExecCmd(&.{ "kcov", "--include-path=src/", "kcov-output", null });
const test_step = b.step("test", "Run Layer 0 + Layer 1 tests (CI)");
test_step.dependOn(&b.addRunArtifact(unit_tests).step);
// test-tsan: ThreadSanitizer-enabled test run (local dev)
const tsan_mod = b.createModule(.{
.root_source_file = b.path("test_root.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
.sanitize_thread = true,
});
tsan_mod.addImport("toml", toml_mod);
tsan_mod.addImport("analyse", capture_analyse_mod);
tsan_mod.addImport("toml_gen", capture_toml_gen_mod);
tsan_mod.addImport("build_options", build_opts.createModule());
if (use_wasm) addWasm3(b, tsan_mod, wasm3_c_flags);
const tsan_tests = b.addTest(.{ .root_module = tsan_mod });
if (use_libusb) {
tsan_tests.linkSystemLibrary("usb-1.0");
} else {
tsan_tests.addIncludePath(b.path("compat"));
}
tsan_tests.linkLibC();
const tsan_step = b.step("test-tsan", "Run tests with ThreadSanitizer");
tsan_step.dependOn(&b.addRunArtifact(tsan_tests).step);
// test-safe: ReleaseSafe test run (catches UB under optimization)
const safe_mod = b.createModule(.{
.root_source_file = b.path("test_root.zig"),
.target = target,
.optimize = .ReleaseSafe,
.sanitize_c = .trap,
});
safe_mod.addImport("toml", toml_mod);
safe_mod.addImport("analyse", capture_analyse_mod);
safe_mod.addImport("toml_gen", capture_toml_gen_mod);
safe_mod.addImport("build_options", build_opts.createModule());
if (use_wasm) addWasm3(b, safe_mod, wasm3_c_flags);
const safe_tests = b.addTest(.{ .root_module = safe_mod });
if (use_libusb) {
safe_tests.linkSystemLibrary("usb-1.0");
} else {
safe_tests.addIncludePath(b.path("compat"));
}
safe_tests.linkLibC();
const safe_step = b.step("test-safe", "Run tests with ReleaseSafe (optimized + safety checks)");
safe_step.dependOn(&b.addRunArtifact(safe_tests).step);
// check-fmt: verify code formatting
const fmt_step = b.step("check-fmt", "Check code formatting");
const fmt = b.addFmt(.{ .paths = &.{ "src/", "tools/" }, .check = true });
fmt_step.dependOn(&fmt.step);
// check-all: local convenience target (runs test + safe + fmt in one invocation).
// CI uses three separate `zig build` processes instead — see ci.yml.
const check_all = b.step("check-all", "Run all checks (test + safe + fmt)");
check_all.dependOn(test_step);
check_all.dependOn(safe_step);
check_all.dependOn(fmt_step);
// fuzz: continuous fuzzing (zig build fuzz --fuzz)
const fuzz_step = b.step("fuzz", "Fuzz test (use: zig build fuzz --fuzz)");
fuzz_step.dependOn(&b.addRunArtifact(unit_tests).step);
// capture L0 tests (analyse pure functions)
const capture_tests = b.addTest(.{ .root_module = capture_analyse_mod });
if (coverage) capture_tests.setExecCmd(&.{ "kcov", "--include-path=src/", "kcov-output", null });
test_step.dependOn(&b.addRunArtifact(capture_tests).step);
// test-integration: Layer 2 (UHID, requires privilege)
const integration_step = b.step("test-integration", "Run Layer 2 integration tests (UHID, local)");
const integ_mod = b.createModule(.{
.root_source_file = b.path("src/test/uhid_integration_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
integ_mod.addImport("toml", toml_mod);
integ_mod.addImport("src", src_mod);
const integ_tests = b.addTest(.{ .root_module = integ_mod });
if (use_libusb) {
integ_tests.linkSystemLibrary("usb-1.0");
} else {
integ_tests.addIncludePath(b.path("compat"));
}
integ_tests.linkLibC();
integration_step.dependOn(&b.addRunArtifact(integ_tests).step);
// uhid_all_devices: L2 config-driven UHID simulation for all device configs
const alldev_mod = b.createModule(.{
.root_source_file = b.path("src/test/uhid_all_devices_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
alldev_mod.addImport("toml", toml_mod);
alldev_mod.addImport("src", src_mod);
const alldev_tests = b.addTest(.{ .root_module = alldev_mod });
if (use_libusb) {
alldev_tests.linkSystemLibrary("usb-1.0");
} else {
alldev_tests.addIncludePath(b.path("compat"));
}
alldev_tests.linkLibC();
integration_step.dependOn(&b.addRunArtifact(alldev_tests).step);
// steam_deck_uhid_e2e: Phase 13 Wave 1 T4d — UhidSimulator → hidraw →
// interpreter end-to-end. Requires /dev/uhid; the harness + tests
// gracefully skip when it's not available.
const deck_e2e_mod = b.createModule(.{
.root_source_file = b.path("src/test/steam_deck_uhid_e2e_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
deck_e2e_mod.addImport("toml", toml_mod);
deck_e2e_mod.addImport("src", src_mod);
const deck_e2e_tests = b.addTest(.{ .root_module = deck_e2e_mod });
if (use_libusb) {
deck_e2e_tests.linkSystemLibrary("usb-1.0");
} else {
deck_e2e_tests.addIncludePath(b.path("compat"));
}
deck_e2e_tests.linkLibC();
integration_step.dependOn(&b.addRunArtifact(deck_e2e_tests).step);
// supervisor_uhid_grace_integration: issue-131-A real-kernel validation.
// Creates a UHID virtual device, attaches via Supervisor, simulates
// disconnect, and asserts grace-window teardown with deterministic clock.
const grace_int_mod = b.createModule(.{
.root_source_file = b.path("src/test/supervisor_uhid_grace_integration_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
grace_int_mod.addImport("toml", toml_mod);
grace_int_mod.addImport("src", src_mod);
const grace_int_tests = b.addTest(.{ .root_module = grace_int_mod });
if (use_libusb) {
grace_int_tests.linkSystemLibrary("usb-1.0");
} else {
grace_int_tests.addIncludePath(b.path("compat"));
}
grace_int_tests.linkLibC();
integration_step.dependOn(&b.addRunArtifact(grace_int_tests).step);
// Phase 13 Wave 3 T5f: Layer 1 routing tests — pure pipe2 fixture, no
// /dev/uhid, no privilege required. Hooks to the main test step so the
// CI green bar covers the UHID routing switch.
const routing_mod = b.createModule(.{
.root_source_file = b.path("src/test/supervisor_uhid_routing_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
routing_mod.addImport("toml", toml_mod);
routing_mod.addImport("src", src_mod);
const routing_tests = b.addTest(.{ .root_module = routing_mod });
if (use_libusb) {
routing_tests.linkSystemLibrary("usb-1.0");
} else {
routing_tests.addIncludePath(b.path("compat"));
}
routing_tests.linkLibC();
test_step.dependOn(&b.addRunArtifact(routing_tests).step);
// Wave 6 T3 + T6: uhid_output_dispatch_test and wave6_pidff_e2e_test are
// imported into src/main.zig's test namespace and compiled into the main
// test artifact. Separate b.addTest artifacts triggered Zig flock deadlock
// (issue #22453) on the cache manifest.
// test-e2e: Layer 3 (UHID+uinput full pipeline, requires privilege)
const e2e_step = b.step("test-e2e", "Run Layer 3 end-to-end tests (UHID+uinput, local)");
const e2e_mod = b.createModule(.{
.root_source_file = b.path("src/test/full_pipeline_e2e_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
e2e_mod.addImport("src", src_mod);
const e2e_tests = b.addTest(.{ .root_module = e2e_mod });
if (use_libusb) {
e2e_tests.linkSystemLibrary("usb-1.0");
} else {
e2e_tests.addIncludePath(b.path("compat"));
}
e2e_tests.linkLibC();
e2e_step.dependOn(&b.addRunArtifact(e2e_tests).step);
// L3 generative e2e: all device configs with mapping layer
const gen_e2e_mod = b.createModule(.{
.root_source_file = b.path("src/test/full_e2e_generative_test.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
gen_e2e_mod.addImport("src", src_mod);
gen_e2e_mod.addImport("toml", toml_mod);
const gen_e2e_tests = b.addTest(.{ .root_module = gen_e2e_mod });
if (use_libusb) {
gen_e2e_tests.linkSystemLibrary("usb-1.0");
} else {
gen_e2e_tests.addIncludePath(b.path("compat"));
}
gen_e2e_tests.linkLibC();
e2e_step.dependOn(&b.addRunArtifact(gen_e2e_tests).step);
// test-uhid-uniq: Phase 13 Wave 5 canary — run ONLY the
// `src/test/uhid_uniq_pairing_test.zig` file as a standalone test binary.
// Split from `test` so the privileged Docker canary doesn't accidentally
// trigger other tests that probe real hidraw devices on the host and
// deadlock in `hid_hw_open`.
const uniq_step = b.step("test-uhid-uniq", "Run UHID uniq pairing test only (Wave 5 canary)");
// Root at src/ so the test's relative imports (../io/uhid.zig etc.)
// stay inside the module path. The `addTest` will still run the tests
// defined in `test/uhid_uniq_pairing_test.zig` via `test-filter`.
const uniq_mod = b.createModule(.{
.root_source_file = b.path("test_root.zig"),
.target = target,
.optimize = optimize,
.sanitize_c = .trap,
});
uniq_mod.addImport("toml", toml_mod);
uniq_mod.addImport("analyse", capture_analyse_mod);
uniq_mod.addImport("toml_gen", capture_toml_gen_mod);
uniq_mod.addImport("build_options", build_opts.createModule());
if (use_wasm) addWasm3(b, uniq_mod, wasm3_c_flags);
const uniq_tests = b.addTest(.{
.root_module = uniq_mod,
.filters = &.{"uhid_uniq_pairing_test"},
});
if (use_libusb) {
uniq_tests.linkSystemLibrary("usb-1.0");
} else {
uniq_tests.addIncludePath(b.path("compat"));
}
uniq_tests.linkLibC();
uniq_step.dependOn(&b.addRunArtifact(uniq_tests).step);
// spike (only available when spike/toml_spike.zig exists)
if (std.fs.cwd().access("spike/toml_spike.zig", .{})) |_| {
const spike_mod = b.createModule(.{
.root_source_file = b.path("spike/toml_spike.zig"),
.target = target,
.optimize = optimize,
});
spike_mod.addImport("toml", toml_mod);
const spike_exe = b.addExecutable(.{ .name = "toml-spike", .root_module = spike_mod });
const spike_step = b.step("spike", "Run TOML spike");
spike_step.dependOn(&b.addRunArtifact(spike_exe).step);
} else |_| {}
}
fn addWasm3(b: *std.Build, mod: *std.Build.Module, c_flags: []const []const u8) void {
mod.addCSourceFiles(.{
.root = b.path("third_party/wasm3/source"),
.files = &.{
"m3_api_libc.c",
"m3_api_meta_wasi.c",
"m3_api_tracer.c",
"m3_bind.c",
"m3_code.c",
"m3_compile.c",
"m3_core.c",
"m3_env.c",
"m3_exec.c",
"m3_function.c",
"m3_info.c",
"m3_module.c",
"m3_parse.c",
},
.flags = c_flags,
});
mod.addIncludePath(b.path("third_party/wasm3/source"));
}