[build.zig]: a concrete approach to build for web
This commit is contained in:
parent
df70c5c952
commit
b44a355a81
7 changed files with 211 additions and 238 deletions
|
|
@ -20,57 +20,76 @@ const rlz = @import("raylib_zig");
|
|||
pub fn build(b: *std.Build) !void {
|
||||
const target = b.standardTargetOptions(.{});
|
||||
const optimize = b.standardOptimizeOption(.{});
|
||||
|
||||
|
||||
const raylib_dep = b.dependency("raylib_zig", .{
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
|
||||
const raylib = raylib_dep.module("raylib");
|
||||
const raylib_artifact = raylib_dep.artifact("raylib");
|
||||
|
||||
const exe_mod = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
.target = target,
|
||||
.optimize = optimize,
|
||||
});
|
||||
exe_mod.addImport("raylib", raylib);
|
||||
exe_mod.linkLibrary(raylib_artifact);
|
||||
|
||||
const run_step = b.step("run", "Run the app");
|
||||
|
||||
//web exports are completely separate
|
||||
if (target.query.os_tag == .emscripten) {
|
||||
const exe_lib = try rlz.emcc.compileForEmscripten(b, "'$PROJECT_NAME'", "src/main.zig", target, optimize);
|
||||
const emsdk = rlz.emsdk;
|
||||
const wasm = b.addLibrary(.{
|
||||
.name = "'$PROJECT_NAME'",
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
|
||||
exe_lib.linkLibrary(raylib_artifact);
|
||||
exe_lib.root_module.addImport("raylib", raylib);
|
||||
const install_dir: std.Build.InstallDir = .{ .custom = "web" };
|
||||
const emcc_flags = emsdk.emccDefaultFlags(b.allocator, .{ .optimize = optimize });
|
||||
const emcc_settings = emsdk.emccDefaultSettings(b.allocator, .{ .optimize = optimize });
|
||||
|
||||
// Note that raylib itself is not actually added to the exe_lib output file, so it also needs to be linked with emscripten.
|
||||
const link_step = try rlz.emcc.linkWithEmscripten(b, &[_]*std.Build.Step.Compile{ exe_lib, raylib_artifact });
|
||||
//this lets your program access files like "resources/my-image.png":
|
||||
link_step.addArg("--emrun");
|
||||
link_step.addArg("--embed-file");
|
||||
link_step.addArg("resources/");
|
||||
|
||||
b.getInstallStep().dependOn(&link_step.step);
|
||||
const run_step = try rlz.emcc.emscriptenRunStep(b);
|
||||
run_step.step.dependOn(&link_step.step);
|
||||
const run_option = b.step("run", "Run '$PROJECT_NAME'");
|
||||
run_option.dependOn(&run_step.step);
|
||||
return;
|
||||
}
|
||||
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "'$PROJECT_NAME'",
|
||||
.root_module = b.createModule(.{
|
||||
.root_source_file = b.path("src/main.zig"),
|
||||
const emcc_step = emsdk.emccStep(b, raylib_artifact, wasm, .{
|
||||
.optimize = optimize,
|
||||
.target = target
|
||||
}),
|
||||
});
|
||||
.flags = emcc_flags,
|
||||
.settings = emcc_settings,
|
||||
.shell_file_path = emsdk.shell(raylib_dep.builder),
|
||||
.embed_paths = &.{
|
||||
.{
|
||||
.src_path = b.pathJoin(&.{ module_subpath, "resources" }),
|
||||
.virtual_path = "resources",
|
||||
},
|
||||
},
|
||||
.install_dir = install_dir,
|
||||
});
|
||||
b.getInstallStep().dependOn(emcc_step);
|
||||
|
||||
exe.linkLibrary(raylib_artifact);
|
||||
exe.root_module.addImport("raylib", raylib);
|
||||
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),
|
||||
&.{},
|
||||
);
|
||||
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
const run_step = b.step("run", "Run '$PROJECT_NAME'");
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
emrun_step.dependOn(emcc_step);
|
||||
run_step.dependOn(emrun_step);
|
||||
} else {
|
||||
const exe = b.addExecutable(.{
|
||||
.name = "'$PROJECT_NAME'",
|
||||
.root_module = exe_mod,
|
||||
});
|
||||
b.installArtifact(exe);
|
||||
|
||||
b.installArtifact(exe);
|
||||
const run_cmd = b.addRunArtifact(exe);
|
||||
run_cmd.step.dependOn(b.getInstallStep());
|
||||
|
||||
run_step.dependOn(&run_cmd.step);
|
||||
}
|
||||
}' >> build.zig
|
||||
|
||||
zig fetch --save git+https://github.com/raylib-zig/raylib-zig#devel
|
||||
zig fetch --save git+https://github.com/Not-Nik/raylib-zig#devel
|
||||
zig fetch --save git+https://github.com/emscripten-core/emsdk#4.0.9
|
||||
|
||||
mkdir resources
|
||||
touch resources/placeholder.txt
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue