build: examples run independently & sysaudio examples renamed as tests

This commit is contained in:
Ronald M Zielaznicki 2024-10-30 00:44:37 -04:00 committed by Stephen Gutekanst
parent 825a676d36
commit 910e8f6e82
9 changed files with 84 additions and 64 deletions

146
build.zig
View file

@ -70,6 +70,39 @@ pub fn build(b: *std.Build) !void {
build_options.addOption(SysgpuBackend, "sysgpu_backend", sysgpu_backend); build_options.addOption(SysgpuBackend, "sysgpu_backend", sysgpu_backend);
build_options.addOption(Platform, "core_platform", core_platform); build_options.addOption(Platform, "core_platform", core_platform);
var examples = [_]Example{
.{ .name = "core-custom-entrypoint", .deps = &.{} },
.{ .name = "core-triangle", .deps = &.{} },
.{ .name = "custom-renderer", .deps = &.{} },
.{ .name = "glyphs", .deps = &.{ .assets, .freetype } },
// .{ .name = "hardware-check", .deps = &.{ .assets, .zigimg } },
.{ .name = "piano", .deps = &.{} },
.{ .name = "play-opus", .deps = &.{.assets} },
// .{ .name = "sprite", .deps = &.{ .zigimg, .assets } },
.{ .name = "text", .deps = &.{.assets} },
};
var sysaudio_tests = [_]SysAudioTest{
.{ .name = "record" },
.{ .name = "sine" },
};
// Setup Steps
const docs_step = b.step("docs", "Generate docs");
for (&examples) |*example| {
example.run_step = b.step(
b.fmt("run-{s}", .{example.name}),
b.fmt("Run example: {s}", .{example.name}),
);
}
const test_step = b.step("test", "Test Run: Unit Tests");
for (&sysaudio_tests) |*sysaudio_test| {
sysaudio_test.run_step = b.step(
b.fmt("test-sysaudio-{s}", .{sysaudio_test.name}),
b.fmt("Test Run: sysaudio {s}", .{sysaudio_test.name}),
);
}
const module = b.addModule("mach", .{ const module = b.addModule("mach", .{
.root_source_file = b.path("src/main.zig"), .root_source_file = b.path("src/main.zig"),
.optimize = optimize, .optimize = optimize,
@ -77,6 +110,14 @@ pub fn build(b: *std.Build) !void {
}); });
module.addImport("build-options", build_options.createModule()); module.addImport("build-options", build_options.createModule());
buildExamples(
b,
optimize,
target,
module,
&examples,
);
if (want_mach) { if (want_mach) {
// Linux gamemode requires libc. // Linux gamemode requires libc.
if (target.result.os.tag == .linux) module.link_libc = true; if (target.result.os.tag == .linux) module.link_libc = true;
@ -98,7 +139,9 @@ pub fn build(b: *std.Build) !void {
} }
if (b.lazyDependency("font_assets", .{})) |dep| module.addImport("font-assets", dep.module("font-assets")); if (b.lazyDependency("font_assets", .{})) |dep| module.addImport("font-assets", dep.module("font-assets"));
if (want_examples) try buildExamples(b, optimize, target, module); if (want_examples) {
for (examples) |example| b.getInstallStep().dependOn(example.install_step);
}
} }
if (want_core) { if (want_core) {
if (target.result.os.tag == .linux) { if (target.result.os.tag == .linux) {
@ -117,30 +160,19 @@ pub fn build(b: *std.Build) !void {
}); });
} }
if (target.result.cpu.arch != .wasm32) { if (target.result.cpu.arch != .wasm32) {
if (want_examples) { for (&sysaudio_tests) |*sysaudio_test| {
// TODO(build): make these 'tests' not 'examples' const test_exe = b.addExecutable(.{
inline for ([_][]const u8{ .name = b.fmt("sysaudio-{s}", .{sysaudio_test.name}),
"sine", .root_source_file = b.path(b.fmt("src/sysaudio/tests/{s}.zig", .{sysaudio_test.name})),
"record", .target = target,
}) |example| { .optimize = optimize,
const example_exe = b.addExecutable(.{ });
.name = "sysaudio-" ++ example, test_exe.root_module.addImport("mach", module);
.root_source_file = b.path("src/sysaudio/examples/" ++ example ++ ".zig"),
.target = target,
.optimize = optimize,
});
example_exe.root_module.addImport("mach", module);
const example_compile_step = b.step("sysaudio-" ++ example, "Compile 'sysaudio-" ++ example ++ "' example"); const run_cmd = b.addRunArtifact(test_exe);
example_compile_step.dependOn(b.getInstallStep()); if (b.args) |args| run_cmd.addArgs(args);
const example_run_cmd = b.addRunArtifact(example_exe); sysaudio_test.run_step.dependOn(&run_cmd.step);
example_run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| example_run_cmd.addArgs(args);
const example_run_step = b.step("run-sysaudio-" ++ example, "Run '" ++ example ++ "' example");
example_run_step.dependOn(&example_run_cmd.step);
}
} }
} }
} }
@ -189,7 +221,6 @@ pub fn build(b: *std.Build) !void {
// Exposes a `test` step to the `zig build --help` menu, providing a way for the user to // Exposes a `test` step to the `zig build --help` menu, providing a way for the user to
// request running the unit tests. // request running the unit tests.
const run_unit_tests = b.addRunArtifact(unit_tests); const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_unit_tests.step); test_step.dependOn(&run_unit_tests.step);
if (want_sysgpu) linkSysgpu(b, &unit_tests.root_module); if (want_sysgpu) linkSysgpu(b, &unit_tests.root_module);
@ -210,7 +241,6 @@ pub fn build(b: *std.Build) !void {
.install_dir = .prefix, .install_dir = .prefix,
.install_subdir = "docs", .install_subdir = "docs",
}); });
const docs_step = b.step("docs", "Generate docs");
docs_step.dependOn(&install_docs.step); docs_step.dependOn(&install_docs.step);
} }
} }
@ -346,48 +376,35 @@ fn linkSysaudio(b: *std.Build, module: *std.Build.Module) void {
} }
} }
const Dependency = enum {
assets,
freetype,
zigimg,
};
const Example = struct {
name: []const u8,
deps: []const Dependency = &.{},
install_step: *std.Build.Step = undefined,
run_step: *std.Build.Step = undefined,
};
fn buildExamples( fn buildExamples(
b: *std.Build, b: *std.Build,
optimize: std.builtin.OptimizeMode, optimize: std.builtin.OptimizeMode,
target: std.Build.ResolvedTarget, target: std.Build.ResolvedTarget,
mach_mod: *std.Build.Module, mach_mod: *std.Build.Module,
) !void { examples: []Example,
const Dependency = enum { ) void {
assets, for (examples) |*example| {
freetype,
zigimg,
};
for ([_]struct {
core: bool = false,
name: []const u8,
deps: []const Dependency = &.{},
has_assets: bool = false,
}{
// Mach core examples
.{ .core = true, .name = "custom-entrypoint", .deps = &.{} },
.{ .core = true, .name = "triangle", .deps = &.{} },
// Mach engine examples
// .{ .name = "hardware-check", .deps = &.{ .assets, .zigimg } },
.{ .name = "custom-renderer", .deps = &.{} },
.{ .name = "glyphs", .deps = &.{ .freetype, .assets } },
.{ .name = "piano", .deps = &.{} },
.{ .name = "play-opus", .deps = &.{.assets} },
// .{ .name = "sprite", .deps = &.{ .zigimg, .assets } },
.{ .name = "text", .deps = &.{.assets} },
}) |example| {
const exe = b.addExecutable(.{ const exe = b.addExecutable(.{
.name = if (example.core) b.fmt("core-{s}", .{example.name}) else example.name, .name = example.name,
.root_source_file = if (example.core) .root_source_file = b.path(b.fmt("examples/{s}/main.zig", .{example.name})),
b.path(b.fmt("examples/core/{s}/main.zig", .{example.name}))
else
b.path(b.fmt("examples/{s}/main.zig", .{example.name})),
.target = target, .target = target,
.optimize = optimize, .optimize = optimize,
}); });
exe.root_module.addImport("mach", mach_mod); exe.root_module.addImport("mach", mach_mod);
b.installArtifact(exe);
for (example.deps) |d| { for (example.deps) |d| {
switch (d) { switch (d) {
@ -412,18 +429,21 @@ fn buildExamples(
} }
} }
const compile_step = b.step(example.name, b.fmt("Compile {s}", .{example.name}));
compile_step.dependOn(b.getInstallStep());
const run_cmd = b.addRunArtifact(exe); const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd.addArgs(args); if (b.args) |args| run_cmd.addArgs(args);
const run_step = b.step(b.fmt("run-{s}", .{example.name}), b.fmt("Run {s}", .{example.name})); const installArtifact = b.addInstallArtifact(exe, .{});
run_step.dependOn(&run_cmd.step); example.install_step = &installArtifact.step;
run_cmd.step.dependOn(&installArtifact.step);
example.run_step.dependOn(&run_cmd.step);
} }
} }
const SysAudioTest = struct {
name: []const u8,
run_step: *std.Build.Step = undefined,
};
comptime { comptime {
const supported_zig = std.SemanticVersion.parse("0.14.0-dev.1911+3bf89f55c") catch unreachable; const supported_zig = std.SemanticVersion.parse("0.14.0-dev.1911+3bf89f55c") catch unreachable;
if (builtin.zig_version.order(supported_zig) != .eq) { if (builtin.zig_version.order(supported_zig) != .eq) {

View file

@ -31,7 +31,7 @@ pub fn main() !void {
while (true) { while (true) {
std.debug.print("( paused = {}, volume = {d} )\n> ", .{ player.paused(), try player.volume() }); std.debug.print("( paused = {}, volume = {d} )\n> ", .{ player.paused(), try player.volume() });
const line = (try std.io.getStdIn().reader().readUntilDelimiterOrEof(&buf, '\n')) orelse break; const line = (try std.io.getStdIn().reader().readUntilDelimiterOrEof(&buf, '\n')) orelse break;
var iter = std.mem.split(u8, line, ":"); var iter = std.mem.splitScalar(u8, line, ':');
const cmd = std.mem.trimRight(u8, iter.first(), &std.ascii.whitespace); const cmd = std.mem.trimRight(u8, iter.first(), &std.ascii.whitespace);
if (std.mem.eql(u8, cmd, "vol")) { if (std.mem.eql(u8, cmd, "vol")) {
const vol = try std.fmt.parseFloat(f32, std.mem.trim(u8, iter.next().?, &std.ascii.whitespace)); const vol = try std.fmt.parseFloat(f32, std.mem.trim(u8, iter.next().?, &std.ascii.whitespace));