mach: update to latest Zig build API

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2023-02-08 02:56:48 -07:00 committed by Stephen Gutekanst
parent e54a4b458a
commit b69079127a

View file

@ -30,11 +30,17 @@ const core = @import("libs/core/sdk.zig").Sdk(.{
.sysjs = sysjs, .sysjs = sysjs,
}); });
pub const pkg = std.build.Pkg{ pub fn module(b: *std.Build) *std.build.Module {
.name = "mach", return b.createModule(.{
.source = .{ .path = sdkPath("/src/main.zig") }, .source_file = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &.{ core.pkg, ecs.pkg, sysaudio.pkg, earcut.pkg }, .dependencies = &.{
}; .{ .name = "core", .module = core.module(b) },
.{ .name = "ecs", .module = ecs.module(b) },
.{ .name = "sysaudio", .module = sysaudio.module(b) },
.{ .name = "earcut", .module = earcut.module(b) },
},
});
}
pub const Options = struct { pub const Options = struct {
core: core.Options = .{}, core: core.Options = .{},
@ -42,8 +48,8 @@ pub const Options = struct {
freetype: freetype.Options = .{}, freetype: freetype.Options = .{},
}; };
pub fn build(b: *std.build.Builder) !void { pub fn build(b: *std.Build) !void {
const mode = b.standardReleaseOptions(); const optimize = b.standardOptimizeOption(.{});
const target = b.standardTargetOptions(.{}); const target = b.standardTargetOptions(.{});
const gpu_dawn_options = gpu_dawn.Options{ const gpu_dawn_options = gpu_dawn.Options{
@ -62,13 +68,13 @@ pub fn build(b: *std.build.Builder) !void {
const model3d_test_step = b.step("test-model3d", "Run Model3D library tests"); const model3d_test_step = b.step("test-model3d", "Run Model3D library tests");
const mach_test_step = b.step("test-mach", "Run Engine library tests"); const mach_test_step = b.step("test-mach", "Run Engine library tests");
core_test_step.dependOn(&(try core.testStep(b, mode, target)).step); core_test_step.dependOn(&(try core.testStep(b, optimize, target)).step);
freetype_test_step.dependOn(&freetype.testStep(b, mode, target).step); freetype_test_step.dependOn(&freetype.testStep(b, optimize, target).step);
ecs_test_step.dependOn(&ecs.testStep(b, mode, target).step); ecs_test_step.dependOn(&ecs.testStep(b, optimize, target).step);
basisu_test_step.dependOn(&basisu.testStep(b, mode, target).step); basisu_test_step.dependOn(&basisu.testStep(b, optimize, target).step);
sysaudio_test_step.dependOn(&sysaudio.testStep(b, mode, target).step); sysaudio_test_step.dependOn(&sysaudio.testStep(b, optimize, target).step);
model3d_test_step.dependOn(&model3d.testStep(b, mode, target).step); model3d_test_step.dependOn(&model3d.testStep(b, optimize, target).step);
mach_test_step.dependOn(&testStep(b, mode, target).step); mach_test_step.dependOn(&testStep(b, optimize, target).step);
all_tests_step.dependOn(core_test_step); all_tests_step.dependOn(core_test_step);
all_tests_step.dependOn(ecs_test_step); all_tests_step.dependOn(ecs_test_step);
@ -84,7 +90,7 @@ pub fn build(b: *std.build.Builder) !void {
.name = "shaderexp", .name = "shaderexp",
.src = "shaderexp/main.zig", .src = "shaderexp/main.zig",
.target = target, .target = target,
.mode = mode, .optimize = optimize,
}, },
); );
try shaderexp_app.link(options); try shaderexp_app.link(options);
@ -103,21 +109,26 @@ pub fn build(b: *std.build.Builder) !void {
compile_all.dependOn(b.getInstallStep()); compile_all.dependOn(b.getInstallStep());
} }
fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) *std.build.RunStep { fn testStep(b: *std.Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) *std.build.RunStep {
const main_tests = b.addTestExe("mach-tests", "src/main.zig"); const main_tests = b.addTest(.{
main_tests.setBuildMode(mode); .name = "mach-tests",
main_tests.setTarget(target); .kind = .test_exe,
for (pkg.dependencies.?) |dependency| { .root_source_file = .{ .path = "src/main.zig" },
main_tests.addPackage(dependency); .target = target,
.optimize = optimize,
});
var iter = module(b).dependencies.iterator();
while (iter.next()) |e| {
main_tests.addModule(e.key_ptr.*, e.value_ptr.*);
} }
main_tests.install(); main_tests.install();
return main_tests.run(); return main_tests.run();
} }
pub const App = struct { pub const App = struct {
b: *std.build.Builder, b: *std.Build,
name: []const u8, name: []const u8,
step: *std.build.LibExeObjStep, step: *std.build.CompileStep,
platform: core.App.Platform, platform: core.App.Platform,
core: core.App, core: core.App,
@ -129,13 +140,13 @@ pub const App = struct {
pub const RunError = core.App.RunError; pub const RunError = core.App.RunError;
pub fn init( pub fn init(
b: *std.build.Builder, b: *std.Build,
options: struct { options: struct {
name: []const u8, name: []const u8,
src: []const u8, src: []const u8,
target: std.zig.CrossTarget, target: std.zig.CrossTarget,
mode: std.builtin.Mode, optimize: std.builtin.OptimizeMode,
deps: ?[]const std.build.Pkg = null, deps: ?[]const std.build.ModuleDependency = null,
res_dirs: ?[]const []const u8 = null, res_dirs: ?[]const []const u8 = null,
watch_paths: ?[]const []const u8 = null, watch_paths: ?[]const []const u8 = null,
@ -145,17 +156,17 @@ pub const App = struct {
use_model3d: bool = false, use_model3d: bool = false,
}, },
) InitError!App { ) InitError!App {
var deps = std.ArrayList(std.build.Pkg).init(b.allocator); var deps = std.ArrayList(std.build.ModuleDependency).init(b.allocator);
if (options.deps) |v| try deps.appendSlice(v); if (options.deps) |v| try deps.appendSlice(v);
try deps.append(pkg); try deps.append(.{ .name = "mach", .module = module(b) });
try deps.append(sysaudio.pkg); try deps.append(.{ .name = "sysaudio", .module = sysaudio.module(b) });
if (options.use_freetype) |_| try deps.append(freetype.pkg); if (options.use_freetype) |_| try deps.append(.{ .name = "freetype", .module = freetype.module(b) });
const app = try core.App.init(b, .{ const app = try core.App.init(b, .{
.name = options.name, .name = options.name,
.src = options.src, .src = options.src,
.target = options.target, .target = options.target,
.mode = options.mode, .optimize = options.optimize,
.deps = deps.items, .deps = deps.items,
.res_dirs = options.res_dirs, .res_dirs = options.res_dirs,
.watch_paths = options.watch_paths, .watch_paths = options.watch_paths,