// 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("brotli", .{ .root_source_file = b.path("src/root.zig"), // Set here rather than only on the test binaries, so that // `zig build test -Doptimize=...` reaches the module's own tests. .target = target, .optimize = optimize, }); // Renders the doc comments into `zig-out/docs`, which CI publishes to // https://jeff.jcollie.page/zig-brotli/. The object file exists only // because compiling one is what emits the documentation. const docs_obj = b.addObject(.{ .name = "brotli", .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 from a `file://` page, so reading the docs // locally means serving them. 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 the machine running the build, never 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(docs_obj.getEmittedDocs()); run_docs_server.addArg(b.fmt("{d}", .{docs_port})); // It 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 command-line front end, compressing or decompressing standard input // to standard output; installed by `zig build`. const cli = b.addExecutable(.{ .name = "zbrotli", .root_module = b.createModule(.{ .root_source_file = b.path("tools/zbrotli.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "brotli", .module = mod }}, }), }); b.installArtifact(cli); // Checks this library against the reference implementation, in both // directions, through the command-line front end. Needs Python with the // `brotli` module, which the devshell has. const interop = b.addSystemCommand(&.{"python3"}); interop.addFileArg(b.path("tools/interop.py")); interop.addArtifactArg(cli); const interop_step = b.step("interop", "Check against the reference implementation"); interop_step.dependOn(&interop.step); // The test vectors live outside `src`, where `@embedFile` from the // library module cannot reach, so the tests that decode them are a // module of their own that imports the library like any other user. const vectors_mod = b.createModule(.{ .root_source_file = b.path("tests/tests.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "brotli", .module = mod }}, }); // Both are compiled by LLVM even in Debug. The self-hosted backend emits // no table of program counters, so `zig build test --fuzz` would have no // coverage to steer by, and kcov cannot read its debug info either. const mod_tests = b.addTest(.{ .name = "brotli-test", .root_module = mod, .use_llvm = true }); const vector_tests = b.addTest(.{ .name = "vector-test", .root_module = vectors_mod, .use_llvm = true }); 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, vector_tests, docs_server_tests }) |t| { test_step.dependOn(&b.addRunArtifact(t).step); } // Compile everything without running it. const check_step = b.step("check", "Check that everything compiles"); check_step.dependOn(&mod_tests.step); check_step.dependOn(&vector_tests.step); check_step.dependOn(&docs_server.step); check_step.dependOn(&cli.step); // kcov runs the tests under a tracer and writes an HTML report to // `zig-out/coverage`, restricted to `src` so that the standard library // stays out of the numbers. const run_kcov = b.addSystemCommand(&.{ "kcov", "--include-path=src" }); const coverage_output = run_kcov.addOutputDirectoryArg("coverage"); run_kcov.addArtifactArg(mod_tests); const run_kcov_vectors = b.addSystemCommand(&.{ "kcov", "--include-path=src" }); run_kcov_vectors.addDirectoryArg(coverage_output); run_kcov_vectors.addArtifactArg(vector_tests); const coverage_step = b.step("coverage", "Generate a test coverage report with kcov"); const install_coverage = b.addInstallDirectory(.{ .source_dir = coverage_output, .install_dir = .prefix, .install_subdir = "coverage", }); install_coverage.step.dependOn(&run_kcov_vectors.step); coverage_step.dependOn(&install_coverage.step); }