summaryrefslogtreecommitdiff
path: root/anim/build.zig
diff options
context:
space:
mode:
Diffstat (limited to 'anim/build.zig')
-rw-r--r--anim/build.zig48
1 files changed, 46 insertions, 2 deletions
diff --git a/anim/build.zig b/anim/build.zig
index 5646f1a..29a4b05 100644
--- a/anim/build.zig
+++ b/anim/build.zig
@@ -11,7 +11,8 @@
//! zig build motion -Dscene=list -Doptimize=ReleaseFast how a scene moves: contact sheets and change plots in out/motion/list
//!
//! Needs, besides what sketchbook and ztrace need: `setarch`, and for
-//! `videos`, `ffmpeg`.
+//! `videos`, `ffmpeg`. The sqlite scene also needs `clang` and SQLite's
+//! amalgamation (`-Dsqlite=DIR`); without it that scene is not made.
const std = @import("std");
const sketchbook = @import("sketchbook");
@@ -136,6 +137,48 @@ pub fn build(b: *std.Build) void {
_ = embedded.addCopyFile(source, r.source);
table.print(b.allocator, "pub const {s}: Program = .{{ .trace = @embedFile(\"{s}\"), .file = \"{s}\", .source = @embedFile(\"{s}\") }};\n", .{ r.name, file, r.source, r.source }) catch @panic("OOM");
}
+ // SQLite, the next rung: its amalgamation is not ours to copy, so the
+ // scene is made only where it is. Built as the bench builds it (-O2,
+ // frame pointers kept), and as the instrumented ones are (no PIE, bound
+ // at load, no canary); run once under the debugger, stopping where a
+ // statement's bytecode is run (`sqlite3_step`), where a row enters the
+ // B-tree (`sqlite3BtreeInsert`), where a page is written
+ // (`unixWrite`) and where its `pwrite64` has returned to it (the
+ // instruction after the call, found by objdump: there is no name for
+ // it); every syscall recorded with the bytes it points at, up to a page.
+ // In a directory of its own each time: the database it makes must not be there.
+ const sqlite_dir = b.option([]const u8, "sqlite", "SQLite's amalgamation (sqlite3.c, sqlite3.h): the sqlite scene is made only with it") orelse b.pathJoin(&.{ b.graph.environ_map.get("HOME") orelse "/", "05-genizah/sqlite/sqlite-amalgamation-3500400" });
+ const sqlite_c = b.pathJoin(&.{ sqlite_dir, "sqlite3.c" });
+ const has_sqlite = if (std.Io.Dir.cwd().access(b.graph.io, sqlite_c, .{})) |_| true else |_| false;
+ if (has_sqlite) {
+ const cc = b.addSystemCommand(&.{ "clang", "-O2", "-g", "-fno-omit-frame-pointer", "-no-pie", "-fno-stack-protector", "-Wl,-z,now", "-w" });
+ cc.addPrefixedDirectoryArg("-I", .{ .cwd_relative = sqlite_dir });
+ cc.addFileArg(b.path("tools/sqlite.c"));
+ cc.addFileArg(.{ .cwd_relative = sqlite_c });
+ cc.addArgs(&.{ "-lm", "-o" });
+ const program = cc.addOutputFileArg("sqlite");
+ const run = b.addSystemCommand(&.{ "sh", "-c",
+ \\set -e
+ \\ztrace=$(realpath "$0") program=$(realpath "$1")
+ \\case $2 in /*) out=$2 ;; *) out=$PWD/$2 ;; esac
+ \\back=$(objdump -d --no-show-raw-insn "$program" | awk '/<unixWrite>:/ { f = 1 } f && /call.*pwrite64/ { getline; sub(":", "", $1); print "0x" $1; exit }')
+ \\[ -n "$back" ] || { echo "no call of pwrite64 in unixWrite" >&2; exit 1; }
+ \\d=$(mktemp -d)
+ \\trap 'rm -rf "$d"' EXIT
+ \\cp "$program" "$d/sqlite"
+ \\cd "$d"
+ \\env -i setarch -R "$ztrace" record --by break -b sqlite3_step -b sqlite3BtreeInsert -b unixWrite -b "*$back" --stack 512 -s 4096 -o "$out" -- ./sqlite
+ });
+ run.addArtifactArg(ztrace);
+ run.addFileArg(program);
+ const trace = run.addOutputFileArg("sqlite.ztrace");
+ run.expectStdErrMatch("exit 0");
+ record_step.dependOn(&b.addInstallFile(trace, "recordings/sqlite.ztrace").step);
+ _ = embedded.addCopyFile(trace, "sqlite.ztrace");
+ _ = embedded.addCopyFile(.{ .cwd_relative = sqlite_c }, "sqlite3.c");
+ table.print(b.allocator, "pub const sqlite: Program = .{{ .trace = @embedFile(\"sqlite.ztrace\"), .file = \"sqlite3.c\", .source = @embedFile(\"sqlite3.c\") }};\n", .{}) catch @panic("OOM");
+ }
+
// The gallery's own: a recording of the list kept in test/gallery, so its
// pictures change when the drawing does and not when a new recording does.
_ = embedded.addCopyFile(b.path("test/gallery/list.ztrace"), "gallery.ztrace");
@@ -170,7 +213,8 @@ pub fn build(b: *std.Build) void {
// ---- Scenes.
const videos = b.step("videos", "Export every scene to out/NAME-1080.mp4 and out/NAME-2160.mp4 (use -Doptimize=ReleaseFast)");
- for (scenes) |name| {
+ for (scenes ++ [_][]const u8{"sqlite"}) |name| {
+ if (std.mem.eql(u8, name, "sqlite") and !has_sqlite) continue;
const scene = sb.addScene(.{ .root_source_file = b.path(b.fmt("scenes/{s}.zig", .{name})), .imports = &.{
.{ .name = "canim", .module = canim },
.{ .name = "recordings", .module = recordings },