-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuild.zig
More file actions
426 lines (365 loc) · 14.2 KB
/
build.zig
File metadata and controls
426 lines (365 loc) · 14.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
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
const std = @import("std");
const build_helpers = @import("build/build_helpers.zig");
/// Build configuration for the MFS Engine
const BuildConfig = struct {
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
enable_vulkan: bool = false,
enable_ray_tracing: bool = false,
enable_tracy: bool = false,
enable_hot_reload: bool = false,
pub fn init(b: *std.Build) BuildConfig {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
return .{
.target = target,
.optimize = optimize,
.enable_vulkan = b.option(bool, "vulkan", "Enable Vulkan support") orelse false,
.enable_ray_tracing = b.option(bool, "ray-tracing", "Enable ray tracing") orelse false,
.enable_tracy = b.option(bool, "tracy", "Enable Tracy profiler") orelse false,
.enable_hot_reload = b.option(bool, "hot-reload", "Enable hot reload") orelse false,
};
}
};
/// Common configuration for creating executables
const ExecutableConfig = struct {
name: []const u8,
root_source_file: []const u8,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
dependencies: ?*std.Build.Dependency = null,
opts: ?*std.Build.Step.Options = null,
};
/// Common configuration for creating run steps
const RunStepConfig = struct {
name: []const u8,
description: []const u8,
exe: *std.Build.Step.Compile,
install_step: ?*std.Build.Step = null,
};
/// Create an executable with common configuration
fn createExecutable(b: *std.Build, config: ExecutableConfig) !*std.Build.Step.Compile {
const exe = b.addExecutable(.{
.name = config.name,
.root_source_file = b.path(config.root_source_file),
.target = config.target,
.optimize = config.optimize,
});
// Add common dependencies
try build_helpers.addSourceModules(b, exe);
if (config.opts) |opts| {
exe.addOptions("build_options", opts);
}
// Add platform-specific dependencies
addPlatformDependencies(exe, config.target.result.os.tag);
// Install the executable
b.installArtifact(exe);
return exe;
}
/// Create a run step with common configuration
fn createRunStep(b: *std.Build, config: RunStepConfig) !*std.Build.Step {
const run_cmd = b.addRunArtifact(config.exe);
run_cmd.step.dependOn(config.install_step orelse b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step(config.name, config.description);
run_step.dependOn(&run_cmd.step);
return run_step;
}
/// Main build function
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// Create build options
const options = b.addOptions();
addBuildOptions(options, target.result);
// Create main library
const lib = b.addModule("mfs", .{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
// Create main executable
const exe = b.addExecutable(.{
.name = "mfs-engine",
.root_module = b.createModule(.{
.root_source_file = b.path("src/bin/main.zig"),
.target = target,
.optimize = optimize,
}),
});
exe.root_module.addImport("mfs", lib);
exe.root_module.addOptions("build_options", options);
addPlatformDependencies(exe, target.result.os.tag);
b.installArtifact(exe);
// Create run step for main executable
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
const run_step = b.step("run", "Run the main executable");
run_step.dependOn(&run_cmd.step);
// Add memory manager tests
const memory_manager_tests = b.addTest(.{
.name = "memory-manager-tests",
.root_module = b.createModule(.{
.root_source_file = b.path("src/graphics/test_memory_manager.zig"),
.target = target,
.optimize = optimize,
}),
});
memory_manager_tests.root_module.addImport("mfs", lib);
memory_manager_tests.root_module.addOptions("build_options", options);
addPlatformDependencies(memory_manager_tests, target.result.os.tag);
// Add Vulkan system library to tests (include path handled by Vulkan SDK environment)
addOptionalLibrary(memory_manager_tests, "vulkan-1");
const vulkan_backend_tests = b.addTest(.{
.name = "vulkan-backend-tests",
.root_module = b.createModule(.{
.root_source_file = b.path("src/graphics/test_vulkan_backend_new.zig"),
.target = target,
.optimize = optimize,
}),
});
vulkan_backend_tests.root_module.addImport("mfs", lib);
vulkan_backend_tests.root_module.addOptions("build_options", options);
addPlatformDependencies(vulkan_backend_tests, target.result.os.tag);
addOptionalLibrary(vulkan_backend_tests, "vulkan-1");
const test_step = b.step("test-graphics", "Run graphics backend tests");
test_step.dependOn(&memory_manager_tests.step);
test_step.dependOn(&vulkan_backend_tests.step);
// Build tests, tools, and web target
buildTests(b, target, optimize, lib, options);
buildTools(b, target, optimize, lib, options);
buildWebTarget(b, target, optimize, lib, options);
}
fn addBuildOptions(options: *std.Build.Step.Options, target: std.Target) void {
// Platform detection
options.addOption(bool, "is_windows", target.os.tag == .windows);
options.addOption(bool, "is_linux", target.os.tag == .linux);
options.addOption(bool, "is_macos", target.os.tag == .macos);
options.addOption(bool, "is_web", target.os.tag == .emscripten or target.os.tag == .wasi);
options.addOption(bool, "is_mobile", false);
// Graphics backend availability
options.addOption(bool, "vulkan_available", target.os.tag == .windows or target.os.tag == .linux);
options.addOption(bool, "d3d11_available", target.os.tag == .windows);
options.addOption(bool, "d3d12_available", target.os.tag == .windows);
options.addOption(bool, "metal_available", target.os.tag == .macos);
options.addOption(bool, "opengl_available", false); // Disabled due to missing GL headers
options.addOption(bool, "webgpu_available", target.os.tag == .emscripten or target.os.tag == .wasi);
// Feature flags
options.addOption(bool, "enable_validation", @import("builtin").mode == .Debug);
options.addOption(bool, "enable_tracy", false);
options.addOption(bool, "enable_hot_reload", @import("builtin").mode == .Debug);
}
fn buildTests(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
mfs: *std.Build.Module,
options: *std.Build.Step.Options,
) void {
// Main test runner that includes all modules
const test_exe = b.addTest(.{
.name = "mfs-tests",
.root_module = b.createModule(.{
.root_source_file = b.path("src/test.zig"),
.target = target,
.optimize = optimize,
}),
});
test_exe.root_module.addImport("mfs", mfs);
test_exe.root_module.addOptions("build_options", options);
addPlatformDependencies(test_exe, target.result.os.tag);
const test_run = b.addRunArtifact(test_exe);
const test_step = b.step("test", "Run all tests");
test_step.dependOn(&test_run.step);
// Individual test suites with proper module access
const test_files = [_]struct { name: []const u8, path: []const u8 }{
.{ .name = "math-tests", .path = "src/tests/test_math.zig" },
.{ .name = "physics-tests", .path = "src/tests/physics_test.zig" },
.{ .name = "comprehensive-tests", .path = "src/tests/comprehensive_tests.zig" },
.{ .name = "benchmark-tests", .path = "src/tests/benchmarks/render_bench.zig" },
};
for (test_files) |test_file| {
const individual_test = b.addTest(.{
.name = test_file.name,
.root_module = b.createModule(.{
.root_source_file = b.path(test_file.path),
.target = target,
.optimize = optimize,
}),
});
individual_test.root_module.addImport("mfs", mfs);
individual_test.root_module.addOptions("build_options", options);
addPlatformDependencies(individual_test, target.result.os.tag);
const individual_test_run = b.addRunArtifact(individual_test);
const individual_test_step = b.step(
b.fmt("test-{s}", .{test_file.name}),
b.fmt("Run {s}", .{test_file.name}),
);
individual_test_step.dependOn(&individual_test_run.step);
}
}
fn buildTools(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
mfs: *std.Build.Module,
options: *std.Build.Step.Options,
) void {
const tools = [_]struct { name: []const u8, path: []const u8 }{
.{ .name = "asset-processor", .path = "tools/asset_processor/asset_processor.zig" },
.{ .name = "model-viewer", .path = "tools/model_viewer.zig" },
.{ .name = "texture-converter", .path = "tools/texture_converter.zig" },
};
for (tools) |tool| {
const exe = b.addExecutable(.{
.name = tool.name,
.root_module = b.createModule(.{
.root_source_file = b.path(tool.path),
.target = target,
.optimize = optimize,
}),
});
exe.root_module.addImport("mfs", mfs);
exe.root_module.addOptions("build_options", options);
addPlatformDependencies(exe, target.result.os.tag);
b.installArtifact(exe);
}
}
/// Build for WebAssembly target
fn buildWebTarget(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
mfs: *std.Build.Module,
options: *std.Build.Step.Options,
) void {
_ = target;
_ = optimize;
_ = options;
const web_step = b.step("web", "Build for WebAssembly");
// Create web-specific target
const web_target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
const web_exe = b.addExecutable(.{
.name = "mfs-web",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = web_target,
.optimize = .ReleaseSmall,
}),
});
web_exe.root_module.addImport("mfs", mfs);
web_exe.entry = .disabled;
// Zig versions prior to 0.15 do not expose `export_symbol_names` on `std.Build.Step.Compile`.
// Use reflection to set it only when available so the build script remains compatible with
// older toolchains (e.g., 0.14.x).
if (@hasField(@TypeOf(web_exe.*), "export_symbol_names")) {
@field(web_exe, "export_symbol_names") = &[_][]const u8{
"web_init",
"web_frame",
"web_deinit",
};
}
b.installArtifact(web_exe);
web_step.dependOn(&web_exe.step);
}
/// Add platform-specific dependencies with improved organization
fn addPlatformDependencies(exe: *std.Build.Step.Compile, os_tag: std.Target.Os.Tag) void {
// Common dependencies
exe.linkLibC();
// Platform-specific dependencies
switch (os_tag) {
.windows => addWindowsDependencies(exe),
.linux => addLinuxDependencies(exe),
.macos => addMacosDependencies(exe),
.ios => addIosDependencies(exe),
.wasi, .emscripten, .freestanding => addWebDependencies(exe),
else => {},
}
}
/// Add Windows-specific dependencies
fn addWindowsDependencies(exe: *std.Build.Step.Compile) void {
const required_libs = [_][]const u8{
"user32", "kernel32", "gdi32", "shell32",
"opengl32", "winmm", "ole32", "uuid",
};
for (required_libs) |lib| {
exe.linkSystemLibrary(lib);
}
// Optional libraries
const optional_libs = [_][]const u8{
"glu32", "xinput", "dwmapi",
"d3d11", "d3d12", "dxgi",
"dxguid", "d3dcompiler",
};
for (optional_libs) |lib| {
addOptionalLibrary(exe, lib);
}
}
/// Add Linux-specific dependencies
fn addLinuxDependencies(exe: *std.Build.Step.Compile) void {
const required_libs = [_][]const u8{ "GL", "X11", "m" };
for (required_libs) |lib| {
exe.linkSystemLibrary(lib);
}
// Optional Linux dependencies
const optional_libs = [_][]const u8{
"Xi", "Xcursor", "Xrandr", "Xinerama",
"wayland-client", "wayland-egl", "wayland-cursor",
};
for (optional_libs) |lib| {
addOptionalLibrary(exe, lib);
}
}
/// Add macOS-specific dependencies
fn addMacosDependencies(exe: *std.Build.Step.Compile) void {
const frameworks = [_][]const u8{
"Cocoa", "OpenGL", "Metal", "MetalKit",
"QuartzCore", "CoreFoundation", "CoreGraphics", "Foundation",
"AppKit", "IOKit",
};
for (frameworks) |framework| {
exe.linkFramework(framework);
}
}
/// Add iOS-specific dependencies
fn addIosDependencies(exe: *std.Build.Step.Compile) void {
const frameworks = [_][]const u8{
"UIKit", "Foundation", "CoreGraphics", "QuartzCore",
"OpenGLES", "Metal", "MetalKit",
};
for (frameworks) |framework| {
exe.linkFramework(framework);
}
}
/// Add Web-specific dependencies
fn addWebDependencies(_: *std.Build.Step.Compile) void {
// Web platform typically doesn't need explicit libraries
}
/// Helper to add optional libraries
fn addOptionalLibrary(exe: *std.Build.Step.Compile, name: []const u8) void {
// For now, just add a debug message for optional libraries
// In production, this would check if the library exists first
std.log.debug("Optional library '{s}' requested", .{name});
// Skip libraries that are commonly missing from default Windows SDK/MinGW setups.
// They are useful when available but should not make the build fail if absent.
const skip_libs = [_][]const u8{
"xinput",
"vulkan",
"vulkan-1",
"d3dcompiler", // Use runtime-loaded d3dcompiler_47.dll instead when present.
};
for (skip_libs) |lib| {
if (std.mem.eql(u8, name, lib)) {
return;
}
}
exe.linkSystemLibrary(name);
}