diff options
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 216 |
1 files changed, 150 insertions, 66 deletions
@@ -59,6 +59,9 @@ const gui_shaders = [_][]const u8{ }; 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 library(b); 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; @@ -233,6 +236,15 @@ pub fn build(b: *std.Build) void { core_mods_len += 1; }; const core_mods = core_mods_buf[0..core_mods_len]; + // The manifest marks every dependency lazy but tree-sitter's runtime, so + // that a package depending on pardes fetches what `library` takes and + // nothing else. pardes takes these whatever it builds: the missing ones + // are asked for here in one round, the grammars with them, where each + // `b.dependency` below would end the configure to fetch its own alone. + const ts = treeSitter(b, io, target, c_optimize, tree_sitter_grammars); + inline for (.{ "cloud9", "vaxis", "ghostty", "uucode", "zstbi", "mvzr", "zstd", "xitdb" }) |name| { + if (!std.Build.package_map.get(@field(zon.dependencies, name).hash).?.available) _ = b.lazyDependency(name, .{}); + } const cloud9 = b.dependency("cloud9", .{ .target = target, .optimize = optimize }).module("cloud9"); for (core_mods) |mod| mod.addImport("cloud9", cloud9); const core_shells = core_shells_buf[0..core_mods_len]; @@ -243,27 +255,12 @@ pub fn build(b: *std.Build) void { // The conditional dependencies, hoisted so one `wireCore` call at the end // can name them all. The unconditional ones are ordinary consts below. - var ts_mod: ?*std.Build.Module = null; - var ts_queries_opts: ?*std.Build.Step.Options = null; + const ts_mod = ts.mod; + const ts_queries_opts = ts.queries; var ghostty_core_vt: ?*std.Build.Module = null; - var ts_libs: std.ArrayList(*std.Build.Step.Compile) = .empty; + const ts_libs = ts.libs; - 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}))); - const themes_mod = b.createModule(.{ - .target = target, - .optimize = optimize, - .root_source_file = themes_out.path(b, "list.zig"), - }); + const themes_mod = generatedThemes(b, io, target, optimize); // forkpty: in libc proper on glibc>=2.34 and darwin; the BSDs keep it in libutil if (target.result.os.tag == .freebsd or target.result.os.tag == .netbsd or target.result.os.tag == .openbsd) @@ -314,53 +311,6 @@ pub fn build(b: *std.Build) void { } } - 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)) - { - const dep = b.dependency(g.dep, .{}); - const query = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); - ts_queries.addOption([]const u8, g.name ++ "_highlights", query); - // 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", - // Relative to the build root, the compiler's working directory. - "-include", - "src/tree_sitter_heap.h", - }; - 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; - } - const zstbi_dep = b.dependency("zstbi", .{ .target = target, .optimize = c_optimize }); const zstbi_mod = zstbi_dep.module("root"); // mvzr backtracks with no bound on its work (its own test "do not make @@ -1556,6 +1506,15 @@ pub fn build(b: *std.Build) void { syntax_test_step.dependOn(&run_syntax_test.step); if (testMatch(b, unit_profile, test_filters, syntax_test_step)) |check| check.addArtifactArg(syntax_test); + // The library as a package that depends on pardes is given it + // (`library`): test/lib's own `zig build test`, over the packages + // this checkout has fetched. + const lib_test = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "test", "--pkg-dir", "../../zig-pkg" }); + lib_test.setCwd(b.path("test/lib")); + lib_test.has_side_effects = true; + const lib_test_step = b.step("lib-test", "build and test the library from a package that depends on pardes (test/lib)"); + lib_test_step.dependOn(&lib_test.step); + const history = b.addExecutable(.{ .name = "pardes-history", .root_module = b.createModule(.{ @@ -1592,6 +1551,7 @@ pub fn build(b: *std.Build) void { // The perf gate runs with every unit-test (a regression like a // contrast search per grapheme costs 100x, far past the gate). unit_step.dependOn(perf_gate_step); + unit_step.dependOn(lib_test_step); const units: Units = .{ .nonce = test_nonce, .build = test_build, .unit = unit_step, .match = testMatch(b, unit_profile, test_filters, unit_step), .profiler = unit_profile, .filters = test_filters }; // The perf and fs bench programs' own tests: they compile no editor. _ = units.add(b, perf_tests, null); @@ -2067,6 +2027,130 @@ fn vendoredThemes(b: *std.Build, io: std.Io) []const []const u8 { return names.items; } +/// The vendored palettes as the `generated_themes` module colors.zig folds +/// into its ring, written by tools/gen_themes.zig. +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"), + }); +} + +const TreeSitter = struct { + mod: ?*std.Build.Module, + queries: ?*std.Build.Step.Options, + libs: std.ArrayList(*std.Build.Step.Compile), +}; + +/// 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. +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 query = configureRead(b, io, dep.builder.root, g.query, 0x100000) catch @panic("read " ++ g.name ++ " highlights"); + ts_queries.addOption([]const u8, g.name ++ "_highlights", query); + // 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 }; +} + +/// pardes as a library, all a package that depends on it is given: the +/// `pardes-syntax` module (src/lib_syntax.zig), the grammars, highlighting +/// and themes for a program that draws source text itself. It takes +/// tree-sitter and the grammars of its `tree-sitter` option and no other +/// dependency, so nothing else is fetched (build.zig.zon). test/lib is such +/// a package, which `lib-test` builds. +fn library(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + const tree_sitter_grammars = b.option(TreeSitterGrammars, "tree-sitter", "tree-sitter grammar set: disabled, zig, minimal (c/c++/zig), full") orelse .full; + var threaded: std.Io.Threaded = .init(b.allocator, .{}); + defer threaded.deinit(); + const io = threaded.io(); + const mod = b.addModule("pardes-syntax", .{ + .target = target, + .optimize = optimize, + .root_source_file = b.path("src/lib_syntax.zig"), + .link_libc = true, + }); + mod.addOptions("pardes_config", shellOptions(b, .{ + .tree_sitter_grammars = tree_sitter_grammars, + .tracy = false, + .workspace_tag = null, + .prebuilt_shaders = false, + .embed_sources = false, + .commit = null, + .change = null, + .built = null, + }, .tui)); + const ts = treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, tree_sitter_grammars); + wireCore(mod, .{ + .themes = generatedThemes(b, io, target, optimize), + .tree_sitter = ts.mod, + .ts_libs = ts.libs.items, + .ts_queries = ts.queries, + .c_heap = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") }), + }); +} + fn wireCore(m: *std.Build.Module, d: struct { kind: ?*std.Build.Module = null, themes: ?*std.Build.Module = null, |
