summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig146
1 files changed, 82 insertions, 64 deletions
diff --git a/build.zig b/build.zig
index 02d312e..aac8d16 100644
--- a/build.zig
+++ b/build.zig
@@ -6,7 +6,7 @@
//!
//! const sb = sketchbook.init(b, .{ .target = target, .optimize = optimize });
//! const app = sb.app(.{ .name = "demo", .root_source_file = b.path("src/main.zig") });
-//! _ = app.addSteps(.{}); // run, lib, probe, e2e, test, install;
+//! _ = app.addSteps(.{}); // run, lib, e2e, test, install;
//! // web and serve with `.web = .{}`
//!
//! or pick the pieces: `app.addLibrary()`, `app.addExecutable()`,
@@ -110,18 +110,7 @@ pub fn build(b: *Build) void {
});
b.installArtifact(host_exe);
- const probe = b.addExecutable(.{
- .name = "sketchbook-probe",
- .root_module = b.createModule(.{
- .root_source_file = b.path("src/host/probe.zig"),
- .target = target,
- .optimize = optimize,
- .imports = &.{.{ .name = "abi", .module = abi }},
- }),
- });
- b.installArtifact(probe);
-
- var sb: Sketchbook = .{ .b = b, .target = target, .optimize = optimize, .modules = .{ .abi = abi, .sketch = sketch, .host = host, .tracy = tracy, .vector = undefined, .scope = undefined, .anim = undefined, .gltf = gltf }, .host_exe = host_exe, .probe_exe = probe, .gltf_generator = gltf_generator, .web = b.path("web"), .glsl = b.path("src/glsl.zig"), .tools = b.path("tools"), .anim_dir = b.path("src/anim"), .gltf_render_dir = b.path("src/gltf_render"), .script_optimize = optimize, .shared = .create(b) };
+ var sb: Sketchbook = .{ .b = b, .target = target, .optimize = optimize, .modules = .{ .abi = abi, .sketch = sketch, .host = host, .tracy = tracy, .vector = undefined, .scope = undefined, .anim = undefined, .gltf = gltf }, .host_exe = host_exe, .gltf_generator = gltf_generator, .web = b.path("web"), .glsl = b.path("src/glsl.zig"), .tools = b.path("tools"), .anim_dir = b.path("src/anim"), .gltf_render_dir = b.path("src/gltf_render"), .script_optimize = optimize, .shared = .create(b) };
// Vector graphics: built with the same shader step apps use. Both forms
// of its shaders are exported, so apps' web builds can swap them too.
@@ -183,6 +172,15 @@ pub fn build(b: *Build) void {
b.createModule(.{ .root_source_file = b.path("src/math/root.zig"), .target = target, .optimize = optimize }),
b.createModule(.{ .root_source_file = b.path("src/host/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 }, .{ .name = "xitdb", .module = xitdb } } }),
}) |module| test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step);
+ // A State bigger than a save holds is a compile error that says what to do.
+ const too_big = b.addObject(.{ .name = "too_big", .root_module = b.createModule(.{
+ .root_source_file = b.addWriteFiles().add("too_big.zig", "comptime { _ = @import(\"abi\").layout.of(struct { bytes: [(32 << 20) + 1]u8 }, .state); }"),
+ .target = target,
+ .optimize = optimize,
+ .imports = &.{.{ .name = "abi", .module = abi }},
+ }) });
+ too_big.expect_errors = .{ .contains = "may be 32 MiB, half of a save, so history has room: lower a capacity (an array, an sk.List), or keep what need not be saved in Session." };
+ test_step.dependOn(&too_big.step);
// The anim module alone: its unit tests, and below the player's scenes.
const test_anim = b.step("test-anim", "Test the anim module: its unit tests and the player on its test scenes");
const anim_tests = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/anim/root.zig"), .target = target, .optimize = optimize, .imports = anim_imports }) }));
@@ -300,6 +298,31 @@ pub fn build(b: *Build) void {
test_talk.dependOn(&talk_check.run.step);
test_step.dependOn(&talk_check.run.step);
+ // The manual example: Figure 2-1 of the Intel SDM, Vol. 2, page 31, as
+ // a talk in the stock player. The PDF is named, never committed: built
+ // without it, the scene says so, and only its script needs it.
+ const pdf = b.option([]const u8, "manual", "The Intel SDM Vol. 2 PDF for the manual example (default ~/01-resources/intel-devref-ii.pdf)") orelse
+ b.pathJoin(&.{ b.graph.environ_map.get("HOME") orelse "/", "01-resources", "intel-devref-ii.pdf" });
+ const have_pdf = if (std.Io.Dir.cwd().access(b.graph.io, pdf, .{})) true else |_| false;
+ const manual_options = b.addOptions();
+ manual_options.addOption(?[]const u8, "pdf", if (have_pdf) pdf else null);
+ const manual = sb.addScene(.{ .root_source_file = b.path("examples/manual.zig"), .imports = &.{.{ .name = "manual", .module = manual_options.createModule() }} });
+ const manual_lib = manual.app.addLibrary();
+ b.step("manual", "Play the manual example in a window (-Dmanual=PDF, Vol. 2 of the Intel SDM)").dependOn(&manual.app.addRun(manual_lib, .{ .state = ".zig-cache/manual.state" }).step);
+ test_step.dependOn(&manual_lib.step);
+ if (have_pdf) {
+ const manual_exe = manual.app.addExecutable();
+ const manual_script = manual.app.addScript(manual_exe, b.path("test/manual.script"), .{});
+ // Two exports (a frame cap apiece, so the build runs both), which
+ // quit at the end, as the stock player does.
+ for ([_][]const u8{ "a", "b" }, [_]u64{ 1000, 1001 }) |define, cap| {
+ manual_script.addArg("--define");
+ manual_script.addPrefixedDirectoryArg(b.fmt("{s}=", .{define}), manual.app.addExport(manual_exe, .{ .fps = 10, .frames = cap, .size = .{ 640, 360 } }).frames);
+ }
+ b.step("test-manual", "Play the manual example through its steps, and export it twice").dependOn(&manual_script.step);
+ test_step.dependOn(&manual_script.step);
+ }
+
// Both vector renderers against resvg, through the host (tools/svgdiff.py).
const svgview = sb.app(.{ .name = "svgview", .root_source_file = b.path("test/svgview.zig"), .imports = &.{.{ .name = "vector", .module = sb.modules.vector }} });
const diff = b.addSystemCommand(&.{ "python3", "tools/svgdiff.py" });
@@ -324,11 +347,13 @@ pub fn build(b: *Build) void {
.{ .name = "anim", .module = sb.modules.anim },
.{ .name = "scene", .module = compiled.module },
.{ .name = "typst", .module = compiled.typst },
+ .{ .name = "manual", .module = manual.module },
+ .{ .name = "manual_typst", .module = manual.typst },
},
}),
}));
bench.has_side_effects = true;
- b.step("bench", "Time parsing and drawing a scene's Typst (release mode, must be under 1ms)").dependOn(&bench.step);
+ b.step("bench", "Time parsing and drawing scenes' Typst (release mode: a simple object under 1ms, a PDF page in its own budget)").dependOn(&bench.step);
// After the tests above, not beside them: it measures time.
for (test_step.dependencies.items) |step| bench.step.dependOn(step);
test_step.dependOn(&bench.step);
@@ -380,17 +405,22 @@ pub fn build(b: *Build) void {
test_step.dependOn(&reload.step);
// A session survives a kill -9 up to its last commit: 255 frames
- // played, four commits of 60 made, the fifth lost with the process.
- // Its sketch records in every mode, so the test does in release builds too.
+ // played, four commits of 60 made, the fifth lost with the process; the
+ // next run starts from frame 240's State, replayed. Its sketch records
+ // in every mode, so the test does in release builds too; and a sketch
+ // that does not record keeps its present all the same.
const recorded = sb.app(.{ .name = "replay", .root_source_file = b.path("test/replay.zig"), .record = true }).addLibrary();
+ const unrecorded = sb.app(.{ .name = "replay", .root_source_file = b.path("test/replay.zig"), .record = false }).addLibrary();
const killed = b.addSystemCommand(&.{ "sh", "-c",
\\set -e
\\db="${3%.state}.xitdb"
- \\rm -f "$3" "$3.store" "$db"
- \\if "$0" --headless --script "$1" --state "$3" "$4"; then echo "killed run exited"; exit 1; else test $? = 137; fi
- \\"$0" --headless --script "$2" --state "$3" "$4"
+ \\for lib in "$4" "$5"; do
+ \\rm -f "$db"
+ \\if "$0" --headless --script "$1" --state "$3" "$lib"; then echo "killed run exited"; exit 1; else test $? = 137; fi
+ \\"$0" --headless --script "$2" --state "$3" "$lib"
\\test "$(stat -c %s "$db")" -lt 1048576
- \\rm -f "$3" "$3.store" "$db"
+ \\rm -f "$db"
+ \\done
});
killed.setName("a session survives kill -9");
killed.addArtifactArg(host_exe);
@@ -398,6 +428,7 @@ pub fn build(b: *Build) void {
killed.addFileArg(b.path("test/killed.script"));
_ = killed.addOutputFileArg("kill.state");
killed.addArtifactArg(recorded);
+ killed.addArtifactArg(unrecorded);
killed.has_side_effects = true;
killed.expectExitCode(0);
test_step.dependOn(&killed.step);
@@ -414,12 +445,12 @@ pub fn build(b: *Build) void {
const past = b.addSystemCommand(&.{ "sh", "-c",
\\set -e
\\db="${4%.state}.xitdb"
- \\rm -f "$4" "$4.store" "$db"
+ \\rm -f "$4" "$db"
\\"$0" --headless --script "$1" --state "$4" "$5"
\\"$0" --headless --script "$2" --state "$4" "$5"
\\"$0" --headless --script "$3" --state "$4" "$6"
\\test "$(stat -c %s "$db")" -lt 1048576
- \\rm -f "$4" "$4.store" "$db"
+ \\rm -f "$4" "$db"
});
past.setName("past sessions replay");
past.addArtifactArg(host_exe);
@@ -459,7 +490,7 @@ pub fn build(b: *Build) void {
// A script run twice records the same database but for the wall clock's
// times: the log and the keyframes follow from the script alone. From
- // fresh state it records in memory: no database at all.
+ // fresh state it records in memory: no file at all.
const same_sessions = b.addExecutable(.{ .name = "same_sessions", .root_module = b.createModule(.{
.root_source_file = b.path("test/same_sessions.zig"),
.target = target,
@@ -473,7 +504,7 @@ pub fn build(b: *Build) void {
\\"$0" --headless --script "$1" --state "$d/b.state" "$4"
\\"$2" "$d/a.xitdb" "$d/b.xitdb"
\\"$0" --headless --fresh --script "$1" --state "$d/c.state" "$4"
- \\test -e "$d/c.state"; test ! -e "$d/c.xitdb"
+ \\test ! -e "$d/c.state"; test ! -e "$d/c.xitdb"
\\rm -f "$d"/*
});
twice.setName("a script recorded twice");
@@ -487,34 +518,36 @@ pub fn build(b: *Build) void {
test_step.dependOn(&twice.step);
b.step("test-same", "A script run twice records the same database but for times").dependOn(&twice.step);
- // Where files go. Without --state: the XDG data directory, state and
- // sessions. In .sketchbook/ (`zig build run`'s): a .gitignore, and an
- // older build's state moved in, once, never over a file there.
+ // Where files go. Without --state: the XDG data directory, one file. In
+ // .sketchbook/ (`zig build run`'s): a .gitignore and the save, whose
+ // present the next run starts from; an older build's state file beside
+ // it is moved in, once, never over a file there, and never deleted.
const paths = b.addSystemCommand(&.{ "sh", "-c",
\\set -e
\\exe=$(realpath "$0"); lib=$(realpath "$1"); mkdir -p "$2"; d=$(realpath "$2")
\\rm -rf "$d"/* "$d"/.sketchbook; cd "$d"
\\XDG_DATA_HOME="$d/data" "$exe" --headless --console quit "$lib"
- \\test -s data/replay/replay.state; test -s data/replay/replay.xitdb
- \\"$exe" --headless --state replay.state --console "set updates 7" --console quit "$lib"
- \\"$exe" --headless --state .sketchbook/replay.state --console quit "$lib" 2>err
- \\grep -q moved err; test ! -e replay.state; test ! -e replay.state.store; grep -qx '*' .sketchbook/.gitignore
+ \\test "$(ls -A data/replay)" = replay.xitdb
+ \\"$exe" --headless --state .sketchbook/replay.xitdb --console "set updates 7" --console quit "$lib"
+ \\test "$(ls -A .sketchbook | tr '\n' ' ')" = ".gitignore replay.xitdb "; grep -qx '*' .sketchbook/.gitignore
\\printf 'expect updates 7\n' >check.script
- \\"$exe" --headless --state .sketchbook/replay.state --script check.script "$lib"
- \\"$exe" --headless --state replay.state --console quit "$lib"
- \\"$exe" --headless --state .sketchbook/replay.state --console quit "$lib" 2>err
- \\grep -q 'both exist' err; test -e replay.state
- \\"$exe" --headless --state .sketchbook/replay.state --script check.script "$lib"
+ \\"$exe" --headless --state .sketchbook/replay.xitdb --script check.script "$lib"
+ \\printf 'not a state file' >old.state
+ \\"$exe" --headless --state .sketchbook/old.xitdb --console quit "$lib" 2>err
+ \\grep -q moved err; test ! -e old.state; test "$(cat .sketchbook/old.state)" = 'not a state file'
+ \\printf 'not a state file' >old.state
+ \\"$exe" --headless --state .sketchbook/old.xitdb --console quit "$lib" 2>err
+ \\grep -q 'both exist' err; test -e old.state
\\rm -rf "$d"/* "$d"/.sketchbook
});
- paths.setName("state and sessions paths");
+ paths.setName("where the save goes");
paths.addArtifactArg(host_exe);
paths.addArtifactArg(recorded);
_ = paths.addOutputDirectoryArg("paths");
paths.has_side_effects = true;
paths.expectExitCode(0);
test_step.dependOn(&paths.step);
- b.step("test-paths", "Check where state and sessions files go").dependOn(&paths.step);
+ b.step("test-paths", "Check where the save goes").dependOn(&paths.step);
// Recorded input replays to the same state: played, seeked, stepped,
// and after a live pointer move (core host behaviour, not an API).
@@ -710,7 +743,6 @@ pub fn init(b: *Build, options: Options) Sketchbook {
.optimize = options.optimize,
.modules = .{ .abi = dep.module("abi"), .sketch = dep.module("sketch"), .host = dep.module("host"), .tracy = dep.module("tracy"), .vector = dep.module("vector"), .scope = dep.module("scope"), .anim = dep.module("anim"), .gltf = dep.module("gltf") },
.host_exe = dep.artifact("sketchbook"),
- .probe_exe = dep.artifact("sketchbook-probe"),
.gltf_generator = dep.artifact("gltf-bindings"),
.web = dep.path("web"),
.glsl = dep.path("src/glsl.zig"),
@@ -762,8 +794,6 @@ pub const Sketchbook = struct {
modules: struct { abi: *Build.Module, sketch: *Build.Module, host: *Build.Module, tracy: *Build.Module, vector: *Build.Module, scope: *Build.Module, anim: *Build.Module, gltf: *Build.Module },
/// The generic host: loads and hot-reloads a sketch library.
host_exe: *Build.Step.Compile,
- /// Prints any sketch's state file.
- probe_exe: *Build.Step.Compile,
/// `host.js` and the `index.html` template.
web: Build.LazyPath,
/// The GLSL declaration generator (see `addGlslLayouts`).
@@ -1223,8 +1253,9 @@ pub const App = struct {
/// Imports for the app's root module, besides `sketch`: for example
/// `sb.modules.vector`, or shaders from `addSpirvModule`.
imports: []const Build.Module.Import = &.{},
- /// Record every interactive run's sessions to a file beside its
- /// state (src/host/Sessions.zig). Null: in Debug builds only. A
+ /// Keep every run's history in the save, the newest 64 MiB of it
+ /// (src/host/Sessions.zig); without, the save keeps the present and
+ /// the marks only. Null: in Debug builds only. A
/// playtest build of a release opts in with one line,
/// `.record = b.option(bool, "record", "collect sessions in a release build")`.
record: ?bool = null,
@@ -1423,9 +1454,8 @@ pub const App = struct {
}
pub const RunOptions = struct {
- /// State file, relative to the working directory;
- /// `.sketchbook/<name>.state` by default, where the host keeps the
- /// sessions database beside it, all ignored by git and jj.
+ /// The save, relative to the working directory;
+ /// `.sketchbook/<name>.xitdb` by default, ignored by git and jj.
state: ?[]const u8 = null,
/// Where relative asset paths resolve; the project root by default.
cwd: ?Build.LazyPath = null,
@@ -1438,7 +1468,7 @@ pub const App = struct {
const install = b.addInstallArtifact(library, .{});
const run = b.addRunArtifact(app.sb.host_exe);
run.step.dependOn(&install.step);
- run.addArgs(&.{ "--state", options.state orelse b.fmt(".sketchbook/{s}.state", .{app.name}) });
+ run.addArgs(&.{ "--state", options.state orelse b.fmt(".sketchbook/{s}.xitdb", .{app.name}) });
run.addFileArg(b.graph.path(.install_lib, library.out_filename));
run.setCwd(options.cwd orelse b.path("."));
run.stdio = .inherit;
@@ -1452,7 +1482,7 @@ pub const App = struct {
cwd: ?Build.LazyPath = null,
};
- /// Plays a script headless, with a throwaway state file, against
+ /// Plays a script headless, from fresh state and saving nothing, against
/// `artifact`: the executable (`addExecutable`), or the hot-reloadable
/// library (`addLibrary`) through the generic host, which scripts can
/// `reload`. Fails when an expectation or a console line does.
@@ -1464,7 +1494,7 @@ pub const App = struct {
run.addFileArg(script);
run.addArg("--state");
// Named per script: runs of one app may share an output directory.
- _ = run.addOutputFileArg(b.fmt("{s}-{s}.state", .{ app.name, std.fs.path.stem(script.getDisplayName()) }));
+ _ = run.addOutputFileArg(b.fmt("{s}-{s}.xitdb", .{ app.name, std.fs.path.stem(script.getDisplayName()) }));
for (options.defines) |pair| run.addArgs(&.{ "--define", b.fmt("{s}={s}", .{ pair[0], pair[1] }) });
if (library) run.addArtifactArg(artifact);
run.setCwd(options.cwd orelse b.path("."));
@@ -1494,7 +1524,7 @@ pub const App = struct {
const b = app.sb.b;
const run = b.addRunArtifact(exe);
run.addArg("--state");
- _ = run.addOutputFileArg(b.fmt("{s}-export.state", .{app.name}));
+ _ = run.addOutputFileArg(b.fmt("{s}-export.xitdb", .{app.name}));
run.addArg("--export");
const frames = run.addOutputDirectoryArg("frames");
run.addArgs(&.{ "--fps", b.fmt("{d}", .{options.fps}) });
@@ -1505,17 +1535,6 @@ pub const App = struct {
return .{ .run = run, .frames = frames };
}
- /// Prints the app's live state; `zig build probe -- --watch` follows it.
- pub fn addProbe(app: App, state: ?[]const u8) *Build.Step.Run {
- const b = app.sb.b;
- const run = b.addRunArtifact(app.sb.probe_exe);
- run.addArg(state orelse b.fmt(".sketchbook/{s}.state", .{app.name}));
- run.setCwd(b.path("."));
- run.stdio = .inherit;
- run.addPassthruArgs();
- return run;
- }
-
/// Tests in `root_source_file` (the app's root by default), with the
/// app's imports and the app itself importable as `app`.
pub fn addTest(app: App, options: struct { root_source_file: ?Build.LazyPath = null }) *Build.Step.Compile {
@@ -1760,7 +1779,7 @@ pub const App = struct {
/// The conventional wiring. Installs the executable, and adds:
/// `run` (host + hot reload), `lib` (rebuild the library; use `--watch`),
- /// `web` and `serve` (with `web` options), `probe`, `export` (frames
+ /// `web` and `serve` (with `web` options), `export` (frames
/// for ffmpeg), `e2e` (scripts) and `test`.
pub fn addSteps(app: App, options: StepsOptions) Steps {
const b = app.sb.b;
@@ -1771,7 +1790,6 @@ pub const App = struct {
const run = app.addRun(library, .{});
b.step("run", "Run with hot reload (rebuild with `zig build lib --watch`)").dependOn(&run.step);
b.step("lib", "Build and install the hot-reloadable library").dependOn(&b.addInstallArtifact(library, .{}).step);
- b.step("probe", "Print the live state (-- --watch to follow)").dependOn(&app.addProbe(null).step);
const web = if (options.web) |web_options| blk: {
const web = app.addWeb(web_options);
@@ -1783,7 +1801,7 @@ pub const App = struct {
// `--export` takes the first argument after `--`.
const exporting = b.addRunArtifact(exe);
- exporting.addArgs(&.{ "--state", b.fmt(".sketchbook/{s}.state", .{app.name}), "--export" });
+ exporting.addArgs(&.{ "--state", b.fmt(".sketchbook/{s}.xitdb", .{app.name}), "--export" });
exporting.addPassthruArgs();
exporting.setCwd(b.path("."));
exporting.stdio = .inherit;