build: fix WASM builds

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2024-04-19 19:34:40 -07:00
parent 07d9855f73
commit 6a2358baf8

View file

@ -531,6 +531,8 @@ pub const CoreApp = struct {
// TODO(sysgpu): remove this once we switch to sysgpu fully // TODO(sysgpu): remove this once we switch to sysgpu fully
pub fn link(mach_builder: *std.Build, step: *std.Build.Step.Compile, mod: *std.Build.Module) void { pub fn link(mach_builder: *std.Build, step: *std.Build.Step.Compile, mod: *std.Build.Module) void {
const target = mod.resolved_target.?.result;
if (target.cpu.arch != .wasm32) {
const gpu_dawn = @import("mach_gpu_dawn"); const gpu_dawn = @import("mach_gpu_dawn");
const Options = struct { const Options = struct {
gpu_dawn_options: gpu_dawn.Options = .{}, gpu_dawn_options: gpu_dawn.Options = .{},
@ -549,6 +551,7 @@ pub fn link(mach_builder: *std.Build, step: *std.Build.Step.Compile, mod: *std.B
step.addCSourceFile(.{ .file = .{ .path = sdkPath("/src/gpu/mach_dawn.cpp") }, .flags = &.{"-std=c++17"} }); step.addCSourceFile(.{ .file = .{ .path = sdkPath("/src/gpu/mach_dawn.cpp") }, .flags = &.{"-std=c++17"} });
step.addIncludePath(.{ .path = sdkPath("/src/gpu") }); step.addIncludePath(.{ .path = sdkPath("/src/gpu") });
} }
}
fn linkSysgpu(b: *std.Build, module: *std.Build.Module) void { fn linkSysgpu(b: *std.Build, module: *std.Build.Module) void {
const resolved_target = module.resolved_target orelse b.host; const resolved_target = module.resolved_target orelse b.host;
@ -659,10 +662,10 @@ fn buildExamples(
} }
}; };
inline for ([_]struct { for ([_]struct {
name: []const u8, name: []const u8,
deps: []const Dependency = &.{}, deps: []const Dependency = &.{},
std_platform_only: bool = false, wasm: bool = false,
has_assets: bool = false, has_assets: bool = false,
}{ }{
.{ .name = "sysaudio", .deps = &.{} }, .{ .name = "sysaudio", .deps = &.{} },
@ -681,19 +684,10 @@ fn buildExamples(
.deps = &.{ .freetype, .assets }, .deps = &.{ .freetype, .assets },
}, },
}) |example| { }) |example| {
// FIXME: this is workaround for a problem that some examples if (target.result.cpu.arch == .wasm32 and !example.wasm) continue;
// (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;
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = example.name, .name = example.name,
.root_source_file = .{ .path = "examples/" ++ example.name ++ "/main.zig" }, .root_source_file = .{ .path = b.fmt("examples/{s}/main.zig", .{example.name}) },
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
@ -707,14 +701,14 @@ fn buildExamples(
exe.root_module.addImport(dep.name, dep.module); exe.root_module.addImport(dep.name, dep.module);
} }
const compile_step = b.step(example.name, "Compile " ++ example.name); const compile_step = b.step(example.name, b.fmt("Compile {s}", .{example.name}));
compile_step.dependOn(b.getInstallStep()); compile_step.dependOn(b.getInstallStep());
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep()); run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args); if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step("run-" ++ example.name, "Run " ++ example.name); const run_step = b.step(b.fmt("run-{s}", .{example.name}), b.fmt("Run {s}", .{example.name}));
run_step.dependOn(&run_cmd.step); run_step.dependOn(&run_cmd.step);
} }
} }