// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{}); // One module. The tables are the bulk of it and every codec is a few // dozen lines on top of them, so splitting per encoding would cost // consumers a dependency edge to save nothing: Zig analyses only what is // referenced, and a program that decodes windows-1252 never compiles the // Big5 tables into its binary. const mod = b.addModule("charset", .{ .root_source_file = b.path("src/root.zig"), .target = target, }); const test_step = b.step("test", "Run tests"); const check_step = b.step("check", "Compile everything without running it"); // A test executable covers one module, so each needs its own. Missing one // out would not fail: its tests would simply never run. test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod })).step); // The conformance tests, which walk whole encodings rather than spot // checks, and the round-trip properties. A module of their own so that // `zig build test` runs them while a consumer never compiles them. const conformance_mod = b.createModule(.{ .root_source_file = b.path("tests/conformance.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "charset", .module = mod }}, }); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = conformance_mod })).step); // -- fuzzing ------------------------------------------------------------- // // What the decoders and encoders must do with input nobody wrote. These // are ordinary tests as well as fuzz tests, 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 = "charset", .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=decode`. const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .filters = if (fuzz_filter) |f| &.{f} else &.{}, // The self-hosted backend, which Debug uses by default, emits no // coverage instrumentation, and without it the fuzzer has no feedback // to go on: `--fuzz=N` ends with "pcs_len was zero". .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); // A step of its own for `zig build fuzz --fuzz`, which needs a run step // holding nothing else: the fuzzer takes over the terminal and runs until // it is stopped, so it must not be reached by `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: no // coverage feedback, but a corpus of inputs that already reach the // interesting states, mutated, and a run that stops after a fixed number // of inputs, which is what CI wants. 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 = "zig-charset-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_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); check_step.dependOn(&fuzz_run.step); // -- cross-checking against somebody else's implementation --------------- // // The tables are generated from the index files, so a test written // against the tables proves only that the generator and the codecs agree. // `tools/crosscheck.py` asks Python's codecs module the same questions // and diffs the answers; this is the half that produces ours. const crosscheck = b.addExecutable(.{ .name = "crosscheck", .root_module = b.createModule(.{ .root_source_file = b.path("tools/crosscheck.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "charset", .module = mod }}, }), }); const run_crosscheck = b.addRunArtifact(crosscheck); run_crosscheck.stdio = .inherit; run_crosscheck.addPassthruArgs(); const crosscheck_step = b.step("crosscheck", "Dump what this library does, for tools/crosscheck.py to diff"); crosscheck_step.dependOn(&run_crosscheck.step); check_step.dependOn(&crosscheck.step); // -- the generated tables ------------------------------------------------ // // `src/tables/` is committed, so this step exists for the day an index // changes upstream rather than for every build. It reads `data/`, which // is deliberately outside the package's `paths`: a consumer fetching this // library has no use for three megabytes of index files, and without them // the step simply is not offered. Looking at the source tree while // configuring is something the build cache has to be told about, or it // would replay a configuration made before `data/` appeared. if (b.root.root_dir.handle.access(b.graph.io, "data", .{})) |_| { b.dependOnDirectoryMetadata(b.path("data")); const gen = b.addExecutable(.{ .name = "gen-tables", .root_module = b.createModule(.{ .root_source_file = b.path("tools/gen_tables.zig"), .target = b.graph.host, .optimize = .Debug, }), }); const run_gen = b.addRunArtifact(gen); run_gen.addArg(b.root.joinString(b.allocator, "data") catch @panic("OOM")); run_gen.addArg(b.root.joinString(b.allocator, "src/tables") catch @panic("OOM")); // It writes into the source tree, so it must run every time it is // asked rather than being cached against its inputs. run_gen.has_side_effects = true; run_gen.stdio = .inherit; const gen_step = b.step("gen-tables", "Regenerate src/tables from data/"); gen_step.dependOn(&run_gen.step); check_step.dependOn(&gen.step); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = gen.root_module })).step); } else |_| {} // -- 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 = "charset", .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.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); }