Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5857c1a

Browse files
committed
Rejig build.zig
1 parent 4c5ddf8 commit 5857c1a

File tree

1 file changed

+139
-151
lines changed

1 file changed

+139
-151
lines changed

build.zig

Lines changed: 139 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,18 @@
11
const builtin = @import("builtin");
22
const std = @import("std");
33

4-
pub fn build(b: *std.Build) void {
5-
if (checkGitLfsContent() == false) {
6-
ensureGit(b.allocator) catch return;
7-
ensureGitLfs(b.allocator, "install") catch return;
8-
ensureGitLfs(b.allocator, "pull") catch return;
9-
if (checkGitLfsContent() == false) {
10-
std.log.err("\n" ++
11-
\\---------------------------------------------------------------------------
12-
\\
13-
\\Something went wrong, Git LFS content has not been downloaded.
14-
\\
15-
\\Please try to re-clone the repo and build again.
16-
\\
17-
\\---------------------------------------------------------------------------
18-
\\
19-
, .{});
20-
return;
21-
}
22-
}
23-
24-
const target = b.standardTargetOptions(.{});
25-
const optimize = b.standardOptimizeOption(.{});
26-
27-
const zpix_enable = b.option(
28-
bool,
29-
"zpix-enable",
30-
"Enable PIX for Windows profiler",
31-
) orelse false;
32-
const options = .{
33-
.optimize = optimize,
34-
.target = target,
35-
.zxaudio2_debug_layer = b.option(
36-
bool,
37-
"zxaudio2_debug_layer",
38-
"Enable XAudio2 debug layer",
39-
) orelse false,
40-
.zd3d12_debug_layer = b.option(
41-
bool,
42-
"zd3d12_debug_layer",
43-
"Enable DirectX 12 debug layer",
44-
) orelse false,
45-
.zd3d12_gbv = b.option(
46-
bool,
47-
"zd3d12_gbv",
48-
"Enable DirectX 12 GPU-Based Validation (GBV)",
49-
) orelse false,
50-
.zpix_enable = zpix_enable,
51-
.zpix_path = b.option(
52-
[]const u8,
53-
"zpix-path",
54-
"Installed PIX path",
55-
) orelse if (zpix_enable) @panic("PIX path is required when enabled") else "",
4+
pub fn build(b: *std.Build) !void {
5+
checkGitLfsContent() catch {
6+
try ensureGit(b.allocator);
7+
try ensureGitLfs(b.allocator, "install");
8+
try ensureGitLfs(b.allocator, "pull");
9+
try checkGitLfsContent();
5610
};
5711

58-
if (target.result.os.tag == .windows) {
59-
if (builtin.target.os.tag == .windows or builtin.target.os.tag == .linux) {
60-
const activate_zwindows = @import("zwindows").activateSdk(b, b.dependency("zwindows", .{}));
61-
b.default_step.dependOn(activate_zwindows);
62-
63-
samples.build(b, options, activate_zwindows, samples.windows_linux_cross);
64-
65-
if (builtin.target.os.tag == .windows) {
66-
// TODO: Try to upgrade these to windows_linux_cross
67-
samples.build(b, options, activate_zwindows, samples.windows_only);
68-
}
69-
} else @panic("Unsupported host OS for Windows target");
70-
}
71-
72-
if (target.result.os.tag == .emscripten) {
73-
const activate_emsdk = @import("zemscripten").activateEmsdkStep(b);
74-
b.default_step.dependOn(activate_emsdk);
75-
76-
const web_options = .{
77-
.optimize = optimize,
78-
.target = target,
79-
};
80-
samples.buildWeb(b, web_options, activate_emsdk);
81-
} else {
82-
samples.build(b, options, null, samples.crossplatform);
83-
}
84-
85-
// Install prebuilt SDL2 libs in bin output dir
86-
if (@import("zsdl").prebuilt_sdl2.install(b, options.target.result, .bin, .{
87-
.ttf = true,
88-
.image = true,
89-
})) |install_sdl2_step| {
90-
b.getInstallStep().dependOn(install_sdl2_step);
91-
}
92-
93-
{ // Benchmarks
94-
const benchmark_step = b.step("benchmark", "Run all benchmarks");
95-
const zmath = b.dependency("zmath", .{
96-
.optimize = .ReleaseFast,
97-
});
98-
benchmark_step.dependOn(&b.addRunArtifact(zmath.artifact("zmath-benchmarks")).step);
99-
}
100-
101-
// Experiments
102-
if (b.option(bool, "experiments", "Build our prototypes and experimental programs") orelse false) {
103-
@import("experiments/build.zig").build(b, options);
12+
const target = b.standardTargetOptions(.{});
13+
switch (target.result.os.tag) {
14+
.emscripten => buildWeb(b, target),
15+
else => buildDesktop(b, target),
10416
}
10517
}
10618

@@ -160,13 +72,15 @@ pub const samples = struct {
16072
/// Sample apps that can be built as web applications using zemscripten.
16173
pub const web = struct {
16274
pub const sdl2_demo = samples.crossplatform.sdl2_demo;
163-
pub const minimal_glfw_gl = samples.crossplatform.minimal_glfw_gl;
16475

165-
// TODO: WebGL samples
76+
// WebGL samples
77+
// TODO:
16678
// pub const minimal_sdl_gl = samples.crossplatform.minimal_sdl_gl;
79+
pub const minimal_glfw_gl = samples.crossplatform.minimal_glfw_gl;
16780
// pub const minimal_zgui_glfw_gl = samples.crossplatform.minimal_zgui_glfw_gl;
16881

169-
// TODO: WebGPU samples
82+
// WebGPU samples
83+
// TODO:
17084
// pub const audio_experiments_wgpu = samples.crossplatform.audio_experiments_wgpu;
17185
// pub const bullet_physics_test_wgpu = samples.crossplatform.bullet_physics_test_wgpu;
17286
// pub const gamepad_wgpu = samples.crossplatform.gamepad_wgpu;
@@ -181,60 +95,132 @@ pub const samples = struct {
18195
// pub const textured_quad_wgpu = samples.crossplatform.textured_quad_wgpu;
18296
// pub const triangle_wgpu = samples.crossplatform.triangle_wgpu;
18397
};
98+
};
18499

185-
fn build(
186-
b: *std.Build,
187-
options: anytype,
188-
maybe_depend_step: ?*std.Build.Step,
189-
comptime apps: anytype,
190-
) void {
191-
inline for (comptime std.meta.declarations(apps)) |d| {
192-
const exe = buildExe(b, options, @field(apps, d.name));
193-
if (maybe_depend_step) |step| {
194-
exe.step.dependOn(step);
195-
}
196-
}
100+
fn buildWeb(b: *std.Build, target: std.Build.ResolvedTarget) void {
101+
const options = .{
102+
.optimize = b.standardOptimizeOption(.{
103+
.preferred_optimize_mode = .ReleaseSmall,
104+
}),
105+
.target = target,
106+
};
107+
108+
const activate_emsdk = @import("zemscripten").activateEmsdkStep(b);
109+
b.default_step.dependOn(activate_emsdk);
110+
111+
inline for (comptime std.meta.declarations(samples.web)) |d| {
112+
const build_web_app_step = @field(samples.web, d.name).buildWeb(b, options);
113+
build_web_app_step.dependOn(activate_emsdk);
114+
115+
b.getInstallStep().dependOn(build_web_app_step);
116+
117+
const html_filename = std.fmt.allocPrint(
118+
b.allocator,
119+
"{s}.html",
120+
.{d.name},
121+
) catch unreachable;
122+
123+
const emrun_step = @import("zemscripten").emrunStep(
124+
b,
125+
b.getInstallPath(.{ .custom = "web" }, html_filename),
126+
&.{},
127+
);
128+
emrun_step.dependOn(build_web_app_step);
129+
130+
b.step(
131+
d.name,
132+
"Build '" ++ d.name ++ "' sample as a web app",
133+
).dependOn(build_web_app_step);
134+
135+
b.step(
136+
d.name ++ "-emrun",
137+
"Build '" ++ d.name ++ "' sample as a web app and serve locally using `emrun`",
138+
).dependOn(emrun_step);
197139
}
198140

199-
fn buildWeb(
200-
b: *std.Build,
201-
options: anytype,
202-
maybe_depend_step: ?*std.Build.Step,
203-
) void {
204-
inline for (comptime std.meta.declarations(samples.web)) |d| {
205-
const build_web_app_step = @field(samples.web, d.name).buildWeb(b, options);
141+
buildBenchmarks(b, options.target);
142+
}
143+
144+
fn buildDesktop(b: *std.Build, target: std.Build.ResolvedTarget) void {
145+
const optimize = b.standardOptimizeOption(.{});
206146

207-
if (maybe_depend_step) |step| {
208-
build_web_app_step.dependOn(step);
147+
const zpix_enable = b.option(
148+
bool,
149+
"zpix-enable",
150+
"Enable PIX for Windows profiler",
151+
) orelse false;
152+
const options = .{
153+
.optimize = optimize,
154+
.target = target,
155+
.zxaudio2_debug_layer = b.option(
156+
bool,
157+
"zxaudio2_debug_layer",
158+
"Enable XAudio2 debug layer",
159+
) orelse false,
160+
.zd3d12_debug_layer = b.option(
161+
bool,
162+
"zd3d12_debug_layer",
163+
"Enable DirectX 12 debug layer",
164+
) orelse false,
165+
.zd3d12_gbv = b.option(
166+
bool,
167+
"zd3d12_gbv",
168+
"Enable DirectX 12 GPU-Based Validation (GBV)",
169+
) orelse false,
170+
.zpix_enable = zpix_enable,
171+
.zpix_path = b.option(
172+
[]const u8,
173+
"zpix-path",
174+
"Installed PIX path",
175+
) orelse if (zpix_enable) @panic("PIX path is required when enabled") else "",
176+
};
177+
178+
if (target.result.os.tag == .windows) {
179+
if (builtin.target.os.tag == .windows or builtin.target.os.tag == .linux) {
180+
const activate_zwindows = @import("zwindows").activateSdk(b, b.dependency("zwindows", .{}));
181+
b.default_step.dependOn(activate_zwindows);
182+
183+
inline for (comptime std.meta.declarations(samples.windows_linux_cross)) |d| {
184+
_ = buildExe(b, options, @field(samples.windows_linux_cross, d.name));
209185
}
210186

211-
b.getInstallStep().dependOn(build_web_app_step);
212-
213-
const html_filename = std.fmt.allocPrint(
214-
b.allocator,
215-
"{s}.html",
216-
.{d.name},
217-
) catch unreachable;
218-
219-
const emrun_step = @import("zemscripten").emrunStep(
220-
b,
221-
b.getInstallPath(.{ .custom = "web" }, html_filename),
222-
&.{},
223-
);
224-
emrun_step.dependOn(build_web_app_step);
225-
226-
b.step(
227-
d.name,
228-
"Build '" ++ d.name ++ "' sample as a web app",
229-
).dependOn(build_web_app_step);
230-
231-
b.step(
232-
d.name ++ "-emrun",
233-
"Build '" ++ d.name ++ "' sample as a web app and serve locally using `emrun`",
234-
).dependOn(emrun_step);
235-
}
187+
if (builtin.target.os.tag == .windows) {
188+
// TODO: Try to upgrade these to windows_linux_cross
189+
inline for (comptime std.meta.declarations(samples.windows_only)) |d| {
190+
_ = buildExe(b, options, @field(samples.windows_only, d.name));
191+
}
192+
}
193+
} else @panic("Unsupported host OS for Windows target");
236194
}
237-
};
195+
196+
inline for (comptime std.meta.declarations(samples.crossplatform)) |d| {
197+
_ = buildExe(b, options, @field(samples.crossplatform, d.name));
198+
}
199+
200+
// Install prebuilt SDL2 libs in bin output dir
201+
if (@import("zsdl").prebuilt_sdl2.install(b, options.target.result, .bin, .{
202+
.ttf = true,
203+
.image = true,
204+
})) |install_sdl2_step| {
205+
b.getInstallStep().dependOn(install_sdl2_step);
206+
}
207+
208+
buildBenchmarks(b, options.target);
209+
210+
// Experiments
211+
if (b.option(bool, "experiments", "Build our prototypes and experimental programs") orelse false) {
212+
@import("experiments/build.zig").build(b, options);
213+
}
214+
}
215+
216+
fn buildBenchmarks(b: *std.Build, target: std.Build.ResolvedTarget) void {
217+
const benchmark_step = b.step("benchmark", "Run all benchmarks");
218+
const zmath = b.dependency("zmath", .{
219+
.target = target,
220+
.optimize = .ReleaseFast,
221+
});
222+
benchmark_step.dependOn(&b.addRunArtifact(zmath.artifact("zmath-benchmarks")).step);
223+
}
238224

239225
fn buildExe(b: *std.Build, options: anytype, sample: anytype) *std.Build.Step.Compile {
240226
const exe = sample.build(b, options);
@@ -323,14 +309,16 @@ fn ensureGitLfs(allocator: std.mem.Allocator, cmd: []const u8) !void {
323309
}
324310
}
325311

326-
fn checkGitLfsContent() bool {
312+
fn checkGitLfsContent() !void {
327313
const expected_contents =
328314
\\DO NOT EDIT OR DELETE
329315
\\This file is used to check if Git LFS content has been downloaded
330316
;
331317
var buf: [expected_contents.len]u8 = undefined;
332318
_ = std.fs.cwd().readFile(".lfs-content-token", &buf) catch {
333-
return false;
319+
return error.GitLfsContentTokenNotFound;
334320
};
335-
return std.mem.eql(u8, expected_contents, &buf);
321+
if (!std.mem.eql(u8, expected_contents, &buf)) {
322+
return error.GitLfsContentCheckFailed;
323+
}
336324
}

0 commit comments

Comments
 (0)