From e7f7737cc8498555d2fdf545584c7641d79127a6 Mon Sep 17 00:00:00 2001 From: iddev5 Date: Fri, 27 May 2022 13:04:52 +0530 Subject: [PATCH] mach: build: Implement run step for wasm applications This actually compiles and runs a http server (based on apple_pie) and uses xdg-open (on unixes) and open (on rest) to launch the browser. The steps actually take place in reverse order because running a web server would block the current process (limitation of RunStep). Hence we are assuming that (xdg-)open is just a launcher and would take a while to open the browser application. --- build.zig | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/build.zig b/build.zig index 92326946..510d8775 100644 --- a/build.zig +++ b/build.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const builtin = @import("builtin"); pub const gpu = @import("gpu/build.zig"); const gpu_dawn = @import("gpu-dawn/build.zig"); pub const glfw = @import("glfw/build.zig"); @@ -64,12 +65,10 @@ pub fn build(b: *std.build.Builder) void { const example_compile_step = b.step("example-" ++ example.name, "Compile '" ++ example.name ++ "' example"); example_compile_step.dependOn(&example_app.getInstallStep().?.step); - if (target.toTarget().cpu.arch != .wasm32) { - const example_run_cmd = example_app.run(); - example_run_cmd.step.dependOn(&example_app.getInstallStep().?.step); - const example_run_step = b.step("run-example-" ++ example.name, "Run '" ++ example.name ++ "' example"); - example_run_step.dependOn(&example_run_cmd.step); - } + const example_run_cmd = example_app.run(); + example_run_cmd.step.dependOn(&example_app.getInstallStep().?.step); + const example_run_step = b.step("run-example-" ++ example.name, "Run '" ++ example.name ++ "' example"); + example_run_step.dependOn(&example_run_cmd.step); } if (target.toTarget().cpu.arch != .wasm32) { @@ -218,10 +217,32 @@ pub const App = struct { } pub fn run(app: *const App) *std.build.RunStep { - if (app.step.target.toTarget().cpu.arch != .wasm32) { + if (app.step.target.toTarget().cpu.arch == .wasm32) { + const http_server = app.b.addExecutable("http-server", "tools/http-server.zig"); + http_server.addPackage(.{ + .name = "apple_pie", + .path = .{ .path = "tools/libs/apple_pie/src/apple_pie.zig" }, + }); + + const launch = app.b.addSystemCommand(&.{ + switch (builtin.os.tag) { + .macos, .windows => "open", + else => "xdg-open", // Assume linux-like + }, + // TODO: use actual application name + "http://127.0.0.1:8000/application.html", + }); + launch.step.dependOn(&app.step.install_step.?.step); + + const serve = http_server.run(); + serve.addArg("application"); + serve.step.dependOn(&launch.step); + serve.cwd = app.b.getInstallPath(web_install_dir, ""); + + return serve; + } else { return app.step.run(); } - unreachable; } };