Repository navigation
Expand file tree
/
Copy pathbuild.zig
More file actions
260 lines (229 loc) · 12.2 KB
/
Copy pathbuild.zig
File metadata and controls
260 lines (229 loc) · 12.2 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
const std = @import("std");
const tools = @import("build/tool_options.zig");
pub const config = @import("build/config.zig");
pub const modules = @import("build/modules.zig");
pub const packaging = @import("build/packaging.zig");
// --- Aether's own build (test app + engine tests) ---
fn directory_exists(b: *std.Build, path: []const u8) bool {
const io = b.graph.io;
// Re-run configure if the entry appears or disappears.
b.dependOnDirectoryContents(b.path("."));
var dir = b.root.openDir(io, path, .{}) catch |err| switch (err) {
error.FileNotFound, error.NotDir => return false,
else => {
std.debug.panic("unable to open directory '{s}': {s}", .{ path, @errorName(err) });
},
};
dir.close(io);
return true;
}
fn make_resource_manifest(b: *std.Build, resource_dir_path: []const u8) []const u8 {
const io = b.graph.io;
b.dependOnDirectoryContents(b.path(resource_dir_path));
var dir = b.root.openDir(io, resource_dir_path, .{ .iterate = true }) catch |err| {
std.debug.panic("unable to open web resource directory '{s}': {s}", .{ resource_dir_path, @errorName(err) });
};
defer dir.close(io);
var walker = dir.walk(b.allocator) catch @panic("OOM");
defer walker.deinit();
var manifest: std.ArrayList(u8) = .empty;
while (walker.next(io) catch |err| {
std.debug.panic("unable to walk web resource directory '{s}': {s}", .{ resource_dir_path, @errorName(err) });
}) |entry| {
// Dependency tracking is not recursive; register every subdirectory.
if (entry.kind == .directory) {
b.dependOnDirectoryContents(b.path(b.pathJoin(&.{ resource_dir_path, entry.path })));
}
if (entry.kind != .file) continue;
if (std.mem.eql(u8, entry.path, "resources.manifest")) continue;
manifest.appendSlice(b.allocator, entry.path) catch @panic("OOM");
manifest.append(b.allocator, '\n') catch @panic("OOM");
}
return manifest.toOwnedSlice(b.allocator) catch @panic("OOM");
}
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const web_resources_path = b.option([]const u8, "web-resources", "WASM/browser: directory to copy into zig-out/web and preload via resources.manifest (default: test)") orelse "test";
const web_host = b.option([]const u8, "web-host", "serve-web: bind host (default: 127.0.0.1)") orelse "127.0.0.1";
const web_port = b.option(u16, "web-port", "serve-web: bind port (default: 8080)") orelse 8080;
// Exposed for dependents as `dependency("engine", ...).module("math")`.
const math_module = modules.math_module(b);
const lint_dep = b.dependency("lint", .{
.target = b.graph.host,
.optimize = .safe,
});
const run_lint = b.addRunArtifact(lint_dep.artifact("lint"));
run_lint.setCwd(b.path("."));
run_lint.addPassthruArgs();
run_lint.addArg(".");
// Follow every module root; named imports do not expose source paths.
run_lint.addFileArg(b.path("core/root.zig"));
run_lint.addFileArg(b.path("platform/platform.zig"));
run_lint.addFileArg(b.path("platform/math/math.zig"));
inline for (.{ "common", "desktop", "psp", "3ds", "switch", "wasm" }) |root| {
run_lint.addFileArg(b.path("platform/root/" ++ root ++ ".zig"));
}
// Its only importer is the excluded Switch I/O file; still check its implementation.
run_lint.addFileArg(b.path("platform/switch/time.zig"));
const lint_step = b.step("lint", "Lint the codebase with tiger_lint");
lint_step.dependOn(&run_lint.step);
const architecture_check = b.addExecutable(.{
.name = "check-architecture",
.root_module = b.createModule(.{
.root_source_file = b.path("tools/check_architecture.zig"),
.target = b.graph.host,
.optimize = .safe,
}),
});
const run_architecture_check = b.addRunArtifact(architecture_check);
run_architecture_check.addDirectoryArg(b.path("."));
const architecture_step = b.step("check-architecture", "Check Core/Platform source ownership and imports");
architecture_step.dependOn(&run_architecture_check.step);
lint_step.dependOn(&run_architecture_check.step);
const overrides: config.Config.Overrides = .{
.gfx = b.option(config.Gfx, "gfx", "Graphics backend override (default: auto-detect from target)"),
.audio = b.option(config.Audio, "audio", "Audio backend override (default: platform default)"),
.psp_display_mode = b.option(config.PspDisplayMode, "psp-display", "PSP display mode: rgba8888 (32-bit, default) or rgb565 (16-bit)"),
.psp_mipmaps = b.option(bool, "psp-mipmaps", "PSP: generate mip levels for VRAM-resident textures (default: false)"),
.use_cwd = b.option(bool, "use-cwd", "Force resources+data dirs to CWD (debug/CI convenience; default: false)"),
.flush_logs = b.option(bool, "flush-logs", "Flush aether.log after every log message (debugging hard hangs; default: false)"),
.mesh_indexing = b.option(bool, "mesh-indexing", "Enable mesh index buffers (default: on except PSP/headless; override works on all backends)"),
};
const resolved_config = config.Config.resolve(target, overrides);
if (!directory_exists(b, "test")) {
const missing_demo = b.addFail("Aether demo steps require the repository test/ directory.");
b.step("run", "Run the app").dependOn(&missing_demo.step);
b.step("web", "Build the browser-playable WASM site in zig-out/web").dependOn(&missing_demo.step);
b.step("serve-web", "Serve zig-out/web with WASM MIME and COOP/COEP headers").dependOn(&missing_demo.step);
b.step("test", "Run tests").dependOn(&missing_demo.step);
return;
}
const exe = modules.add_game(b, b, .{
.name = "Aether",
.root_source_file = b.path("test/main.zig"),
.target = target,
.optimize = optimize,
.overrides = overrides,
});
const api_smoke = modules.add_game(b, b, .{
.name = "aether-api-smoke",
.root_source_file = b.path("test/api_smoke.zig"),
.target = target,
.optimize = optimize,
.overrides = overrides,
});
b.step("check-api", "Compile public API probes for the selected target").dependOn(&api_smoke.step);
if (resolved_config.platform == .linux or resolved_config.platform == .macos or resolved_config.platform == .windows) {
const run_api_smoke = b.addRunArtifact(api_smoke);
run_api_smoke.addArg("--exercise");
b.step("test-api", "Run CPU geometry and thread lifetime probes").dependOn(&run_api_smoke.step);
}
const nintendo_romfs = b.addWriteFiles();
_ = nintendo_romfs.addCopyFile(b.path("test/test.png"), "test.png");
_ = nintendo_romfs.addCopyFile(b.path("test/calm1.wav"), "calm1.wav");
_ = nintendo_romfs.addCopyFile(b.path("test/grass1.wav"), "grass1.wav");
const package = packaging.export_artifact_with_outputs(b, b, exe, resolved_config, .{
.title = "Aether",
.output_dir = switch (resolved_config.platform) {
.psp => "Aether-PSP",
.nintendo_3ds => "Aether-3DS",
.nintendo_switch => "Aether-Switch",
else => null,
},
.resources = &.{
.{ .path = b.path("test/test.png"), .name = "test.png" },
.{ .path = b.path("test/calm1.wav"), .name = "calm1.wav" },
.{ .path = b.path("test/grass1.wav"), .name = "grass1.wav" },
},
.nintendo_3ds_romfs = if (resolved_config.platform == .nintendo_3ds) nintendo_romfs.getDirectory() else null,
.switch_romfs = if (resolved_config.platform == .nintendo_switch) nintendo_romfs.getDirectory() else null,
});
const web_target = config.web_target(b);
const web_overrides: config.Config.Overrides = .{
.gfx = .webgl,
.use_cwd = true,
};
const web_exe = modules.add_game(b, b, .{
.name = "Aether",
.root_source_file = b.path("test/web_main.zig"),
.target = web_target,
.optimize = optimize,
.overrides = web_overrides,
});
const web_install = packaging.add_web_bundle(b, b, web_exe, .{
.web_resources = b.path(web_resources_path),
.web_resource_manifest = make_resource_manifest(b, web_resources_path),
});
const web_step = b.step("web", "Build the browser-playable WASM site in zig-out/web");
web_step.dependOn(&web_install.step);
const serve_web_cmd = packaging.add_serve_web_step(b, b, "aether-serve-web", web_install, web_host, web_port);
const serve_web_step = b.step("serve-web", "Serve zig-out/web with WASM MIME and COOP/COEP headers");
serve_web_step.dependOn(&serve_web_cmd.step);
const run_step = b.step("run", "Run the app");
if (resolved_config.platform == .nintendo_switch) {
// Switch can't run natively on the host. nxlink pushes the .nro to
// nx-hbloader on a networked Switch.
const dkp = tools.devkit_pro_path(b);
const link_cmd = b.addSystemCommand(&.{b.pathJoin(&.{ dkp, "tools/bin/nxlink" })});
if (b.option([]const u8, "nxlink-address", "Switch: target IP for nxlink push (default: mDNS auto-discover)")) |ip| {
link_cmd.addArgs(&.{ "-a", ip });
}
if (b.option(u32, "nxlink-retries", "Switch: nxlink retry count (default: 10)")) |n| {
link_cmd.addArgs(&.{ "-r", b.fmt("{d}", .{n}) });
}
if (b.option(bool, "nxlink-server", "Switch: pass -s so nxlink stays listening after upload (relays stdout/stderr from nro)") orelse false) {
link_cmd.addArg("-s");
}
link_cmd.addFileArg(b.graph.path(.install_bin, "Aether-Switch/Aether.nro"));
link_cmd.step.dependOn(b.getInstallStep());
// Arguments after the nro path are forwarded to the nro's argv.
link_cmd.addPassthruArgs();
const link_step = b.step("nxlink", "Push the nro to a networked Switch via nxlink");
link_step.dependOn(&link_cmd.step);
run_step.dependOn(&link_cmd.step);
} else if (resolved_config.platform == .nintendo_3ds) {
// Zitrus owns the 3dslink-protocol client, so running a 3DS target
// does not need devkitPro's external 3dslink executable.
const threedsx = package.nintendo_3dsx orelse unreachable;
const link_cmd = packaging.add_link3dsx(b, threedsx, .{
.address = b.option([]const u8, "3dslink-address", "3DS: target IP/hostname for Zitrus link (default: broadcast discovery)"),
.retries = b.option(u32, "3dslink-retries", "3DS: Zitrus link broadcast retry count"),
});
link_cmd.step.dependOn(b.getInstallStep());
const link_step = b.step("3dslink", "Send the 3dsx to a networked 3DS via Zitrus");
link_step.dependOn(&link_cmd.step);
run_step.dependOn(&link_cmd.step);
} else {
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
run_cmd.addPassthruArgs();
run_step.dependOn(&run_cmd.step);
}
// Engine unit tests (desktop only -- PSP/3DS/Switch pull in symbols that
// can't be linked or analyzed under the test runner).
if (resolved_config.platform != .psp and resolved_config.platform != .nintendo_3ds and resolved_config.platform != .nintendo_switch) {
const mod_tests = b.addTest(.{
.root_module = modules.engine_module(exe),
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const platform_tests = b.addTest(.{
.root_module = modules.platform_module(exe),
});
const run_platform_tests = b.addRunArtifact(platform_tests);
// Its own module, so Platform's tests don't reach it
const math_tests = b.addTest(.{
.root_module = b.createModule(.{
.root_source_file = math_module.root_source_file,
.target = target,
.optimize = optimize,
}),
});
const run_math_tests = b.addRunArtifact(math_tests);
const test_step = b.step("test", "Run tests");
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_platform_tests.step);
test_step.dependOn(&run_math_tests.step);
test_step.dependOn(&run_architecture_check.step);
}
}