-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbuild.zig
More file actions
452 lines (404 loc) · 16.1 KB
/
build.zig
File metadata and controls
452 lines (404 loc) · 16.1 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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
pub fn build(b: *std.Build) void {
// basic support for running in Visual Studio using ZigVS
const running_in_vs = blk: {
_ = std.process.getEnvVarOwned(b.allocator, "VisualStudioEdition") catch break :blk false;
break :blk true;
};
// tells the build where to find your FATDATA folder
// defaults to ./fatdata
const assets_path = b.option(std.Build.LazyPath, "assets-path", "Path to assets") orelse b.path("fatdata");
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const crash_debug = b.option(bool, "crash-debug", "Enable crash dump friendly C build flags") orelse false;
const c_flags: []const []const u8 = if (crash_debug)
&.{ "-fwrapv", "-fno-omit-frame-pointer" }
else
&.{"-fwrapv"};
const python_checks = b.option(
bool,
"python-checks",
"Run optional Python-based seam checks",
) orelse false;
const exe_mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
// Only sanitize C code when building in Debug mode
// So that release builds are more "stable"
exe_mod.sanitize_c = if (optimize == .Debug) .full else .off;
exe_mod.addIncludePath(b.path("external/Nuklear-4.13.2"));
exe_mod.addCSourceFiles(.{
.flags = c_flags,
.files = &.{
"PROJECTS/ROLLER/debug_overlay.c",
"PROJECTS/ROLLER/crashdump.c",
"PROJECTS/ROLLER/3d.c",
"PROJECTS/ROLLER/building.c",
"PROJECTS/ROLLER/car.c",
"PROJECTS/ROLLER/carplans.c",
"PROJECTS/ROLLER/cdx.c",
"PROJECTS/ROLLER/colision.c",
"PROJECTS/ROLLER/comms.c",
"PROJECTS/ROLLER/control.c",
"PROJECTS/ROLLER/date.c",
"PROJECTS/ROLLER/drawtrk3.c",
"PROJECTS/ROLLER/render_queue_3d.c",
"PROJECTS/ROLLER/engines.c",
"PROJECTS/ROLLER/frontend.c",
"PROJECTS/ROLLER/frontend_config.c",
"PROJECTS/ROLLER/frontend_data.c",
"PROJECTS/ROLLER/frontend_lobby.c",
"PROJECTS/ROLLER/frontend_pause.c",
"PROJECTS/ROLLER/frontend_network.c",
"PROJECTS/ROLLER/frontend_screens.c",
"PROJECTS/ROLLER/frontend_select_car.c",
"PROJECTS/ROLLER/frontend_select_disk.c",
"PROJECTS/ROLLER/frontend_select_players.c",
"PROJECTS/ROLLER/frontend_select_track.c",
"PROJECTS/ROLLER/frontend_select_type.c",
"PROJECTS/ROLLER/frontend_util.c",
"PROJECTS/ROLLER/func2.c",
"PROJECTS/ROLLER/func3.c",
"PROJECTS/ROLLER/function.c",
"PROJECTS/ROLLER/graphics.c",
"PROJECTS/ROLLER/horizon.c",
"PROJECTS/ROLLER/loadtrak.c",
"PROJECTS/ROLLER/menu_render.c",
"PROJECTS/ROLLER/menu_render_gpu.c",
"PROJECTS/ROLLER/menu_render_software.c",
"PROJECTS/ROLLER/game_render.c",
"PROJECTS/ROLLER/game_render_software.c",
"PROJECTS/ROLLER/scene_render.c",
"PROJECTS/ROLLER/scene_render_software.c",
"PROJECTS/ROLLER/mouse.c",
"PROJECTS/ROLLER/moving.c",
"PROJECTS/ROLLER/network.c",
"PROJECTS/ROLLER/plans.c",
"PROJECTS/ROLLER/png_writer.c",
"PROJECTS/ROLLER/polyf.c",
"PROJECTS/ROLLER/polytex.c",
"PROJECTS/ROLLER/replay.c",
"PROJECTS/ROLLER/roller.c",
"PROJECTS/ROLLER/rollercd.c",
"PROJECTS/ROLLER/rollercomms.c",
"PROJECTS/ROLLER/snapshot.c",
"PROJECTS/ROLLER/snapshot_scenes.c",
"PROJECTS/ROLLER/sound.c",
"PROJECTS/ROLLER/svgacpy.c",
"PROJECTS/ROLLER/tower.c",
"PROJECTS/ROLLER/transfrm.c",
"PROJECTS/ROLLER/userfns.c",
"PROJECTS/ROLLER/view.c",
},
});
const exe = b.addExecutable(.{
.name = "roller",
.root_module = exe_mod,
});
switch (target.result.os.tag) {
.windows => {
exe_mod.addCMacro("WILDMIDI_STATIC", "1");
exe.addWin32ResourceFile(.{
.file = b.path("ROLLER.rc"),
});
exe.subsystem = .Windows;
exe.linkSystemLibrary("dbghelp");
exe.linkSystemLibrary("user32");
exe.linkSystemLibrary("ws2_32");
exe.linkSystemLibrary("iphlpapi");
},
else => {
exe_mod.addCMacro("__int16", "int16");
exe_mod.addCMacro("_O_RDONLY", "O_RDONLY");
exe_mod.addCMacro("_O_BINARY", "0x200");
},
}
b.installArtifact(exe);
if (python_checks) {
const scene_render_seam_check = b.addSystemCommand(&.{
pythonExe(),
"tests/scene_render_seam_check.py",
});
exe.step.dependOn(&scene_render_seam_check.step);
}
configureDependencies(b, exe, target, optimize);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.setCwd(assets_path);
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run roller");
run_step.dependOn(&run_cmd.step);
if (running_in_vs) {
const cp = b.addInstallDirectory(.{
.source_dir = assets_path,
.install_dir = .bin,
.install_subdir = "",
});
exe.step.dependOn(&cp.step);
}
// copies fatdata directory to the bin folder
// only happens when using `zig build run`
const assets_install = b.addInstallDirectory(.{
.source_dir = assets_path,
.install_dir = .bin,
.install_subdir = "fatdata",
});
run_step.dependOn(&assets_install.step);
// copies wildmidi configuration files to the bin folder
// only happens when using `zig build run`
const wildmidi_config = b.addWriteFiles();
const wildmidi_config_copy = wildmidi_config.addCopyDirectory(b.path("midi"), "midi", .{});
const wildmidi_config_install = b.addInstallDirectory(.{
.source_dir = wildmidi_config_copy,
.install_dir = .bin,
.install_subdir = "midi",
});
run_step.dependOn(&wildmidi_config_install.step);
configureRenderQueue3DTests(b, target, optimize, c_flags, python_checks);
// Snapshot regression harness: drive the snapshot binary serially across
// every configured intro replay, writing PNGs straight into the
// checked-in baseline directory, then run `git diff --exit-code` against
// that directory. Any pixel change shows up as a tracked-file diff,
// which renders as a binary image diff in pull requests. The
// -Dupdate-snapshots flag suppresses the diff check so an explicit
// refresh run produces a clean exit before the developer commits.
configureSnapshotTests(b, exe, assets_path);
}
fn configureRenderQueue3DTests(
b: *Build,
target: ResolvedTarget,
optimize: OptimizeMode,
c_flags: []const []const u8,
python_checks: bool,
) void {
const test_mod = b.createModule(.{
.target = target,
.optimize = optimize,
.link_libc = true,
});
const sdl = b.dependency("sdl", .{
.target = target,
.optimize = optimize,
.lto = .none,
});
test_mod.addIncludePath(sdl.builder.path("include"));
test_mod.addIncludePath(b.path("PROJECTS/ROLLER"));
test_mod.addCSourceFiles(.{
.flags = c_flags,
.files = &.{
"PROJECTS/ROLLER/render_queue_3d.c",
"tests/render_queue_3d_test.c",
},
});
const test_exe = b.addExecutable(.{
.name = "render_queue_3d_test",
.root_module = test_mod,
});
const run_unit = b.addRunArtifact(test_exe);
const render_queue_tests = b.step(
"test-render-queue-3d",
"Run render_queue_3d sort/mapping tests",
);
render_queue_tests.dependOn(&run_unit.step);
if (python_checks) {
const seam_check = b.addSystemCommand(&.{
pythonExe(),
"tools/check_render_queue_3d_seams.py",
});
render_queue_tests.dependOn(&seam_check.step);
}
const test_step = b.step("test", "Run focused unit tests and optional seam checks");
test_step.dependOn(render_queue_tests);
}
const SnapshotReplay = struct {
name: []const u8,
frames: []const u8,
};
const SnapshotScene = struct {
name: []const u8,
frames: []const u8,
};
// Hand-picked frames per intro replay. Spread across each replay's length
// (intro3 is ~200 frames; the others are 800+ frames). Pinned to single-host
// pixels per the ADR.
const snapshot_replays = [_]SnapshotReplay{
.{ .name = "intro1", .frames = "60,240,480,720" },
.{ .name = "intro2", .frames = "60,300,720,1200" },
.{ .name = "intro3", .frames = "30,60,120,180" },
.{ .name = "intro4", .frames = "60,100,180,360,540" },
.{ .name = "intro5", .frames = "60,300,720,1200" },
.{ .name = "intro6", .frames = "60,300,720,1200" },
// intro7 frame 900 currently produces non-deterministic pixels across
// runs (suspected: an unseeded RNG consumer reachable in the deep
// replay path). Track via the "flaky deep-frame determinism" follow-up;
// for now capture three earlier frames so the harness stays green.
.{ .name = "intro7", .frames = "60,300,600" },
};
const snapshot_scenes = [_]SnapshotScene{
.{ .name = "menu-main", .frames = "30" },
.{ .name = "menu-select-car", .frames = "30" },
.{ .name = "menu-select-track", .frames = "30" },
.{ .name = "menu-select-type", .frames = "30" },
.{ .name = "menu-select-players", .frames = "30" },
.{ .name = "menu-select-disk", .frames = "30" },
.{ .name = "menu-configure", .frames = "30" },
.{ .name = "winner-race", .frames = "30" },
.{ .name = "winner-championship", .frames = "30" },
.{ .name = "championship-over", .frames = "30" },
.{ .name = "race-result", .frames = "1" },
.{ .name = "championship-standings", .frames = "30" },
.{ .name = "lap-records", .frames = "1" },
.{ .name = "time-trials", .frames = "1" },
};
fn configureSnapshotTests(
b: *Build,
roller_exe: *Compile,
assets_path: LazyPath,
) void {
const scratch = b.option(
bool,
"scratch",
"Capture into zig-out/snapshot-scratch/ and skip the diff check. Use this on non-canonical hosts to sanity-check captures without mutating the LFS-tracked baselines.",
) orelse false;
const baselines_dir = "tests/snapshots/baselines";
const scratch_dir = "zig-out/snapshot-scratch";
const out_rel = if (scratch) scratch_dir else baselines_dir;
const out_abs = b.pathJoin(&.{ b.build_root.path orelse ".", out_rel });
const test_snapshots = b.step(
"test-snapshots",
"Run rendering snapshot regression tests across the intro replays",
);
const assets_abs = assets_path.getPath2(b, null);
const assets_available = blk: {
var assets_dir = std.fs.cwd().openDir(assets_abs, .{}) catch break :blk false;
assets_dir.close();
break :blk true;
};
if (!assets_available) {
const missing_assets = b.addFail(b.fmt(
"snapshot assets directory not found: {s}\n" ++
"Run `mise run link-worktree-data` or pass `-Dassets-path=/path/to/fatdata` before `zig build test-snapshots`.",
.{assets_abs},
));
test_snapshots.dependOn(&missing_assets.step);
return;
}
// Drive the snapshot binary serially. Parallel invocations introduced
// non-deterministic pixels at long-running replay frames (suspected:
// contention on shared system probes during early init); chaining each
// run through the previous one's step forces a one-at-a-time schedule.
var prev_run: ?*Step = null;
for (snapshot_replays) |replay| {
const run_capture = b.addRunArtifact(roller_exe);
run_capture.addArg("--no-crash-handler");
run_capture.addArg("--whiplash-root");
run_capture.addDirectoryArg(assets_path);
run_capture.addArg("--snapshot");
run_capture.addArg(b.fmt("{s}.gss", .{replay.name}));
run_capture.addArg("--frames");
run_capture.addArg(replay.frames);
run_capture.addArg("--out");
run_capture.addArg(out_abs);
run_capture.has_side_effects = true;
if (prev_run) |p| run_capture.step.dependOn(p);
prev_run = &run_capture.step;
}
for (snapshot_scenes) |scene| {
const run_capture = b.addRunArtifact(roller_exe);
run_capture.addArg("--no-crash-handler");
run_capture.addArg("--whiplash-root");
run_capture.addDirectoryArg(assets_path);
run_capture.addArg("--snapshot-scene");
run_capture.addArg(scene.name);
run_capture.addArg("--frames");
run_capture.addArg(scene.frames);
run_capture.addArg("--out");
run_capture.addArg(out_abs);
run_capture.has_side_effects = true;
if (prev_run) |p| run_capture.step.dependOn(p);
prev_run = &run_capture.step;
}
if (scratch) {
// Scratch mode never touches the LFS-tracked baselines, so the
// git-diff gate doesn't apply. Developers compare the scratch
// directory against the baselines with whatever tool they prefer
// (e.g. `diff -rq tests/snapshots/baselines zig-out/snapshot-scratch`).
if (prev_run) |p| test_snapshots.dependOn(p);
return;
}
// After the captures land in the canonical baseline directory, fail the
// build if any baseline diverged from HEAD. The diff itself is what
// reviewers see in the PR (GitHub renders LFS-backed PNGs as
// side-by-side image diffs). To bless an intentional change the
// developer reruns, eyeballs the working-tree diff, and commits.
const diff_check = b.addSystemCommand(&.{
"git",
"diff",
"--exit-code",
"--stat",
"--",
baselines_dir,
});
diff_check.has_side_effects = true;
if (prev_run) |p| diff_check.step.dependOn(p);
test_snapshots.dependOn(&diff_check.step);
}
fn configureDependencies(b: *Build, exe: *Compile, target: ResolvedTarget, optimize: OptimizeMode) void {
const exe_mod = exe.root_module;
// build dependencies
const wildmidi = b.dependency("wildmidi", .{
.target = target,
.optimize = optimize,
});
const wildmidi_lib = wildmidi.artifact("wildmidi");
const sdl_image = b.dependency("SDL_image", .{
.target = target,
.optimize = optimize,
});
const sdl_image_lib = sdl_image.artifact("SDL3_image");
const sdl = b.dependency("sdl", .{
.target = target,
.optimize = optimize,
.lto = .none,
});
const sdl_lib = sdl.artifact("SDL3");
const libcdio = b.dependency("libcdio", .{
.target = target,
.optimize = optimize,
});
const libcdio_lib = libcdio.artifact("cdio");
exe_mod.linkLibrary(sdl_lib);
exe_mod.linkLibrary(sdl_image_lib);
exe_mod.linkLibrary(wildmidi_lib);
exe_mod.linkLibrary(libcdio_lib);
const sdl_image_source = sdl_image.builder.dependency("SDL_image", .{
.lto = .none,
});
var cflags = compile_flagz.addCompileFlags(b);
cflags.addIncludePath(sdl.builder.path("include"));
cflags.addIncludePath(sdl_image_source.builder.path("include"));
cflags.addIncludePath(wildmidi.builder.path("include"));
cflags.addIncludePath(libcdio.builder.path("include"));
cflags.addIncludePath(libcdio.builder.path("zig-config"));
cflags.addIncludePath(b.path("external/Nuklear-4.13.2"));
const cflags_step = b.step("compile-flags", "Generate compile flags");
cflags_step.dependOn(&cflags.step);
}
fn pythonExe() []const u8 {
return if (host_builtin.os.tag == .windows) "python" else "python3";
}
const compile_flagz = @import("compile_flagz");
const host_builtin = @import("builtin");
const std = @import("std");
const ArrayList = std.ArrayListUnmanaged;
const Build = std.Build;
const LazyPath = Build.LazyPath;
const Module = Build.Module;
const ResolvedTarget = Build.ResolvedTarget;
const Step = Build.Step;
const Compile = Step.Compile;
const builtin = std.builtin;
const OptimizeMode = builtin.OptimizeMode;