From 76c55b5bce0f181b0b15b16596c900397dfef544 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 06:24:42 -0300 Subject: kinds #13, pdf.so: addons/pdf/pdf.c is the PDF kind against ABI 2.1 (its record the file's bytes, renders on jobs over two MuPDF lanes a cookie aborts, its strip laid out at `at` with crops and repainted rows for hover and selection, its text the body, search, links and PdfSections through look and text, P:F S places, PdfFit, PdfTint, PdfSections, its keys), pdf_bridge moves beside it, and the build makes it from position-independent MuPDF, FreeType and zlib exporting pardes_addon alone, installs /lib/pardes/addons/pdf.so, -Dmupdf now builds it or not and mupdf-check runs its addon-test script Co-Authored-By: Claude Fable 5.1 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- build.zig | 116 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 58 insertions(+), 58 deletions(-) (limited to 'build.zig') diff --git a/build.zig b/build.zig index 0e01cdec..f275475a 100644 --- a/build.zig +++ b/build.zig @@ -93,12 +93,14 @@ pub fn build(b: *std.Build) void { &.{f} else &.{}; - const enable_mupdf = b.option(bool, "mupdf", "PDF rendering with MuPDF (AGPL/commercial; default on; -Dmupdf=false disables)") orelse true; + const enable_mupdf = b.option(bool, "mupdf", "build the pdf addon, PDF panes over MuPDF (AGPL/commercial; default on; -Dmupdf=false leaves it out)") orelse true; 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")); // The docs (docs/typ, docs/site): each output is one typst run, and each // reads docs/typ/builtins-*.json; `builtins-json` refreshes this build's from the // registry first (the tool is wired below, once the core module is). @@ -152,17 +154,11 @@ pub fn build(b: *std.Build) void { 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 the committed shaders/prebuilt/*.spv instead of running glslc (default: on for a bare `zig build`, off when -Dplatform names a shell)") orelse also_gui; - const mupdf_check = b.step("mupdf-check", "compile, link, render, and search docs/design.pdf with MuPDF"); - const pdf_bench_step = b.step("pdf-bench", "benchmark real MuPDF page rendering (-- [--json] [--reps N] [--warmup N] [--path FILE] [--page N] [--pages N])"); - const pdf_sections_bench_step = b.step("pdf-sections-bench", "benchmark PDF outline/sections paths in ReleaseFast (-Doptimize=ReleaseFast -- [--json] [--reps N] [--warmup N])"); + 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])"); if (!enable_mupdf) mupdf_check.dependOn(&b.addFail("mupdf-check is unavailable with -Dmupdf=false").step); - if (!enable_mupdf) - pdf_bench_step.dependOn(&b.addFail("pdf-bench is unavailable with -Dmupdf=false").step); - if (!enable_mupdf) - pdf_sections_bench_step.dependOn(&b.addFail("pdf-sections-bench is unavailable with -Dmupdf=false").step); if (!enable_mupdf) pdf_scroll_bench_step.dependOn(&b.addFail("pdf-scroll-bench is unavailable with -Dmupdf=false").step); if (!enable_mupdf) @@ -239,7 +235,6 @@ pub fn build(b: *std.Build) void { // can name them all. The unconditional ones are ordinary consts below. var ts_mod: ?*std.Build.Module = null; var ts_queries_opts: ?*std.Build.Step.Options = null; - var mupdf_core_mod: ?*std.Build.Module = null; var ghostty_core_vt: ?*std.Build.Module = null; var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty; @@ -280,7 +275,6 @@ pub fn build(b: *std.Build) void { const shell_cfg: ShellConfig = .{ .tree_sitter_grammars = tree_sitter_grammars, .tracy = tracy != null, - .mupdf = enable_mupdf, .workspace_tag = workspace_tag, .prebuilt_shaders = prebuilt_shaders, .embed_sources = embed_sources, @@ -476,54 +470,50 @@ pub fn build(b: *std.Build) void { // hands back that same artifact rather than a second copy. const zlib = freetype_dep.builder.dependency("zlib", .{ .target = target, .optimize = c_optimize }).artifact("z"); + // The PDF addon (addons/pdf/pdf.c over MuPDF and pdf_bridge.c), built + // and installed as the image addon is. MuPDF is compiled against the + // build's FreeType and zlib and links them into the library, which holds + // its own copy of each beside a window's FreeType (docs/shared-libs.md); + // so all three are position-independent, and the library exports its + // `pardes_addon` alone. + var pdf_addon: ?*std.Build.Step.Compile = null; if (enable_mupdf) { if (b.lazyDependency("mupdf", .{})) |mupdf_dep| { + freetype.root_module.pic = true; + zlib.root_module.pic = true; const mupdf = mupdf_build.add(b, io, mupdf_dep, .{ .target = target, .optimize = c_optimize, .jpx = enable_jpx, .freetype = freetype, .zlib = zlib, + .pic = true, }); - // src/pdf.zig over this MuPDF: the core's, and pdf-bench's at - // ReleaseFast. - var pdf_mods: [2]*std.Build.Module = undefined; - for (&pdf_mods, [_]std.lang.Optimize{ optimize, .fast }) |*m, o| { - m.* = b.createModule(.{ .target = target, .optimize = o, .root_source_file = b.path("src/pdf.zig"), .link_libc = true }); - mupdf.linkTo(m.*); - m.*.addImport("c_heap", c_heap_mod); - m.*.addImport("pdf_c", b.addTranslateC(.{ .root_source_file = b.path("src/pdf_bridge.h"), .target = target, .optimize = o }).createModule()); - m.*.addIncludePath(b.path("src")); - m.*.addCSourceFile(.{ .file = b.path("src/pdf_bridge.c"), .flags = &.{"-std=gnu11"} }); - } - const mupdf_mod = pdf_mods[0]; - mupdf_core_mod = mupdf_mod; - const mupdf_test = b.addTest(.{ .root_module = mupdf_mod }); - const run_mupdf_test = b.addRunArtifact(mupdf_test); - run_mupdf_test.has_side_effects = true; - mupdf_check.dependOn(&run_mupdf_test.step); + const lib = b.addLibrary(.{ .name = "pdf", .linkage = .dynamic, .root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }) }); + mupdf.linkTo(lib.root_module); + lib.root_module.addIncludePath(b.path("src/kinds")); + lib.root_module.addIncludePath(b.path("addons/pdf")); + lib.root_module.addCSourceFile(.{ .file = b.path("addons/pdf/pdf.c"), .flags = &.{ "-std=gnu11", "-Wall", "-Wextra", "-Werror" } }); + lib.root_module.addCSourceFile(.{ .file = b.path("addons/pdf/pdf_bridge.c"), .flags = &.{"-std=gnu11"} }); + lib.setVersionScript(b.addWriteFiles().add("pdf.map", "{ global: pardes_addon; local: *; };\n")); + pdf_addon = lib; mupdf_check.dependOn(mupdf_build.addProbe(b, mupdf, .{ .target = target, .pdf = b.path("docs/design.pdf"), })); - // The PDF benches: pdf-bench MuPDF's own rendering at - // ReleaseFast, the others the core over it. + // The PDF benches: the core over the addon, a pane of it. const config = b.addOptions(); config.addOption(bool, "release_fast_core", optimize == .fast); config.addOption([]const u8, "core_optimize", @tagName(optimize)); - for ([_]struct { step: *std.Build.Step, exe: []const u8, file: []const u8, optimize: std.lang.Optimize, config: ?[]const u8 = null }{ - .{ .step = pdf_bench_step, .exe = "pardes-pdf-bench", .file = "test/pdf_bench.zig", .optimize = .fast }, - .{ .step = pdf_sections_bench_step, .exe = "pardes-pdf-sections-bench", .file = "test/pdf_sections_bench.zig", .optimize = .fast, .config = "pdf_sections_bench_config" }, + config.addOptionPath("pdf_addon", lib.getEmittedBin()); + for ([_]struct { step: *std.Build.Step, exe: []const u8, file: []const u8, optimize: std.lang.Optimize }{ .{ .step = pdf_pointer_bench_step, .exe = "pardes-pdf-pointer-bench", .file = "test/pdf_pointer_bench.zig", .optimize = .fast }, - .{ .step = pdf_scroll_bench_step, .exe = "pardes-pdf-scroll-bench", .file = "test/pdf_scroll_bench.zig", .optimize = optimize, .config = "pdf_scroll_bench_config" }, + .{ .step = pdf_scroll_bench_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 }) }); - if (bench.step == pdf_bench_step) exe.root_module.addImport("mupdf", pdf_mods[1]) else { - exe.root_module.addImport("pardes", core_module); - exe.root_module.addImport("mupdf", mupdf_mod); - } - if (bench.config) |name| exe.root_module.addOptions(name, config); + 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(".")); @@ -843,7 +833,6 @@ pub fn build(b: *std.Build) void { .zstbi = zstbi_mod, .mvzr = mvzr_mod, .c_heap = c_heap_mod, - .mupdf = mupdf_core_mod, .ghostty_vt = ghostty_core_vt, .vaxis = vaxis_mod, .uucode = uucode_mod, @@ -1135,14 +1124,16 @@ pub fn build(b: *std.Build) void { }); installHome(b, exe, home_bin); if (target.result.os.tag == .linux and !static) { - b.getInstallStep().dependOn(&b.addInstallFileWithDir(image_addon.getEmittedBin(), .lib, "pardes/addons/image.so").step); - if (home_bin) |dir| { - const copy = b.addSystemCommand(&.{ "install", "-Dm755" }); - copy.addFileArg(image_addon.getEmittedBin()); - copy.addArg(b.pathJoin(&.{ dir, "..", "lib", "pardes", "addons", "image.so" })); - copy.has_side_effects = true; - b.getInstallStep().dependOn(©.step); - } + for ([_]?*std.Build.Step.Compile{ image_addon, pdf_addon }, [_][]const u8{ "image.so", "pdf.so" }) |lib, name| if (lib) |l| { + b.getInstallStep().dependOn(&b.addInstallFileWithDir(l.getEmittedBin(), .lib, b.fmt("pardes/addons/{s}", .{name})).step); + if (home_bin) |dir| { + const copy = b.addSystemCommand(&.{ "install", "-Dm755" }); + copy.addFileArg(l.getEmittedBin()); + copy.addArg(b.pathJoin(&.{ dir, "..", "lib", "pardes", "addons", name })); + copy.has_side_effects = true; + b.getInstallStep().dependOn(©.step); + } + }; } if (gui_mod) |gm| { var gui_name: []const u8 = "pardes-gui"; @@ -1170,9 +1161,10 @@ pub fn build(b: *std.Build) void { iso_step.dependOn(&run_iso.step); } + const addons: []const *std.Build.Step.Compile = if (pdf_addon) |pdf| b.allocator.dupe(*std.Build.Step.Compile, &.{ image_addon, pdf }) catch @panic("OOM") else &.{image_addon}; snap_build.addTty(b, .{ .exe = exe, - .image_addon = image_addon, + .addons = addons, .target = target, .optimize = optimize, .ghostty_vt = ghostty_vt_for_snap, @@ -1236,12 +1228,13 @@ pub fn build(b: *std.Build) void { 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 (enable_mupdf) { + if (pdf_addon != null) { const run_pdf_harness = b.addRunArtifact(image_harness); run_pdf_harness.addArtifactArg(exe); run_pdf_harness.addArtifactArg(image_addon); run_pdf_harness.addArg(if (platform == .gui) "gui" else "tui"); run_pdf_harness.addArg("pdf"); + run_pdf_harness.addArtifactArg(pdf_addon.?); run_pdf_harness.has_side_effects = true; pdf_harness_step.dependOn(&run_pdf_harness.step); } else { @@ -1372,6 +1365,8 @@ pub fn build(b: *std.Build) void { .prebuilt_shaders = platform == .gui and prebuilt_shaders, }); perf_options.addOptionPath("image_addon", image_addon.getEmittedBin()); + perf_options.addOption(bool, "has_pdf_addon", pdf_addon != null); + if (pdf_addon) |l| perf_options.addOptionPath("pdf_addon", l.getEmittedBin()); const perf = b.addExecutable(.{ .name = "pardes-perf", .root_module = b.createModule(.{ @@ -1528,9 +1523,11 @@ pub fn build(b: *std.Build) void { pane_module.addImport("pardes_config", core_module.import_table.get("pardes_config").?); const test_addons = b.addOptions(); test_addons.addOptionPath("image", image_addon.getEmittedBin()); + test_addons.addOption(bool, "has_pdf", pdf_addon != null); + if (pdf_addon) |l| test_addons.addOptionPath("pdf", l.getEmittedBin()); pane_module.addOptions("test_addons", test_addons); const pane_tests = b.addTest(.{ .root_module = pane_module, .filters = test_filters }); - _ = units.add(b, pane_tests, b.step("pane-test", "run pane, output, PDF and namespace integration tests")); + _ = units.add(b, pane_tests, b.step("pane-test", "run pane, output and namespace integration tests")); // The behavioural tests (docs/next-tests.md): their module has the // session alone, so none can reach past what a 9P client sees. @@ -1572,6 +1569,8 @@ pub fn build(b: *std.Build) void { if (std.mem.eql(u8, variant[0], "fake_addon")) fake_addon_lib = lib; } session_options.addOptionPath("image_addon", image_addon.getEmittedBin()); + session_options.addOption(bool, "has_pdf_addon", pdf_addon != null); + if (pdf_addon) |l| session_options.addOptionPath("pdf_addon", l.getEmittedBin()); session_module.addOptions("session_options", session_options); const behaviour_module = b.createModule(.{ .target = target, @@ -1589,21 +1588,26 @@ pub fn build(b: *std.Build) void { // An addon's own test: `--addon-test` over the fake (the template) // and the snake (a shipped example, built but not installed), each // with its script, compared with what it printed when written. - const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake and snake addons, against addons/*/*.out"); + const addon_test_step = b.step("addon-test", "pardes --addon-test over the fake, snake and pdf addons, against addons/*/*.out"); const snake_lib = b.addLibrary(.{ .name = "snake", .linkage = .dynamic, .root_module = b.createModule(.{ .target = target, .optimize = optimize, .link_libc = true }) }); snake_lib.root_module.addIncludePath(b.path("src/kinds")); snake_lib.root_module.addCSourceFile(.{ .file = b.path("addons/snake/snake.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } }); - for ([_]struct { *std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" } }) |t| { + // ...and the pdf addon's, which mupdf-check runs too (its script + // opens docs/design.pdf, from the build root). + const libs = [_]struct { ?*std.Build.Step.Compile, []const u8 }{ .{ fake_addon_lib.?, "addons/fake/fake" }, .{ snake_lib, "addons/snake/snake" }, .{ pdf_addon, "addons/pdf/pdf" } }; + for (libs) |t| if (t[0]) |lib| { const run_addon_test = b.addRunArtifact(exe); + run_addon_test.setCwd(b.path(".")); run_addon_test.addArg("--addon-test"); - run_addon_test.addArtifactArg(t[0]); + run_addon_test.addArtifactArg(lib); run_addon_test.addFileArg(b.path(b.fmt("{s}.script", .{t[1]}))); const compare = b.addSystemCommand(&.{ "diff", "-u" }); compare.addFileArg(b.path(b.fmt("{s}.out", .{t[1]}))); compare.addFileArg(run_addon_test.captureStdOut(.{})); behaviour_test_step.dependOn(&compare.step); addon_test_step.dependOn(&compare.step); - } + if (lib == pdf_addon) mupdf_check.dependOn(&compare.step); + }; // The 9P monkey at T1 (test/monkey9p.zig): random requests against // an in-process session, the documented rules checked after each. // Its fixed-seed smoke run is part of behaviour-test. @@ -1891,7 +1895,6 @@ fn gitCommit(b: *std.Build) ?[]const u8 { const ShellConfig = struct { tree_sitter_grammars: TreeSitterGrammars, tracy: bool, - mupdf: bool, /// null = "the platform's own default": the macOS shell hands the row to /// its native menu bar, every other shell keeps the acme topbar. workspace_tag: ?bool, @@ -1908,7 +1911,6 @@ fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build. o.addOption(bool, "syntax_minimal_grammars", cfg.tree_sitter_grammars == .minimal or cfg.tree_sitter_grammars == .full); o.addOption(bool, "syntax_full_grammars", cfg.tree_sitter_grammars == .full); o.addOption(bool, "enable_tracy", cfg.tracy); - o.addOption(bool, "mupdf", cfg.mupdf); o.addOption(bool, "workspace_tag", cfg.workspace_tag orelse (platform != .macos)); o.addOption(bool, "embed_sources", cfg.embed_sources); o.addOption([]const u8, "version", display_version); @@ -1973,7 +1975,6 @@ fn wireCore(m: *std.Build.Module, d: struct { zstbi: ?*std.Build.Module = null, mvzr: ?*std.Build.Module = null, c_heap: ?*std.Build.Module = null, - mupdf: ?*std.Build.Module = null, ghostty_vt: ?*std.Build.Module = null, vaxis: ?*std.Build.Module = null, uucode: ?*std.Build.Module = null, @@ -1988,7 +1989,6 @@ fn wireCore(m: *std.Build.Module, d: struct { if (d.zstbi) |x| m.addImport("zstbi", x); if (d.mvzr) |x| m.addImport("mvzr", x); if (d.c_heap) |x| m.addImport("c_heap", x); - if (d.mupdf) |x| m.addImport("mupdf", x); if (d.ghostty_vt) |x| m.addImport("ghostty-vt", x); if (d.vaxis) |x| m.addImport("vaxis", x); if (d.uucode) |x| m.addImport("uucode", x); -- cgit v1.3