diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 13:39:00 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 14:24:59 -0300 |
| commit | 31706a1d1c13c26c69f67f107b72179019cbd255 (patch) | |
| tree | 9cb7ee1e529155d4a80c9409fd1104130588647e | |
| parent | 0f81de017ca7b52e951bb70f6f5f3e8c88b425e6 (diff) | |
| download | pardes-31706a1d1c13c26c69f67f107b72179019cbd255.tar.gz pardes-31706a1d1c13c26c69f67f107b72179019cbd255.zip | |
kinds #26, one static pardes: `zig build static -Dplatform=tui -Dtarget=x86_64-linux-musl` (musl's, as glibc links only dynamically; never the default, and -Dstatic gone) links the terminal binary statically with both addons compiled in, image.zig as a module and pdf.c with MuPDF, its export renamed by a -D, which Addon.zig registers at start as if loaded (/addons says builtin, Addon reload refuses one, no library is scanned), and execveat and close_range go by their syscalls, which musl has no wrappers for; macOS's way to image and PDF panes too, say a comment, building.typ, shared-libs.md and the changelog
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
| -rw-r--r-- | addons/image/image.zig | 2 | ||||
| -rw-r--r-- | build.zig | 44 | ||||
| -rw-r--r-- | docs/shared-libs.md | 12 | ||||
| -rw-r--r-- | docs/typ/building.typ | 2 | ||||
| -rw-r--r-- | src/Addon.zig | 21 | ||||
| -rw-r--r-- | src/CHANGELOG.md | 4 | ||||
| -rw-r--r-- | src/daemon.zig | 4 | ||||
| -rw-r--r-- | src/upgrade.zig | 10 |
8 files changed, 83 insertions, 16 deletions
diff --git a/addons/image/image.zig b/addons/image/image.zig index 824d708f..fbcc501d 100644 --- a/addons/image/image.zig +++ b/addons/image/image.zig @@ -105,7 +105,7 @@ pub const Image = struct { } }; -export const pardes_addon = addon.ops(Image); +pub export const pardes_addon = addon.ops(Image); test "a job decodes a picture to its size and pixels, and one past max_dim is scaled to fit" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); @@ -86,7 +86,6 @@ pub fn build(b: *std.Build) void { }, }); const optimize = b.standardOptimizeOption(.{}); - const static = b.option(bool, "static", "statically link") orelse false; // 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| @@ -195,6 +194,15 @@ pub fn build(b: *std.Build) void { .link_libc = true, }) else null; + // `zig build static`'s: the terminal's, the addons compiled in + // (Addon.zig); musl's alone, as glibc links only dynamically. + const static_mod: ?*std.Build.Module = if (platform == .tui and target.result.isMuslLibC()) b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("src/main.zig"), + .link_libc = true, + }) else null; + const gui_mod: ?*std.Build.Module = if (also_gui) b.createModule(.{ .target = target, .optimize = optimize, @@ -203,8 +211,8 @@ pub fn build(b: *std.Build) void { }) else null; const CoreMod = struct { mod: ?*std.Build.Module, shell: Platform }; - var core_mods_buf: [4]*std.Build.Module = undefined; - var core_shells_buf: [4]Platform = undefined; + var core_mods_buf: [5]*std.Build.Module = undefined; + var core_shells_buf: [5]Platform = undefined; var core_mods_len: usize = 0; // root_mod FIRST: the module map below is folded out of its import table. for ([_]CoreMod{ @@ -213,6 +221,7 @@ pub fn build(b: *std.Build) void { // configuration compiled two more ways, not two more frontends. .{ .mod = core_module, .shell = platform }, .{ .mod = isolated_mod, .shell = platform }, + .{ .mod = static_mod, .shell = platform }, .{ .mod = gui_mod, .shell = .gui }, }) |entry| if (entry.mod) |m| { core_mods_buf[core_mods_len] = m; @@ -483,6 +492,17 @@ pub fn build(b: *std.Build) void { 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; + // ...and for a static pardes the same C compiled into it, its + // export renamed: it is one of two in a binary (Addon.zig). + if (static_mod) |m| { + const in = b.addLibrary(.{ .name = "pdf-builtin", .linkage = .static, .root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }) }); + mupdf.linkTo(in.root_module); + in.root_module.addIncludePath(b.path("src/kinds")); + in.root_module.addIncludePath(b.path("addons/pdf")); + in.root_module.addCSourceFile(.{ .file = b.path("addons/pdf/pdf.c"), .flags = &.{ "-std=gnu11", "-Wall", "-Wextra", "-Werror", "-Dpardes_addon=pardes_addon_pdf" } }); + in.root_module.addCSourceFile(.{ .file = b.path("addons/pdf/pdf_bridge.c"), .flags = &.{"-std=gnu11"} }); + m.linkLibrary(in); + } mupdf_check.dependOn(mupdf_build.addProbe(b, mupdf, .{ .target = target, .pdf = b.path("docs/design.pdf"), @@ -818,6 +838,7 @@ pub fn build(b: *std.Build) void { image_mod.addIncludePath(zstbi_dep.path("libs/stbi")); image_mod.addCSourceFile(.{ .file = b.path("addons/image/stb.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } }); const image_addon = b.addLibrary(.{ .name = "image", .linkage = .dynamic, .root_module = image_mod }); + if (static_mod) |m| m.addImport("image_addon", image_mod); for (core_mods) |m| wireCore(m, .{ .kind = kind_mod, .themes = themes_mod, @@ -838,12 +859,17 @@ pub fn build(b: *std.Build) void { iso_off.addOption(bool, "isolated", false); const iso_on = b.addOptions(); iso_on.addOption(bool, "isolated", true); + const builtin_off = b.addOptions(); + builtin_off.addOption(bool, "on", false); + const builtin_on = b.addOptions(); + builtin_on.addOption(bool, "on", true); for (core_mods, core_shells) |mod, shell| { // config.zig's test checks the words and keys docs/typ names exist. mod.addOptions("doc_typ", doc_typ_opts); mod.addOptions("pardes_config", if (also_gui and shell == .gui) gui_opts.? else opts); mod.addOptions("pardes_isolation", if (isolated_mod == mod) iso_on else iso_off); + mod.addOptions("builtin_addons", if (static_mod == mod) builtin_on else builtin_off); } // docs/typ/builtins-<platform>.json: every builtin, from the registry and the Help @@ -1113,7 +1139,6 @@ pub fn build(b: *std.Build) void { 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, - .linkage = if (static) .static else null, .root_module = root_mod, }); installHome(b, exe, home_bin); @@ -1141,17 +1166,24 @@ pub fn build(b: *std.Build) void { 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, - .linkage = if (static) .static else null, .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 static pardes with the image and pdf addons in it (-Dplatform=tui -Dtarget=x86_64-linux-musl)"); + if (static_mod) |m| + static_step.dependOn(&b.addInstallArtifact(b.addExecutable(.{ .name = "pardes", .linkage = .static, .root_module = m }), .{}).step) + else + static_step.dependOn(&b.addFail("static is the terminal's and musl's: -Dplatform=tui -Dtarget=x86_64-linux-musl").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", - .linkage = if (static) .static else null, .root_module = iso, }); const run_iso = b.addRunArtifact(iso_exe); diff --git a/docs/shared-libs.md b/docs/shared-libs.md index 384dc2f1..4a07f80a 100644 --- a/docs/shared-libs.md +++ b/docs/shared-libs.md @@ -29,6 +29,18 @@ pdf` (or `zig build addons`, both) builds and installs each library in home install). MuPDF is compiled only for a step that needs it (`pdf`, the tests); `-Dmupdf` is gone. +## One static pardes (2026-11-06) + +`zig build static -Dplatform=tui -Dtarget=x86_64-linux-musl` links the +terminal binary statically (musl: glibc links only dynamically) with both +addons compiled in: image.zig as a module, its stb_image static in stb.c +beside the binary's zstbi resize, and pdf.c with MuPDF, its FreeType and +zlib, its export renamed (`-Dpardes_addon=pardes_addon_pdf`), so the +binary holds one copy of each. Addon.zig registers both at start as if +loaded. It loads no library (a static binary has no dlopen); it is also +the way a build for macOS, whose loader has no memfd, could have image and +PDF panes. + ## PDF panes became an addon (2026-11-06) `lib/pardes/addons/pdf.so` (addons/pdf/pdf.c and pdf_bridge.c) holds MuPDF, diff --git a/docs/typ/building.typ b/docs/typ/building.typ index 311d98c0..8520fbbf 100644 --- a/docs/typ/building.typ +++ b/docs/typ/building.typ @@ -22,6 +22,7 @@ for the `pdf` step and the tests. [`zig build -Dplatform=tui`], [the terminal shell alone], [`zig build -Dplatform=gui`], [the SDL3 window alone], [`zig build -Dplatform=macos`], [an AppKit and CoreText app over a static `libpardes.a` (`docs/macos.md`)], + [`zig build static -Dplatform=tui -Dtarget=x86_64-linux-musl`], [one statically linked `pardes` with the image and PDF addons compiled in (`/addons` lists them `builtin`); musl's, as glibc links only dynamically, and never the default], ) A bare `zig build` installs into the prefix (`zig-out`, or `--prefix @@ -40,7 +41,6 @@ must share with its session; `pardes --help` lists every flag. #pairs( [`-Dplatform`], [`tui`, `gui`, `macos`; absent: the tui and SDL shells together, installed into `~/.local`], - [`-Dstatic`], [bool, `false`], [`-Djpx`], [bool, `true`: JPEG 2000, and with it scanned PDFs], [`-Dtree-sitter`], [`disabled`, `zig`, `minimal`, `full`; `full`], [`-Dembed-sources`], [bool, `false`: serve the sources under #file("/src")], diff --git a/src/Addon.zig b/src/Addon.zig index c6dca34b..5219f2b1 100644 --- a/src/Addon.zig +++ b/src/Addon.zig @@ -174,9 +174,25 @@ var found_words: [Kind.max_kinds][64]LoadedWord = undefined; var found_text: [Kind.max_kinds][2][4096]u8 = undefined; var nfound: usize = 0; -/// Every library of the two directories, in name order each, the user's -/// first. A library that crashed at its last load is listed, not loaded. +/// A static pardes's addons (`zig build static`), compiled in: the image +/// addon's module and the PDF's C, its `pardes_addon` renamed. A static +/// binary cannot dlopen, and macOS's loader has no memfd: this is how +/// either has image and PDF panes. +const compiled_in: []const *const Kind.Ops = if (@import("builtin_addons").on) &.{ &@import("image_addon").pardes_addon, &pardes_addon_pdf } else &.{}; +extern const pardes_addon_pdf: Kind.Ops; + +/// The addons compiled in, as if loaded (`builtin` their path); else every +/// library of the two directories, in name order each, the user's first. A +/// library that crashed at its last load is listed, not loaded. pub fn scan() []const Loaded { + for (compiled_in) |ops| { + found[nfound] = loadedOf(ops, "builtin", @splat(0), &found_words[nfound]); + if (ops.init) |init| if (init(&Kind.api) != Kind.c.PA_OK) { + found[nfound].why = "refused: its init failed"; + }; + nfound += 1; + } + if (compiled_in.len > 0) return found[0..nfound]; var data_buf: [4096]u8 = undefined; const data = dataDir(&data_buf); const crashed = crashedPath(data); @@ -491,6 +507,7 @@ pub fn run(p: *Pardes, id: usize, arg: ?[]const u8) void { } const k = p.kinds.byName(rest) orelse return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon {t}: no addon brought kind {s}", .{ op, rest }) catch takes); const e = &p.kinds.kinds[k]; + if (op == .reload and std.mem.eql(u8, e.path.slice(), "builtin")) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon reload: {s} is built into this pardes", .{rest}) catch takes); if (op == .reload) return p.emit(.{ .addon = .{ .op = .reload, .pane = @intCast(id), .arg = .from(e.path.slice()) } }); const n = openPanes(p, rest); if (n > 0) return p.reportFailure(id, std.fmt.bufPrint(&said, "Addon unload: {d} {s} pane{s} open", .{ n, rest, if (n == 1) " is" else "s are" }) catch takes); diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md index fb47ac8e..c465032d 100644 --- a/src/CHANGELOG.md +++ b/src/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- `zig build static -Dplatform=tui -Dtarget=x86_64-linux-musl`: one + statically linked `pardes` with the image and PDF addons compiled in + (`/addons` lists them `builtin`; `Addon reload` refuses them). It + replaces `-Dstatic`, which glibc refused (it links only dynamically). - Addons in Zig: `src/kinds/addon.zig` (over the same header, nothing of the core) makes a kind's `pa_ops` from a struct of its functions, with the pane's arena as an Allocator, typed keys, a window to `put` text in, diff --git a/src/daemon.zig b/src/daemon.zig index bc9e9773..6c0a0adf 100644 --- a/src/daemon.zig +++ b/src/daemon.zig @@ -247,7 +247,9 @@ fn spawn(listener: c_int, lock: c_int) bool { const l = libc.fcntl(listener, libc.F.DUPFD, @as(c_int, 10)); const k = libc.fcntl(lock, libc.F.DUPFD, @as(c_int, 10)); if (l < 0 or k < 0 or libc.dup2(l, 3) < 0 or libc.dup2(k, 4) < 0) libc._exit(127); - if (close_range(5, std.math.maxInt(c_uint), 0) != 0) { + // Linux's by its syscall: musl (a static pardes) has no close_range. + const shut = if (comptime builtin.os.tag == .linux) std.os.linux.close_range(5, std.math.maxInt(i32), .{ .UNSHARE = false, .CLOEXEC = false }) == 0 else close_range(5, std.math.maxInt(c_uint), 0) == 0; + if (!shut) { var fd: c_int = 5; while (fd < 1024) : (fd += 1) _ = libc.close(fd); } diff --git a/src/upgrade.zig b/src/upgrade.zig index 7819f216..97d3be4f 100644 --- a/src/upgrade.zig +++ b/src/upgrade.zig @@ -322,13 +322,13 @@ pub fn exec(gpa: std.mem.Allocator, path: ?[:0]const u8, old: c_int, rec: *const for (given, argv[2..]) |a, *slot| slot.* = a.ptr; rec.closeOnExec(false); const envp: [*:null]const ?[*:0]const u8 = @ptrCast(libc.environ); - const r = if (path) |p| - libc.execve(p, argv.ptr, envp) + // execveat by its syscall: musl (a static pardes) has no wrapper. + const e = if (path) |p| + libc.errno(libc.execve(p, argv.ptr, envp)) else if (comptime builtin.os.tag == .linux) - libc.execveat(old, "", argv.ptr, envp, .{ .SYMLINK_NOFOLLOW = false, .EMPTY_PATH = true }) + std.os.linux.errno(std.os.linux.execveat(old, "", argv.ptr, envp, .{ .SYMLINK_NOFOLLOW = false, .EMPTY_PATH = true })) else - -1; - const e = libc.errno(r); + libc.errno(@as(c_int, -1)); rec.closeOnExec(true); return switch (e) { .NOENT => "it is not there", |
