const std = @import("std"); const builtin = @import("builtin"); const Platform = @import("../build.zig").Platform; const PerfBuild = @import("../build.zig").PerfBuild; const TreeSitterGrammars = @import("../build.zig").TreeSitterGrammars; const core = @import("core.zig"); const testMatch = @import("tests.zig").testMatch; /// A program of this build's, from `root`, with libc and `imports`: the /// harnesses, benches and runners below and in tests.zig. pub fn program(b: *std.Build, name: []const u8, root: []const u8, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, imports: []const std.Build.Module.Import) *std.Build.Step.Compile { return b.addExecutable(.{ .name = name, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path(root), .link_libc = true, .imports = imports }) }); } /// What unit-test runs of what these make. pub const Tools = struct { perf_gate_step: *std.Build.Step, /// The perf program's build identity, which its own tests read too. perf_options: *std.Build.Step.Options, lib_test_step: *std.Build.Step, }; /// The harnesses, the helix comparisons, the benches and the perf gate, the /// syntax snapshots and their tests, the library's package test and history. pub fn add(b: *std.Build, c: struct { c_optimize: std.lang.Optimize, core_module: *std.Build.Module, enable_jpx: bool, exe: *std.Build.Step.Compile, ghostty_optimize: std.lang.Optimize, ghostty_simd: bool, ghostty_vt_for_tests: ?*std.Build.Module, image_addon: *std.Build.Step.Compile, /// core-test's core: the syntax module's tests run on it. lean: core.Core, no_pdf: ?*std.Build.Step, optimize: std.lang.Optimize, parts: core.Parts, pdf_addon: ?*std.Build.Step.Compile, platform: Platform, prebuilt_shaders: bool, shell_cfg: core.ShellConfig, target: std.Build.ResolvedTarget, test_filters: []const []const u8, tracy: ?[]const u8, tree_sitter_grammars: TreeSitterGrammars, unit_profile: *std.Build.Step.Compile, }) Tools { const image_harness = program(b, "pardes-image-harness", "test/image_harness.zig", c.target, c.optimize, &.{}); if (c.ghostty_vt_for_tests) |vt| image_harness.root_module.addImport("ghostty-vt", vt); switch (c.target.result.os.tag) { .freebsd, .netbsd, .openbsd => image_harness.root_module.linkSystemLibrary("util", .{}), else => {}, } const run_image_harness = b.addRunArtifact(image_harness); run_image_harness.addArtifactArg(c.exe); run_image_harness.addArtifactArg(c.image_addon); run_image_harness.addArg(if (c.platform == .gui) "gui" else "tui"); run_image_harness.has_side_effects = true; b.step("image-harness", "exercise native Kitty/SDL image rendering end to end").dependOn(&run_image_harness.step); // Kitty graphics through a terminal pane, end to end: a client in // the pane's bash (and kitten icat, chafa when installed), a kitty // terminal outside (tui) or the GPU capture (gui). test/kitty.zig // is the same protocol at the core. const kitty_client = program(b, "pardes-kitty-client", "test/kitty_client.zig", c.target, c.optimize, &.{}); const kitty_harness = program(b, "pardes-kitty-harness", "test/kitty_harness.zig", c.target, c.optimize, &.{}); if (c.ghostty_vt_for_tests) |vt| kitty_harness.root_module.addImport("ghostty-vt", vt); switch (c.target.result.os.tag) { .freebsd, .netbsd, .openbsd => kitty_harness.root_module.linkSystemLibrary("util", .{}), else => {}, } const run_kitty_harness = b.addRunArtifact(kitty_harness); run_kitty_harness.addArtifactArg(c.exe); run_kitty_harness.addArtifactArg(kitty_client); run_kitty_harness.addArg(if (c.platform == .gui) "gui" else "tui"); run_kitty_harness.has_side_effects = true; b.step("kitty-harness", "kitty graphics through a terminal pane end to end (a client inside, a kitty terminal or the GPU capture outside)").dependOn(&run_kitty_harness.step); const pdf_harness_step = b.step("pdf-harness", "exercise continuous PDF rendering, search, and sections navigation end to end"); if (c.no_pdf) |s| pdf_harness_step.dependOn(s) else { const run_pdf_harness = b.addRunArtifact(image_harness); run_pdf_harness.addArtifactArg(c.exe); run_pdf_harness.addArtifactArg(c.image_addon); run_pdf_harness.addArg(if (c.platform == .gui) "gui" else "tui"); run_pdf_harness.addArg("pdf"); run_pdf_harness.addArtifactArg(c.pdf_addon.?); run_pdf_harness.has_side_effects = true; pdf_harness_step.dependOn(&run_pdf_harness.step); } const hxdiff = program(b, "pardes-hxdiff", "test/hxdiff.zig", c.target, c.optimize, &.{.{ .name = "pardes", .module = c.core_module }}); const run_hxdiff = b.addRunArtifact(hxdiff); run_hxdiff.has_side_effects = true; // Bare, it runs the checked-in suite (test/hxdiff.zig); `-- --parity` // runs each case in a file pane and a pty pane and compares the two. run_hxdiff.addPassthruArgs(); run_hxdiff.setCwd(b.path(".")); // the new line's indent per file type, against the reference helix // with its grammars (test/hxcases/hx_indent.py): before the suite, // whose count stays the last line const run_hxindent = b.addRunArtifact(hxdiff); run_hxindent.has_side_effects = true; run_hxindent.addArgs(&.{ "--strict", "test/hxcases/indent.jsonl", "test/hxcases/indent-goldens.jsonl" }); run_hxindent.setCwd(b.path(".")); run_hxdiff.step.dependOn(&run_hxindent.step); const hxdiff_step = b.step("hxdiff", "compare Helix results (-- --strict cases reference [waivers]; cases alone emits; --parity: a pty pane against a file pane)"); hxdiff_step.dependOn(&run_hxdiff.step); // The same cases on the library (src/lib_editor.zig): test/hxlib.zig // types them into an editor value and prints its results, which // hxdiff holds against helix's goldens, or with `-- --parity` // against the file pane, so the library cannot drift from either. const hxlib = program(b, "pardes-hxlib", "test/hxlib.zig", c.target, c.optimize, &.{}); hxlib.root_module.addImport("pardes-editor", core.editorModule(b, null, c.target, c.optimize, c.shell_cfg, .{ .themes = c.parts.themes, .ts = c.parts.ts, .uucode = c.parts.uucode, .mvzr = c.parts.mvzr, .vaxis = c.parts.vaxis })); const run_hxlib = b.addRunArtifact(hxlib); run_hxlib.addArgs(&.{ "test/hxcases/cases.jsonl", "test/hxcases/parity.jsonl" }); run_hxlib.setCwd(b.path(".")); run_hxlib.has_side_effects = true; const run_hxdiff_lib = b.addRunArtifact(hxdiff); run_hxdiff_lib.has_side_effects = true; run_hxdiff_lib.addPrefixedFileArg("--actual=", run_hxlib.captureStdOut(.{})); run_hxdiff_lib.addPassthruArgs(); run_hxdiff_lib.setCwd(b.path(".")); b.step("hxlib", "hxdiff's cases on the library, against helix's goldens (-- --parity: against the file pane)").dependOn(&run_hxdiff_lib.step); const helix_harness = b.option([]const u8, "helix-harness", "path to the reference hx-harness executable") orelse b.graph.environ_map.get("HX_HARNESS") orelse "hx-harness"; const reference = b.addSystemCommand(&.{helix_harness}); reference.addFileArg(b.path("test/hxcases/cases.jsonl")); reference.has_side_effects = true; const reference_results = reference.captureStdOut(.{}); const live_diff = b.addRunArtifact(hxdiff); live_diff.addArg("--strict"); live_diff.addFileArg(b.path("test/hxcases/cases.jsonl")); live_diff.addFileArg(reference_results); live_diff.addFileArg(b.path("test/hxcases/waivers.jsonl")); live_diff.has_side_effects = true; b.step("hxdiff-live", "compare the editor against a freshly run Helix reference").dependOn(&live_diff.step); const update_reference = b.addUpdateSourceFiles(); update_reference.addCopyFileToSource(reference_results, "test/hxcases/goldens.jsonl"); update_reference.step.dependOn(&live_diff.step); b.step("hxdiff-update", "replace checked-in Helix goldens after the live comparison passes").dependOn(&update_reference.step); // every helix-golf example, one case per step of its walkthrough const run_hxgolf = b.addRunArtifact(hxdiff); run_hxgolf.has_side_effects = true; run_hxgolf.addArgs(&.{ "--strict", "test/hxcases/golf.jsonl", "test/hxcases/golf-goldens.jsonl", "test/hxcases/golf-waivers.jsonl" }); run_hxgolf.setCwd(b.path(".")); b.step("hxgolf", "compare the helix-golf examples against their Helix results").dependOn(&run_hxgolf.step); const lspbench = program(b, "pardes-lspbench", "test/lspbench.zig", c.target, .fast, &.{.{ .name = "pardes", .module = c.core_module }}); const run_lspbench = b.addRunArtifact(lspbench); run_lspbench.addPassthruArgs(); run_lspbench.setCwd(b.path(".")); run_lspbench.has_side_effects = true; const lspbench_step = b.step("lspbench", "language-backend latency + feature matrix (-- [--json] [--check] [repo-root]), or one query (-- probe : [arg] [--reps N])"); lspbench_step.dependOn(&run_lspbench.step); const rollback_bench = program(b, "pardes-rollback-bench", "test/rollback_bench.zig", c.target, c.optimize, &.{.{ .name = "pardes", .module = c.core_module }}); const run_rollback_bench = b.addRunArtifact(rollback_bench); run_rollback_bench.has_side_effects = true; b.step("rollback-bench", "a replica's rollback copy (walked state, pinned blobs, walk time) for a typical and a big session, and a fork of the big one (-Doptimize=ReleaseFast)").dependOn(&run_rollback_bench.step); const replicas = program(b, "pardes-replicas", "test/replicas.zig", c.target, c.optimize, &.{ .{ .name = "pardes", .module = c.core_module }, .{ .name = "term", .module = c.parts.term } }); const run_replicas = b.addRunArtifact(replicas); run_replicas.addPassthruArgs(); run_replicas.has_side_effects = true; b.step("replicas", "one authority and several replicas in process, each client typing over a delayed link: digests at quiescence, rollback rate and costs per RTT (-- [--seeds N] [--steps M] [--clients C] [--rtt 30,80,207])").dependOn(&run_replicas.step); const locations_bench_step = b.step("locations-bench", "measure cold/warm result formatting (-Doptimize=ReleaseFast; CSV on stderr)"); if (c.optimize != .fast) { locations_bench_step.dependOn(&b.addFail("locations-bench requires -Doptimize=ReleaseFast").step); } else { const locations_bench_module = b.createModule(.{ .target = c.target, .optimize = .fast, .root_source_file = b.path("test/locations_bench.zig"), .link_libc = true, }); locations_bench_module.addImport("pardes", c.core_module); const locations_bench = b.addTest(.{ .root_module = locations_bench_module, .filters = &.{"locations benchmark cold and warm formatting"} }); const run_locations_bench = b.addRunArtifact(locations_bench); run_locations_bench.has_side_effects = true; locations_bench_step.dependOn(&run_locations_bench.step); } const perf_optimize: std.lang.Optimize = .fast; const perf_options = b.addOptions(); const perf_target = std.Target.Query.fromTarget(&c.target.result); perf_options.addOption(PerfBuild, "identity", .{ .compiler = builtin.zig_version_string, .target = perf_target.zigTriple(b.allocator) catch @panic("out of memory"), .cpu = perf_target.serializeCpuAlloc(b.allocator) catch @panic("out of memory"), .driver_optimize = @tagName(perf_optimize), .core_optimize = @tagName(c.optimize), .c_optimize = @tagName(c.c_optimize), .terminal_optimize = @tagName(c.ghostty_optimize), .platform = @tagName(c.platform), .grammars = @tagName(c.tree_sitter_grammars), .mupdf = c.pdf_addon != null, .jpx = c.enable_jpx, .tracy = c.tracy != null, .terminal_simd = c.ghostty_simd, .prebuilt_shaders = c.platform == .gui and c.prebuilt_shaders, }); perf_options.addOptionPath("image_addon", c.image_addon.getEmittedBin()); perf_options.addOption(bool, "has_pdf_addon", c.pdf_addon != null); if (c.pdf_addon) |l| perf_options.addOptionPath("pdf_addon", l.getEmittedBin()); const perf = program(b, "pardes-perf", "test/perf.zig", c.target, perf_optimize, &.{.{ .name = "pardes", .module = c.core_module }}); // --live drives the real binary through a pty (test/e2e_harness.zig). if (c.ghostty_vt_for_tests) |vt| perf.root_module.addImport("ghostty-vt", vt); perf.root_module.addOptions("perf_options", perf_options); const run_perf = b.addRunArtifact(perf); run_perf.has_side_effects = true; run_perf.addPassthruArgs(); run_perf.setCwd(b.path(".")); const perf_step = b.step("perf", "gesture latency or bounded terminal stress (-- [--json] [--reps N] [--base old.json] [--terminal-mib N])"); perf_step.dependOn(&run_perf.step); // The gate: the 50k-line file's gestures against the baseline this // build shape recorded, each within 3x of its best. Record one with // `zig build perf -Dplatform=P [-Doptimize=O] -- --only medium // --reps 10 --json > test/perf-baseline-P-O.json` (a build that is // known good). ponytail: the numbers are this machine's; another // machine records its own. const perf_gate = b.addRunArtifact(perf); perf_gate.has_side_effects = true; perf_gate.setCwd(b.path(".")); perf_gate.addArgs(&.{ "--only", "medium", "--reps", "10", "--base", b.fmt("test/perf-baseline-{t}-{t}.json", .{ c.platform, c.optimize }), "--max-ratio", "3" }); const perf_gate_step = b.step("perf-gate", "fail when a gesture on the 50k-line file takes over 3x its recorded baseline (test/perf-baseline--.json)"); perf_gate_step.dependOn(&perf_gate.step); const fs_bench = program(b, "pardes-fs-bench", "test/fs_bench.zig", c.target, .fast, &.{.{ .name = "pardes", .module = c.core_module }}); const run_fs_bench = b.addRunArtifact(fs_bench); run_fs_bench.has_side_effects = true; run_fs_bench.addPassthruArgs(); run_fs_bench.setCwd(b.path(".")); const fs_bench_step = b.step("fs-bench", "9P filesystem request cost and allocation count (-- [--json] [--reps N])"); fs_bench_step.dependOn(&run_fs_bench.step); // The syntax module's own tests and snapshots, on the core without // a terminal: its tests reach the editor's modes (modal.zig). var syntax_cfg = c.lean; syntax_cfg.root = "src/syntax.zig"; const syntax_module = core.module(b, c.parts, syntax_cfg); const syntax_runner = program(b, "pardes-syntax", "test/syntax.zig", c.target, c.optimize, &.{.{ .name = "syntax", .module = syntax_module }}); const run_syntax = b.addRunArtifact(syntax_runner); run_syntax.addPassthruArgs(); run_syntax.has_side_effects = true; b.step("syntax", "emit deterministic syntax snapshots (-- --bench: highlighting latency and allocations)").dependOn(&run_syntax.step); const syntax_test = b.addTest(.{ .root_module = syntax_module, .filters = if (c.test_filters.len == 0) &.{"syntax "} else c.test_filters, }); const run_syntax_test = b.addRunArtifact(syntax_test); run_syntax_test.has_side_effects = true; const syntax_test_step = b.step("syntax-test", "test syntax correctness without building the editor"); syntax_test_step.dependOn(&run_syntax_test.step); if (testMatch(b, c.unit_profile, c.test_filters, syntax_test_step)) |check| check.addArtifactArg(syntax_test); // The library as a package that depends on pardes is given it // (`library`): test/lib's own `zig build test`, over the packages // this checkout has fetched. const lib_test = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test", "--pkg-dir", "../../zig-pkg" }); lib_test.setCwd(b.path("test/lib")); lib_test.has_side_effects = true; const lib_test_step = b.step("lib-test", "build and test the library from a package that depends on pardes (test/lib)"); lib_test_step.dependOn(&lib_test.step); const history = b.addExecutable(.{ .name = "pardes-history", .root_module = b.createModule(.{ .target = c.target, .optimize = c.optimize, .root_source_file = b.path("test/history.zig"), }), }); const run_history = b.addRunArtifact(history); run_history.addPassthruArgs(); run_history.has_side_effects = true; b.step("history", "compare command output and test/build runtime across jj revisions").dependOn(&run_history.step); return .{ .perf_gate_step = perf_gate_step, .perf_options = perf_options, .lib_test_step = lib_test_step }; } /// The PDF benches: the core over the pdf addon, a pane of it. pub fn pdfBenches(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, core_module: *std.Build.Module, lib: *std.Build.Step.Compile, pointer_step: *std.Build.Step, scroll_step: *std.Build.Step) void { const config = b.addOptions(); config.addOption(bool, "release_fast_core", optimize == .fast); config.addOption([]const u8, "core_optimize", @tagName(optimize)); config.addOptionPath("pdf_addon", lib.getEmittedBin()); for ([_]struct { step: *std.Build.Step, exe: []const u8, file: []const u8, optimize: std.lang.Optimize }{ .{ .step = pointer_step, .exe = "pardes-pdf-pointer-bench", .file = "test/pdf_pointer_bench.zig", .optimize = .fast }, .{ .step = scroll_step, .exe = "pardes-pdf-scroll-bench", .file = "test/pdf_scroll_bench.zig", .optimize = optimize }, }) |bench| { const exe = b.addExecutable(.{ .name = bench.exe, .root_module = b.createModule(.{ .target = target, .optimize = bench.optimize, .root_source_file = b.path(bench.file), .link_libc = true }) }); exe.root_module.addImport("pardes", core_module); exe.root_module.addOptions("pdf_bench_config", config); const run = b.addRunArtifact(exe); run.addPassthruArgs(); run.setCwd(b.path(".")); bench.step.dependOn(&run.step); } }