From 31706a1d1c13c26c69f67f107b72179019cbd255 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Fri, 6 Nov 2026 13:39:00 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Addon.zig | 21 +++++++++++++++++++-- src/CHANGELOG.md | 4 ++++ src/daemon.zig | 4 +++- src/upgrade.zig | 10 +++++----- 4 files changed, 31 insertions(+), 8 deletions(-) (limited to '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", -- cgit v1.3