forked from zig-gamedev/zig-gamedev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.zig
425 lines (377 loc) · 15.6 KB
/
build.zig
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
const builtin = @import("builtin");
const std = @import("std");
pub const min_zig_version = std.SemanticVersion{
.major = 0,
.minor = 13,
.patch = 0,
.pre = "dev.351",
};
pub fn build(b: *std.Build) void {
ensureZigVersion() catch return;
if (checkGitLfsContent() == false) {
ensureGit(b.allocator) catch return;
ensureGitLfs(b.allocator, "install") catch return;
ensureGitLfs(b.allocator, "pull") catch return;
if (checkGitLfsContent() == false) {
std.log.err("\n" ++
\\---------------------------------------------------------------------------
\\
\\Something went wrong, Git LFS content has not been downloaded.
\\
\\Please try to re-clone the repo and build again.
\\
\\---------------------------------------------------------------------------
\\
, .{});
return;
}
}
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const zpix_enable = b.option(
bool,
"zpix-enable",
"Enable PIX for Windows profiler",
) orelse false;
const options = .{
.optimize = optimize,
.target = target,
.zxaudio2_debug_layer = b.option(
bool,
"zxaudio2_debug_layer",
"Enable XAudio2 debug layer",
) orelse false,
.zd3d12_debug_layer = b.option(
bool,
"zd3d12_debug_layer",
"Enable DirectX 12 debug layer",
) orelse false,
.zd3d12_gbv = b.option(
bool,
"zd3d12_gbv",
"Enable DirectX 12 GPU-Based Validation (GBV)",
) orelse false,
.zpix_enable = zpix_enable,
.zpix_path = b.option(
[]const u8,
"zpix-path",
"Installed PIX path",
) orelse if (zpix_enable) @panic("PIX path is required when enabled") else "",
};
if (target.result.os.tag == .emscripten) {
buildAndInstallSamplesWeb(b, .{
.optimize = optimize,
.target = target,
});
} else {
buildAndInstallSamples(b, options, samples_cross_platform);
if (target.result.os.tag == .windows) {
if (builtin.os.tag == .windows or builtin.os.tag == .linux) {
buildAndInstallSamples(b, options, samples_windows_linux);
if (builtin.os.tag == .windows) {
buildAndInstallSamples(b, options, samples_windows);
}
}
}
}
{ // Tests
const test_step = b.step("test", "Run all tests");
tests(b, target, optimize, test_step);
}
{ // Benchmarks
const benchmark_step = b.step("benchmark", "Run all benchmarks");
const zmath = b.dependency("zmath", .{
.optimize = .ReleaseFast,
});
benchmark_step.dependOn(&b.addRunArtifact(zmath.artifact("zmath-benchmarks")).step);
}
// Experiments
if (b.option(bool, "experiments", "Build our prototypes and experimental programs") orelse false) {
@import("experiments/build.zig").build(b, options);
}
}
pub const samples_windows = struct {
pub const audio_experiments = @import("samples/audio_experiments/build.zig");
pub const audio_playback_test = @import("samples/audio_playback_test/build.zig");
pub const directml_convolution_test = @import("samples/directml_convolution_test/build.zig");
pub const vector_graphics_test = @import("samples/vector_graphics_test/build.zig");
};
pub const samples_windows_linux = struct {
pub const bindless = @import("samples/bindless/build.zig");
pub const mesh_shader_test = @import("samples/mesh_shader_test/build.zig");
pub const minimal_d3d12 = @import("samples/minimal_d3d12/build.zig");
pub const minimal_glfw_d3d12 = @import("samples/minimal_glfw_d3d12/build.zig");
pub const minimal_zgui_glfw_d3d12 = @import("samples/minimal_zgui_glfw_d3d12/build.zig");
pub const minimal_zgui_win32_d3d12 = @import("samples/minimal_zgui_win32_d3d12/build.zig");
pub const openvr_test = @import("samples/openvr_test/build.zig");
pub const simple_openvr = @import("samples/simple_openvr/build.zig");
pub const rasterization = @import("samples/rasterization/build.zig");
// TODO: get simple raytracer working again
//pub const simple_raytracer = @import("samples/simple_raytracer/build.zig");
pub const textured_quad = @import("samples/textured_quad/build.zig");
pub const triangle = @import("samples/triangle/build.zig");
pub const zphysics_instanced_cubes_d3d12 = @import("samples/zphysics_instanced_cubes_d3d12/build.zig");
};
pub const samples_cross_platform = struct {
pub const sdl2_demo = @import("samples/sdl2_demo/build.zig");
// OpenGL samples
pub const minimal_glfw_gl = @import("samples/minimal_glfw_gl/build.zig");
pub const minimal_sdl_gl = @import("samples/minimal_sdl_gl/build.zig");
pub const minimal_zgui_glfw_gl = @import("samples/minimal_zgui_glfw_gl/build.zig");
// WebGPU samples
pub const audio_experiments_wgpu = @import("samples/audio_experiments_wgpu/build.zig");
pub const bullet_physics_test_wgpu = @import("samples/bullet_physics_test_wgpu/build.zig");
pub const frame_pacing_wgpu = @import("samples/frame_pacing_wgpu/build.zig");
pub const gamepad_wgpu = @import("samples/gamepad_wgpu/build.zig");
pub const gui_test_wgpu = @import("samples/gui_test_wgpu/build.zig");
pub const instanced_pills_wgpu = @import("samples/instanced_pills_wgpu/build.zig");
pub const layers_wgpu = @import("samples/layers_wgpu/build.zig");
pub const minimal_zgpu_zgui = @import("samples/minimal_zgpu_zgui/build.zig");
pub const monolith = @import("samples/monolith/build.zig");
pub const physically_based_rendering_wgpu = @import("samples/physically_based_rendering_wgpu/build.zig");
pub const physics_test_wgpu = @import("samples/physics_test_wgpu/build.zig");
pub const procedural_mesh_wgpu = @import("samples/procedural_mesh_wgpu/build.zig");
pub const textured_quad_wgpu = @import("samples/textured_quad_wgpu/build.zig");
pub const triangle_wgpu = @import("samples/triangle_wgpu/build.zig");
};
pub const samples_web = struct {
pub const sdl2_demo = samples_cross_platform.sdl2_demo;
// TODO: WebGL samples
// pub const minimal_glfw_gl = samples_cross_platform.minimal_glfw_gl;
// pub const minimal_sdl_gl = samples_cross_platform.minimal_sdl_gl;
// pub const minimal_zgui_glfw_gl = samples_cross_platform.minimal_zgui_glfw_gl;
// TODO: WebGPU samples
// pub const audio_experiments_wgpu = samples_cross_platform.audio_experiments_wgpu;
// pub const bullet_physics_test_wgpu = samples_cross_platform.bullet_physics_test_wgpu;
// pub const gamepad_wgpu = samples_cross_platform.gamepad_wgpu;
// pub const gui_test_wgpu = samples_cross_platform.gui_test_wgpu;
// pub const instanced_pills_wgpu = samples_cross_platform.instanced_pills_wgpu;
// pub const layers_wgpu = samples_cross_platform.layers_wgpu;
// pub const minimal_zgpu_zgui = samples_cross_platform.minimal_zgpu_zgui;
// pub const monolith = samples_cross_platform.monolith;
// pub const physically_based_rendering_wgpu = samples_cross_platform.physically_based_rendering_wgpu;
// pub const physics_test_wgpu = samples_cross_platform.physics_test_wgpu;
// pub const procedural_mesh_wgpu = samples_cross_platform.procedural_mesh_wgpu;
// pub const textured_quad_wgpu = samples_cross_platform.textured_quad_wgpu;
// pub const triangle_wgpu = samples_cross_platform.triangle_wgpu;
};
fn buildAndInstallSamples(b: *std.Build, options: anytype, comptime samples: anytype) void {
inline for (comptime std.meta.declarations(samples)) |d| {
const exe = @field(samples, d.name).build(b, options);
// TODO: Problems with LTO on Windows.
if (exe.rootModuleTarget().os.tag == .windows) {
exe.want_lto = false;
}
if (exe.root_module.optimize != .Debug) {
exe.root_module.strip = true;
}
const install_exe = b.addInstallArtifact(exe, .{});
b.getInstallStep().dependOn(&install_exe.step);
b.step(d.name, "Build '" ++ d.name ++ "' demo").dependOn(&install_exe.step);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(&install_exe.step);
b.step(d.name ++ "-run", "Run '" ++ d.name ++ "' demo").dependOn(&run_cmd.step);
}
}
fn buildAndInstallSamplesWeb(b: *std.Build, options: anytype) void {
const zemscripten = @import("zemscripten");
const activate_emsdk_step = zemscripten.activateEmsdkStep(b);
inline for (comptime std.meta.declarations(samples_web)) |d| {
const build_web_app_step = @field(samples_web, d.name).buildWeb(b, options);
build_web_app_step.dependOn(activate_emsdk_step);
b.getInstallStep().dependOn(build_web_app_step);
const html_filename = std.fmt.allocPrint(
b.allocator,
"{s}.html",
.{d.name},
) catch unreachable;
const emrun_step = zemscripten.emrunStep(
b,
b.getInstallPath(.{ .custom = "web" }, html_filename),
&.{},
);
emrun_step.dependOn(build_web_app_step);
b.step(
d.name,
"Build '" ++ d.name ++ "' sample as a web app",
).dependOn(build_web_app_step);
b.step(
d.name ++ "-emrun",
"Build '" ++ d.name ++ "' sample as a web app and serve locally using `emrun`",
).dependOn(emrun_step);
}
}
fn tests(
b: *std.Build,
target: std.Build.ResolvedTarget,
optimize: std.builtin.OptimizeMode,
test_step: *std.Build.Step,
) void {
// TODO: Renable randomly failing zaudio tests on windows
if (target.result.os.tag != .windows) {
const zaudio = b.dependency("zaudio", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zaudio.artifact("zaudio-tests")).step);
}
// TODO: Get zbullet tests working on Windows again
if (target.result.os.tag != .windows) {
const zbullet = b.dependency("zbullet", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zbullet.artifact("zbullet-tests")).step);
}
const zflecs = b.dependency("zflecs", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zflecs.artifact("zflecs-tests")).step);
const zgpu = b.dependency("zgpu", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zgpu.artifact("zgpu-tests")).step);
const zgui = b.dependency("zgui", .{
.target = target,
.optimize = optimize,
.with_te = true,
});
test_step.dependOn(&b.addRunArtifact(zgui.artifact("zgui-tests")).step);
const zmath = b.dependency("zmath", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zmath.artifact("zmath-tests")).step);
const zmesh = b.dependency("zmesh", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zmesh.artifact("zmesh-tests")).step);
test_step.dependOn(&b.addRunArtifact(b.dependency("zphysics", .{
.target = target,
.optimize = optimize,
.use_double_precision = false,
}).artifact("zphysics-tests")).step);
test_step.dependOn(&b.addRunArtifact(b.dependency("zphysics", .{
.target = target,
.optimize = optimize,
.use_double_precision = true,
}).artifact("zphysics-tests")).step);
const zpool = b.dependency("zpool", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zpool.artifact("zpool-tests")).step);
const zjobs = b.dependency("zjobs", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zjobs.artifact("zjobs-tests")).step);
const zstbi = b.dependency("zstbi", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(zstbi.artifact("zstbi-tests")).step);
const ztracy = b.dependency("ztracy", .{
.target = target,
.optimize = optimize,
});
test_step.dependOn(&b.addRunArtifact(ztracy.artifact("ztracy-tests")).step);
}
// TODO: Delete this once Zig checks minimum_zig_version in build.zig.zon
fn ensureZigVersion() !void {
var installed_ver = builtin.zig_version;
installed_ver.build = null;
if (installed_ver.order(min_zig_version) == .lt) {
std.log.err("\n" ++
\\---------------------------------------------------------------------------
\\
\\Installed Zig compiler version is too old.
\\
\\Min. required version: {any}
\\Installed version: {any}
\\
\\Please install newer version and try again.
\\Latest version can be found here: https://ziglang.org/download/
\\
\\---------------------------------------------------------------------------
\\
, .{ min_zig_version, installed_ver });
return error.ZigIsTooOld;
}
}
fn ensureGit(allocator: std.mem.Allocator) !void {
const printErrorMsg = (struct {
fn impl() void {
std.log.err("\n" ++
\\---------------------------------------------------------------------------
\\
\\'git version' failed. Is Git not installed?
\\
\\---------------------------------------------------------------------------
\\
, .{});
}
}).impl;
const argv = &[_][]const u8{ "git", "version" };
const result = std.process.Child.run(.{
.allocator = allocator,
.argv = argv,
}) catch { // e.g. FileNotFound
printErrorMsg();
return error.GitNotFound;
};
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.term.Exited != 0) {
printErrorMsg();
return error.GitNotFound;
}
}
fn ensureGitLfs(allocator: std.mem.Allocator, cmd: []const u8) !void {
const printNoGitLfs = (struct {
fn impl() void {
std.log.err("\n" ++
\\---------------------------------------------------------------------------
\\
\\Please install Git LFS (Large File Support) extension and run 'zig build' again.
\\
\\For more info about Git LFS see: https://git-lfs.github.com/
\\
\\---------------------------------------------------------------------------
\\
, .{});
}
}).impl;
const argv = &[_][]const u8{ "git", "lfs", cmd };
const result = std.process.Child.run(.{
.allocator = allocator,
.argv = argv,
}) catch { // e.g. FileNotFound
printNoGitLfs();
return error.GitLfsNotFound;
};
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.term.Exited != 0) {
printNoGitLfs();
return error.GitLfsNotFound;
}
}
fn checkGitLfsContent() bool {
const expected_contents =
\\DO NOT EDIT OR DELETE
\\This file is used to check if Git LFS content has been downloaded
;
var buf: [expected_contents.len]u8 = undefined;
_ = std.fs.cwd().readFile(".lfs-content-token", &buf) catch {
return false;
};
return std.mem.eql(u8, expected_contents, &buf);
}