mach: build: Add platform field in App to simplify target checking
Also now add js-runtime package to app for web platform.
This commit is contained in:
parent
e712969a87
commit
7b27544cce
1 changed files with 28 additions and 10 deletions
38
build.zig
38
build.zig
|
|
@ -138,8 +138,19 @@ pub const App = struct {
|
||||||
b: *std.build.Builder,
|
b: *std.build.Builder,
|
||||||
name: []const u8,
|
name: []const u8,
|
||||||
step: *std.build.LibExeObjStep,
|
step: *std.build.LibExeObjStep,
|
||||||
|
platform: Platform,
|
||||||
res_dirs: ?[]const []const u8,
|
res_dirs: ?[]const []const u8,
|
||||||
|
|
||||||
|
pub const Platform = enum {
|
||||||
|
native,
|
||||||
|
web,
|
||||||
|
|
||||||
|
pub fn fromTarget(target: std.Target) Platform {
|
||||||
|
if (target.cpu.arch == .wasm32) return .web;
|
||||||
|
return .native;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
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,
|
||||||
|
|
@ -147,20 +158,26 @@ pub const App = struct {
|
||||||
deps: ?[]const Pkg = null,
|
deps: ?[]const Pkg = null,
|
||||||
res_dirs: ?[]const []const u8 = null,
|
res_dirs: ?[]const []const u8 = null,
|
||||||
}) App {
|
}) App {
|
||||||
const mach_deps: []const Pkg = &.{ glfw.pkg, gpu.pkg, pkg };
|
const target = (std.zig.system.NativeTargetInfo.detect(b.allocator, options.target) catch unreachable).target;
|
||||||
const deps = if (options.deps) |app_deps|
|
const platform = Platform.fromTarget(target);
|
||||||
std.mem.concat(b.allocator, Pkg, &.{ mach_deps, app_deps }) catch unreachable
|
|
||||||
else
|
var deps = std.ArrayList(std.build.Pkg).init(b.allocator);
|
||||||
mach_deps;
|
deps.append(pkg) catch unreachable;
|
||||||
|
deps.append(gpu.pkg) catch unreachable;
|
||||||
|
switch (platform) {
|
||||||
|
.native => deps.append(glfw.pkg) catch unreachable,
|
||||||
|
.web => deps.append(js_runtime.pkg) catch unreachable,
|
||||||
|
}
|
||||||
|
if (options.deps) |app_deps| deps.appendSlice(app_deps) catch unreachable;
|
||||||
|
|
||||||
const app_pkg = std.build.Pkg{
|
const app_pkg = std.build.Pkg{
|
||||||
.name = "app",
|
.name = "app",
|
||||||
.source = .{ .path = options.src },
|
.source = .{ .path = options.src },
|
||||||
.dependencies = deps,
|
.dependencies = deps.toOwnedSlice(),
|
||||||
};
|
};
|
||||||
|
|
||||||
const step = blk: {
|
const step = blk: {
|
||||||
if (options.target.toTarget().cpu.arch == .wasm32) {
|
if (platform == .web) {
|
||||||
const lib = b.addSharedLibrary(options.name, thisDir() ++ "/src/platform/wasm.zig", .unversioned);
|
const lib = b.addSharedLibrary(options.name, thisDir() ++ "/src/platform/wasm.zig", .unversioned);
|
||||||
lib.addPackage(gpu.pkg);
|
lib.addPackage(gpu.pkg);
|
||||||
lib.addPackage(js_runtime.pkg);
|
lib.addPackage(js_runtime.pkg);
|
||||||
|
|
@ -183,6 +200,7 @@ pub const App = struct {
|
||||||
.b = b,
|
.b = b,
|
||||||
.step = step,
|
.step = step,
|
||||||
.name = options.name,
|
.name = options.name,
|
||||||
|
.platform = platform,
|
||||||
.res_dirs = options.res_dirs,
|
.res_dirs = options.res_dirs,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -192,7 +210,7 @@ pub const App = struct {
|
||||||
|
|
||||||
// Install additional files (src/mach.js and template.html)
|
// Install additional files (src/mach.js and template.html)
|
||||||
// in case of wasm
|
// in case of wasm
|
||||||
if (app.step.target.toTarget().cpu.arch == .wasm32) {
|
if (app.platform == .web) {
|
||||||
// Set install directory to '{prefix}/www'
|
// Set install directory to '{prefix}/www'
|
||||||
app.getInstallStep().?.dest_dir = web_install_dir;
|
app.getInstallStep().?.dest_dir = web_install_dir;
|
||||||
|
|
||||||
|
|
@ -238,7 +256,7 @@ pub const App = struct {
|
||||||
.gpu_dawn_options = @bitCast(@import("gpu/libs/mach-gpu-dawn/build.zig").Options, options.gpu_dawn_options),
|
.gpu_dawn_options = @bitCast(@import("gpu/libs/mach-gpu-dawn/build.zig").Options, options.gpu_dawn_options),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (app.step.target.toTarget().cpu.arch != .wasm32) {
|
if (app.platform != .web) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
@ -253,7 +271,7 @@ 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.platform == .web) {
|
||||||
ensureDependencySubmodule(app.b.allocator, "tools/libs/apple_pie") catch unreachable;
|
ensureDependencySubmodule(app.b.allocator, "tools/libs/apple_pie") catch unreachable;
|
||||||
|
|
||||||
const http_server = app.b.addExecutable("http-server", thisDir() ++ "/tools/http-server.zig");
|
const http_server = app.b.addExecutable("http-server", thisDir() ++ "/tools/http-server.zig");
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue