const std = @import("std"); const builtin = @import("builtin"); const core = @import("build/core.zig"); const docs = @import("build/docs.zig"); const gui = @import("build/gui.zig"); const addons = @import("build/addons.zig"); const macos = @import("build/macos.zig"); const tools = @import("build/tools.zig"); const tests = @import("build/tests.zig"); const process_build = @import("build/process.zig"); /// The package manifest, imported untyped (build/core.zig has the reason). const zon = @import("build.zig.zon"); pub const Platform = enum { tui, gui, macos }; pub const PerfBuild = struct { compiler: []const u8, target: []const u8, cpu: []const u8, driver_optimize: []const u8, core_optimize: []const u8, c_optimize: []const u8, terminal_optimize: []const u8, platform: []const u8, grammars: []const u8, mupdf: bool, jpx: bool, tracy: bool, terminal_simd: bool, prebuilt_shaders: bool, }; pub const macos_min_version: std.SemanticVersion = .{ .major = 13, .minor = 0, .patch = 0 }; pub const TreeSitterGrammars = enum { disabled, zig, minimal, full }; pub fn build(b: *std.Build) void { // A package that depends on pardes takes its library; nothing of the // editor is configured for it. if (!b.isRoot()) return @import("build/library.zig").build(b); // This configure reads the environment (HOME, XDG_DATA_HOME, // HX_HARNESS), the clock (-Dtest-rebuild) and vendor/themes and // vendor/queries without telling the configure cache, so it is never // cached (REORG decision 20; typst's lookup did this before it moved to // make time). b.graph.poisonCache(); const requested_platform = b.option(Platform, "platform", "which shell to build (tui, gui, macos); absent builds the tui cli and the SDL gui together"); const platform = requested_platform orelse .tui; const also_gui = requested_platform == null; // A bare `zig build` also copies the binaries into ~/.local/bin, and the // addon steps theirs into the user's data directory. Zig 0.17 keeps the // prefix in the maker process, out of this script's reach, so `--prefix` // no longer turns it off: -Dhome-install=false does. With -Dplatform // neither is copied, but the addons with -Dhome-install=true. const home_option = b.option(bool, "home-install", "copy into the user directories: a bare `zig build` the binaries and addons, -Dplatform the addons only when true (default: true for a bare `zig build`)"); const home_bin: ?[]const u8 = if (also_gui and (home_option orelse true)) if (b.graph.environ_map.get("HOME")) |home| b.pathJoin(&.{ home, ".local", "bin" }) else null else null; const home_addons: ?[]const u8 = if (!(home_option orelse also_gui)) null else if (b.graph.environ_map.get("XDG_DATA_HOME")) |data| b.pathJoin(&.{ data, "pardes", "addons" }) else if (b.graph.environ_map.get("HOME")) |home| b.pathJoin(&.{ home, ".local", "share", "pardes", "addons" }) else null; const home_install = home_bin != null; const target = b.standardTargetOptions(.{ .default_target = switch (platform) { .macos => if (builtin.os.tag.isDarwin()) .{ .cpu_arch = builtin.target.cpu.arch, .os_tag = .macos, .os_version_min = .{ .semver = macos_min_version }, } else .{}, .tui, .gui => .{ .cpu_arch = .x86_64, .os_tag = .linux, .abi = .gnu, .glibc_version = .{ .major = 2, .minor = 38, .patch = 0 }, }, }, }); const optimize = b.standardOptimizeOption(.{}); // one substring, because that is what a dev loop needs: see the unit-test // step for why the whole-binary run is worth narrowing const test_filters: []const []const u8 = if (b.option([]const u8, "test-filter", "run only tests whose name contains this")) |f| &.{f} else &.{}; const enable_jpx = b.option(bool, "jpx", "JPEG 2000 in PDFs, for scanned documents (default on; -Djpx=false drops openjpeg)") orelse true; const embed_sources = b.option(bool, "embed-sources", "serve the editor's own sources under /src of its 9P tree (default off)") orelse false; const test_build = b.step("test-build", "compile unit-test programs without running them"); const doc_typ_opts = b.addOptions(); doc_typ_opts.addOptionPathUntracked("dir", b.path("docs/typ")); // The words the shipped addons bring (PdfFit...), which the docs name too. doc_typ_opts.addOptionPathUntracked("addons", b.path("addons")); const doc_renders = docs.add(b); const test_nonce: ?*std.Build.Module = if (b.option(bool, "test-rebuild", "force fresh Zig test compilation while retaining cached C dependencies") orelse false) blk: { const files = b.addTempFiles(); const source = files.add("test_nonce.zig", b.fmt("pub const nonce = {d};\n", .{std.Io.Clock.awake.now(b.graph.io).nanoseconds})); break :blk b.createModule(.{ .root_source_file = source, .target = target, .optimize = optimize }); } else null; const unit_profile = b.addExecutable(.{ .name = "pardes-unit-profile", .root_module = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("test/unit_profile.zig"), }), }); const c_optimize: std.lang.Optimize = if (optimize == .debug) .fast else optimize; const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: disabled, zig, minimal (c/c++/zig), full") orelse .full; const tracy = b.option([]const u8, "tracy", "enable Tracy profiling; supply the path to a Tracy source checkout"); const macos_sdk = b.option([]const u8, "macos-sdk", "the macOS SDK a gui built for macOS from another system takes SDL's headers and frameworks from"); const macos_identity = b.option([]const u8, "macos-identity", "codesigning identity for pardes.app (default: ad-hoc)") orelse "-"; const prebuilt_shaders = b.option(bool, "prebuilt-shaders", "embed sketchbook's committed dev/draw/shaders/prebuilt/*.spv instead of running glslc over its GLSL (default: on for a bare `zig build`, off when -Dplatform names a shell)") orelse also_gui; // The macOS shell carries the workspace tag row's commands in its native // menu bar; the row itself goes away. `-Dworkspace-tag=true` puts it back. const workspace_tag = b.option(bool, "workspace-tag", "draw the workspace tag row (default: on except on macOS, whose menu bar carries its commands)"); // The commit is a build input: every `jj new` or `jj commit` moves HEAD, // changes the options module and so recompiles every binary that imports // it, which is all of them (~0.6 GB of fresh test objects per move). Only // builds someone keeps are stamped: release builds and the bare // `zig build` that installs into ~/.local. Every other Debug build (any // -Dplatform or --prefix, so every test and snapshot run) prints // `pardes ` alone, like a tarball build; -Dstamp-commit=true // stamps one anyway. const stamp_commit = b.option(bool, "stamp-commit", "embed the git commit in --version and crash records (default: release builds and the ~/.local install)") orelse (optimize != .debug or home_install); // MuPDF, and the FreeType and zlib it is built against, are lazy: a // build asks for them only when it builds the pdf addon (or a window, // FreeType). Zig 0.17's configure is told the -D options and never the // steps asked for, so "the pdf step's" is an option, on once MuPDF is // fetched (and off Linux, where the addons are compiled in): a fresh // checkout's plain build fetches none of the three, and -Dpdf fetches // MuPDF once. const pdf = b.option(bool, "pdf", "build the pdf addon over MuPDF (default: on once MuPDF is fetched, as -Dpdf does)") orelse (std.Build.package_map.get(zon.dependencies.mupdf.hash).?.available or target.result.os.tag != .linux); const mupdf_check = b.step("mupdf-check", "the pdf addon's check: MuPDF compiles, links, renders and searches docs/design.pdf, and its script runs"); const pdf_pointer_bench_step = b.step("pdf-pointer-bench", "time PDF hover and selection gestures and prove their pixels (-Doptimize=ReleaseFast -- [--label L] [--reps N] [--path FILE] [--cell WxH])"); const pdf_scroll_bench_step = b.step("pdf-scroll-bench", "benchmark fast continuous-strip PDF scrolling in ReleaseFast (-Doptimize=ReleaseFast -- [--json] [--reps N] [--warmup N] [--path FILE])"); const shell_cfg: core.ShellConfig = .{ .tree_sitter_grammars = tree_sitter_grammars, .tracy = tracy != null, .workspace_tag = workspace_tag, .prebuilt_shaders = prebuilt_shaders, .embed_sources = embed_sources, .commit = if (stamp_commit) core.gitCommit(b) else null, .change = if (stamp_commit) core.jjChange(b) else null, .built = if (stamp_commit) core.command(b, &.{ "date", "-u", "+%Y-%m-%d %H:%M UTC" }) else null, }; // What every core is made of, the shells' options and the addons // (build/core.zig). const a = core.assemble(b, .{ .target = target, .optimize = optimize, .c_optimize = c_optimize, .tree_sitter_grammars = tree_sitter_grammars, .tracy = tracy, .shell_cfg = shell_cfg, .platform = platform, .also_gui = also_gui, .pdf = pdf, .enable_jpx = enable_jpx, .doc_typ = doc_typ_opts, .mupdf_check = mupdf_check, .pdf_pointer_bench_step = pdf_pointer_bench_step, .pdf_scroll_bench_step = pdf_scroll_bench_step, }); const parts = a.parts; const opts = a.opts; // Off Linux, whose memfd Addon.zig's loader needs, every core has the // addons compiled in. const compiled_in = target.result.os.tag != .linux; // The macOS shell is a library whose host owns main(): it roots at // its own flat C ABI instead of src/main.zig. Tracy's client and BSD // libutil go to the shell (and Tracy's to the core) alone, as before // core.module. const root_mod = core.module(b, parts, .{ .root = if (platform == .macos) "src/macos.zig" else "src/main.zig", .config = opts, .builtin_addons = compiled_in, .tracy = true, .util = true }); // hxdiff's headless core and pardes-isolate are the primary shell's // configuration compiled two more ways, not two more frontends. const core_cfg: core.Core = .{ .root = "src/pardes.zig", .config = opts, .builtin_addons = compiled_in, .tracy = true }; const core_module = core.module(b, parts, core_cfg); // The core without the terminal module (docs/next-terminal.md, Phase 1): // what core-test is compiled from. No ghostty-vt, wuffs, zstbi or C++; // a shell boot is a scratch pane, a test that needs a terminal skips, // and shell-test runs them all with one. const lean_cfg: core.Core = .{ .root = "src/pardes.zig", .config = opts, .builtin_addons = compiled_in, .terminal = false }; const core_lean = core.module(b, parts, lean_cfg); const isolated_mod: ?*std.Build.Module = if (platform == .tui) core.module(b, parts, .{ .root = "src/main.zig", .config = opts, .builtin_addons = compiled_in, .isolated = true }) else null; // `zig build static`'s: the addons compiled in (Addon.zig). Linked // statically on musl alone, as glibc links only dynamically; the gui's // is dynamic (SDL and the GPU need the display's libraries) with the // addons in it all the same. const static_linkage: ?std.builtin.LinkMode = if (target.result.isMuslLibC()) .static else null; const static_mod: ?*std.Build.Module = if ((platform == .tui and static_linkage != null) or platform == .gui) core.module(b, parts, .{ .root = "src/main.zig", .config = opts, .builtin_addons = true }) else null; const gui_mod: ?*std.Build.Module = if (also_gui) core.module(b, parts, .{ .root = "src/main.zig", .config = a.gui_opts.?, .builtin_addons = compiled_in }) else null; if (a.pdf.lib) |lib| tools.pdfBenches(b, target, optimize, core_module, lib, pdf_pointer_bench_step, pdf_scroll_bench_step); const builtins_json_step = docs.builtinsJson(b, requested_platform, platform, target, optimize, core_module, doc_renders); // The window's shells: the bare build's pardes-gui; -Dplatform=gui's // pardes-gui and its static. var sketchbook: ?*std.Build.Dependency = null; if (also_gui or platform == .gui) { const window = gui.make(b, target, optimize, macos_sdk, prebuilt_shaders, a.freetype); for ([_]?*std.Build.Module{ gui_mod, if (platform == .gui) root_mod else null, if (platform == .gui) static_mod else null }) |m| if (m) |gm| window.wire(gm); sketchbook = window.sketchbook; } const snake_shader_lib = addons.gpu(b, target, optimize, a.addon_mod, sketchbook); if (platform == .macos) macos.add(b, .{ .macos_identity = macos_identity, .optimize = optimize, .root_mod = root_mod, .target = target, .test_build = test_build, .test_filters = test_filters, .test_nonce = test_nonce, .unit_profile = unit_profile, }) else { var exe_name: []const u8 = if (platform == .gui) "pardes-gui" else "pardes"; if (target.result.os.tag != .linux or target.result.cpu.arch != .x86_64) exe_name = b.fmt("{s}-{s}-{s}", .{ exe_name, @tagName(target.result.os.tag), @tagName(target.result.cpu.arch) }); const exe = b.addExecutable(.{ .name = exe_name, .root_module = root_mod, }); installHome(b, exe, home_bin); if (requested_platform == .tui) docs.commandLine(b, exe, builtins_json_step, doc_renders); // The addons, a step each: the default install is the binaries // alone, and `zig build image pdf` (or `addons`) puts the libraries // where the start scan finds them, /lib/pardes/addons (and // $XDG_DATA_HOME/pardes/addons as home_addons says). const addons_step = b.step("addons", "install the image and pdf addons"); for ([_]?*std.Build.Step.Compile{ a.image_addon, a.pdf.lib }, [_][]const u8{ "image", "pdf" }) |lib, name| { const step = b.step(name, b.fmt("install the {s} addon, /lib/pardes/addons/{s}.so", .{ name, name })); addons_step.dependOn(step); const l = lib orelse { step.dependOn(a.pdf.no_pdf.?); continue; }; step.dependOn(&b.addInstallFileWithDir(l.getEmittedBin(), .lib, b.fmt("pardes/addons/{s}.so", .{name})).step); if (home_addons) |dir| { const copy = b.addUpdateSourceFiles(); copy.addCopyFileToSource(l.getEmittedBin(), b.pathJoin(&.{ dir, b.fmt("{s}.so", .{name}) })); step.dependOn(©.step); } } if (gui_mod) |gm| { var gui_name: []const u8 = "pardes-gui"; if (target.result.os.tag != .linux or target.result.cpu.arch != .x86_64) gui_name = b.fmt("{s}-{s}-{s}", .{ gui_name, @tagName(target.result.os.tag), @tagName(target.result.cpu.arch) }); const gui_exe = b.addExecutable(.{ .name = gui_name, .root_module = gm, }); installHome(b, gui_exe, home_bin); } // One static pardes, never the default: the terminal's, both // addons compiled in and registered at start as if loaded. It is // also how a build for macOS, whose loader has no memfd, could have // image and PDF panes. const static_step = b.step("static", "a pardes with the image and pdf addons in it: the terminal's static (-Dplatform=tui -Dtarget=x86_64-linux-musl), the gui's dynamic (-Dplatform=gui)"); if (a.pdf.no_pdf) |s| static_step.dependOn(s) else if (static_mod) |m| static_step.dependOn(&b.addInstallArtifact(b.addExecutable(.{ .name = exe_name, .linkage = if (platform == .gui) null else static_linkage, .root_module = m }), .{}).step) else static_step.dependOn(&b.addFail("static is the terminal's on musl (-Dplatform=tui -Dtarget=x86_64-linux-musl) or the gui's (-Dplatform=gui)").step); const run = b.addRunArtifact(exe); b.step("run", "descend to the pardes").dependOn(&run.step); if (isolated_mod) |iso| { const iso_exe = b.addExecutable(.{ .name = "pardes-isolate", .root_module = iso, }); const run_iso = b.addRunArtifact(iso_exe); // Something to look at on arrival, read out of the binary itself. run_iso.addPassthruArgs(); const iso_step = b.step("run-isolated", "descend to a pardes with no host but the terminal"); iso_step.dependOn(&run_iso.step); } const addon_libs: []const *std.Build.Step.Compile = if (a.pdf.lib) |l| b.allocator.dupe(*std.Build.Step.Compile, &.{ a.image_addon, l }) catch @panic("OOM") else &.{a.image_addon}; // The scripted language server the tests' sessions and the tty cases ask. const lspmock = tools.program(b, "pardes-lspmock", "test/lspmock.zig", target, optimize, &.{}); process_build.add(b, .{ .exe = exe, .lspmock = lspmock, .addons = addon_libs, .target = target, .optimize = optimize, .ghostty_vt = a.ghostty_vt, .cloud9 = parts.cloud9, .gui = platform == .gui, }); const t = tools.add(b, .{ .c_optimize = c_optimize, .core_module = core_module, .enable_jpx = enable_jpx, .exe = exe, .ghostty_optimize = a.ghostty_optimize, .ghostty_simd = a.ghostty_simd, .ghostty_vt_for_tests = a.ghostty_vt, .image_addon = a.image_addon, .lean = lean_cfg, .no_pdf = a.pdf.no_pdf, .optimize = optimize, .parts = parts, .pdf_addon = a.pdf.lib, .platform = platform, .prebuilt_shaders = prebuilt_shaders, .shell_cfg = shell_cfg, .target = target, .test_filters = test_filters, .tracy = tracy, .tree_sitter_grammars = tree_sitter_grammars, .unit_profile = unit_profile, }); tests.add(b, .{ .addon_mod = a.addon_mod, .core_lean = core_lean, .core_module = core_module, .core_cfg = core_cfg, .exe = exe, .ghostty_vt_for_tests = a.ghostty_vt, .image_addon = a.image_addon, .lspmock = lspmock, .mupdf_check = mupdf_check, .optimize = optimize, .parts = parts, .pdf_addon = a.pdf.lib, .platform = platform, .root_mod = root_mod, .snake_shader_lib = snake_shader_lib, .target = target, .test_build = test_build, .test_filters = test_filters, .test_nonce = test_nonce, .tools = t, .unit_profile = unit_profile, }); } // The test-build programs installed rather than run, for a machine the // build cannot run them on (`-Dtarget=x86_64-freebsd.15.0`): each into // /test, named for its root file. const test_install = b.step("test-install", "install the test-build programs into /test, to run elsewhere"); for (test_build.dependencies.items) |dep| { const t = dep.cast(std.Build.Step.Compile) orelse continue; const name = switch (t.root_module.root_source_file orelse continue) { .src_path => |p| std.fs.path.stem(p.sub_path), else => t.name, }; test_install.dependOn(&b.addInstallArtifact(t, .{ .dest_dir = .{ .override = .{ .custom = "test" } }, .dest_sub_path = name }).step); } } /// Installs `artifact` in the prefix and, given `home_bin`, copies it there /// too (`install` unlinks first, so a running binary is replaced). fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]const u8) void { b.installArtifact(artifact); const dir = home_bin orelse return; const copy = b.addSystemCommand(&.{ "install", "-Dm755" }); copy.addArtifactArg(artifact); copy.addArg(b.pathJoin(&.{ dir, artifact.name })); copy.has_side_effects = true; b.getInstallStep().dependOn(©.step); }