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.
This commit is contained in:
parent
84af4e118c
commit
e7f7737cc8
1 changed files with 29 additions and 8 deletions
37
build.zig
37
build.zig
|
|
@ -1,4 +1,5 @@
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
|
const builtin = @import("builtin");
|
||||||
pub const gpu = @import("gpu/build.zig");
|
pub const gpu = @import("gpu/build.zig");
|
||||||
const gpu_dawn = @import("gpu-dawn/build.zig");
|
const gpu_dawn = @import("gpu-dawn/build.zig");
|
||||||
pub const glfw = @import("glfw/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");
|
const example_compile_step = b.step("example-" ++ example.name, "Compile '" ++ example.name ++ "' example");
|
||||||
example_compile_step.dependOn(&example_app.getInstallStep().?.step);
|
example_compile_step.dependOn(&example_app.getInstallStep().?.step);
|
||||||
|
|
||||||
if (target.toTarget().cpu.arch != .wasm32) {
|
const example_run_cmd = example_app.run();
|
||||||
const example_run_cmd = example_app.run();
|
example_run_cmd.step.dependOn(&example_app.getInstallStep().?.step);
|
||||||
example_run_cmd.step.dependOn(&example_app.getInstallStep().?.step);
|
const example_run_step = b.step("run-example-" ++ example.name, "Run '" ++ example.name ++ "' example");
|
||||||
const example_run_step = b.step("run-example-" ++ example.name, "Run '" ++ example.name ++ "' example");
|
example_run_step.dependOn(&example_run_cmd.step);
|
||||||
example_run_step.dependOn(&example_run_cmd.step);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (target.toTarget().cpu.arch != .wasm32) {
|
if (target.toTarget().cpu.arch != .wasm32) {
|
||||||
|
|
@ -218,10 +217,32 @@ pub const App = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run(app: *const App) *std.build.RunStep {
|
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();
|
return app.step.run();
|
||||||
}
|
}
|
||||||
unreachable;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue