mach: build system SDK improvements for wasm

- Target is now passed as an option in App.init()
- Always install() in all case
- Add functions getInstallStep(), setBuildMode() and run()
This commit is contained in:
iddev5 2022-05-21 18:54:37 +05:30 committed by Stephen Gutekanst
parent 01f88df397
commit 7466f32b62

View file

@ -45,17 +45,15 @@ pub fn build(b: *std.build.Builder) void {
.{ .{
.name = "example-" ++ example.name, .name = "example-" ++ example.name,
.src = "examples/" ++ example.name ++ "/main.zig", .src = "examples/" ++ example.name ++ "/main.zig",
.target = target,
.deps = comptime example.packages ++ &[_]Pkg{ glfw.pkg, gpu.pkg, pkg }, .deps = comptime example.packages ++ &[_]Pkg{ glfw.pkg, gpu.pkg, pkg },
}, },
); );
const example_exe = example_app.step; example_app.setBuildMode(mode);
example_exe.setTarget(target);
example_exe.setBuildMode(mode);
example_app.link(options); example_app.link(options);
example_exe.install();
const example_run_cmd = example_exe.run(); const example_run_cmd = example_app.run();
example_run_cmd.step.dependOn(&example_exe.install_step.?.step); example_run_cmd.step.dependOn(&example_app.getInstallStep().?.step);
const example_run_step = b.step("run-example-" ++ example.name, "Run the example"); const example_run_step = b.step("run-example-" ++ example.name, "Run the example");
example_run_step.dependOn(&example_run_cmd.step); example_run_step.dependOn(&example_run_cmd.step);
} }
@ -65,17 +63,15 @@ pub fn build(b: *std.build.Builder) void {
.{ .{
.name = "shaderexp", .name = "shaderexp",
.src = "shaderexp/main.zig", .src = "shaderexp/main.zig",
.target = target,
.deps = &.{ glfw.pkg, gpu.pkg, pkg }, .deps = &.{ glfw.pkg, gpu.pkg, pkg },
}, },
); );
const shaderexp_exe = shaderexp_app.step; shaderexp_app.setBuildMode(mode);
shaderexp_exe.setTarget(target);
shaderexp_exe.setBuildMode(mode);
shaderexp_app.link(options); shaderexp_app.link(options);
shaderexp_exe.install();
const shaderexp_run_cmd = shaderexp_exe.run(); const shaderexp_run_cmd = shaderexp_app.run();
shaderexp_run_cmd.step.dependOn(&shaderexp_exe.install_step.?.step); shaderexp_run_cmd.step.dependOn(&shaderexp_app.getInstallStep().?.step);
const shaderexp_run_step = b.step("run-shaderexp", "Run shaderexp"); const shaderexp_run_step = b.step("run-shaderexp", "Run shaderexp");
shaderexp_run_step.dependOn(&shaderexp_run_cmd.step); shaderexp_run_step.dependOn(&shaderexp_run_cmd.step);
@ -106,26 +102,45 @@ const Packages = struct {
}; };
const App = struct { const App = struct {
step: *std.build.LibExeObjStep,
b: *std.build.Builder, b: *std.build.Builder,
name: []const u8,
step: *std.build.LibExeObjStep,
pub fn init(b: *std.build.Builder, options: struct { pub fn init(b: *std.build.Builder, options: struct {
name: []const u8, name: []const u8,
src: []const u8, src: []const u8,
target: std.zig.CrossTarget,
deps: ?[]const Pkg = null, deps: ?[]const Pkg = null,
}) App { }) App {
const exe = b.addExecutable(options.name, "src/native.zig"); const app_pkg = std.build.Pkg{
exe.addPackage(.{
.name = "app", .name = "app",
.path = .{ .path = options.src }, .path = .{ .path = options.src },
.dependencies = options.deps, .dependencies = options.deps,
}); };
exe.addPackage(gpu.pkg);
exe.addPackage(glfw.pkg); const step = blk: {
if (options.target.toTarget().cpu.arch == .wasm32) {
// TODO: use options.name
const lib = b.addSharedLibrary("application", "src/wasm.zig", .unversioned);
break :blk lib;
} else {
const exe = b.addExecutable(options.name, "src/native.zig");
exe.addPackage(gpu.pkg);
exe.addPackage(glfw.pkg);
break :blk exe;
}
};
step.addPackage(app_pkg);
step.setTarget(options.target);
step.install();
return .{ return .{
.b = b, .b = b,
.step = exe, .step = step,
.name = options.name,
}; };
} }
@ -138,6 +153,21 @@ const App = struct {
glfw.link(app.b, app.step, options.glfw_options); glfw.link(app.b, app.step, options.glfw_options);
gpu.link(app.b, app.step, gpu_options); gpu.link(app.b, app.step, gpu_options);
} }
pub fn setBuildMode(app: *const App, mode: std.builtin.Mode) void {
app.step.setBuildMode(mode);
}
pub fn getInstallStep(app: *const App) ?*std.build.InstallArtifactStep {
return app.step.install_step;
}
pub fn run(app: *const App) *std.build.RunStep {
if (app.step.target.toTarget().cpu.arch != .wasm32) {
return app.step.run();
}
unreachable;
}
}; };
pub const pkg = std.build.Pkg{ pub const pkg = std.build.Pkg{