summaryrefslogtreecommitdiff
path: root/build.zig
diff options
context:
space:
mode:
Diffstat (limited to 'build.zig')
-rw-r--r--build.zig150
1 files changed, 144 insertions, 6 deletions
diff --git a/build.zig b/build.zig
index e9c3277..4235ca4 100644
--- a/build.zig
+++ b/build.zig
@@ -11,7 +11,8 @@
//!
//! or pick the pieces: `app.addLibrary()`, `app.addExecutable()`,
//! `app.addWeb(...)`, `app.addRun(...)`, `app.addScript(...)`,
-//! `app.addProbe(...)`, `app.addTest(...)`. GPU shaders are GLSL, built by
+//! `app.addProbe(...)`, `app.addTest(...)`. `sb.addScene(...)` is the app
+//! that plays an `anim` scene file. GPU shaders are GLSL, built by
//! `sb.addShaders(...)` into SPIR-V for native hosts and WGSL for the web;
//! `sb.addGlslLayouts(...)` and `sb.addGlslc(...)` are its pieces. Each
//! returns the ordinary `std.Build` objects, so anything can be extended or
@@ -19,8 +20,8 @@
//!
//! Tools, found on PATH when a step needs them: `glslc` (shaderc or the
//! Vulkan SDK) for any shader, `naga` (`cargo install naga-cli`) only for
-//! the web build of a sketch with shaders, and `miniserve` and `openssl`
-//! only for `serve`.
+//! the web build of a sketch with shaders, `typst` only for a scene with
+//! Typst in it, and `miniserve` and `openssl` only for `serve`.
const std = @import("std");
const Build = std.Build;
const translate_c = @import("translate_c");
@@ -67,7 +68,7 @@ pub fn build(b: *Build) void {
});
b.installArtifact(probe);
- var sb: Sketchbook = .{ .b = b, .target = target, .optimize = optimize, .modules = .{ .abi = abi, .sketch = sketch, .host = host, .vector = undefined, .scope = undefined }, .host_exe = host_exe, .probe_exe = probe, .web = b.path("web"), .glsl = b.path("src/glsl.zig"), .tools = b.path("tools"), .script_optimize = optimize, .shared = .create(b) };
+ var sb: Sketchbook = .{ .b = b, .target = target, .optimize = optimize, .modules = .{ .abi = abi, .sketch = sketch, .host = host, .vector = undefined, .scope = undefined, .anim = undefined }, .host_exe = host_exe, .probe_exe = probe, .web = b.path("web"), .glsl = b.path("src/glsl.zig"), .tools = b.path("tools"), .anim_dir = b.path("src/anim"), .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.
@@ -86,6 +87,10 @@ pub fn build(b: *Build) void {
};
sb.modules.vector = b.addModule("vector", .{ .root_source_file = b.path("src/vector/root.zig"), .imports = vector_imports });
+ // manim-style animation: scenes as code, played by `src/anim/player.zig`.
+ const anim_imports: []const Build.Module.Import = &.{ .{ .name = "sketch", .module = sketch }, .{ .name = "vector", .module = sb.modules.vector } };
+ sb.modules.anim = b.addModule("anim", .{ .root_source_file = b.path("src/anim/root.zig"), .imports = anim_imports });
+
// Sound scopes: waveform, spectrogram, spectrum and oscilloscope of a
// stream of samples, analysed and drawn on the GPU.
const scope_layouts = b.createModule(.{ .root_source_file = b.path("src/scope/layouts.zig") });
@@ -114,6 +119,7 @@ pub fn build(b: *Build) void {
for ([_]*Build.Module{
b.createModule(.{ .root_source_file = b.path("src/vector/root.zig"), .target = target, .optimize = optimize, .imports = vector_imports }),
b.createModule(.{ .root_source_file = b.path("src/scope/root.zig"), .target = target, .optimize = optimize, .imports = scope_imports }),
+ b.createModule(.{ .root_source_file = b.path("src/anim/root.zig"), .target = target, .optimize = optimize, .imports = anim_imports }),
b.createModule(.{ .root_source_file = b.path("src/glsl.zig"), .target = target, .optimize = optimize }),
b.createModule(.{ .root_source_file = b.path("src/abi.zig"), .target = target, .optimize = optimize }),
b.createModule(.{ .root_source_file = b.path("src/sketch/root.zig"), .target = target, .optimize = optimize, .imports = sketch_imports }),
@@ -179,6 +185,51 @@ pub fn build(b: *Build) void {
// `Host.textSize` against the text drawn (test/text.zig).
const text = sb.app(.{ .name = "text", .root_source_file = b.path("test/text.zig") });
test_step.dependOn(&text.addScript(text.addExecutable(), b.path("test/text.script"), .{}).step);
+ // The anim module's player on its test scenes: a live one with a
+ // custom object between vector objects, and a compiled one, which is
+ // also exported, at half size.
+ const live = sb.addScene(.{ .root_source_file = b.path("test/scene_live.zig") });
+ test_step.dependOn(&live.app.addScript(live.app.addExecutable(), b.path("test/scene_live.script"), .{}).step);
+ const compiled = sb.addScene(.{ .root_source_file = b.path("test/scene_compiled.zig") });
+ const compiled_exe = compiled.app.addExecutable();
+ const compiled_script = compiled.app.addScript(compiled_exe, b.path("test/scene_compiled.script"), .{});
+ compiled_script.addArg("--define");
+ compiled_script.addPrefixedDirectoryArg("frames=", compiled.app.addExport(compiled_exe, .{ .fps = 30, .size = .{ 640, 360 }, .frames = 200 }).frames);
+ test_step.dependOn(&compiled_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" });
+ diff.addArtifactArg(svgview.addExecutable());
+ diff.addDirectoryArg(b.path("test/svg"));
+ diff.addArg(".zig-cache/svgdiff");
+ diff.setCwd(b.path("."));
+ diff.has_side_effects = true;
+ b.step("diff", "Compare the CPU and GPU vector renderers with resvg").dependOn(&diff.step);
+ test_step.dependOn(&diff.step);
+
+ // Timings always in release mode: a debug build measures nothing useful.
+ const bench = b.addRunArtifact(b.addExecutable(.{
+ .name = "bench",
+ .root_module = b.createModule(.{
+ .root_source_file = b.path("tools/bench.zig"),
+ .target = target,
+ .optimize = .ReleaseFast,
+ .imports = &.{
+ .{ .name = "vector", .module = sb.modules.vector },
+ .{ .name = "abi", .module = abi },
+ .{ .name = "anim", .module = sb.modules.anim },
+ .{ .name = "scene", .module = compiled.module },
+ .{ .name = "typst", .module = compiled.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);
+ // 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);
+
// The examples, so they always compile.
for ([_][]const u8{ "pen", "flock" }) |example| {
test_step.dependOn(&sb.app(.{ .name = example, .root_source_file = b.path(b.fmt("examples/{s}.zig", .{example})) }).addLibrary().step);
@@ -380,12 +431,13 @@ pub fn init(b: *Build, options: Options) Sketchbook {
.b = b,
.target = options.target,
.optimize = options.optimize,
- .modules = .{ .abi = dep.module("abi"), .sketch = dep.module("sketch"), .host = dep.module("host"), .vector = dep.module("vector"), .scope = dep.module("scope") },
+ .modules = .{ .abi = dep.module("abi"), .sketch = dep.module("sketch"), .host = dep.module("host"), .vector = dep.module("vector"), .scope = dep.module("scope"), .anim = dep.module("anim") },
.host_exe = dep.artifact("sketchbook"),
.probe_exe = dep.artifact("sketchbook-probe"),
.web = dep.path("web"),
.glsl = dep.path("src/glsl.zig"),
.tools = dep.path("tools"),
+ .anim_dir = dep.path("src/anim"),
.script_optimize = options.script_optimize orelse options.optimize,
.shared = shared,
};
@@ -423,7 +475,8 @@ pub const Sketchbook = struct {
optimize: std.builtin.OptimizeMode,
/// `vector`: SVG, cubic geometry, and CPU and GPU renderers. `scope`:
/// waveform, spectrogram, spectrum and oscilloscope views of a sound.
- modules: struct { abi: *Build.Module, sketch: *Build.Module, host: *Build.Module, vector: *Build.Module, scope: *Build.Module },
+ /// `anim`: manim-style scenes as code (see `addScene`).
+ modules: struct { abi: *Build.Module, sketch: *Build.Module, host: *Build.Module, vector: *Build.Module, scope: *Build.Module, anim: *Build.Module },
/// The generic host: loads and hot-reloads a sketch library.
host_exe: *Build.Step.Compile,
/// Prints any sketch's state file.
@@ -434,6 +487,8 @@ pub const Sketchbook = struct {
glsl: Build.LazyPath,
/// Build-time tools (`spirv_strip.zig`, `reload_edit.zig`, ...).
tools: Build.LazyPath,
+ /// The `anim` module's player and Typst generator (see `addScene`).
+ anim_dir: Build.LazyPath,
/// The optimize mode of executables that play scripts (`e2e`): Debug
/// is slow for per-pixel loops, and that is often the only reason to
/// build a script run differently from the rest.
@@ -719,6 +774,89 @@ pub const Sketchbook = struct {
return copy;
}
+ pub const SceneOptions = struct {
+ /// The app's name; the scene file's by default (`scenes/intro.zig`
+ /// is `intro`).
+ name: ?[]const u8 = null,
+ /// The scene: a file with `pub const scene = anim.Scene(construct)`
+ /// or `anim.Live(frame, typst)`.
+ root_source_file: Build.LazyPath,
+ /// Imports for the scene file, besides `anim` and `sketch`.
+ imports: []const Build.Module.Import = &.{},
+ };
+
+ pub const Scene = struct {
+ /// The player built around the scene: an app like any other
+ /// (`addSteps`, `addExecutable`, `addScript`, `addExport`).
+ app: App,
+ /// The scene file's module.
+ module: *Build.Module,
+ /// Its Typst sources as SVG: `find(source) ?[]const u8`.
+ typst: *Build.Module,
+ };
+
+ /// The `anim` player (`src/anim/player.zig`) built around one scene
+ /// file, whose Typst is compiled here, at build time.
+ pub fn addScene(sb: Sketchbook, options: SceneOptions) Scene {
+ const b = sb.b;
+ const imports = b.allocator.alloc(Build.Module.Import, options.imports.len + 2) catch @panic("OOM");
+ imports[0] = .{ .name = "anim", .module = sb.modules.anim };
+ imports[1] = .{ .name = "sketch", .module = sb.modules.sketch };
+ @memcpy(imports[2..], options.imports);
+ const scene = b.createModule(.{ .root_source_file = options.root_source_file, .imports = imports });
+ const typst = sb.addTypst(scene);
+ return .{
+ .app = sb.app(.{
+ .name = options.name orelse std.fs.path.stem(fileName(options.root_source_file)),
+ .root_source_file = sb.anim_dir.path(b, "player.zig"),
+ .imports = &.{
+ .{ .name = "vector", .module = sb.modules.vector },
+ .{ .name = "anim", .module = sb.modules.anim },
+ .{ .name = "scene", .module = scene },
+ .{ .name = "typst", .module = typst },
+ },
+ }),
+ .module = scene,
+ .typst = typst,
+ };
+ }
+
+ /// Compiles the Typst sources a scene declares (`scene.typst_sources`),
+ /// after its `typst_preamble`, with the `typst` CLI, and returns the
+ /// module the player finds them in (see `src/anim/typst.zig`). `scene`
+ /// is the scene file's module. A scene without Typst runs no `typst`.
+ pub fn addTypst(sb: Sketchbook, scene: *Build.Module) *Build.Module {
+ const b = sb.b;
+ const tool = b.addExecutable(.{
+ .name = "typst-scene",
+ .root_module = b.createModule(.{
+ .root_source_file = b.addWriteFiles().add("typst_scene.zig",
+ \\const std = @import("std");
+ \\const scene = @import("scene");
+ \\pub fn main(init: std.process.Init) !void {
+ \\ var args = init.minimal.args.iterate();
+ \\ _ = args.skip();
+ \\ const out = args.next().?;
+ \\ const cache = args.next().?;
+ \\ try @import("typst").generate(scene, scene.scene.typst_sources, init.arena.allocator(), init.io, out, cache);
+ \\}
+ ),
+ .target = b.graph.host,
+ .imports = &.{
+ .{ .name = "typst", .module = b.createModule(.{ .root_source_file = sb.anim_dir.path(b, "typst.zig") }) },
+ .{ .name = "scene", .module = scene },
+ },
+ }),
+ });
+ const run = b.addRunArtifact(tool);
+ const dir = run.addOutputDirectoryArg("typst");
+ // A cache that outlives the build cache's own entries: the local
+ // .zig-cache, relative to the project root.
+ run.setCwd(b.path("."));
+ run.addArg(".zig-cache/typst");
+ return b.createModule(.{ .root_source_file = dir.path(b, "typst.zig") });
+ }
+
pub fn app(sb: Sketchbook, options: App.Options) App {
const imports = sb.b.allocator.alloc(Build.Module.Import, options.imports.len + 1) catch @panic("OOM");
imports[0] = .{ .name = "sketch", .module = sb.modules.sketch };