1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
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 <version>` 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 runtime/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_set = core.module(b, parts, .{ .root = if (platform == .macos) "runtime/macos.zig" else "runtime/main.zig", .config = opts, .builtin_addons = compiled_in, .tracy = true, .util = true });
const root_mod = root_set.root;
// 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 = "core/root.zig", .config = opts, .builtin_addons = compiled_in, .tracy = true };
const core_set = core.module(b, parts, core_cfg);
const core_module = core_set.root;
// 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 = "core/root.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 = "runtime/main.zig", .config = opts, .builtin_addons = compiled_in, .isolated = true }).root 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_set: ?core.Modules = if ((platform == .tui and static_linkage != null) or platform == .gui) core.module(b, parts, .{ .root = "runtime/main.zig", .config = opts, .builtin_addons = true }) else null;
const static_mod: ?*std.Build.Module = if (static_set) |s| s.root else null;
const gui_set: ?core.Modules = if (also_gui) core.module(b, parts, .{ .root = "runtime/main.zig", .config = a.gui_opts.?, .builtin_addons = compiled_in }) else null;
const gui_mod: ?*std.Build.Module = if (gui_set) |s| s.root 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);
// ...into the devices, whose gui/gui.zig names the window.
for ([_]?core.Modules{ gui_set, if (platform == .gui) root_set else null, if (platform == .gui) static_set else null }) |m| if (m) |s| window.wire(s.dev);
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, <prefix>/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, <prefix>/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,
.runtime = core_set.runtime,
.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,
.root_set = root_set,
.core_wire = core_set.wire,
.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
// <prefix>/test, named for its root file.
const test_install = b.step("test-install", "install the test-build programs into <prefix>/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| if (std.mem.eql(u8, std.fs.path.basename(p.sub_path), "root.zig")) std.fs.path.basename(std.fs.path.dirname(p.sub_path).?) else 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);
}
|