diff options
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 278 |
1 files changed, 177 insertions, 101 deletions
@@ -338,94 +338,7 @@ pub fn build(b: *std.Build) void { // each entry to matchPattern, which its backtracking recurses through, // spends a step, and with none left the match fails and says so // (src/regexp.zig). A bump that moves the anchor stops the build. - const mvzr_mod = mvzr: { - const dep = b.dependency("mvzr", .{ .target = target, .optimize = optimize }); - const src = configureRead(b, io, dep.builder.root, "src/mvzr.zig", 1 << 20) catch @panic("read mvzr"); - const anchor = "fn matchPattern(patt: []const RegOp, sets: []const CharSet, haystack: []const u8, i_in: usize) ?OpMatch {\n"; - const at = (std.mem.indexOf(u8, src, anchor) orelse @panic("mvzr's matchPattern changed: redo its step budget in build.zig")) + anchor.len; - const spend = - \\ if (steps_left == 0) { - \\ exhausted = true; - \\ return null; - \\ } - \\ steps_left -= 1; - \\ - ; - const budget = - \\ - \\/// pardes's patch (its build.zig): matchPattern entries left before a - \\/// match gives up, and whether one did. Set by the caller per search. - \\pub threadlocal var steps_left: u64 = std.math.maxInt(u64); - \\pub threadlocal var exhausted: bool = false; - \\ - ; - const budgeted = b.fmt("{s}{s}{s}{s}", .{ src[0..at], spend, src[at..], budget }); - // A group's greedy match never gave back to what follows the group: - // on `aab`, `(a+)ab` missed where `a+ab` matched. hasAlt counts the - // group's own `)` as an alternative, so every group with pattern - // after it takes matchGroup's alternatives branch, which tries each - // alternative's one (greedy) match and gives up. When the rest fails - // after it, try that alternative's shorter matches, longest first - // (docs/mvzr-group-backtrack.md is the report for upstream). - const group_anchor = - \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); - \\ if (next_match) |m2| { - \\ // Whole group matches, and we can use m2.j - \\ return m2; - \\ } else { // Try our next pattern, if any. - \\ - ; - const group_backoff = - \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); - \\ if (next_match) |m2| { - \\ // Whole group matches, and we can use m2.j - \\ return m2; - \\ } else { // Try our next pattern, if any. - \\ // pardes's patch (its build.zig): back the group off. - \\ var e = m1.i; - \\ while (e > i) { - \\ e -= 1; - \\ const shorter = matchAlt(remaining_patt, sets, haystack[0..e], i) orelse continue; - \\ if (shorter.i != e) continue; - \\ if (matchPattern(next_patt, sets, haystack, e)) |whole| return whole; - \\ } - \\ - ; - if (std.mem.count(u8, budgeted, group_anchor) != 1) @panic("mvzr's matchGroup changed: redo its group backtracking in build.zig"); - const backed = std.mem.replaceOwned(u8, b.allocator, budgeted, group_anchor, group_backoff) catch @panic("OOM"); - // `$` is refused anywhere but last, so `h$|zz` was a bad pattern - // while `zz|h$` was fine. An alternative or a group ends at `|` and - // `)`, and the matcher's `.end` ends the alternative it is in, so - // `$` may stand before either (docs/mvzr-group-backtrack.md). - const end_anchor = - \\ '$' => { - \\ if (i + 1 < in.len) { - \\ - ; - const end_before_alt = - \\ '$' => { - \\ // pardes's patch (its build.zig): `$` may end an alternative. - \\ if (i + 1 < in.len and in[i + 1] != '|' and in[i + 1] != ')') { - \\ - ; - if (std.mem.count(u8, backed, end_anchor) != 1) @panic("mvzr's parser changed: redo its `$` before `|` in build.zig"); - var patched = std.mem.replaceOwned(u8, b.allocator, backed, end_anchor, end_before_alt) catch @panic("OOM"); - // Zig 0.17 removed `**`; mvzr still has it (its PR #16 replaces it upstream). - // A class's range ending at `\xff` counted to 256 in a u8, an - // overflow (a panic in a safe build, a runaway loop in a fast one): - // `[\x80-\xff]` in any search took the editor down. - for ([_][3][]const u8{ - .{ "for (c1..c2 + 1) |c_range|", "for (c1..@as(usize, c2) + 1) |c_range|", "1" }, - .{ "[1]RegOp{.{ .unused = {} }} ** ops", "@splat(.{ .unused = {} })", "1" }, - .{ "[1]CharSet{.{ .low = 0, .hi = 0 }} ** char_sets", "@splat(.{ .low = 0, .hi = 0 })", "1" }, - .{ "\"a\" ** 2048", "&@as([2048]u8, @splat('a'))", "2" }, - .{ "\"12345,\" ** 255", "std.mem.asBytes(&@as([255][6]u8, @splat(\"12345,\".*)))", "1" }, - }) |edit| { - if (std.mem.count(u8, patched, edit[0]) != std.fmt.parseInt(usize, edit[2], 10) catch unreachable) std.debug.panic("mvzr changed at `{s}`: redo its `**` patch in build.zig", .{edit[0]}); - patched = std.mem.replaceOwned(u8, b.allocator, patched, edit[0], edit[1]) catch @panic("OOM"); - } - break :mvzr b.createModule(.{ .root_source_file = b.addWriteFiles().add("mvzr.zig", patched), .target = target, .optimize = optimize }); - }; + const mvzr_mod = patchedMvzr(b, io, b.dependency("mvzr", .{ .target = target, .optimize = optimize }), target, optimize); // The C heap every C library with an allocator hook is handed: the core's // tree-sitter and fonts, and pdf.zig's MuPDF, which is a module of its own. @@ -540,18 +453,7 @@ pub fn build(b: *std.Build) void { const no_pdf: ?*std.Build.Step = if (pdf_addon == null) &b.addFail("this step needs the pdf addon, over MuPDF: add -Dpdf (it is fetched once, and on from then)").step else null; if (no_pdf) |s| for ([_]*std.Build.Step{ mupdf_check, pdf_pointer_bench_step, pdf_scroll_bench_step }) |step| step.dependOn(s); - const uucode_config = b.path("uucode_config.zig"); - const uucode_tables = b.dependency("uucode", .{ - .target = target, - .optimize = optimize, - .build_config_path = uucode_config, - }).namedLazyPath("tables.zig"); - const uucode_mod = b.dependency("uucode", .{ - .target = target, - .optimize = optimize, - .build_config_path = uucode_config, - .tables_path = uucode_tables, - }).module("uucode"); + const uucode_mod = uucodeModule(b, target, optimize).?; const vaxis_dep = b.dependency("vaxis", .{ .target = target, @@ -2028,6 +1930,9 @@ const ShellConfig = struct { /// `Build` word's, stamped with the commit (see `stamp_commit`). change: ?[]const u8, built: ?[]const u8, + /// 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, }; fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build.Step.Options { @@ -2047,6 +1952,7 @@ fn shellOptions(b: *std.Build, cfg: ShellConfig, platform: Platform) *std.Build. 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; } @@ -2214,13 +2120,183 @@ fn library(b: *std.Build) void { .built = null, }, .tui)); const ts = treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, tree_sitter_grammars); + const themes = generatedThemes(b, io, target, optimize); wireCore(mod, .{ - .themes = generatedThemes(b, io, target, optimize), + .themes = themes, .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") }), }); + // The editor, `pardes-editor`, to a package that asks for it: uucode, + // mvzr and vaxis (with what its build names) are fetched then and only + // then. Of vaxis it takes the grapheme width, which uucode alone feeds. + if (!(b.option(bool, "editor", "also export `pardes-editor`, the text pane as a library (fetches uucode, mvzr and vaxis)") orelse false)) return; + const uucode = uucodeModule(b, target, optimize) orelse return; + const mvzr = patchedMvzr(b, io, b.lazyDependency("mvzr", .{ .target = target, .optimize = optimize }) orelse return, target, optimize); + const vaxis_dep = b.lazyDependency("vaxis", .{ .target = target, .optimize = optimize, .external_uucode = true }) orelse return; + const width = b.addWriteFiles(); + _ = width.addCopyFile(vaxis_dep.path("src/gwidth.zig"), "gwidth.zig"); + const vaxis = b.createModule(.{ + .target = target, + .optimize = optimize, + .root_source_file = width.add("vaxis.zig", "pub const gwidth = @import(\"gwidth.zig\");\n"), + .imports = &.{.{ .name = "uucode", .module = uucode }}, + }); + _ = editorModule(b, "pardes-editor", target, optimize, .{ + .tree_sitter_grammars = tree_sitter_grammars, + .tracy = false, + .workspace_tag = null, + .prebuilt_shaders = false, + .embed_sources = false, + .commit = null, + .change = null, + .built = null, + }, .{ .themes = themes, .tree_sitter = ts.mod, .ts_libs = ts.libs.items, .ts_queries = ts.queries, .uucode = uucode, .mvzr = mvzr, .vaxis = vaxis }); +} + +fn patchedMvzr(b: *std.Build, io: std.Io, dep: *std.Build.Dependency, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode) *std.Build.Module { + const src = configureRead(b, io, dep.builder.root, "src/mvzr.zig", 1 << 20) catch @panic("read mvzr"); + const anchor = "fn matchPattern(patt: []const RegOp, sets: []const CharSet, haystack: []const u8, i_in: usize) ?OpMatch {\n"; + const at = (std.mem.indexOf(u8, src, anchor) orelse @panic("mvzr's matchPattern changed: redo its step budget in build.zig")) + anchor.len; + const spend = + \\ if (steps_left == 0) { + \\ exhausted = true; + \\ return null; + \\ } + \\ steps_left -= 1; + \\ + ; + const budget = + \\ + \\/// pardes's patch (its build.zig): matchPattern entries left before a + \\/// match gives up, and whether one did. Set by the caller per search. + \\pub threadlocal var steps_left: u64 = std.math.maxInt(u64); + \\pub threadlocal var exhausted: bool = false; + \\ + ; + const budgeted = b.fmt("{s}{s}{s}{s}", .{ src[0..at], spend, src[at..], budget }); + // A group's greedy match never gave back to what follows the group: + // on `aab`, `(a+)ab` missed where `a+ab` matched. hasAlt counts the + // group's own `)` as an alternative, so every group with pattern + // after it takes matchGroup's alternatives branch, which tries each + // alternative's one (greedy) match and gives up. When the rest fails + // after it, try that alternative's shorter matches, longest first + // (docs/mvzr-group-backtrack.md is the report for upstream). + const group_anchor = + \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); + \\ if (next_match) |m2| { + \\ // Whole group matches, and we can use m2.j + \\ return m2; + \\ } else { // Try our next pattern, if any. + \\ + ; + const group_backoff = + \\ const next_match = matchPattern(next_patt, sets, haystack, m1.i); + \\ if (next_match) |m2| { + \\ // Whole group matches, and we can use m2.j + \\ return m2; + \\ } else { // Try our next pattern, if any. + \\ // pardes's patch (its build.zig): back the group off. + \\ var e = m1.i; + \\ while (e > i) { + \\ e -= 1; + \\ const shorter = matchAlt(remaining_patt, sets, haystack[0..e], i) orelse continue; + \\ if (shorter.i != e) continue; + \\ if (matchPattern(next_patt, sets, haystack, e)) |whole| return whole; + \\ } + \\ + ; + if (std.mem.count(u8, budgeted, group_anchor) != 1) @panic("mvzr's matchGroup changed: redo its group backtracking in build.zig"); + const backed = std.mem.replaceOwned(u8, b.allocator, budgeted, group_anchor, group_backoff) catch @panic("OOM"); + // `$` is refused anywhere but last, so `h$|zz` was a bad pattern + // while `zz|h$` was fine. An alternative or a group ends at `|` and + // `)`, and the matcher's `.end` ends the alternative it is in, so + // `$` may stand before either (docs/mvzr-group-backtrack.md). + const end_anchor = + \\ '$' => { + \\ if (i + 1 < in.len) { + \\ + ; + const end_before_alt = + \\ '$' => { + \\ // pardes's patch (its build.zig): `$` may end an alternative. + \\ if (i + 1 < in.len and in[i + 1] != '|' and in[i + 1] != ')') { + \\ + ; + if (std.mem.count(u8, backed, end_anchor) != 1) @panic("mvzr's parser changed: redo its `$` before `|` in build.zig"); + var patched = std.mem.replaceOwned(u8, b.allocator, backed, end_anchor, end_before_alt) catch @panic("OOM"); + // Zig 0.17 removed `**`; mvzr still has it (its PR #16 replaces it upstream). + // A class's range ending at `\xff` counted to 256 in a u8, an + // overflow (a panic in a safe build, a runaway loop in a fast one): + // `[\x80-\xff]` in any search took the editor down. + for ([_][3][]const u8{ + .{ "for (c1..c2 + 1) |c_range|", "for (c1..@as(usize, c2) + 1) |c_range|", "1" }, + .{ "[1]RegOp{.{ .unused = {} }} ** ops", "@splat(.{ .unused = {} })", "1" }, + .{ "[1]CharSet{.{ .low = 0, .hi = 0 }} ** char_sets", "@splat(.{ .low = 0, .hi = 0 })", "1" }, + .{ "\"a\" ** 2048", "&@as([2048]u8, @splat('a'))", "2" }, + .{ "\"12345,\" ** 255", "std.mem.asBytes(&@as([255][6]u8, @splat(\"12345,\".*)))", "1" }, + }) |edit| { + if (std.mem.count(u8, patched, edit[0]) != std.fmt.parseInt(usize, edit[2], 10) catch unreachable) std.debug.panic("mvzr changed at `{s}`: redo its `**` patch in build.zig", .{edit[0]}); + patched = std.mem.replaceOwned(u8, b.allocator, patched, edit[0], edit[1]) catch @panic("OOM"); + } + return b.createModule(.{ .root_source_file = b.addWriteFiles().add("mvzr.zig", patched), .target = target, .optimize = optimize }); +} + +/// uucode with pardes's configuration (uucode_config.zig), which ghostty +/// and vaxis share; null while it is still to be fetched. +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. +fn editorModule(b: *std.Build, name: ?[]const u8, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, cfg: ShellConfig, d: struct { + themes: *std.Build.Module, + tree_sitter: ?*std.Build.Module, + ts_libs: []const *std.Build.Step.Compile, + ts_queries: ?*std.Build.Step.Options, + 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 mod = if (name) |n| b.addModule(n, options) else b.createModule(options); + var lib_cfg = cfg; + lib_cfg.lib = true; + mod.addOptions("pardes_config", shellOptions(b, lib_cfg, .tui)); + wireCore(mod, .{ + .plain = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/plain/plain.zig") }), + .themes = d.themes, + .tree_sitter = d.tree_sitter, + .ts_libs = d.ts_libs, + .ts_queries = d.ts_queries, + .mvzr = d.mvzr, + .uucode = d.uucode, + .vaxis = d.vaxis, + .c_heap = b.createModule(.{ .target = target, .optimize = optimize, .root_source_file = b.path("src/c_heap.zig") }), + }); + return mod; } fn wireCore(m: *std.Build.Module, d: struct { |
