// 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(.{}); const mod = b.addModule("z46", .{ .root_source_file = b.path("src/root.zig"), // The optimize mode has to be set here, not only on the executable // below: without it the module's tests always build in Debug and // `zig build test -Doptimize=...` reaches only the executable's tests. .target = target, .optimize = optimize, }); // The library also ships a CLI that validates and describes its arguments. const exe = b.addExecutable(.{ .name = "z46", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z46", .module = mod }, }, }), }); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run the app"); run_step.dependOn(&run_cmd.step); // Renders the module's doc comments into browsable HTML under // `zig-out/docs`, which CI publishes to https://jeff.jcollie.page/z46/. // The documentation comes from an object file because that is what emits // it; the object is never linked or installed. const docs_obj = b.addObject(.{ .name = "z46", .root_module = mod, }); const install_docs = b.addInstallDirectory(.{ .source_dir = docs_obj.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Generate 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); // Served straight from the emitted directory rather than from // `zig-out/docs`, because the build no longer knows the install prefix // while it is being configured. Depending on the install step keeps // `docs-serve` leaving the same `zig-out/docs` behind that `docs` does. run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(docs_obj.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); // A test binary covers one module at a time, so the library, the CLI and // the documentation server need one each. // // The library's tests are always compiled by LLVM, which is what // ReleaseSafe and ReleaseFast use anyway; only Debug would otherwise get // the self-hosted backend. That backend emits no table of program // counters, so `zig build test --fuzz` has nothing to measure coverage // against -- a bounded run ends with "pcs_len was zero" and an unbounded // one panics in the build runner -- and kcov cannot read its debug info // either. The price is a Debug compile of a few seconds instead of under // one. const mod_tests = b.addTest(.{ .name = "z46-test", .root_module = mod, .use_llvm = true, }); const exe_tests = b.addTest(.{ .name = "z46-cli-test", .root_module = exe.root_module }); const docs_server_tests = b.addTest(.{ .name = "docs-server-test", .root_module = docs_server.root_module, }); const test_step = b.step("test", "Run tests"); for ([_]*std.Build.Step.Compile{ mod_tests, exe_tests, docs_server_tests }) |test_binary| { test_step.dependOn(&b.addRunArtifact(test_binary).step); } // Benchmarks are only meaningful optimized, so the library is built again // as ReleaseFast here rather than reusing `mod`, whose optimize mode // follows `-Doptimize` and defaults to Debug. const bench_mod = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = .ReleaseFast, }); const bench_exe = b.addExecutable(.{ .name = "z46-bench", .root_module = b.createModule(.{ .root_source_file = b.path("tools/bench.zig"), .target = target, .optimize = .ReleaseFast, .imports = &.{ .{ .name = "z46", .module = bench_mod }, }, }), }); // The benchmark harness has tests of its own, and nothing else compiles // it, so without this it could stop building and no test would notice. const bench_tests = b.addTest(.{ .name = "bench-test", .root_module = bench_exe.root_module, }); test_step.dependOn(&b.addRunArtifact(bench_tests).step); const run_bench = b.addRunArtifact(bench_exe); // Timings go straight to the terminal; capturing them would serve nothing. run_bench.stdio = .inherit; const bench_step = b.step("bench", "Run the microbenchmarks"); bench_step.dependOn(&run_bench.step); // kcov runs the library's tests under a tracer and writes an HTML report // to `zig-out/coverage`. Restricting it to `src` keeps the standard // library, which these tests exercise heavily, out of the numbers. // // Only the library's tests are measured. kcov reads the CLI and // documentation-server test binaries without error but records no lines // for this package's files in them, even though their debug info names the // same source directory, so including them would cost a traced run each // and change nothing in the report. // // Coverage wants an unoptimised build: under `-Doptimize=ReleaseFast` the // inlining leaves the line counts meaningless. // // It relies on `mod_tests` being compiled by LLVM. Built by the // self-hosted backend, the binary still runs under kcov and every test // still passes, but the report comes out as 0.00% of zero lines rather // than as an error. // // A note on reading the output: the build runner prints a `failed command` // line for any step that writes to stderr, which a passing test run does. // The exit status is what says whether the step failed. const run_kcov = b.addSystemCommand(&.{ "kcov", "--include-path=src" }); const coverage_output = run_kcov.addOutputDirectoryArg("coverage"); run_kcov.addArtifactArg(mod_tests); const coverage_step = b.step("coverage", "Generate a test coverage report with kcov"); coverage_step.dependOn(&b.addInstallDirectory(.{ .source_dir = coverage_output, .install_dir = .prefix, .install_subdir = "coverage", }).step); }