Something went wrong. Try again.
An XSLT 1.0 processor for Zig 0.16, built on zxml.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
/// Every locale CLDR 48.2 ships a collation file for, which is what/// `-Dlocales=all` means and what a default build compiles in. 112 of them/// carry rules; the other 23 -- `de`, `fr`, `en`, `it`, `nl`, `pt` and the/// rest -- need no tailoring at all, because the root collation is already/// their answer, and naming them costs nothing but makes the list CLDR's/// rather than a selection of somebody's favourite languages.////// Written out rather than read from the archive because the archive is a/// lazy dependency of zig-collate and this build cannot see it. A name CLDR/// drops fails the build at the generator, naming the file it could not find,/// which is the loud half of the bargain; a name CLDR adds is simply missed/// until this list is updated alongside the dependency.const cldr_locales = [_][]const u8{ "aa", "af", "am", "ar", "as", "az", "bal", "bal_Latn", "be", "bg", "blo", "bn", "bo", "br", "bs", "bs_Cyrl", "ca", "ceb", "chr", "cs", "cu", "cy", "da", "de", "de_AT", "dsb", "dz", "ee", "el", "en", "en_US", "en_US_POSIX", "eo", "es", "et", "fa", "fa_AF", "ff", "ff_Adlm", "fi", "fil", "fo", "fr", "fr_CA", "fy", "ga", "gl", "gu", "ha", "haw", "he", "hi", "hr", "hsb", "hu", "hy", "id", "ig", "is", "it", "ja", "ka", "kk", "kk_Arab", "kl", "km", "kn", "ko", "kok", "ku", "ky", "lb", "lij", "lkt", "ln", "lo", "lt", "lv", "mk", "ml", "mn", "mr", "ms", "mt", "my", "nb", "ne", "nl", "nn", "no", "nso", "om", "or", "pa", "pl", "ps", "pt", "ro", "ru", "sa", "se", "sgs", "si", "sk", "sl", "smn", "sq", "sr", "sr_Latn", "ssy", "st", "sv", "sw", "ta", "te", "th", "tk", "tn", "to", "tr", "ug", "uk", "ur", "uz", "vi", "vo", "wae", "wo", "xh", "yi", "yo", "zh", "zh_Hant", "zu",};
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const zxpath = b.dependency("zxpath", .{ .target = target, .optimize = optimize, });
// One module. XPath is not a separable library here: the evaluator needs // the tree, the tree's node identity is what a node-set holds, and the // XSLT engine calls into both on nearly every instruction. Splitting them // would cost a consumer three dependency edges to save nothing, since Zig // only analyses what is referenced. const datetime = b.dependency("datetime", .{ .target = target, .optimize = optimize, });
const locales_option = b.option( []const u8, "locales", "CLDR collation tailorings to compile in for `xsl:sort lang=`, comma " ++ "separated: -Dlocales=sv,tr. `all` is every locale CLDR ships a " ++ "collation for, and is the default; an empty list compiles none, " ++ "and every language then gets the root collation -- which is not " ++ "an error, since XSLT 1.0 leaves the ordering `lang` produces " ++ "implementation-defined (default: all)", ) orelse "all"; const locales = if (std.mem.eql(u8, locales_option, "all")) std.mem.join(b.allocator, ",", &cldr_locales) catch @panic("OOM") else locales_option;
const collate = b.dependency("collate", .{ .target = target, .optimize = optimize, // CLDR resolves its per-locale rules against CLDR's own root // collation, not against DUCET, so a tailoring laid over DUCET is a // delta computed against a table it is not being applied to. It // happens to agree for the Latin letters and is not guaranteed to // anywhere else -- so asking for a tailoring means asking for the // root it was written against. `Options.collation` follows this: see // `root_table` in `src/transform.zig`. // // The cost is the reason it is not simply always on. The CLDR rules // arrive in a 66 MB archive, which a build that has asked for no // tailorings has no use for. .@"embed-cldr" = locales.len != 0, .locales = locales, });
const zregex = b.dependency("zregex", .{ .target = target, .optimize = optimize, });
const mod = b.addModule("zxsl", .{ .root_source_file = b.path("src/root.zig"), .target = target, .imports = &.{ .{ .name = "zxpath", .module = zxpath.module("zxpath") }, .{ .name = "ztree", .module = zxpath.builder.dependency("ztree", .{ .target = target, .optimize = optimize, }).module("ztree") }, // EXSLT's dates-and-times module is a calendar, and a calendar is // the kind of thing that is wrong in the same way in every project // that writes its own. .{ .name = "datetime", .module = datetime.module("datetime") }, // And `xsl:sort` needs a collation, which is the same kind of // thing for the same reason. .{ .name = "collate", .module = collate.module("collate") }, // As is a regular expression engine, which EXSLT's `regexp` // module is three functions on top of. .{ .name = "zregex", .module = zregex.module("zregex") }, }, });
const exe = b.addExecutable(.{ .name = "zxsl", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "zxsl", .module = mod }}, }), }); b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.stdio = .inherit; run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the transformer"); run_step.dependOn(&run_cmd.step);
// A test executable covers one module, so each needs its own. Missing one // out would not fail: its tests would simply never run. const test_step = b.step("test", "Run tests"); for ([_]*std.Build.Module{ mod, exe.root_module }) |m| { test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = m })).step); }
// The oracle suite: every case in `tests/cases` transformed and compared // against what libxslt produced for it. It is a module of its own so that // a consumer of the library never compiles it. // // The cases are read from disk at run time rather than embedded, so that // adding one is adding a directory and nothing has to be registered. The // path comes through build options because a test binary is run from // wherever the build runner happens to put it. const case_options = b.addOptions(); case_options.addOptionPathDirectory("cases_dir", b.path("tests/cases")); // The second suite, recorded from Saxon rather than libxslt, because // libxslt is XSLT 1.0 only and so cannot say anything about 2.0. case_options.addOptionPathDirectory("cases_20_dir", b.path("tests/cases-20"));
const conformance_mod = b.createModule(.{ .root_source_file = b.path("tests/conformance.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "zxsl", .module = mod }, .{ .name = "build_options", .module = case_options.createModule() }, }, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = conformance_mod })).step);
// The fuzz targets: what the expression parser, the tree builder, the // stylesheet reader and the engine must do with input nobody wrote. They // are ordinary tests as well as fuzz targets, so `zig build test` // exercises the same properties on the seeds checked in beside them. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "zxsl", .module = mod }}, }); // Zig's fuzzer takes one test at a time and keeps a coverage file per // test, so naming a target is what you want when a finding is being // chased: `zig build fuzz --fuzz -Dfuzz-filter=xpath`. const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); // LLVM, because the self-hosted backend that Debug uses by default emits // no coverage instrumentation: under it the fuzzer's table of program // counters comes back empty, and `--fuzz` fails in the build runner rather // than finding anything. `build.zig` cannot see whether `--fuzz` was // passed, so this is always on, at a cost of a few seconds of compiling. const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .filters = if (fuzz_filter) |f| &.{f} else &.{}, .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// A step of its own for `zig build fuzz --fuzz`, holding nothing else: the // fuzzer takes over the terminal and runs until it is stopped, so it must // not be reachable from `zig build test`. const fuzz_step = b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(fuzz_tests).step);
// The loop that drives those same targets without Zig's fuzzer, from a // corpus of valid inputs and bounded by a count, which is what CI runs: // `tools/fuzz.zig` says why it is kept beside `--fuzz`. Optimised, because // a fuzzer's whole job is how many inputs it gets through, and ReleaseSafe // keeps every check that makes a failure a failure. const fuzz_run = b.addExecutable(.{ .name = "zxsl-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .safe, .imports = &.{.{ .name = "fuzz_targets", .module = fuzz_mod }}, }), }); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; run_fuzz.addPassthruArgs(); const fuzz_run_step = b.step("fuzz-run", "Fuzz the targets with a loop of our own"); fuzz_run_step.dependOn(&run_fuzz.step);
// Regenerates the `expected` file beside every case from libxslt. It is a // step rather than something the test run does on its own, because a test // that rewrites its own expectations cannot fail. const oracle = b.addSystemCommand(&.{"bash"}); oracle.addFileArg(b.path("tools/oracle.sh")); oracle.stdio = .inherit; oracle.addPassthruArgs(); const oracle_step = b.step("oracle", "Re-record tests/cases from libxslt (needs xsltproc)"); oracle_step.dependOn(&oracle.step);
// The same for the 2.0 cases, from Saxon. A separate step rather than a // flag on the one above, because they need different tools: this one // drags a JVM in, and somebody re-recording the 1.0 cases should not have // to have one. const oracle20 = b.addSystemCommand(&.{"bash"}); oracle20.addFileArg(b.path("tools/oracle20.sh")); oracle20.stdio = .inherit; oracle20.addPassthruArgs(); const oracle20_step = b.step("oracle20", "Re-record tests/cases-20 from Saxon (needs saxonb-xslt)"); oracle20_step.dependOn(&oracle20.step);
// libxslt's own test corpus, run by its own project in `corpus/`. It is // several megabytes of somebody else's tests under somebody else's // licence, so it is fetched rather than vendored, and it lives in a // separate manifest so that nothing which merely builds this library has // to know it exists. const corpus_step = b.step("corpus", "Run libxslt's test corpus, in corpus/"); const corpus_run = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "run" }); corpus_run.setCwd(b.path("corpus")); corpus_run.stdio = .inherit; corpus_run.addArg("--"); corpus_run.addPassthruArgs(); corpus_step.dependOn(&corpus_run.step);
// The W3C's own XSLT suite, run by its own project in `corpus-w3c/`. It // is 390 MB of somebody else's tests, so it is fetched rather than // vendored and lives in a manifest of its own; and it is separate from // `corpus/` because the two share only the discipline, not the shape -- // libxslt's cases are three files side by side and all in scope, the // W3C's are described by a catalog that says which specification each // needs and what counts as passing. const w3c_step = b.step("corpus-w3c", "Run the W3C XSLT test suite, in corpus-w3c/"); const w3c_run = b.addSystemCommand(&.{ b.graph.zig_exe, "build", "run" }); w3c_run.setCwd(b.path("corpus-w3c")); w3c_run.stdio = .inherit; w3c_run.addArg("--"); w3c_run.addPassthruArgs(); w3c_step.dependOn(&w3c_run.step);
const check_step = b.step("check", "Compile everything without running it"); // Nothing else builds the standalone fuzz driver, so without this it could // stop compiling and `zig build test` would not notice. check_step.dependOn(&fuzz_run.step);
// -- documentation ------------------------------------------------------- // // Zig emits the API documentation as a side effect of compiling, so the // module is built as a library purely to get at it. What comes out is not // a page but a program: a WebAssembly viewer, its javascript, and a tar of // the sources it reads from. const library = b.addLibrary(.{ .name = "zxsl", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); docs_step.dependOn(&install_docs.step);
// That viewer fetches `sources.tar` and `main.wasm` at runtime, which a // browser refuses to do from a `file://` page, so reading the docs // locally means serving them. It is the same reason `zig std` runs a // server rather than opening a file. const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000;
const docs_server = b.addExecutable(.{ .name = "docs-server", .root_module = b.createModule(.{ .root_source_file = b.path("tools/docs_server.zig"), // Always built for the machine running the build, never for // whatever -Dtarget the library is being built for. .target = b.graph.host, .optimize = .debug, }), });
const run_docs_server = b.addRunArtifact(docs_server); run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(library.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // The server runs until interrupted, so its output has to reach the // terminal rather than being captured by the build runner. run_docs_server.stdio = .inherit;
const docs_serve_step = b.step("docs-serve", "Serve the API documentation over HTTP"); docs_serve_step.dependOn(&run_docs_server.step);
// The server has tests of its own; without this they would never run. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step);}