add updateTargetForWeb back

This commit is contained in:
Blue 2023-08-11 11:36:44 -06:00
parent 7120d454fd
commit 7c296e8667
2 changed files with 41 additions and 23 deletions

View File

@ -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 { 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. // Emscripten is completely unable to find Zig's entry point.
// Solution: create a C compatible entry point in zig, // Solution: create a C compatible entry point in zig,
// and then a C file that calls that entry point in the main method. // 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); 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 //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{} }); 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", raylib);
exe_lib.addModule("raylib-math", raylib_math); 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(&write_zig_entrypoint.step);
exe_lib.step.dependOn(&webhack_c_file_step.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. //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) { if (b.sysroot == null) {
@panic("Pass '--sysroot \"[path to emsdk installation]/upstream/emscripten\"'"); @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 }); 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. //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 }); 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 //Actually link everything together
@ -289,6 +290,26 @@ fn lastIndexOf(string: []const u8, character: u8) usize {
} }
return string.len - 1; 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 { 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. // EXPORTING to WEB, the only reasonable output is emscripten ReleaseSafe.

View File

@ -14,29 +14,26 @@ const rl = @import("raylib-zig/build.zig");
pub fn build(b: *std.Build) !void { pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const web_export = target.getOsTag() == .emscripten;
const optimize = b.standardOptimizeOption(.{}); 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. //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)); 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 ' >> build.zig