core: 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:46:24 -07:00 committed by Stephen Gutekanst
parent f80e02b7c1
commit e54a4b458a
3 changed files with 68 additions and 58 deletions

View file

@ -26,9 +26,9 @@ Then in your `build.zig` add:
... ...
const core = @import("libs/mach-core/build.zig"); const core = @import("libs/mach-core/build.zig");
pub fn build(b: *Builder) void { pub fn build(b: *Build) void {
... ...
exe.addPackage(core.pkg); exe.addModule("core", core.module(b));
core.link(b, exe, .{}); core.link(b, exe, .{});
} }
``` ```

View file

@ -21,8 +21,8 @@ const core = @import("sdk.zig").Sdk(.{
.sysjs = sysjs, .sysjs = sysjs,
}); });
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{
@ -37,9 +37,9 @@ pub fn build(b: *std.build.Builder) !void {
const gpu_test_step = b.step("test-gpu", "Run GPU library tests"); const gpu_test_step = b.step("test-gpu", "Run GPU library tests");
const core_test_step = b.step("test-core", "Run Mach Core library tests"); const core_test_step = b.step("test-core", "Run Mach Core library tests");
glfw_test_step.dependOn(&(try glfw.testStep(b, mode, target)).step); glfw_test_step.dependOn(&(try glfw.testStep(b, optimize, target)).step);
gpu_test_step.dependOn(&(try gpu.testStep(b, mode, target, options.gpuOptions())).step); gpu_test_step.dependOn(&(try gpu.testStep(b, optimize, target, options.gpuOptions())).step);
core_test_step.dependOn(&(try core.testStep(b, mode, target)).step); core_test_step.dependOn(&(try core.testStep(b, optimize, target)).step);
all_tests_step.dependOn(glfw_test_step); all_tests_step.dependOn(glfw_test_step);
all_tests_step.dependOn(gpu_test_step); all_tests_step.dependOn(gpu_test_step);
@ -47,11 +47,11 @@ pub fn build(b: *std.build.Builder) !void {
// TODO: we need a way to test wasm stuff // TODO: we need a way to test wasm stuff
// const sysjs_test_step = b.step( "test-sysjs", "Run sysjs library tests"); // const sysjs_test_step = b.step( "test-sysjs", "Run sysjs library tests");
// sysjs_test_step.dependOn(&sysjs.testStep(b, mode, target).step); // sysjs_test_step.dependOn(&sysjs.testStep(b, optimize, target).step);
// all_tests_step.dependOn(sysjs_test_step); // all_tests_step.dependOn(sysjs_test_step);
// Compiles the `libmachcore` shared library // Compiles the `libmachcore` shared library
const shared_lib = try core.buildSharedLib(b, mode, target, options); const shared_lib = try core.buildSharedLib(b, optimize, target, options);
shared_lib.install(); shared_lib.install();
} }

View file

@ -2,12 +2,6 @@ const std = @import("std");
pub fn Sdk(comptime deps: anytype) type { pub fn Sdk(comptime deps: anytype) type {
return struct { return struct {
pub const pkg = std.build.Pkg{
.name = "core",
.source = .{ .path = sdkPath("/src/main.zig") },
.dependencies = &.{deps.gpu.pkg},
};
pub const Options = struct { pub const Options = struct {
glfw_options: deps.glfw.Options = .{}, glfw_options: deps.glfw.Options = .{},
gpu_dawn_options: deps.gpu_dawn.Options = .{}, gpu_dawn_options: deps.gpu_dawn.Options = .{},
@ -19,35 +13,46 @@ pub fn Sdk(comptime deps: anytype) type {
} }
}; };
pub fn testStep(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget) !*std.build.RunStep { pub fn module(b: *std.Build) *std.build.Module {
const main_tests = b.addTestExe("core-tests", "src/main.zig"); return b.createModule(.{
main_tests.setBuildMode(mode); .source_file = .{ .path = sdkPath("/src/main.zig") },
main_tests.setTarget(target); .dependencies = &.{
for (pkg.dependencies.?) |dependency| { .{ .name = "gpu", .module = deps.gpu.module(b) },
main_tests.addPackage(dependency); },
});
}
pub fn testStep(b: *std.Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget) !*std.build.RunStep {
const main_tests = b.addTest(.{
.name = "core-tests",
.kind = .test_exe,
.root_source_file = .{ .path = sdkPath("/src/main.zig") },
.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.addPackage(deps.glfw.pkg); main_tests.addModule("glfw", deps.glfw.module(b));
try deps.glfw.link(b, main_tests, .{}); try deps.glfw.link(b, main_tests, .{});
main_tests.addIncludePath(sdkPath("/include")); main_tests.addIncludePath(sdkPath("/include"));
main_tests.install(); main_tests.install();
return main_tests.run(); return main_tests.run();
} }
pub fn buildSharedLib(b: *std.build.Builder, mode: std.builtin.Mode, target: std.zig.CrossTarget, options: Options) !*std.build.LibExeObjStep { pub fn buildSharedLib(b: *std.Build, optimize: std.builtin.OptimizeMode, target: std.zig.CrossTarget, options: Options) !*std.build.CompileStep {
// TODO(build): this should use the App abstraction instead of being built manually // TODO(build): this should use the App abstraction instead of being built manually
const lib = b.addSharedLibrary("machcore", "src/platform/libmachcore.zig", .unversioned); const lib = b.addSharedLibrary(.{ .name = "machcore", .root_source_file = .{ .path = "src/platform/libmachcore.zig" }, .target = target, .optimize = optimize });
lib.setTarget(target);
lib.setBuildMode(mode);
lib.main_pkg_path = "src/"; lib.main_pkg_path = "src/";
const app_pkg = std.build.Pkg{ const app_module = b.createModule(.{
.name = "app", .source_file = .{ .path = "src/platform/libmachcore.zig" },
.source = .{ .path = "src/platform/libmachcore.zig" }, });
}; lib.addModule("app", app_module);
lib.addPackage(app_pkg); lib.addModule("glfw", deps.glfw.module(b));
lib.addPackage(deps.glfw.pkg); lib.addModule("gpu", deps.gpu.module(b));
lib.addPackage(deps.gpu.pkg);
if (target.isLinux()) { if (target.isLinux()) {
lib.addPackage(deps.gamemode.pkg); lib.addModule("gamemode", deps.gamemode.module(b));
deps.gamemode.link(lib); deps.gamemode.link(lib);
} }
try deps.glfw.link(b, lib, options.glfw_options); try deps.glfw.link(b, lib, options.glfw_options);
@ -64,9 +69,9 @@ pub fn Sdk(comptime deps: anytype) type {
} }
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: Platform, platform: Platform,
res_dirs: ?[]const []const u8, res_dirs: ?[]const []const u8,
watch_paths: ?[]const []const u8, watch_paths: ?[]const []const u8,
@ -90,13 +95,13 @@ pub fn Sdk(comptime deps: anytype) type {
}; };
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,
}, },
@ -104,44 +109,46 @@ pub fn Sdk(comptime deps: anytype) type {
const target = (try std.zig.system.NativeTargetInfo.detect(options.target)).target; const target = (try std.zig.system.NativeTargetInfo.detect(options.target)).target;
const platform = Platform.fromTarget(target); const platform = Platform.fromTarget(target);
var dependencies = std.ArrayList(std.build.Pkg).init(b.allocator); var dependencies = std.ArrayList(std.build.ModuleDependency).init(b.allocator);
try dependencies.append(pkg); try dependencies.append(.{ .name = "core", .module = module(b) });
try dependencies.append(deps.gpu.pkg); try dependencies.append(.{ .name = "gpu", .module = deps.gpu.module(b) });
switch (platform) { switch (platform) {
.native => try dependencies.append(deps.glfw.pkg), .native => try dependencies.append(.{ .name = "glfw", .module = deps.glfw.module(b) }),
.web => try dependencies.append(deps.sysjs.pkg), .web => try dependencies.append(.{ .name = "sysjs", .module = deps.sysjs.module(b) }),
} }
if (options.deps) |app_deps| try dependencies.appendSlice(app_deps); if (options.deps) |app_deps| try dependencies.appendSlice(app_deps);
const app_pkg = std.build.Pkg{ const app_module = b.createModule(.{
.name = "app", .source_file = .{ .path = options.src },
.source = .{ .path = options.src },
.dependencies = try dependencies.toOwnedSlice(), .dependencies = try dependencies.toOwnedSlice(),
}; });
const step = blk: { const step = blk: {
if (platform == .web) { if (platform == .web) {
const lib = b.addSharedLibrary(options.name, sdkPath("/src/main.zig"), .unversioned); const lib = b.addSharedLibrary(.{ .name = options.name, .root_source_file = .{ .path = sdkPath("/src/main.zig") }, .target = options.target, .optimize = options.optimize });
lib.rdynamic = true; lib.rdynamic = true;
lib.addPackage(deps.sysjs.pkg); lib.addModule("sysjs", deps.sysjs.module(b));
break :blk lib; break :blk lib;
} else { } else {
const exe = b.addExecutable(options.name, sdkPath("/src/main.zig")); const exe = b.addExecutable(.{
exe.addPackage(deps.glfw.pkg); .name = options.name,
.root_source_file = .{ .path = sdkPath("/src/main.zig") },
.target = options.target,
.optimize = options.optimize,
});
exe.addModule("glfw", deps.glfw.module(b));
if (target.os.tag == .linux) if (target.os.tag == .linux)
exe.addPackage(deps.gamemode.pkg); exe.addModule("gamemode", deps.gamemode.module(b));
break :blk exe; break :blk exe;
} }
}; };
step.main_pkg_path = sdkPath("/src"); step.main_pkg_path = sdkPath("/src");
step.addPackage(deps.gpu.pkg); step.addModule("gpu", deps.gpu.module(b));
step.addPackage(app_pkg); step.addModule("app", app_module);
step.setTarget(options.target);
step.setBuildMode(options.mode);
return .{ return .{
.b = b, .b = b,
@ -180,7 +187,10 @@ pub fn Sdk(comptime deps: anytype) type {
app.getInstallStep().?.step.dependOn(&install_js.step); app.getInstallStep().?.step.dependOn(&install_js.step);
} }
const html_generator = app.b.addExecutable("html-generator", sdkPath("/tools/html-generator/main.zig")); const html_generator = app.b.addExecutable(.{
.name = "html-generator",
.root_source_file = .{ .path = sdkPath("/tools/html-generator/main.zig") },
});
const run_html_generator = html_generator.run(); const run_html_generator = html_generator.run();
run_html_generator.addArgs(&.{ "index.html", app.name }); run_html_generator.addArgs(&.{ "index.html", app.name });