Skip to content
31 changes: 15 additions & 16 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,18 @@ pub const emsdk = struct {
}

pub fn emccStep(b: *std.Build, raylib: *std.Build.Step.Compile, wasm: *std.Build.Step.Compile, options: zemscripten.StepOptions) *std.Build.Step {
const activate_emsdk_step = zemscripten.activateEmsdkStep(b);

const emsdk_dep = b.dependency("emsdk", .{});
raylib.root_module.addIncludePath(emsdk_dep.path("upstream/emscripten/cache/sysroot/include"));
wasm.root_module.addIncludePath(emsdk_dep.path("upstream/emscripten/cache/sysroot/include"));

const emcc_step = zemscripten.emccStep(b, wasm, options);
emcc_step.dependOn(activate_emsdk_step);
const emcc_step = zemscripten.emccStep(b, &.{}, &.{wasm}, options);

return emcc_step;
}

pub fn emrunStep(
b: *std.Build,
html_path: []const u8,
html_path: std.Build.LazyPath,
extra_args: []const []const u8,
) *std.Build.Step {
return zemscripten.emrunStep(b, html_path, extra_args);
Expand All @@ -92,7 +89,7 @@ pub fn linkWindows(mod: *std.Build.Module, opengl: bool, comptime shcore: bool)
}

