forked from hexops/mach-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_examples.zig
277 lines (255 loc) · 11.4 KB
/
build_examples.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
const std = @import("std");
const core = @import("build.zig");
// TODO(sysgpu): re-enable, see https://github.com/hexops/mach/issues/1144
// const sysgpu = @import("mach_sysgpu");
pub fn build(
b: *std.Build,
optimize: std.builtin.OptimizeMode,
target: std.Build.ResolvedTarget,
mach_core_mod: *std.Build.Module,
) !void {
try ensureDependencies(b.allocator);
const Dependency = enum {
zigimg,
model3d,
assets,
pub fn dependency(
dep: @This(),
b2: *std.Build,
target2: std.Build.ResolvedTarget,
optimize2: std.builtin.OptimizeMode,
) std.Build.Module.Import {
const path = switch (dep) {
.zigimg => "examples/libs/zigimg/zigimg.zig",
.assets => return std.Build.Module.Import{
.name = "assets",
.module = b2.dependency("mach_core_example_assets", .{
.target = target2,
.optimize = optimize2,
}).module("mach-core-example-assets"),
},
.model3d => return std.Build.Module.Import{
.name = "model3d",
.module = b2.dependency("mach_model3d", .{
.target = target2,
.optimize = optimize2,
}).module("mach-model3d"),
},
};
return std.Build.Module.Import{
.name = @tagName(dep),
.module = b2.createModule(.{ .root_source_file = .{ .path = path } }),
};
}
};
inline for ([_]struct {
name: []const u8,
deps: []const Dependency = &.{},
std_platform_only: bool = false,
sysgpu: bool = false,
}{
.{ .name = "wasm-test" },
.{ .name = "triangle" },
.{ .name = "triangle-msaa" },
.{ .name = "clear-color" },
.{ .name = "procedural-primitives" },
.{ .name = "boids" },
.{ .name = "rotating-cube" },
.{ .name = "pixel-post-process" },
.{ .name = "two-cubes" },
.{ .name = "instanced-cube" },
.{ .name = "gen-texture-light" },
.{ .name = "fractal-cube" },
.{ .name = "map-async" },
.{ .name = "rgb-quad" },
.{
.name = "pbr-basic",
.deps = &.{ .model3d, .assets },
.std_platform_only = true,
},
.{
.name = "deferred-rendering",
.deps = &.{ .model3d, .assets },
.std_platform_only = true,
},
.{ .name = "textured-cube", .deps = &.{ .zigimg, .assets } },
.{ .name = "textured-quad", .deps = &.{ .zigimg, .assets } },
.{ .name = "sprite2d", .deps = &.{ .zigimg, .assets } },
.{ .name = "image", .deps = &.{ .zigimg, .assets } },
.{ .name = "image-blur", .deps = &.{ .zigimg, .assets } },
.{ .name = "cubemap", .deps = &.{ .zigimg, .assets } },
// // sysgpu
// .{ .name = "boids", .sysgpu = true },
// .{ .name = "clear-color", .sysgpu = true },
// .{ .name = "cubemap", .deps = &.{ .zigimg, .assets }, .sysgpu = true },
// .{ .name = "deferred-rendering", .deps = &.{ .model3d, .assets }, .std_platform_only = true, .sysgpu = true },
// .{ .name = "fractal-cube", .sysgpu = true },
// .{ .name = "gen-texture-light", .sysgpu = true },
// .{ .name = "image-blur", .deps = &.{ .zigimg, .assets }, .sysgpu = true },
// .{ .name = "instanced-cube", .sysgpu = true },
// .{ .name = "map-async", .sysgpu = true },
// .{ .name = "pbr-basic", .deps = &.{ .model3d, .assets }, .std_platform_only = true, .sysgpu = true },
// .{ .name = "pixel-post-process", .sysgpu = true },
// .{ .name = "procedural-primitives", .sysgpu = true },
// .{ .name = "rotating-cube", .sysgpu = true },
// .{ .name = "sprite2d", .deps = &.{ .zigimg, .assets }, .sysgpu = true },
// .{ .name = "image", .deps = &.{ .zigimg, .assets }, .sysgpu = true },
// .{ .name = "textured-cube", .deps = &.{ .zigimg, .assets }, .sysgpu = true },
// .{ .name = "textured-quad", .deps = &.{ .zigimg, .assets }, .sysgpu = true },
// .{ .name = "triangle", .sysgpu = true },
// .{ .name = "triangle-msaa", .sysgpu = true },
// .{ .name = "two-cubes", .sysgpu = true },
// .{ .name = "rgb-quad", .sysgpu = true },
}) |example| {
// FIXME: this is workaround for a problem that some examples
// (having the std_platform_only=true field) as well as zigimg
// uses IO and depends on gpu-dawn which is not supported
// in freestanding environments. So break out of this loop
// as soon as any such examples is found. This does means that any
// example which works on wasm should be placed before those who dont.
if (example.std_platform_only)
if (target.result.cpu.arch == .wasm32)
break;
var deps = std.ArrayList(std.Build.Module.Import).init(b.allocator);
try deps.append(std.Build.Module.Import{
.name = "zmath",
.module = b.createModule(.{
.root_source_file = .{ .path = "examples/zmath.zig" },
}),
});
for (example.deps) |d| try deps.append(d.dependency(b, target, optimize));
const cmd_name = if (example.sysgpu) "sysgpu-" ++ example.name else example.name;
const app = try core.App.init(
b,
b,
.{
.name = cmd_name,
.src = if (example.sysgpu)
"examples/sysgpu/" ++ example.name ++ "/main.zig"
else
"examples/" ++ example.name ++ "/main.zig",
.target = target,
.optimize = optimize,
.deps = deps.items,
.watch_paths = if (example.sysgpu)
&.{"examples/sysgpu/" ++ example.name}
else
&.{"examples/" ++ example.name},
.mach_core_mod = mach_core_mod,
},
);
for (example.deps) |dep| switch (dep) {
.model3d => app.compile.linkLibrary(b.dependency("mach_model3d", .{
.target = target,
.optimize = optimize,
}).artifact("mach-model3d")),
else => {},
};
// TODO(sysgpu): re-enable, see https://github.com/hexops/mach/issues/1144
// if (example.sysgpu) {
// const sysgpu_dep = b.dependency("mach_sysgpu", .{
// .target = target,
// .optimize = optimize,
// });
// app.compile.linkLibrary(sysgpu_dep.artifact("mach-sysgpu"));
// @import("mach_sysgpu").addPaths(app.compile);
// }
const install_step = b.step(cmd_name, "Install " ++ cmd_name);
install_step.dependOn(&app.install.step);
b.getInstallStep().dependOn(install_step);
const run_step = b.step("run-" ++ cmd_name, "Run " ++ cmd_name);
run_step.dependOn(&app.run.step);
}
}
pub fn copyFile(src_path: []const u8, dst_path: []const u8) void {
std.fs.cwd().makePath(std.fs.path.dirname(dst_path).?) catch unreachable;
std.fs.cwd().copyFile(src_path, std.fs.cwd(), dst_path, .{}) catch unreachable;
}
fn sdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
fn ensureDependencies(allocator: std.mem.Allocator) !void {
try optional_dependency.ensureGitRepoCloned(
allocator,
"https://github.com/slimsag/zigimg",
"ad6ad042662856f55a4d67499f1c4606c9951031",
sdkPath("/examples/libs/zigimg"),
);
}
const optional_dependency = struct {
fn ensureGitRepoCloned(allocator: std.mem.Allocator, clone_url: []const u8, revision: []const u8, dir: []const u8) !void {
if (xIsEnvVarTruthy(allocator, "NO_ENSURE_SUBMODULES") or xIsEnvVarTruthy(allocator, "NO_ENSURE_GIT")) {
return;
}
xEnsureGit(allocator);
if (std.fs.openDirAbsolute(dir, .{})) |_| {
const current_revision = try xGetCurrentGitRevision(allocator, dir);
if (!std.mem.eql(u8, current_revision, revision)) {
// Reset to the desired revision
xExec(allocator, &[_][]const u8{ "git", "fetch" }, dir) catch |err| std.debug.print("warning: failed to 'git fetch' in {s}: {s}\n", .{ dir, @errorName(err) });
try xExec(allocator, &[_][]const u8{ "git", "checkout", "--quiet", "--force", revision }, dir);
try xExec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir);
}
return;
} else |err| return switch (err) {
error.FileNotFound => {
std.log.info("cloning required dependency..\ngit clone {s} {s}..\n", .{ clone_url, dir });
try xExec(allocator, &[_][]const u8{ "git", "clone", "-c", "core.longpaths=true", clone_url, dir }, ".");
try xExec(allocator, &[_][]const u8{ "git", "checkout", "--quiet", "--force", revision }, dir);
try xExec(allocator, &[_][]const u8{ "git", "submodule", "update", "--init", "--recursive" }, dir);
return;
},
else => err,
};
}
fn xExec(allocator: std.mem.Allocator, argv: []const []const u8, cwd: []const u8) !void {
var child = std.ChildProcess.init(argv, allocator);
child.cwd = cwd;
_ = try child.spawnAndWait();
}
fn xGetCurrentGitRevision(allocator: std.mem.Allocator, cwd: []const u8) ![]const u8 {
const result = try std.ChildProcess.run(.{ .allocator = allocator, .argv = &.{ "git", "rev-parse", "HEAD" }, .cwd = cwd });
allocator.free(result.stderr);
if (result.stdout.len > 0) return result.stdout[0 .. result.stdout.len - 1]; // trim newline
return result.stdout;
}
fn xEnsureGit(allocator: std.mem.Allocator) void {
const argv = &[_][]const u8{ "git", "--version" };
const result = std.ChildProcess.run(.{
.allocator = allocator,
.argv = argv,
.cwd = ".",
}) catch { // e.g. FileNotFound
std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{});
std.process.exit(1);
};
defer {
allocator.free(result.stderr);
allocator.free(result.stdout);
}
if (result.term.Exited != 0) {
std.log.err("mach: error: 'git --version' failed. Is git not installed?", .{});
std.process.exit(1);
}
}
fn xIsEnvVarTruthy(allocator: std.mem.Allocator, name: []const u8) bool {
if (std.process.getEnvVarOwned(allocator, name)) |truthy| {
defer allocator.free(truthy);
if (std.mem.eql(u8, truthy, "true")) return true;
return false;
} else |_| {
return false;
}
}
fn xSdkPath(comptime suffix: []const u8) []const u8 {
if (suffix[0] != '/') @compileError("suffix must be an absolute path");
return comptime blk: {
const root_dir = std.fs.path.dirname(@src().file) orelse ".";
break :blk root_dir ++ suffix;
};
}
};