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
|
const std = @import("std");
const Platform = @import("../build.zig").Platform;
const TreeSitterGrammars = @import("../build.zig").TreeSitterGrammars;
const grammar_manifest = @import("../src/grammar_manifest.zig");
/// The package manifest, imported UNTYPED on purpose — see the `version`
/// option in `shellOptions` for why that word is load-bearing.
const zon = @import("../build.zig.zon");
/// Releases are numbered 0.N; the zon holds "0.N.0" because it must be semver,
/// and what a user sees drops that ".0".
pub const display_version = if (std.mem.endsWith(u8, zon.version, ".0") and std.mem.count(u8, zon.version, ".") == 2)
zon.version[0 .. zon.version.len - 2]
else
zon.version;
/// Every dependency the manifest pins, by its content hash: a grammar,
/// ghostty or the Unicode tables steps a core differently, so they are part
/// of a replica's semantics hash (src/wire.zig `semantics`).
const deps_text = blk: {
var text: []const u8 = "";
for (@typeInfo(@TypeOf(zon.dependencies)).@"struct".field_names) |name| {
const dep = @field(zon.dependencies, name);
text = text ++ name ++ " " ++ (if (@hasField(@TypeOf(dep), "hash")) dep.hash else "path") ++ "\n";
}
break :blk text;
};
/// A file read while configuring, under a package's root. A dependency's is
/// hash-addressed and never changes under the same pin; this repo's are not
/// tracked one by one, since the configure runs again on every build anyway
/// (findProgram and gitCommit poison its cache).
// ponytail: untracked repo reads; b.dependOnFileContents each one if the configure cache ever stays pure.
pub fn configureRead(b: *std.Build, io: std.Io, root: std.Build.Cache.Path, sub: []const u8, limit: usize) ![]u8 {
return root.root_dir.handle.readFileAlloc(io, b.pathJoin(&.{ root.subPathOrDot(), sub }), b.allocator, .limited(limit));
}
pub fn gitCommit(b: *std.Build) ?[]const u8 {
return command(b, &.{ "git", "rev-parse", "--short=12", "HEAD" });
}
/// The jj change git HEAD is in, by its commit (jj 0.46 has no revset for
/// git's HEAD), or null without jj.
pub fn jjChange(b: *std.Build) ?[]const u8 {
const head = command(b, &.{ "git", "rev-parse", "HEAD" }) orelse return null;
return command(b, &.{ "jj", "log", "-r", head, "--no-graph", "-T", "change_id.short()" });
}
/// One line a command prints, or null: HEAD, the change, the clock are not
/// files the configure cache can track, so the cache is poisoned.
pub fn command(b: *std.Build, argv: []const []const u8) ?[]const u8 {
b.graph.poisonCache();
var code: u8 = 0;
const out = b.runAllowFail(argv, &code, .ignore) catch return null;
const trimmed = std.mem.trim(u8, out, " \t\r\n");
return if (trimmed.len == 0) null else trimmed;
}
pub const ShellConfig = struct {
tree_sitter_grammars: TreeSitterGrammars,
tracy: bool = false,
/// 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 = null,
prebuilt_shaders: bool = false,
embed_sources: bool = false,
commit: ?[]const u8 = null,
/// The jj change git HEAD is in, and when this configure ran: the
/// `Build` word's, stamped with the commit (see `stamp_commit`).
change: ?[]const u8 = null,
built: ?[]const u8 = null,
/// The library's editor (src/lib_editor.zig): a text with no pane, so
/// the core is not compiled (src/Editor.zig).
lib: bool = false,
};
pub fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build.Step.Options {
const o = b.addOptions();
o.addOption(Platform, "platform", platform);
o.addOption(bool, "syntax_highlighting", cfg.tree_sitter_grammars != .disabled);
o.addOption(bool, "syntax_zig_grammar", cfg.tree_sitter_grammars != .disabled);
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, "workspace_tag", cfg.workspace_tag orelse (platform != .macos));
o.addOption(bool, "embed_sources", cfg.embed_sources);
o.addOption([]const u8, "version", display_version);
// ...and the commit it was built from, when there is one and the build
// is stamped at all (see `stamp_commit`). Read at configure time.
o.addOption(?[]const u8, "commit", cfg.commit);
o.addOption(?[]const u8, "change", cfg.change);
o.addOption(?[]const u8, "built", cfg.built);
o.addOption([]const u8, "deps", deps_text);
o.addOption(bool, "lib", cfg.lib);
return o;
}
fn vendoredThemes(b: *std.Build, io: std.Io) []const []const u8 {
var names: std.ArrayList([]const u8) = .empty;
var dir = b.root.root_dir.handle.openDir(io, "vendor/themes", .{ .iterate = true }) catch @panic("open vendor/themes");
defer dir.close(io);
var it = dir.iterate();
while (it.next(io) catch @panic("read vendor/themes")) |e| {
if (!std.mem.endsWith(u8, e.name, ".toml") and !std.mem.endsWith(u8, e.name, ".json")) continue;
names.append(b.allocator, b.dupe(e.name)) catch @panic("OOM");
}
std.mem.sort([]const u8, names.items, {}, struct {
fn lt(_: void, a: []const u8, c: []const u8) bool {
return std.mem.order(u8, a, c) == .lt;
}
}.lt);
return names.items;
}
/// The vendored palettes as the `generated_themes` module colors.zig folds
/// into its ring, written by tools/gen_themes.zig.
pub fn generatedThemes(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize) *std.Build.Module {
const theme_gen = b.addExecutable(.{
.name = "pardes-gen-themes",
.root_module = b.createModule(.{
.target = b.graph.host,
.optimize = .debug,
.root_source_file = b.path("tools/gen_themes.zig"),
}),
});
const run_theme_gen = b.addRunArtifact(theme_gen);
const themes_out = run_theme_gen.addOutputDirectoryArg("themes");
for (vendoredThemes(b, io)) |name| run_theme_gen.addFileArg(b.path(b.fmt("vendor/themes/{s}", .{name})));
return b.createModule(.{
.target = target,
.optimize = optimize,
.root_source_file = themes_out.path(b, "list.zig"),
});
}
pub const TreeSitter = struct {
mod: ?*std.Build.Module,
queries: ?*std.Build.Step.Options,
libs: std.ArrayList(*std.Build.Step.Compile),
/// tree-sitter, the grammars' parsers and their queries, into `m`.
pub fn wire(ts: TreeSitter, m: *std.Build.Module) void {
if (ts.mod) |x| m.addImport("tree-sitter", x);
for (ts.libs.items) |lib| m.linkLibrary(lib);
if (ts.queries) |x| m.addOptions("ts_queries", x);
}
};
/// tree-sitter's bindings, and for each grammar of the set its queries (the
/// `ts_queries` options syntax.zig reads) and its parser, a static library.
/// A grammar not fetched yet is asked for and left out: the configure runs
/// again once it is there.
pub fn treeSitter(b: *std.Build, io: std.Io, target: std.Build.ResolvedTarget, c_optimize: std.lang.Optimize, tree_sitter_grammars: TreeSitterGrammars) TreeSitter {
var ts_mod: ?*std.Build.Module = null;
var ts_queries_opts: ?*std.Build.Step.Options = null;
var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty;
if (tree_sitter_grammars != .disabled) {
const tree_sitter_mod = b.dependency("tree_sitter", .{ .target = target, .optimize = c_optimize }).module("tree_sitter");
ts_mod = tree_sitter_mod;
const ts_queries = b.addOptions();
inline for (grammar_manifest.all) |g| {
if (tree_sitter_grammars == .full or
(tree_sitter_grammars == .minimal and g.tier != .full) or
(tree_sitter_grammars == .zig and g.tier == .zig))
grammar: {
const dep = b.lazyDependency(g.dep, .{}) orelse break :grammar;
const upstream = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights");
// the patterns that colour, filtered here once and not in
// every compile, and the whole query, the filter's test's
const query = std.mem.concat(b.allocator, u8, &.{ upstream, g.highlights_extra }) catch @panic("OOM");
ts_queries.addOption([]const u8, g.name ++ "_highlights_unfiltered", query);
ts_queries.addOption([]const u8, g.name ++ "_highlights", grammar_manifest.colorQuery(query, b.allocator.alloc(u8, query.len) catch @panic("OOM")));
// helix's textobject queries, vendored (vendor/queries); a
// grammar without one gets none
const objects = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/textobjects.scm", 0x100000) catch "";
ts_queries.addOption([]const u8, g.name ++ "_textobjects", objects);
// and their indent queries, beside them
const indents = configureRead(b, io, b.root, "vendor/queries/" ++ g.name ++ "/indents.scm", 0x100000) catch "";
ts_queries.addOption([]const u8, g.name ++ "_indents", indents);
const lib = b.addLibrary(.{
.name = "tree-sitter-" ++ g.name,
.root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }),
.linkage = .static,
});
// Every allocation a grammar's external scanner makes goes to the
// runtime's allocator, which syntax.zig points at a Zig one:
// TREE_SITTER_REUSE_ALLOCATOR for the scanners that use
// ts_malloc, and src/tree_sitter_heap.h for the ones that call
// malloc themselves.
const ts_cflags = [_][]const u8{
"-fno-sanitize=function",
"-DTREE_SITTER_REUSE_ALLOCATOR",
// From the compiler's working directory, the root
// package's: pardes's own, or the one that depends on it.
"-include",
b.root.joinString(b.allocator, "src/tree_sitter_heap.h") catch @panic("OOM"),
};
lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/parser.c"), .flags = &ts_cflags });
if (g.scanner) lib.root_module.addCSourceFile(.{ .file = dep.path(g.src ++ "/scanner.c"), .flags = &ts_cflags });
lib.root_module.addIncludePath(dep.path(g.src));
ts_libs.append(b.allocator, lib) catch @panic("OOM");
}
}
ts_queries_opts = ts_queries;
}
return .{ .mod = ts_mod, .queries = ts_queries_opts, .libs = ts_libs };
}
/// uucode with pardes's configuration (uucode_config.zig), which ghostty
/// and vaxis share; null while it is still to be fetched.
pub fn uucodeModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) ?*std.Build.Module {
const uucode_config = b.path("uucode_config.zig");
const uucode_tables = (b.lazyDependency("uucode", .{
.target = target,
.optimize = optimize,
.build_config_path = uucode_config,
}) orelse return null).namedLazyPath("tables.zig");
return (b.lazyDependency("uucode", .{
.target = target,
.optimize = optimize,
.build_config_path = uucode_config,
.tables_path = uucode_tables,
}) orelse return null).module("uucode");
}
/// The editor as a library (src/lib_editor.zig, docs/next-editor.md): the
/// text pane's own code built with `lib`, so no pane and no core, over
/// tree-sitter, uucode and mvzr, with pardes-syntax in it as `.syntax`.
/// `library` exports it to a package that asks; hxdiff runs its cases on it.
pub fn editorModule(b: *std.Build, name: ?[]const u8, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, cfg: ShellConfig, d: struct {
themes: *std.Build.Module,
ts: TreeSitter,
uucode: *std.Build.Module,
mvzr: *std.Build.Module,
/// vaxis's, or a module of its grapheme width alone (`library`): a
/// cell's width for j and k's goal column.
vaxis: *std.Build.Module,
}) *std.Build.Module {
const options: std.Build.Module.CreateOptions = .{
.target = target,
.optimize = optimize,
.root_source_file = b.path("src/lib_editor.zig"),
.link_libc = true,
};
const m = if (name) |n| b.addModule(n, options) else b.createModule(options);
var lib_cfg = cfg;
lib_cfg.lib = true;
m.addOptions("pardes_config", shellOptions(b, lib_cfg, .tui));
m.addImport("plain", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/plain/plain.zig") }));
m.addImport("generated_themes", d.themes);
d.ts.wire(m);
m.addImport("mvzr", d.mvzr);
m.addImport("c_heap", b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") }));
m.addImport("vaxis", d.vaxis);
m.addImport("uucode", d.uucode);
return m;
}
/// What every core module is made of: build.zig makes each once.
pub const Parts = struct {
target: std.Build.ResolvedTarget,
optimize: std.lang.Optimize,
cloud9: *std.Build.Module,
/// -Dtracy's checkout, whose client a core that asks compiles in.
tracy: ?[]const u8,
zstd: *std.Build.Step.Compile,
/// The addons, for a core that has them compiled in (Addon.zig); the
/// pdf one only when it is built (-Dpdf=false leaves it out).
image: *std.Build.Module,
pdf: ?*std.Build.Module,
term: *std.Build.Module,
plain: *std.Build.Module,
/// The kinds' C ABI (src/kinds/pardes_addon.h), translated.
kind: *std.Build.Module,
themes: *std.Build.Module,
ts: TreeSitter,
mvzr: *std.Build.Module,
c_heap: *std.Build.Module,
vaxis: *std.Build.Module,
uucode: *std.Build.Module,
xitdb: *std.Build.Module,
doc_typ: *std.Build.Step.Options,
/// One options module for each value of `terminal`, `isolated` and
/// `on`, shared by every core.
term_on: *std.Build.Step.Options,
term_off: *std.Build.Step.Options,
iso_on: *std.Build.Step.Options,
iso_off: *std.Build.Step.Options,
builtin_on: *std.Build.Step.Options,
builtin_off: *std.Build.Step.Options,
};
/// Which core: its root and the few ways the products differ.
pub const Core = struct {
/// src/main.zig for a shell, src/pardes.zig for the core, or a file
/// whose tests run on the core's imports.
root: []const u8,
/// The shell's options (`pardes_config`): this build's, or the window's
/// in the bare build.
config: *std.Build.Step.Options,
/// The terminal module; without it (core-test) a shell boot is a scratch pane.
terminal: bool = true,
/// pardes-isolate's: no host but the terminal.
isolated: bool = false,
/// The addons compiled in and registered at start (Addon.zig): the
/// static step's, and every core off Linux, whose loader has no memfd.
builtin_addons: bool = false,
/// Tracy's client with -Dtracy: the shell's and the core's.
tracy: bool = false,
/// libutil, for forkpty on the BSDs: the shell's.
util: bool = false,
};
/// A core module, everything it imports and links named here and nowhere else.
pub fn module(b: *std.Build, p: Parts, c: Core) *std.Build.Module {
const m = b.createModule(.{ .target = p.target, .optimize = p.optimize, .root_source_file = b.path(c.root), .link_libc = true });
m.addImport("cloud9", p.cloud9);
// forkpty: in libc proper on glibc>=2.34 and darwin; the BSDs keep it in libutil
if (c.util and (p.target.result.os.tag == .freebsd or p.target.result.os.tag == .netbsd or p.target.result.os.tag == .openbsd))
m.linkSystemLibrary("util", .{});
if (c.tracy) if (p.tracy) |tracy_path| {
m.addIncludePath(.{ .cwd_relative = tracy_path });
m.addCSourceFile(.{
.file = .{ .cwd_relative = b.pathJoin(&.{ tracy_path, "public", "TracyClient.cpp" }) },
.flags = &.{
"-DTRACY_ENABLE=1",
"-DTRACY_NO_SAMPLING",
"-DTRACY_NO_CALLSTACK",
"-DTRACY_NO_SYSTEM_TRACING",
"-DTRACY_NO_CRASH_HANDLER",
"-DTRACY_NO_CODE_TRANSFER",
"-fno-sanitize=undefined",
},
});
m.link_libcpp = true;
};
if (c.builtin_addons) if (p.pdf) |pdf| m.addImport("pdf_addon", pdf);
m.linkLibrary(p.zstd);
if (c.builtin_addons) m.addImport("image_addon", p.image);
if (c.terminal) {
m.addImport("term", p.term);
m.addOptions("term_options", p.term_on);
} else m.addOptions("term_options", p.term_off);
m.addImport("plain", p.plain);
m.addImport("pardes_addon", p.kind);
m.addImport("generated_themes", p.themes);
p.ts.wire(m);
m.addImport("mvzr", p.mvzr);
m.addImport("c_heap", p.c_heap);
m.addImport("vaxis", p.vaxis);
m.addImport("uucode", p.uucode);
m.addImport("xitdb", p.xitdb);
// config.zig's test checks the words and keys docs/typ names exist.
m.addOptions("doc_typ", p.doc_typ);
m.addOptions("pardes_config", c.config);
m.addOptions("pardes_isolation", if (c.isolated) p.iso_on else p.iso_off);
m.addOptions("builtin_addons", if (c.builtin_addons) p.builtin_on else p.builtin_off);
return m;
}
|