-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.zig
More file actions
473 lines (442 loc) · 19.1 KB
/
Copy pathbuild.zig
File metadata and controls
473 lines (442 loc) · 19.1 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
const std = @import("std");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
// ===== Tree-sitter & grammar dependencies =====
// All fetched by Zig's package manager — see build.zig.zon.
const ts_dep = b.dependency("tree_sitter", .{});
const ts_zig_dep = b.dependency("tree_sitter_zig", .{});
const ts_python_dep = b.dependency("tree_sitter_python", .{});
const ts_js_dep = b.dependency("tree_sitter_javascript", .{});
const ts_typescript_dep = b.dependency("tree_sitter_typescript", .{});
const ts_json_dep = b.dependency("tree_sitter_json", .{});
const ts_bash_dep = b.dependency("tree_sitter_bash", .{});
const ts_go_dep = b.dependency("tree_sitter_go", .{});
const ts_html_dep = b.dependency("tree_sitter_html", .{});
const ts_css_dep = b.dependency("tree_sitter_css", .{});
const ts_rust_dep = b.dependency("tree_sitter_rust", .{});
const ts_c_dep = b.dependency("tree_sitter_c", .{});
const ts_cpp_dep = b.dependency("tree_sitter_cpp", .{});
const ts_java_dep = b.dependency("tree_sitter_java", .{});
const ts_ruby_dep = b.dependency("tree_sitter_ruby", .{});
const ts_csharp_dep = b.dependency("tree_sitter_c_sharp", .{});
// Tier 2 languages
const ts_php_dep = b.dependency("tree_sitter_php", .{});
const ts_swift_dep = b.dependency("tree_sitter_swift", .{});
const ts_kotlin_dep = b.dependency("tree_sitter_kotlin", .{});
const ts_lua_dep = b.dependency("tree_sitter_lua", .{});
const ts_dart_dep = b.dependency("tree_sitter_dart", .{});
const ts_elixir_dep = b.dependency("tree_sitter_elixir", .{});
const ts_haskell_dep = b.dependency("tree_sitter_haskell", .{});
const ts_ocaml_dep = b.dependency("tree_sitter_ocaml", .{});
const ts_scala_dep = b.dependency("tree_sitter_scala", .{});
const ts_r_dep = b.dependency("tree_sitter_r", .{});
const ts_perl_dep = b.dependency("tree_sitter_perl", .{});
const ts_erlang_dep = b.dependency("tree_sitter_erlang", .{});
// The tree-sitter repo uses `lib/src/tree_sitter/{parser,array,alloc}.h`
// symlinks that point to `lib/src/*.h`. Zig's package extractor drops
// symlinks, so we stage the headers at `tree_sitter/*` by copying the real
// files. Several scanners (TypeScript, PHP, OCaml, etc.) include these
// under the `tree_sitter/` prefix.
const ts_shim = b.addWriteFiles();
_ = ts_shim.addCopyFile(ts_dep.path("lib/src/parser.h"), "tree_sitter/parser.h");
_ = ts_shim.addCopyFile(ts_dep.path("lib/src/array.h"), "tree_sitter/array.h");
_ = ts_shim.addCopyFile(ts_dep.path("lib/src/alloc.h"), "tree_sitter/alloc.h");
const ts_shim_dir = ts_shim.getDirectory();
const mod = b.addModule("stem", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
// Tree-sitter C flags - POSIX macros only needed on Linux.
// `-Dassert(...)=` neutralizes the bare `assert()` calls in several
// upstream scanners (php's common/scanner.h, etc.) that never
// include <assert.h>. Under C11+ those would be a hard error in
// ReleaseFast; we don't want grammar runtime assertions anyway.
const tree_sitter_cflags = if (target.result.os.tag == .linux)
&[_][]const u8{ "-std=c11", "-fno-sanitize=all", "-D_POSIX_C_SOURCE=200112L", "-D_DEFAULT_SOURCE", "-Dassert(x)=((void)0)" }
else
&[_][]const u8{ "-std=c11", "-fno-sanitize=all", "-Dassert(x)=((void)0)" };
// Tree-sitter core
mod.addCSourceFile(.{
.file = ts_dep.path("lib/src/lib.c"),
.flags = tree_sitter_cflags,
});
mod.addIncludePath(ts_dep.path("lib/include"));
mod.addIncludePath(ts_dep.path("lib/src"));
mod.addIncludePath(ts_shim_dir);
// Zig grammar
mod.addCSourceFile(.{
.file = ts_zig_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
// Python grammar
mod.addCSourceFile(.{
.file = ts_python_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_python_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// JavaScript grammar
mod.addCSourceFile(.{
.file = ts_js_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_js_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// TypeScript grammars (typescript and tsx are separate parsers).
// The TS scanners include common/scanner.h which references
// <tree_sitter/parser.h>; that path is provided by ts_dep's lib/include.
mod.addCSourceFile(.{
.file = ts_typescript_dep.path("typescript/src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_typescript_dep.path("typescript/src/scanner.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_typescript_dep.path("tsx/src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_typescript_dep.path("tsx/src/scanner.c"),
.flags = tree_sitter_cflags,
});
// JSON grammar (no scanner)
mod.addCSourceFile(.{
.file = ts_json_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
// Bash grammar
mod.addCSourceFile(.{
.file = ts_bash_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_bash_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// Go grammar
mod.addCSourceFile(.{
.file = ts_go_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
// HTML grammar
mod.addCSourceFile(.{
.file = ts_html_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_html_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// CSS grammar
mod.addCSourceFile(.{
.file = ts_css_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_css_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// C grammar (no scanner)
mod.addCSourceFile(.{
.file = ts_c_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
// C++ grammar
mod.addCSourceFile(.{
.file = ts_cpp_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_cpp_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// Java grammar (no scanner)
mod.addCSourceFile(.{
.file = ts_java_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
// Ruby grammar
mod.addCSourceFile(.{
.file = ts_ruby_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_ruby_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// C# grammar
mod.addCSourceFile(.{
.file = ts_csharp_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_csharp_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// Rust grammar
mod.addCSourceFile(.{
.file = ts_rust_dep.path("src/parser.c"),
.flags = tree_sitter_cflags,
});
mod.addCSourceFile(.{
.file = ts_rust_dep.path("src/scanner.c"),
.flags = tree_sitter_cflags,
});
// ===== Tier 2 grammars =====
// PHP (with-HTML variant; we use the `php/` parser which mixes HTML+PHP)
mod.addCSourceFile(.{ .file = ts_php_dep.path("php/src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_php_dep.path("php/src/scanner.c"), .flags = tree_sitter_cflags });
// Swift
mod.addCSourceFile(.{ .file = ts_swift_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_swift_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Kotlin
mod.addCSourceFile(.{ .file = ts_kotlin_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_kotlin_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Lua
mod.addCSourceFile(.{ .file = ts_lua_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_lua_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Dart
mod.addCSourceFile(.{ .file = ts_dart_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_dart_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Elixir
mod.addCSourceFile(.{ .file = ts_elixir_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_elixir_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Haskell
mod.addCSourceFile(.{ .file = ts_haskell_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_haskell_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// OCaml (multi-grammar package; we use the main `ocaml` variant)
mod.addCSourceFile(.{ .file = ts_ocaml_dep.path("grammars/ocaml/src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_ocaml_dep.path("grammars/ocaml/src/scanner.c"), .flags = tree_sitter_cflags });
// Scala
mod.addCSourceFile(.{ .file = ts_scala_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_scala_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// R
mod.addCSourceFile(.{ .file = ts_r_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_r_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Perl
mod.addCSourceFile(.{ .file = ts_perl_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_perl_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
// Erlang
mod.addCSourceFile(.{ .file = ts_erlang_dep.path("src/parser.c"), .flags = tree_sitter_cflags });
mod.addCSourceFile(.{ .file = ts_erlang_dep.path("src/scanner.c"), .flags = tree_sitter_cflags });
const options = b.addOptions();
// Version is defined in build.zig.zon, embed it at comptime
const zon = @import("build.zig.zon");
const version_string = zon.version;
options.addOption([]const u8, "version", version_string);
// Embed the short git commit hash at build time so `stem --version` /
// bug reports include actionable build identification. Falls back to
// "unknown" when not in a git checkout (e.g. tarball builds).
const git_hash: []const u8 = blk: {
var exit_code: u8 = undefined;
const out = b.runAllowFail(
&.{ "git", "rev-parse", "--short", "HEAD" },
&exit_code,
.ignore,
) catch break :blk "unknown";
break :blk std.mem.trim(u8, out, " \t\r\n");
};
options.addOption([]const u8, "git_hash", git_hash);
mod.addOptions("config", options);
const exe = b.addExecutable(.{
.name = "stem",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "stem", .module = mod },
},
}),
});
exe.root_module.addOptions("config", options);
exe.root_module.link_libc = true;
const vaxis_dep = b.dependency("vaxis", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("vaxis", vaxis_dep.module("vaxis"));
const vigil_dep = b.dependency("vigil", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("vigil", vigil_dep.module("vigil"));
const zls_dep = b.dependency("zls", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("zls", zls_dep.module("zls"));
const lsp_dep = b.dependency("lsp", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("lsp", lsp_dep.module("lsp"));
const uucode_dep = b.dependency("uucode", .{
.target = target,
.optimize = optimize,
.fields = @as([]const []const u8, &.{
"east_asian_width",
"grapheme_break",
"general_category",
"is_emoji_presentation",
}),
});
exe.root_module.addImport("uucode", uucode_dep.module("uucode"));
// wick: the wasm interpreter that runs the plugin system. Extracted
// from this repo (formerly src/plugins/wasm/interpreter.zig); the
// compatibility contract lives in wick's docs/stem-contract.md.
const wick_dep = b.dependency("wick", .{
.target = target,
.optimize = optimize,
});
exe.root_module.addImport("wick", wick_dep.module("wick"));
// Add dependencies to the main module as well (needed for tests)
mod.addImport("vaxis", vaxis_dep.module("vaxis"));
mod.addImport("vigil", vigil_dep.module("vigil"));
mod.addImport("zls", zls_dep.module("zls"));
mod.addImport("lsp", lsp_dep.module("lsp"));
mod.addImport("uucode", uucode_dep.module("uucode"));
mod.addImport("wick", wick_dep.module("wick"));
// Include paths for the exe so it can see tree-sitter headers.
exe.root_module.addIncludePath(ts_dep.path("lib/include"));
exe.root_module.addIncludePath(ts_dep.path("lib/src"));
b.installArtifact(exe);
const lsp_host_exe = b.addExecutable(.{
.name = "stem-lsp-host",
.root_module = b.createModule(.{
.root_source_file = b.path("src/lsp_host.zig"),
.target = target,
.optimize = optimize,
}),
});
lsp_host_exe.root_module.link_libc = true;
lsp_host_exe.root_module.addImport("zls", zls_dep.module("zls"));
b.installArtifact(lsp_host_exe);
b.getInstallStep().dependOn(&b.addInstallArtifact(lsp_host_exe, .{
.dest_sub_path = "stem-lsp-zig",
}).step);
// ===== Bundled WebAssembly plugins =====
// All bundled plugins target the wasm runtime. The host still
// supports exec (out-of-process JSON-RPC) plugins, but nothing
// bundled uses that path. Built for `wasm32-freestanding`; host
// imports (env.stem_*) are left unresolved at build time and bound
// by the wick interpreter at instantiate time.
const wasm_target = b.resolveTargetQuery(.{
.cpu_arch = .wasm32,
.os_tag = .freestanding,
});
const WasmPlugin = struct { name: []const u8, source: []const u8 };
const plugin_sdk_mod = b.createModule(.{
.root_source_file = b.path("bundled/plugins/sdk/stem.zig"),
.target = wasm_target,
.optimize = .ReleaseSmall,
});
const wasm_plugins = [_]WasmPlugin{
.{ .name = "echo", .source = "bundled/plugins/echo/src/main.zig" },
.{ .name = "git-wasm", .source = "bundled/plugins/git-wasm/src/main.zig" },
.{ .name = "plugin-manager-wasm", .source = "bundled/plugins/plugin-manager-wasm/src/main.zig" },
.{ .name = "sdk-demo", .source = "bundled/plugins/sdk-demo/src/main.zig" },
};
inline for (wasm_plugins) |wp| {
const plugin_root_mod = b.createModule(.{
.root_source_file = b.path(wp.source),
.target = wasm_target,
.optimize = .ReleaseSmall,
});
plugin_root_mod.addImport("stem", plugin_sdk_mod);
const exe_wasm = b.addExecutable(.{
.name = wp.name,
.root_module = plugin_root_mod,
});
exe_wasm.entry = .disabled;
exe_wasm.rdynamic = true;
b.installArtifact(exe_wasm);
}
const run_step = b.step("run", "Run the app");
const run_cmd = b.addRunArtifact(exe);
run_step.dependOn(&run_cmd.step);
run_cmd.step.dependOn(b.getInstallStep());
if (b.args) |args| {
run_cmd.addArgs(args);
}
mod.link_libc = true;
const mod_tests = b.addTest(.{
.root_module = mod,
});
const run_mod_tests = b.addRunArtifact(mod_tests);
const exe_tests = b.addTest(.{
.root_module = exe.root_module,
});
const run_exe_tests = b.addRunArtifact(exe_tests);
// Diagnostic: `zig build query-check` reports which grammar's highlight
// query (if any) fails to compile against the linked tree-sitter
// language. Useful when adding a new language or after a grammar bump.
const query_check = b.addExecutable(.{
.name = "query-check",
.root_module = b.createModule(.{
.root_source_file = b.path("src/tools/query_check.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "stem", .module = mod },
},
}),
});
query_check.root_module.link_libc = true;
query_check.root_module.addIncludePath(ts_dep.path("lib/include"));
query_check.root_module.addIncludePath(ts_dep.path("lib/src"));
const run_query_check = b.addRunArtifact(query_check);
const query_check_step = b.step("query-check", "Verify every shipped tree-sitter query compiles");
query_check_step.dependOn(&run_query_check.step);
// Benchmark harness: `zig build bench -Doptimize=ReleaseFast` runs
// the piece-table microbenchmarks in src/bench/main.zig. Uses the
// C allocator, so it links libc and needs the tree-sitter headers
// the `stem` module's C sources pull in (same as query-check).
const bench_exe = b.addExecutable(.{
.name = "bench",
.root_module = b.createModule(.{
.root_source_file = b.path("src/bench/main.zig"),
.target = target,
.optimize = optimize,
.imports = &.{
.{ .name = "stem", .module = mod },
},
}),
});
bench_exe.root_module.link_libc = true;
bench_exe.root_module.addIncludePath(ts_dep.path("lib/include"));
bench_exe.root_module.addIncludePath(ts_dep.path("lib/src"));
const run_bench = b.addRunArtifact(bench_exe);
if (b.args) |args| run_bench.addArgs(args);
const bench_step = b.step("bench", "Run microbenchmarks (build -Doptimize=ReleaseFast)");
bench_step.dependOn(&run_bench.step);
const test_step = b.step("test", "Run unit tests");
test_step.dependOn(&run_mod_tests.step);
test_step.dependOn(&run_exe_tests.step);
// ===== Fuzz Testing =====
const fuzz_mod = b.createModule(.{
.root_source_file = b.path("src/fuzz/mod.zig"),
.target = target,
.optimize = optimize,
});
fuzz_mod.addImport("stem", mod);
fuzz_mod.addImport("vaxis", vaxis_dep.module("vaxis"));
fuzz_mod.addImport("vigil", vigil_dep.module("vigil"));
fuzz_mod.link_libc = true;
const fuzz_tests = b.addTest(.{
.root_module = fuzz_mod,
});
fuzz_mod.addIncludePath(ts_dep.path("lib/include"));
fuzz_mod.addIncludePath(ts_dep.path("lib/src"));
const run_fuzz_tests = b.addRunArtifact(fuzz_tests);
const fuzz_step = b.step("fuzz", "Run fuzz tests (use: zig build --fuzz fuzz)");
fuzz_step.dependOn(&run_fuzz_tests.step);
}