diff options
Diffstat (limited to 'build.zig')
| -rw-r--r-- | build.zig | 102 |
1 files changed, 94 insertions, 8 deletions
@@ -26,8 +26,21 @@ //! //! `.grammars`: the tree-sitter grammars compiled in: `none` (the runtime //! alone), `minimal` (C, C++ and Zig) or `full` (all of them; the default). -//! `.editor`: exports `pardes-editor` besides `pardes-syntax`, and with it -//! fetches uucode, mvzr and vaxis. +//! `.languages`: instead of `.grammars`, the languages compiled in by name +//! (`"rust"`, or `&.{ "rust", "go" }`), each with what it needs (markdown +//! its inline grammar); a name the manifest does not have fails the +//! configure. `.editor`: exports `pardes-editor` besides `pardes-syntax`, +//! and with it fetches uucode, mvzr and vaxis. +//! +//! The manifest, for a package's build script to read at configure time +//! (`@import("pardes").grammar_manifest.all`: each grammar's name, its +//! extensions and what it needs), is `grammar_manifest`. +//! +//! A browser's module, one call a language (`wasmModule`): +//! +//! const pardes = @import("pardes"); +//! b.installArtifact(pardes.wasmModule(b, .{ .language = "rust" })); +//! b.installArtifact(pardes.wasmModule(b, .{})); // the patch view alone //! //! Every dependency but tree-sitter's runtime is lazy in the manifest, so //! such a package fetches what it asked for and nothing else. @@ -72,13 +85,20 @@ pub const macos_min_version: std.SemanticVersion = .{ .major = 13, .minor = 0, . pub const TreeSitterGrammars = enum { none, minimal, full }; +/// Every grammar pardes knows (the head comment). +pub const grammar_manifest = @import("core/grammar_manifest.zig"); + pub fn build(b: *std.Build) void { // What a package that depends on pardes may ask for (the head comment). // pardes's own build takes the mode and the grammars too, and its target // below, whose default -Dplatform says. const optimize = b.standardOptimizeOption(.{}); - const grammars = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)") orelse .full; - if (!b.isRoot()) return library(b, b.standardTargetOptions(.{}), optimize, grammars, b.option(bool, "editor", "exports pardes-editor besides pardes-syntax, and with it fetches uucode, mvzr and vaxis") orelse false); + const grammars_option = b.option(TreeSitterGrammars, "grammars", "the tree-sitter grammars compiled in: none (the runtime alone), minimal (C, C++ and Zig) or full (all of them; the default)"); + const grammars = grammars_option orelse .full; + if (!b.isRoot()) return library(b, b.standardTargetOptions(.{}), optimize, librarySet( + grammars_option, + b.option([]const []const u8, "languages", "instead of grammars, the languages compiled in by name, each with what it needs"), + ), b.option(bool, "editor", "exports pardes-editor besides pardes-syntax, and with it fetches uucode, mvzr and vaxis") orelse false); // Everything below is pardes's own: the editor, its tests and tools. // This configure reads the environment (HOME, XDG_DATA_HOME, @@ -389,6 +409,46 @@ pub fn build(b: *std.Build) void { } } +/// A browser's module, one call a language (the head comment): wasm32-wasi +/// as a reactor (no `main`; `_initialize`, then any export), with +/// `language`'s grammar and what it needs, tree-sitter's runtime and +/// pardes's highlighter; or, with no language, the patch view alone, with +/// no tree-sitter in it. Its exports are abi/wasm.zig's (`memory`, +/// `_initialize`, `reserve`, `release`, `patch`, and with a language +/// `highlight`), the same in every module; docs/typ/building.typ has their +/// layouts and the WASI imports a page stubs. The package's dependency on +/// pardes is named "pardes". A name the manifest does not have fails the +/// configure, as `.languages` does. +pub fn wasmModule(b: *std.Build, options: struct { + language: ?[]const u8 = null, + optimize: std.lang.Optimize = .ReleaseSmall, +}) *std.Build.Step.Compile { + const target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .wasi }); + const pardes = if (options.language) |name| + b.dependency("pardes", .{ .target = target, .optimize = options.optimize, .languages = name }) + else + b.dependency("pardes", .{ .target = target, .optimize = options.optimize, .grammars = .none }); + const exports = b.addOptions(); + exports.addOption(bool, "highlight", options.language != null); + const module = b.addExecutable(.{ + .name = b.fmt("pardes-{s}", .{options.language orelse "patch"}), + .root_module = b.createModule(.{ + .target = target, + .optimize = options.optimize, + .root_source_file = pardes.path("abi/wasm.zig"), + .imports = &.{ + .{ .name = "pardes-syntax", .module = pardes.module("pardes-syntax") }, + .{ .name = "wasm_options", .module = exports.createModule() }, + }, + }), + }); + module.entry = .disabled; + module.wasi_exec_model = .reactor; + // Its own functions alone: tree-sitter's and libc's stay inside. + module.root_module.export_symbol_names = if (options.language != null) &.{ "reserve", "release", "patch", "highlight" } else &.{ "reserve", "release", "patch" }; + return module; +} + /// 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 { @@ -401,13 +461,38 @@ fn installHome(b: *std.Build, artifact: *std.Build.Step.Compile, home_bin: ?[]co b.getInstallStep().dependOn(©.step); } +/// The grammars a dependent package asked for: the languages it named and +/// what each needs (the manifest's), or its tier's. A language the manifest +/// does not have, or a tier beside languages, ends the configure: Zig +/// reports an option it cannot read and builds on (exit code 0). +fn librarySet(grammars: ?TreeSitterGrammars, languages: ?[]const []const u8) core.GrammarSet { + const names = languages orelse return core.tierSet(grammars orelse .full); + if (grammars) |tier| fatal("pardes: `.languages` replaces `.grammars` (here .{t}): give one of them", .{tier}); + var set: core.GrammarSet = @splat(false); + for (names) |name| { + const i = grammar_manifest.byName(name) orelse fatal("pardes: no language \"{s}\" in its manifest (`.languages`); it has:{s}", .{ name, comptime known: { + var all: []const u8 = ""; + for (grammar_manifest.all) |g| all = all ++ " " ++ g.name; + break :known all; + } }); + set[i] = true; + for (grammar_manifest.all[i].needs) |need| set[grammar_manifest.byName(need).?] = true; + } + return set; +} + +fn fatal(comptime format: []const u8, args: anytype) noreturn { + std.log.err(format, args); + std.process.exit(1); +} + /// What a package that depends on pardes is given (the head comment): /// `pardes-syntax`, the grammars, highlighting and themes, over tree-sitter /// and the grammars of its set alone; and with `editor`, `pardes-editor`, /// whose 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. -fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, grammars: TreeSitterGrammars, editor: bool) void { +fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.Optimize, set: core.GrammarSet, editor: bool) void { var threaded: std.Io.Threaded = .init(b.allocator, .{}); defer threaded.deinit(); const io = threaded.io(); @@ -417,10 +502,11 @@ fn library(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.lang.O .root_source_file = b.path("core/lib_syntax.zig"), .link_libc = true, }); - // A library's options: its grammars, nothing stamped, no window. - const cfg: core.ShellConfig = .{ .tree_sitter_grammars = grammars }; + // A library's options: nothing stamped, no window, and a tier only + // pardes's own tests read (syntax.zig knows the set by its queries). + const cfg: core.ShellConfig = .{ .tree_sitter_grammars = if (std.mem.eql(bool, &set, &core.tierSet(.full))) .full else if (std.mem.eql(bool, &set, &core.tierSet(.minimal))) .minimal else .none }; mod.addOptions("pardes_options", core.options(b, cfg, .{ .root = "core/lib_syntax.zig", .platform = .tui, .terminal = false }, false)); - const ts = core.treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, grammars); + const ts = core.treeSitter(b, io, target, if (optimize == .debug) .fast else optimize, set); const themes = core.generatedThemes(b, io, target, optimize); mod.addImport("generated_themes", themes); ts.wire(mod); |
