diff --git a/build.zig b/build.zig index ee817f1..2da52bd 100755 --- a/build.zig +++ b/build.zig @@ -214,6 +214,7 @@ pub fn emscriptenRunStep(b: *std.Build) !*std.Build.Step.Run { } pub fn buildForEmscripten(b: *std.Build, name: []const u8, root_source_file: []const u8, target: std.zig.CrossTarget, optimize: std.builtin.Mode, raylib: *std.Build.Module, raylib_math: *std.Build.Module) !*std.Build.Step.Run { + const new_target = updateTargetForWeb(target); // Emscripten is completely unable to find Zig's entry point. // Solution: create a C compatible entry point in zig, // and then a C file that calls that entry point in the main method. @@ -240,14 +241,15 @@ pub fn buildForEmscripten(b: *std.Build, name: []const u8, root_source_file: []c const webhack_c_file = webhack_c_file_step.add("webhack.c", webhack_c); //the project is built as a library and linked with everything later - const exe_lib = b.addStaticLibrary(.{ .name = name, .root_source_file = .{ .path = zig_entrypoint_file }, .target = target, .optimize = optimize }); + const exe_lib = b.addStaticLibrary(.{ .name = name, .root_source_file = .{ .path = zig_entrypoint_file }, .target = new_target, .optimize = optimize }); exe_lib.addCSourceFile(.{ .file = webhack_c_file, .flags = &[_][]const u8{} }); + const raylib_artifact = getArtifact(b, target, optimize); + exe_lib.linkLibrary(raylib_artifact); exe_lib.addModule("raylib", raylib); exe_lib.addModule("raylib-math", raylib_math); - //link with libc since raylib isn't going to be linked in until later - exe_lib.linkLibC(); exe_lib.step.dependOn(&write_zig_entrypoint.step); exe_lib.step.dependOn(&webhack_c_file_step.step); + exe_lib.step.dependOn(&raylib_artifact.step); //Emcc has to be the one that links the project, in order to make sure that the web libraries are correctly linked. if (b.sysroot == null) { @panic("Pass '--sysroot \"[path to emsdk installation]/upstream/emscripten\"'"); @@ -262,7 +264,6 @@ pub fn buildForEmscripten(b: *std.Build, name: []const u8, root_source_file: []c emcc_run_arg = try std.fmt.bufPrint(emcc_run_arg, "{s}" ++ std.fs.path.sep_str ++ "{s}", .{ b.sysroot.?, emccExe }); - const raylib_artifact = getArtifact(b, target, optimize); //create the output directory because emcc can't do it. const mkdir_command = b.addSystemCommand(&[_][]const u8{ "mkdir", "-p", "zig-out" ++ std.fs.path.sep_str ++ "htmlout" ++ std.fs.path.sep_str }); //Actually link everything together @@ -289,6 +290,26 @@ fn lastIndexOf(string: []const u8, character: u8) usize { } return string.len - 1; } +// TODO: each zig update, remove this and see if everything still works. +// TODO: submit an issue to zig's repo to fix the problem that this works around +fn updateTargetForWeb(target: std.zig.CrossTarget) std.zig.CrossTarget { + //zig building to emscripten doesn't really work, + // as the standard library doesn't compile for some reason + // So build to wasi instead. + return std.zig.CrossTarget{ + .cpu_arch = target.cpu_arch, + .cpu_model = target.cpu_model, + .cpu_features_add = target.cpu_features_add, + .cpu_features_sub = target.cpu_features_sub, + .os_tag = .wasi, + .os_version_min = target.os_version_min, + .os_version_max = target.os_version_max, + .glibc_version = target.glibc_version, + .abi = target.abi, + .dynamic_linker = target.dynamic_linker, + .ofmt = target.ofmt, + }; +} pub fn webExport(b: *std.Build, root_source_file: []const u8, comptime rl_path: []const u8, optimize: std.builtin.OptimizeMode) !*std.Build.Step { // EXPORTING to WEB, the only reasonable output is emscripten ReleaseSafe. diff --git a/project_setup.sh b/project_setup.sh index fc78832..443a50e 100755 --- a/project_setup.sh +++ b/project_setup.sh @@ -14,29 +14,26 @@ const rl = @import("raylib-zig/build.zig"); pub fn build(b: *std.Build) !void { const target = b.standardTargetOptions(.{}); - const web_export = target.getOsTag() == .emscripten; const optimize = b.standardOptimizeOption(.{}); - if (!web_export) { - var raylib = rl.getModule(b, "raylib-zig"); - var raylib_math = rl.math.getModule(b, "raylib-zig"); - - const exe = b.addExecutable(.{ .name = "'$PROJECT_NAME'", .root_source_file = .{ .path = "src/main.zig" }, .optimize = optimize, .target = target }); - - rl.link(b, exe, target, optimize); - exe.addModule("raylib", raylib); - exe.addModule("raylib-math", raylib_math); - - const run_cmd = b.addRunArtifact(exe); - const run_step = b.step("run", "Run '$PROJECT_NAME'"); - run_step.dependOn(&run_cmd.step); - - b.installArtifact(exe); - } - //web exports are completely separate, due to the amount of hackery required. - if (web_export) { + if (target.getOsTag()) { b.getInstallStep().dependOn(try rl.webExport(b, "src/main.zig", "raylib-zig", optimize)); + return; } + var raylib = rl.getModule(b, "raylib-zig"); + var raylib_math = rl.math.getModule(b, "raylib-zig"); + + const exe = b.addExecutable(.{ .name = "'$PROJECT_NAME'", .root_source_file = .{ .path = "src/main.zig" }, .optimize = optimize, .target = target }); + + rl.link(b, exe, target, optimize); + exe.addModule("raylib", raylib); + exe.addModule("raylib-math", raylib_math); + + const run_cmd = b.addRunArtifact(exe); + const run_step = b.step("run", "Run '$PROJECT_NAME'"); + run_step.dependOn(&run_cmd.step); + + b.installArtifact(exe); } ' >> build.zig