Something went wrong. Try again.
A URI parser and formatter for Zig, modelled on Python's hyperlink package, with LDAP URLs, file: URIs and sdns:// DNS stamps beside it.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
/// Unicode character properties this library needs from uucode. Listing them/// explicitly keeps the generated tables to only what IDNA actually consults.const uucode_fields: []const []const u8 = &.{ // Canonical composition, for the NFC pass UTS #46 requires. "canonical_combining_class", "decomposition_type", "decomposition_mapping", "is_composition_exclusion", // Validity and the CONTEXTJ/CONTEXTO and Bidi rules. "general_category", "bidi_class", "joining_type", "script",};
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
// Off by default: without it the library has no dependencies at all, and // `idna` and IRI output are compile-time unavailable. const unicode = b.option( bool, "unicode", "Enable IDNA and IRI support (adds a dependency on uucode)", ) orelse false;
const options = b.addOptions(); options.addOption(bool, "unicode", unicode);
// The published module carries no target/optimize of its own, so a // dependent builds it for whatever it is building for. const uri = b.addModule("uri", .{ .root_source_file = b.path("src/root.zig"), }); uri.addOptions("build_options", options);
const test_module = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); test_module.addOptions("build_options", options);
// Address literals are core RFC 3986 syntax rather than an add-on, so z46 // is an unconditional dependency. It is small, allocation-free and has no // dependencies of its own. const z46 = b.dependency("z46", .{ .target = target, .optimize = optimize, }).module("z46"); uri.addImport("z46", z46); test_module.addImport("z46", z46);
if (unicode) { if (b.lazyDependency("uucode", .{ .target = target, .optimize = optimize, .fields = uucode_fields, })) |dep| { const uucode = dep.module("uucode"); uri.addImport("uucode", uucode); test_module.addImport("uucode", uucode); } }
// -- WebAssembly ------------------------------------------------------ // // A freestanding wasm32 module with a flat, string-in/string-out ABI, for // `web/uri.js` to drive from a browser. It is a separate root // (`src/wasm.zig`) rather than the library module itself, because the // exports have to be reachable from the compilation root to be emitted. const wasm_optimize = b.option( std.builtin.OptimizeMode, "wasm-optimize", "Optimization mode for `zig build wasm` (default ReleaseSmall)", ) orelse .ReleaseSmall;
const wasm_target = b.resolveTargetQuery(.{ .cpu_arch = .wasm32, .os_tag = .freestanding, });
const wasm_module = b.createModule(.{ .root_source_file = b.path("src/wasm.zig"), .target = wasm_target, .optimize = wasm_optimize, }); wasm_module.addOptions("build_options", options); wasm_module.addImport("z46", b.dependency("z46", .{ .target = wasm_target, .optimize = wasm_optimize, }).module("z46"));
if (unicode) { if (b.lazyDependency("uucode", .{ .target = wasm_target, .optimize = wasm_optimize, .fields = uucode_fields, })) |dep| { wasm_module.addImport("uucode", dep.module("uucode")); } }
const wasm = b.addExecutable(.{ .name = "uri", .root_module = wasm_module, }); // No `main`: the module is a bag of exports the host calls into, and // `rdynamic` is what keeps them from being stripped as unreachable. wasm.entry = .disabled; wasm.rdynamic = true;
// The loader, the demo page and the module all end up in one directory, // which is the whole of what has to be served. const install_wasm = b.addInstallArtifact(wasm, .{ .dest_dir = .{ .override = .{ .custom = "web" } }, }); const install_web = b.addInstallDirectory(.{ .source_dir = b.path("web"), .install_dir = .prefix, .install_subdir = "web", // The test suite is run from the source tree, and has no business in // a directory whose whole purpose is to be served. .exclude_extensions = &.{".test.js"}, });
const wasm_step = b.step("wasm", "Build the WebAssembly module and its JavaScript loader"); wasm_step.dependOn(&install_wasm.step); wasm_step.dependOn(&install_web.step);
// Exercising the JavaScript needs a JavaScript engine, so this is its own // step rather than part of `zig build test`: `node` is in the devshell but // is not something the library itself requires. const run_web_tests = b.addSystemCommand(&.{"node"}); run_web_tests.addFileArg(b.path("web/uri.test.js")); run_web_tests.addArg(b.getInstallPath(.prefix, "web/uri.wasm")); run_web_tests.step.dependOn(&install_wasm.step); run_web_tests.stdio = .inherit;
const web_test_step = b.step("test-web", "Run the JavaScript binding's tests under node"); web_test_step.dependOn(&run_web_tests.step);
const unit_tests = b.addTest(.{ .root_module = test_module }); const run_unit_tests = b.addRunArtifact(unit_tests);
const test_step = b.step("test", "Run unit tests"); test_step.dependOn(&run_unit_tests.step);
// -- fuzzing ---------------------------------------------------------- // // The targets in `src/fuzz.zig` are ordinary tests as well, so `zig build // test` runs each against one smoke input and they cost that run nothing. // Searching is `zig build test --fuzz=N`, which in Zig 0.16.0 cannot steer // -- the coverage table comes out empty -- so `zig build fuzz-run` is a loop // of our own with a corpus in place of the feedback. `tools/fuzz.zig` says // what that costs and what it buys. // // 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_module = b.createModule(.{ .root_source_file = b.path("src/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, }); fuzz_module.addOptions("build_options", options); fuzz_module.addImport("z46", b.dependency("z46", .{ .target = b.graph.host, .optimize = .ReleaseSafe, }).module("z46")); if (unicode) { if (b.lazyDependency("uucode", .{ .target = b.graph.host, .optimize = .ReleaseSafe, .fields = uucode_fields, })) |dep| { fuzz_module.addImport("uucode", dep.module("uucode")); } }
const fuzz_run = b.addExecutable(.{ .name = "zig-uri-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "fuzz_targets", .module = fuzz_module }}, }), });
const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; if (b.args) |args| run_fuzz.addArgs(args); const fuzz_run_step = b.step("fuzz-run", "Fuzz the targets with a loop of our own"); fuzz_run_step.dependOn(&run_fuzz.step);
// Nothing else builds the driver, so without this it could stop compiling // and no test would notice. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&fuzz_run.step);
const docs = b.addObject(.{ .name = "uri", .root_module = test_module, }); const install_docs = b.addInstallDirectory(.{ .source_dir = docs.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation"); docs_step.dependOn(&install_docs.step);
// The generated 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. This is the same reason `zig std` runs a // server rather than just 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.addArg(b.getInstallPath(.prefix, "docs")); 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);
// A wasm module fetched from a `file://` page is refused by every browser, // so trying the demo means serving it too. The docs server already knows // the handful of MIME types involved, so it does this job as well. const web_port = b.option(u16, "web-port", "Port for `zig build web-serve` (default 8001)") orelse 8001;
const run_web_server = b.addRunArtifact(docs_server); run_web_server.step.dependOn(&install_wasm.step); run_web_server.step.dependOn(&install_web.step); run_web_server.addArg(b.getInstallPath(.prefix, "web")); run_web_server.addArg(b.fmt("{d}", .{web_port})); run_web_server.stdio = .inherit;
const web_serve_step = b.step("web-serve", "Serve the WebAssembly demo page over HTTP"); web_serve_step.dependOn(&run_web_server.step);
// The server has tests of its own; without this they would never run. const docs_server_tests = b.addTest(.{ .root_module = docs_server.root_module }); test_step.dependOn(&b.addRunArtifact(docs_server_tests).step); check_step.dependOn(&docs_server.step);}