diff options
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 478 |
1 files changed, 462 insertions, 16 deletions
@@ -31,16 +31,17 @@ pub fn build(b: *Build) void { // Target-free: they compile for whatever imports them, native or wasm. const abi = b.addModule("abi", .{ .root_source_file = b.path("src/abi.zig") }); - const sketch = b.addModule("sketch", .{ - .root_source_file = b.path("src/sketch/root.zig"), - .imports = &.{.{ .name = "abi", .module = abi }}, - }); + // Geometry and noise, shared by the sketch library and `vector`. + const math = b.addModule("math", .{ .root_source_file = b.path("src/math/root.zig") }); + const sketch_imports: []const Build.Module.Import = &.{ .{ .name = "abi", .module = abi }, .{ .name = "math", .module = math } }; + const sketch = b.addModule("sketch", .{ .root_source_file = b.path("src/sketch/root.zig"), .imports = sketch_imports }); const c = cModule(b, target, optimize); + // No optimize mode of its own: the host's Zig code builds in the mode + // of the executable it is part of (`script_optimize` makes use of it). const host = b.addModule("host", .{ .root_source_file = b.path("src/host/root.zig"), .target = target, - .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c } }, }); @@ -66,19 +67,20 @@ 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"), .shared = .create(b) }; + 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) }; // 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. const vector_layouts = b.createModule(.{ .root_source_file = b.path("src/vector/layouts.zig") }); const vector_shaders = sb.addShaders(.{ - .layouts = .{ .module = vector_layouts, .types = &.{ "Vertex", "Viewport", "Paint" } }, - .sources = &.{ b.path("src/vector/shaders/path.vert"), b.path("src/vector/shaders/stencil.frag"), b.path("src/vector/shaders/cover.frag") }, + .layouts = .{ .module = vector_layouts, .types = &.{ "Vertex", "Painted", "Viewport", "Paint" } }, + .sources = &.{ b.path("src/vector/shaders/path.vert"), b.path("src/vector/shaders/stencil.frag"), b.path("src/vector/shaders/paint.vert"), b.path("src/vector/shaders/paint.frag") }, }); b.modules.put(b.graph.arena, "vector_shaders", vector_shaders.module) catch @panic("OOM"); b.modules.put(b.graph.arena, "vector_shaders_web", vector_shaders.web) catch @panic("OOM"); const vector_imports: []const Build.Module.Import = &.{ .{ .name = "abi", .module = abi }, + .{ .name = "math", .module = math }, .{ .name = "vector_layouts", .module = vector_layouts }, .{ .name = "vector_shaders", .module = vector_shaders.module }, }; @@ -114,14 +116,70 @@ pub fn build(b: *Build) void { 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/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 = &.{.{ .name = "abi", .module = abi }} }), + b.createModule(.{ .root_source_file = b.path("src/sketch/root.zig"), .target = target, .optimize = optimize, .imports = sketch_imports }), + 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 } } }), }) |module| test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step); + // The noise tests again on wasm32 as the web builds it, under Node's + // WASI: the golden bits must come out the same in the browser as + // natively. (Only noise: LLVM 21 cannot select code for one of the + // Bézier tests' vector constants on wasm32 in release modes.) + const wasm_math = b.addTest(.{ .root_module = b.createModule(.{ + .root_source_file = b.path("src/math/noise.zig"), + .target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }), + .optimize = .ReleaseSafe, + }) }); + const wasi = Build.Step.Run.create(b, "noise tests on wasm32-wasi"); + wasi.addFileArg(sb.program(&sb.shared.node, &.{"node"})); + wasi.addArg("--no-warnings"); + wasi.addFileArg(b.path("tools/wasi_run.mjs")); + wasi.addArtifactArg(wasm_math); + // The test runner's progress is not a warning. + _ = wasi.captureStdErr(.{}); + wasi.expectExitCode(0); + test_step.dependOn(&wasi.step); + // A sketch reloaded mid-run with a changed layout: state carried over by // field name, through the real dlopen path and the headless host. const v1 = sb.app(.{ .name = "counter", .root_source_file = b.path("test/counter_v1.zig") }).addLibrary(); const v2 = sb.app(.{ .name = "counter", .root_source_file = b.path("test/counter_v2.zig") }).addLibrary(); + // Command.shape on the native host: the parity cases, screenshotted + // and checked pixel by pixel (tools/shapes_web.mjs compares the same + // sketch in headless Chrome: `zig build test-web`). + const shapes = sb.app(.{ .name = "shapes", .root_source_file = b.path("test/shapes.zig") }); + const shapes_native = shapes.addScript(shapes.addExecutable(), b.path("test/shapes.script"), .{}); + test_step.dependOn(&shapes_native.step); + const shapes_web = Build.Step.Run.create(b, "shapes in headless Chrome"); + shapes_web.addFileArg(sb.program(&sb.shared.node, &.{"node"})); + shapes_web.addFileArg(b.path("tools/shapes_web.mjs")); + shapes_web.addDirectoryArg(shapes.addWeb(.{}).dir); + shapes_web.addFileArg(b.path(".zig-cache/shapes/native.png")); + shapes_web.addFileArg(b.findProgramLazy(.{ .names = &.{ "google-chrome-stable", "google-chrome", "chromium", "chromium-browser" } })); + shapes_web.step.dependOn(&shapes_native.step); + shapes_web.has_side_effects = true; + shapes_web.expectExitCode(0); + const test_web = b.step("test-web", "Compare shapes, targets, blending and images in headless Chrome with the native host, and time 5000 circles"); + test_web.dependOn(&shapes_web.step); + // Targets, blend modes, image placement and tint, colours per subpath + // (test/render.zig): natively, checked pixel by pixel, and in Chrome. + const render = sb.app(.{ .name = "render", .root_source_file = b.path("test/render.zig") }); + const render_native = render.addScript(render.addExecutable(), b.path("test/render.script"), .{}); + test_step.dependOn(&render_native.step); + const render_web = Build.Step.Run.create(b, "render in headless Chrome"); + render_web.addFileArg(sb.program(&sb.shared.node, &.{"node"})); + render_web.addFileArg(b.path("tools/render_web.mjs")); + render_web.addDirectoryArg(render.addWeb(.{}).dir); + render_web.addFileArg(b.path(".zig-cache/render/native.png")); + render_web.addFileArg(b.findProgramLazy(.{ .names = &.{ "google-chrome-stable", "google-chrome", "chromium", "chromium-browser" } })); + render_web.step.dependOn(&render_native.step); + render_web.has_side_effects = true; + render_web.expectExitCode(0); + test_web.dependOn(&render_web.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); + } // The README's example, so the documentation always compiles. test_step.dependOn(&sb.app(.{ .name = "readme", .root_source_file = b.path("test/readme.zig") }).addLibrary().step); @@ -135,12 +193,24 @@ pub fn build(b: *Build) void { reload.addArtifactArg(v1); reload.expectExitCode(0); test_step.dependOn(&reload.step); + + // Recorded input replays to the same state: played, seeked, stepped, + // and after a live pointer move (core host behaviour, not an API). + const replay = b.addRunArtifact(host_exe); + replay.addArgs(&.{ "--headless", "--fresh", "--script" }); + replay.addFileArg(b.path("test/replay.script")); + replay.addArg("--state"); + _ = replay.addOutputFileArg("replay.state"); + replay.addArtifactArg(sb.app(.{ .name = "replay", .root_source_file = b.path("test/replay.zig") }).addLibrary()); + replay.expectExitCode(0); + test_step.dependOn(&replay.step); } fn cModule(b: *Build, target: Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *Build.Module { const sdl_dep = b.dependency("sdl", .{ .target = target, .optimize = optimize }); const sdl = sdl_dep.artifact("SDL3"); if (target.result.os.tag == .linux) fixSdlThreadCleanup(b, sdl.root_module, sdl_dep.path("src")); + fixSdlVulkanAllocationLeak(b, sdl.root_module, sdl_dep.path("src")); const translator: translate_c.Translator = .init(b.dependency("translate_c", .{}), .{ .c_source_file = b.addWriteFiles().add("c.h", \\#define SDL_DISABLE_OLD_NAMES @@ -196,6 +266,51 @@ fn fixSdlThreadCleanup(b: *Build, sdl: *Build.Module, source: Build.LazyPath) vo sdl.addCSourceFile(.{ .file = patched, .flags = flags.? }); } +/// SDL 3.4's Vulkan backend leaks a memory allocation's region list and +/// mutex when `vkAllocateMemory` fails (SDL_gpu_vulkan.c, +/// `VULKAN_INTERNAL_AllocateMemory`), which it then survives by trying +/// another memory type. Many headless hosts at once (parallel `e2e`) run +/// small heaps out, and the leak check failed random scripts. The file is +/// compiled from a copy with the two frees added. +fn fixSdlVulkanAllocationLeak(b: *Build, sdl: *Build.Module, source: Build.LazyPath) void { + const file = "src/gpu/vulkan/SDL_gpu_vulkan.c"; + var found = false; + var flags: []const []const u8 = &.{}; + for (sdl.link_objects.items) |object| { + if (object != .c_source_files) continue; + const group = object.c_source_files; + for (group.files, 0..) |f, i| { + if (!std.mem.eql(u8, f, file)) continue; + const files = b.allocator.alloc([]const u8, group.files.len - 1) catch @panic("OOM"); + @memcpy(files[0..i], group.files[0..i]); + @memcpy(files[i..], group.files[i + 1 ..]); + group.files = files; + flags = group.flags; + found = true; + break; + } + } + if (!found) return; + const tool = b.addExecutable(.{ .name = "reload-edit", .root_module = b.createModule(.{ + .root_source_file = b.path("tools/reload_edit.zig"), + .target = b.graph.host, + }) }); + const edit = b.addRunArtifact(tool); + edit.setName("patch SDL_gpu_vulkan.c"); + edit.addDirectoryArg(source.path(b, "gpu/vulkan")); + const out = edit.addOutputDirectoryArg("vulkan"); + edit.addArgs(&.{ + "SDL_gpu_vulkan.c", + " // Uh oh, we couldn't allocate, time to clean up\n SDL_free(allocation->freeRegions);\n", + " // Uh oh, we couldn't allocate, time to clean up\n SDL_free(allocation->freeRegions);\n SDL_free(allocation->usedRegions);\n SDL_DestroyMutex(allocation->memoryLock);\n", + }); + // The copy's relative includes (`../SDL_sysgpu.h`) resolve from the + // original directory. + sdl.addIncludePath(source.path(b, "gpu/vulkan")); + sdl.addIncludePath(source); + sdl.addCSourceFile(.{ .file = out.path(b, "SDL_gpu_vulkan.c"), .flags = flags }); +} + // --------------------------------------------------------------------------- // Build API for apps @@ -204,6 +319,12 @@ pub const Options = struct { optimize: std.builtin.OptimizeMode, /// This package's name in the app's build.zig.zon. dependency: []const u8 = "sketchbook", + /// The optimize mode of the executables `e2e` plays scripts with + /// (`App.addScriptExecutable`); `optimize` when null. Pass it a + /// `b.option`, say `-Dscript-optimize=ReleaseSafe`, when Debug is too + /// slow for a sketch's scripts. Only the host's and the app's Zig code + /// change mode; SDL and the C pieces keep `optimize`. + script_optimize: ?std.builtin.OptimizeMode = null, }; pub fn init(b: *Build, options: Options) Sketchbook { @@ -221,6 +342,8 @@ pub fn init(b: *Build, options: Options) Sketchbook { .probe_exe = dep.artifact("sketchbook-probe"), .web = dep.path("web"), .glsl = dep.path("src/glsl.zig"), + .tools = dep.path("tools"), + .script_optimize = options.script_optimize orelse options.optimize, .shared = shared, }; } @@ -229,15 +352,20 @@ pub fn init(b: *Build, options: Options) Sketchbook { const Shared = struct { /// SPIR-V shader modules and their WGSL twins (see `addShaders`). web_twins: std.AutoArrayHashMapUnmanaged(*Build.Module, *Build.Module) = .empty, + /// How each shader module was made, so `addReloadCheck` can make it + /// again from edited sources. + recipes: std.AutoArrayHashMapUnmanaged(*Build.Module, Recipe) = .empty, /// Lookups of external tools, made once, run when a step needs them. glslc: ?Build.LazyPath = null, naga: ?Build.LazyPath = null, miniserve: ?Build.LazyPath = null, + node: ?Build.LazyPath = null, openssl: ?Build.LazyPath = null, /// A self-signed certificate and its key, for `addServe`. certificate: ?Certificate = null, const Certificate = struct { cert: Build.LazyPath, key: Build.LazyPath }; + const Recipe = union(enum) { glsl: Sketchbook.ShaderOptions, zig: Sketchbook.ZigShaderOptions }; fn create(b: *Build) *Shared { const shared = b.graph.arena.create(Shared) catch @panic("OOM"); @@ -261,6 +389,12 @@ pub const Sketchbook = struct { web: Build.LazyPath, /// The GLSL declaration generator (see `addGlslLayouts`). glsl: Build.LazyPath, + /// Build-time tools (`spirv_strip.zig`, `reload_edit.zig`, ...). + tools: 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. + script_optimize: std.builtin.OptimizeMode, shared: *Shared, pub const GlslLayouts = struct { @@ -316,7 +450,7 @@ pub const Sketchbook = struct { optimize: bool = true, }) Glslc { const b = sb.b; - const name = std.fs.path.basename(options.source.getDisplayName()); + const name = fileName(options.source); const run = Build.Step.Run.create(b, b.fmt("glslc {s}", .{name})); run.addFileArg(sb.glslc()); run.addArg(b.fmt("--target-env={s}", .{options.target_env})); @@ -353,14 +487,16 @@ pub const Sketchbook = struct { /// them `buffer`), and no `coherent` (WGSL's barriers make storage /// writes visible to the workgroup anyway, so leave it out under /// `#ifdef SKETCHBOOK_WEB`). - pub fn addShaders(sb: Sketchbook, options: struct { + pub const ShaderOptions = struct { sources: []const Build.LazyPath, /// Uniform and vertex layouts generated from Zig types, which the /// sources `#include` as `<Type>.glsl` (see `addGlslLayouts`). layouts: ?struct { module: *Build.Module, types: []const []const u8 } = null, /// More `#include` directories. include_dirs: []const Build.LazyPath = &.{}, - }) Shaders { + }; + + pub fn addShaders(sb: Sketchbook, options: ShaderOptions) Shaders { const b = sb.b; const include_dirs = b.graph.arena.alloc(Build.LazyPath, options.include_dirs.len + 1) catch @panic("OOM"); @memcpy(include_dirs[0..options.include_dirs.len], options.include_dirs); @@ -375,7 +511,7 @@ pub const Sketchbook = struct { var native_source: std.ArrayList(u8) = .empty; var web_source: std.ArrayList(u8) = .empty; for (options.sources) |source| { - const file = std.fs.path.basename(source.getDisplayName()); + const file = fileName(source); const name = std.mem.replaceOwned(u8, b.graph.arena, file, ".", "_") catch @panic("OOM"); const spirv = sb.addGlslc(.{ .source = source, .include_dirs = dirs }).spirv; _ = native.addCopyFile(spirv, b.fmt("{s}.spv", .{name})); @@ -395,6 +531,84 @@ pub const Sketchbook = struct { .web = b.createModule(.{ .root_source_file = web.add("wgsl.zig", web_source.items) }), }; sb.shared.web_twins.put(b.graph.arena, shaders.module, shaders.web) catch @panic("OOM"); + var kept = options; + kept.sources = b.graph.arena.dupe(Build.LazyPath, options.sources) catch @panic("OOM"); + kept.include_dirs = b.graph.arena.dupe(Build.LazyPath, options.include_dirs) catch @panic("OOM"); + sb.shared.recipes.put(b.graph.arena, shaders.module, .{ .glsl = kept }) catch @panic("OOM"); + return shaders; + } + + pub const ZigShaderOptions = struct { + /// One shader per file, its entry point `export fn main` with a + /// SPIR-V calling convention (`.spirv_fragment`, `.spirv_vertex`). + sources: []const Build.LazyPath, + /// Modules the shaders import (shared uniform structs, helpers). + imports: []const Build.Module.Import = &.{}, + }; + + /// Shaders written in Zig, as `addShaders` makes them from GLSL: the + /// same `Shaders` (SPIR-V module, WGSL twin swapped in for the web), so + /// a sketch cannot tell which language a shader came from. `fill.zig` + /// is declared as `fill`. + /// + /// Each source is compiled by Zig's own SPIR-V backend. The steps, and + /// why each one is there: + /// - target `spirv32-vulkan`: `spirv64` asks for physical storage + /// buffer pointers, which naga refuses; + /// - the self-hosted backend (`use_llvm = false`), ReleaseFast: through + /// LLVM the object comes out empty, without an error; + /// - `tools/spirv_strip.zig` then drops the `Aligned` memory operands + /// Zig puts on every OpLoad/OpStore: Vulkan accepts them, naga's + /// SPIR-V reader does not ("invalid operand count"); + /// - for the web, `naga --keep-coordinate-space` translates the + /// stripped SPIR-V to WGSL, as for GLSL. + /// + /// What shader code can be: no table-driven `std.math` (`atan2` indexes + /// const tables through generic pointers, which logical addressing + /// forbids: call GLSL.std.450 through inline assembly), and no + /// `@Vector` in an `extern struct` shared with the CPU (use `[2]f32` + /// there and `@Vector` only when `builtin.cpu.arch.isSpirV()`; std140 + /// wants a vec2 array stride of 16, which a `[2]f32` array lacks). + pub fn addZigShaders(sb: Sketchbook, options: ZigShaderOptions) Shaders { + const b = sb.b; + const spirv = b.resolveTargetQuery(.{ .cpu_arch = .spirv32, .os_tag = .vulkan }); + const strip = b.addExecutable(.{ .name = "spirv-strip", .root_module = b.createModule(.{ + .root_source_file = sb.tools.path(b, "spirv_strip.zig"), + .target = b.graph.host, + }) }); + const native = b.addWriteFiles(); + const web = b.addWriteFiles(); + var native_source: std.ArrayList(u8) = .empty; + var web_source: std.ArrayList(u8) = .empty; + for (options.sources) |source| { + const name = std.fs.path.stem(fileName(source)); + const object = b.addObject(.{ + .name = name, + .root_module = b.createModule(.{ .root_source_file = source, .target = spirv, .optimize = .ReleaseFast, .imports = options.imports }), + .use_llvm = false, + }); + const stripped = b.addRunArtifact(strip); + stripped.addFileArg(object.getEmittedBin()); + const spv = stripped.addOutputFileArg(b.fmt("{s}.spv", .{name})); + _ = native.addCopyFile(spv, b.fmt("{s}.spv", .{name})); + native_source.print(b.graph.arena, "const {s}_words align(4) = @embedFile(\"{s}.spv\").*;\npub const {s}: []const u8 = &{s}_words;\n", .{ name, name, name, name }) catch @panic("OOM"); + + const translate = Build.Step.Run.create(b, b.fmt("naga {s}", .{fileName(source)})); + translate.addFileArg(sb.naga()); + translate.addArg("--keep-coordinate-space"); + translate.addFileArg(spv); + _ = web.addCopyFile(translate.addOutputFileArg(b.fmt("{s}.wgsl", .{name})), b.fmt("{s}.wgsl", .{name})); + web_source.print(b.graph.arena, "pub const {s}: []const u8 = @embedFile(\"{s}.wgsl\");\n", .{ name, name }) catch @panic("OOM"); + } + const shaders: Shaders = .{ + .module = b.createModule(.{ .root_source_file = native.add("spirv.zig", native_source.items) }), + .web = b.createModule(.{ .root_source_file = web.add("wgsl.zig", web_source.items) }), + }; + sb.shared.web_twins.put(b.graph.arena, shaders.module, shaders.web) catch @panic("OOM"); + sb.shared.recipes.put(b.graph.arena, shaders.module, .{ .zig = .{ + .sources = b.graph.arena.dupe(Build.LazyPath, options.sources) catch @panic("OOM"), + .imports = b.graph.arena.dupe(Build.Module.Import, options.imports) catch @panic("OOM"), + } }) catch @panic("OOM"); return shaders; } @@ -412,7 +626,7 @@ pub const Sketchbook = struct { } /// A tool found on PATH when a step needs it, looked up once. - fn program(sb: Sketchbook, slot: *?Build.LazyPath, names: []const []const u8) Build.LazyPath { + pub fn program(sb: Sketchbook, slot: *?Build.LazyPath, names: []const []const u8) Build.LazyPath { if (slot.* == null) slot.* = sb.b.findProgramLazy(.{ .names = names }); return slot.*.?; } @@ -518,6 +732,16 @@ pub const App = struct { /// Host and sketch in one executable, nothing loaded at runtime: for /// shipping, and for scripts that must not depend on a library path. pub fn addExecutable(app: App) *Build.Step.Compile { + return app.executable(app.sb.optimize); + } + + /// The executable as scripts play it: built in `script_optimize` mode + /// (see `Options`), the same as `addExecutable` unless that was set. + pub fn addScriptExecutable(app: App) *Build.Step.Compile { + return app.executable(app.sb.script_optimize); + } + + fn executable(app: App, optimize: std.builtin.OptimizeMode) *Build.Step.Compile { return app.sb.b.addExecutable(.{ .name = app.name, .root_module = app.wrap( @@ -525,7 +749,7 @@ pub const App = struct { \\pub fn main(init: std.process.Init) !void { \\ return @import("host").main(init, @import("sketch").sketch(@import("app"))); \\} - , app.sb.target, app.sb.optimize), + , app.sb.target, optimize), }); } @@ -747,6 +971,217 @@ pub const App = struct { return b.addTest(.{ .root_module = module }); } + pub const Edit = struct { + /// Relative to the app's build root, as given to `b.path`: a Zig + /// source, a GLSL source, or a Zig shader source the app builds. + file: []const u8, + /// Must occur exactly once in `file`, or the build fails. + old: []const u8, + new: []const u8, + }; + + pub const ReloadCheck = struct { + run: *Build.Step.Run, + /// The library as written, and as edited. + library: *Build.Step.Compile, + edited: *Build.Step.Compile, + }; + + /// Checks that the app hot-reloads with its State kept: builds its + /// library as written, and again from a copy of its sources with + /// `edits` applied, then plays `script` headless through the generic + /// host on the first, with `{v2}` defined as the second, so the script + /// can `load {v2}` midway and check that State carried over (and, with + /// `expect-different-screenshot`, that the edit shows). + /// + /// The copy covers the directory of every source an edited file is in + /// (a module's root's directory holds what it imports), and every + /// module and shader set (`addShaders`, `addZigShaders`) built from it + /// is built again from the copy, so edits to Zig code, GLSL and Zig + /// shaders all reach the second library. + pub fn addReloadCheck(app: App, options: struct { + script: Build.LazyPath, + edits: []const Edit, + }) ReloadCheck { + const b = app.sb.b; + var remap: Remap = .{ .sb = app.sb }; + remap.collect(app.module); + for (options.edits) |edit| remap.cover(edit); + remap.makeCopies(options.edits); + var edited_app = app; + edited_app.module = remap.remapModule(app.module); + + const library = app.addLibrary(); + const edited = edited_app.addLibrary(); + const run = b.addRunArtifact(app.sb.host_exe); + run.setName(b.fmt("reload check {s}", .{options.script.getDisplayName()})); + run.addArgs(&.{ "--headless", "--fresh", "--script" }); + run.addFileArg(options.script); + run.addArg("--state"); + _ = run.addOutputFileArg(b.fmt("{s}-{s}.state", .{ app.name, std.fs.path.stem(options.script.getDisplayName()) })); + run.addArg("--define"); + run.addPrefixedArtifactArg("v2=", edited); + run.addArtifactArg(library); + run.setCwd(b.path(".")); + run.expectExitCode(0); + run.has_side_effects = true; + return .{ .run = run, .library = library, .edited = edited }; + } + + /// The app's module graph, with the sources under some directories + /// replaced by edited copies (see `addReloadCheck`). + const Remap = struct { + sb: Sketchbook, + /// Source directories (relative to the build root) the graph uses. + dirs: std.ArrayList([]const u8) = .empty, + /// The directories copied, and their edited copies. + roots: std.ArrayList([]const u8) = .empty, + copies: std.ArrayList(Build.LazyPath) = .empty, + seen: std.AutoArrayHashMapUnmanaged(*Build.Module, void) = .empty, + done: std.AutoArrayHashMapUnmanaged(*Build.Module, *Build.Module) = .empty, + + fn arena(r: *Remap) std.mem.Allocator { + return r.sb.b.graph.arena; + } + + fn ours(r: *Remap, path: Build.LazyPath) ?[]const u8 { + return switch (path) { + .src_path => |p| if (p.owner == r.sb.b) p.sub_path else null, + else => null, + }; + } + + fn note(r: *Remap, path: ?Build.LazyPath) void { + const sub = r.ours(path orelse return) orelse return; + r.dirs.append(r.arena(), std.fs.path.dirname(sub) orelse ".") catch @panic("OOM"); + } + + fn collect(r: *Remap, m: *Build.Module) void { + if ((r.seen.getOrPut(r.arena(), m) catch @panic("OOM")).found_existing) return; + if (r.sb.shared.recipes.get(m)) |recipe| switch (recipe) { + .glsl => |o| { + for (o.sources) |src| r.note(src); + if (o.layouts) |l| r.collect(l.module); + }, + .zig => |o| { + for (o.sources) |src| r.note(src); + for (o.imports) |i| r.collect(i.module); + }, + }; + r.note(m.root_source_file); + for (m.import_table.values()) |i| r.collect(i); + } + + fn under(dir: []const u8, path: []const u8) bool { + if (std.mem.eql(u8, dir, ".")) return true; + return path.len > dir.len and std.mem.startsWith(u8, path, dir) and path[dir.len] == '/'; + } + + /// Picks the outermost source directory holding `edit.file`. + fn cover(r: *Remap, edit: Edit) void { + var best: ?[]const u8 = null; + for (r.dirs.items) |dir| if (under(dir, edit.file) and (best == null or under(dir, best.?))) { + best = dir; + }; + const dir = best orelse std.debug.panic("addReloadCheck: {s} is in no directory of a source the app builds", .{edit.file}); + for (r.roots.items) |*root| { + if (under(root.*, dir) or std.mem.eql(u8, root.*, dir)) return; + if (under(dir, root.*)) { + root.* = dir; + return; + } + } + r.roots.append(r.arena(), dir) catch @panic("OOM"); + } + + fn makeCopies(r: *Remap, edits: []const Edit) void { + const b = r.sb.b; + const tool = b.addExecutable(.{ .name = "reload-edit", .root_module = b.createModule(.{ + .root_source_file = r.sb.tools.path(b, "reload_edit.zig"), + .target = b.graph.host, + }) }); + for (r.roots.items) |root| { + // Copied first: the copy's directory is named by its + // contents, so the edit below reruns whenever they change. + const copy = b.addWriteFiles(); + const source = copy.addCopyDirectory(b.path(root), "", .{}); + const run = b.addRunArtifact(tool); + run.setName(b.fmt("edit a copy of {s}", .{root})); + run.addDirectoryArg(source); + const out = run.addOutputDirectoryArg("src"); + for (edits) |edit| { + if (!under(root, edit.file)) continue; + run.addArgs(&.{ if (std.mem.eql(u8, root, ".")) edit.file else edit.file[root.len + 1 ..], edit.old, edit.new }); + } + r.copies.append(r.arena(), out) catch @panic("OOM"); + } + } + + fn remapPath(r: *Remap, original: Build.LazyPath) Build.LazyPath { + const sub = r.ours(original) orelse return original; + for (r.roots.items, r.copies.items) |root, copy| { + if (!under(root, sub)) continue; + return copy.path(r.sb.b, if (std.mem.eql(u8, root, ".")) sub else sub[root.len + 1 ..]); + } + return original; + } + + fn paths(r: *Remap, originals: []const Build.LazyPath, changed: *bool) []const Build.LazyPath { + const out = r.arena().alloc(Build.LazyPath, originals.len) catch @panic("OOM"); + for (originals, out) |o, *p| { + p.* = r.remapPath(o); + changed.* = changed.* or std.meta.activeTag(p.*) != std.meta.activeTag(o); + } + return out; + } + + fn remapModule(r: *Remap, m: *Build.Module) *Build.Module { + if (r.done.get(m)) |copy| return copy; + r.done.put(r.arena(), m, m) catch @panic("OOM"); + var changed = false; + const result = if (r.sb.shared.recipes.get(m)) |recipe| switch (recipe) { + .glsl => |o| blk: { + var e = o; + e.sources = r.paths(o.sources, &changed); + if (o.layouts) |l| { + e.layouts = .{ .module = r.remapModule(l.module), .types = l.types }; + changed = changed or e.layouts.?.module != l.module; + } + break :blk if (changed) r.sb.addShaders(e).module else m; + }, + .zig => |o| blk: { + var e = o; + e.sources = r.paths(o.sources, &changed); + const imports = r.arena().alloc(Build.Module.Import, o.imports.len) catch @panic("OOM"); + for (o.imports, imports) |i, *out| { + out.* = .{ .name = i.name, .module = r.remapModule(i.module) }; + changed = changed or out.module != i.module; + } + e.imports = imports; + break :blk if (changed) r.sb.addZigShaders(e).module else m; + }, + } else blk: { + const root = if (m.root_source_file) |file| r.remapPath(file) else null; + if (m.root_source_file) |file| changed = std.meta.activeTag(root.?) != std.meta.activeTag(file); + const imports = r.arena().alloc(*Build.Module, m.import_table.count()) catch @panic("OOM"); + for (m.import_table.values(), imports) |i, *out| { + out.* = r.remapModule(i); + changed = changed or out.* != i; + } + if (!changed) break :blk m; + const copy = r.arena().create(Build.Module) catch @panic("OOM"); + copy.init(m.owner, .{ .existing = m }); + copy.root_source_file = root; + copy.import_table = .empty; + copy.cached_graph = .{ .modules = &.{}, .names = &.{} }; + for (m.import_table.keys(), imports) |name, i| copy.addImport(name, i); + break :blk copy; + }; + r.done.put(r.arena(), m, result) catch @panic("OOM"); + return result; + } + }; + pub const StepsOptions = struct { web: ?WebOptions = null, /// Scripts run by the `e2e` step. @@ -788,7 +1223,8 @@ pub const App = struct { } else null; const e2e = b.step("e2e", "Play the end-to-end scripts headless"); - for (options.scripts) |script| e2e.dependOn(&app.addScript(exe, script, .{}).step); + const script_exe = if (app.sb.script_optimize == app.sb.optimize) exe else app.addScriptExecutable(); + for (options.scripts) |script| e2e.dependOn(&app.addScript(script_exe, script, .{}).step); const test_step = b.step("test", "Run unit tests and end-to-end scripts"); test_step.dependOn(&b.addRunArtifact(app.addTest(.{})).step); @@ -798,6 +1234,16 @@ pub const App = struct { } }; +/// A source's file name, for naming what is made from it: also for a file +/// inside a generated directory (an edited copy, see `addReloadCheck`), +/// whose display name would be "generated". +fn fileName(path: Build.LazyPath) []const u8 { + return switch (path) { + .generated => |g| if (g.sub_path.len > 0) std.fs.path.basename(g.sub_path) else path.getDisplayName(), + else => std.fs.path.basename(path.getDisplayName()), + }; +} + /// Fills `{title}` and `{head}` in the page template. const page_tool = \\const std = @import("std"); |