fn findWaylandScanner(b: *std.Build) void {
_ = b.findProgram(&.{"wayland-scanner"}, &.{}) catch {
_ = b.findProgram(.{ .names = &.{"wayland-scanner"} }) orelse {
std.log.err(
\\ `wayland-scanner` may not be installed on the system.
\\ You can switch to X11 in your `build.zig` by changing `Options.linux_display_backend`
Expand Down Expand Up @@ -648,7 +645,7 @@ fn addExamples(
const all = b.step(module, "All " ++ module ++ " examples");
const module_subpath = b.pathJoin(&.{ "examples", module });

var dir = try b.build_root.handle.openDir(b.graph.io, module_subpath, .{ .iterate = true });
var dir = try b.root.openDir(b.graph.io, module_subpath, .{ .iterate = true });
defer dir.close(b.graph.io);

var iter = dir.iterate();
Expand Down Expand Up @@ -698,17 +695,18 @@ fn addExamples(
exe_mod.addCMacro("PLATFORM_WEB", "");

const wasm = b.addLibrary(.{
.name = filename,
.name = b.fmt("{s}.html", .{filename}),
.root_module = exe_mod,
});

const install_dir: std.Build.InstallDir = .{ .custom = b.fmt("web/{s}/{s}", .{ module, filename }) };
const emcc_flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize });
const emcc_settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize });

const EmccExamplesPreloadMap = std.static_string_map.StaticStringMap([]const emsdk.zemscripten.EmccFilePath);
const EmccExamplesPreloadKV = struct { []const u8, []const emsdk.zemscripten.EmccFilePath };
const emcc_examples_preloads: []const EmccExamplesPreloadKV = @import("examples/example_resources.zon");
const EmccExamplesPreloadMap = std.static_string_map.StaticStringMap([]const emsdk.zemscripten.ResourceFile);
const EmccExamplesPreloadKV = struct { []const u8, []const emsdk.zemscripten.ResourceFile };
const emcc_examples_preloads_ = @import("examples/example_resources.zon");
const emcc_examples_preloads: []const EmccExamplesPreloadKV = @ptrCast(@alignCast(&emcc_examples_preloads_));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Besides the preload_paths bug on zemscripten this part is not working, the new src_path is a std.Build.LazyPath, this cast is not filling it up. Here is what I did:

            const ResourceDesc = struct {
                src_path: []const u8,
                virtual_path: []const u8,
            };

            const EmccExamplesPreloadMap = std.StaticStringMap([]const ResourceDesc);
            const EmccExamplesPreloadKV = struct { []const u8, []const ResourceDesc };
            const emcc_examples_preloads: []const EmccExamplesPreloadKV = @import("examples/example_resources.zon");
            const emcc_examples_preloads_map = EmccExamplesPreloadMap.initComptime(emcc_examples_preloads);

            const preload_paths_: []const ResourceDesc = emcc_examples_preloads_map.get(filename) orelse &.{};

            var preload_paths = std.ArrayList(emsdk.zemscripten.ResourceFile).empty;
            defer preload_paths.deinit(b.allocator);

            for (preload_paths_) |preload_path| {
                try preload_paths.append(b.allocator, .{
                    .src_path = b.path(preload_path.src_path),
                    .virtual_path = preload_path.virtual_path,
                });
            }

            const emcc_step = emsdk.emccStep(b, raylib, wasm, .{
                .optimize = optimize,
                .flags = emcc_flags,
                .settings = emcc_settings,
                .preload_paths = preload_paths.items,
                .shell_file_path = b.path("src/shell.html"),
                .install_dir = install_dir,
                .out_file_name = wasm.name,
            });

also there is a bug with preload_paths not using the ResourceFile.get() they added.

I tried to fix it using your proposed solution emcc.addArg(path.get(b)) but turns out the whole ResourceFile.get() is wrong, as it was never used looks like zig never bothered to validate this function, so the self.src_path.path(b, "") it is using just returns another LazyPath instead of a string, but the function needs a string. This is from a commit after the one locked in the master raylib so that is why it works now in raylib, after they changed to that ResourceFile things didn't work anymore, but they didn't realize 🙄.

This hole is deeper than we thought.

const emcc_examples_preloads_map = EmccExamplesPreloadMap.initComptime(emcc_examples_preloads);

const emcc_step = emsdk.emccStep(b, raylib, wasm, .{
Expand All @@ -718,13 +716,15 @@ fn addExamples(
.preload_paths = emcc_examples_preloads_map.get(filename) orelse &.{},
.shell_file_path = b.path("src/shell.html"),
.install_dir = install_dir,
.out_file_name = wasm.name,
});
b.getInstallStep().dependOn(emcc_step);

const html_filename = try std.fmt.allocPrint(b.allocator, "{s}.html", .{wasm.name});
const emrun_step = emsdk.emrunStep(
b,
b.getInstallPath(install_dir, html_filename),
b.graph.path(.install_prefix, b.fmt(
"web/{s}/{s}/{s}",
.{ module, filename, wasm.name },
)),
&.{},
);

Expand Down Expand Up @@ -759,7 +759,7 @@ fn waylandGenerate(
comptime waylandDir: []const u8,
comptime source: bool,
) !void {
const dir = try b.build_root.handle.openDir(b.graph.io, waylandDir, .{ .iterate = true });
const dir = try b.root.openDir(b.graph.io, waylandDir, .{ .iterate = true });
defer dir.close(b.graph.io);

var iter = dir.iterate();
Expand Down Expand Up @@ -793,4 +793,3 @@ fn waylandGenerate(
}
}
}

6 changes: 3 additions & 3 deletions build.zig.zon
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.{
.name = .raylib,
.version = "6.0.0",
.minimum_zig_version = "0.16.0",
.minimum_zig_version = "0.17.0-dev.1426+58a94eaae",

.fingerprint = 0x13035e5cb8bc1ac2, // Changing this has security and trust implications.

Expand All @@ -21,8 +21,8 @@
.hash = "N-V-__8AAJl1DwBezhYo_VE6f53mPVm00R-Fk28NPW7P14EQ",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm seeing a hash mismatch for this package with the patch on 0.17.0-dev.1158+1d1193aa7, where it fetches N-V-__8AALBDi159DH5nlv03KX8rG4GFfY8m-GWnFC9DSQL2. Either I'm being MITMed or the hash actually changed (through Zig or emsdk itself). Could someone verify (on a clean build/system).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ran it on a clean build, cleaned all caches, with both the most recent zig version available at the moment 0.17.0-dev.1417+20befa4e6 as well the minimum version set in this PR 0.17.0-dev.1158+1d1193aa7 and I did not get any hash error, the hash downloaded is the N-V-__8AAJl1DwBezhYo_VE6f53mPVm00R-Fk28NPW7P14EQ.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cleaned up all my cached files as well, and the mismatch is gone. Strange it occurred at all though..

},
.zemscripten = .{
.url = "git+https://github.com/zig-gamedev/zemscripten#3fa4b778852226c7346bdcc3c1486e875a9a6d02",
.hash = "zemscripten-0.2.0-dev-sRlDqApRAACspTbAZnuNKWIzfWzSYgYkb2nWAXZ-tqqt",
.url = "git+https://github.com/Yinameah/zemscripten#06d49243789d638ba10d4af004c46df4da83579c",
.hash = "zemscripten-0.2.0-dev-sRlDqKFRAAAXl8aICu541O6PiY70v4HIr3FTmCg--WqM",
},
},

Expand Down