// 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 z46 = b.dependency("z46", .{ .target = target, .optimize = optimize, }); // One module. Decoding and encoding are two halves of one wire format and // share every type between them, so splitting them would cost a consumer a // dependency edge to save nothing: Zig analyzes only what is referenced, // so a program that never builds a message never compiles the builder. const mod = b.addModule("dns", .{ .root_source_file = b.path("src/root.zig"), .target = target, .imports = &.{ .{ .name = "z46", .module = z46.module("z46") }, }, }); // The captured messages, as a module of their own: the tests that read // them, the fuzzer that mutates them and the tool that hands them to // another implementation all want the same bytes, and a second copy of a // packet capture is a second thing to keep right. const corpus = b.addModule("corpus", .{ .root_source_file = b.path("tests/corpus.zig"), .target = target, }); // 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"); const check_step = b.step("check", "Compile everything without running it"); const mod_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "z46", .module = z46.module("z46") }, }, }), }); test_step.dependOn(&b.addRunArtifact(mod_tests).step); // The tests that are about the library rather than about one file of it: // messages captured from real servers, decoded and built again, and the // properties that must hold of any input at all. for ([_][]const u8{ "tests/messages.zig", "tests/fuzz.zig" }) |path| { const t = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path(path), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "dns", .module = mod }, .{ .name = "corpus", .module = corpus }, }, }), }); test_step.dependOn(&b.addRunArtifact(t).step); } // 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=name`. const fuzz_filter = b.option( []const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this", ); const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "dns", .module = mod }, .{ .name = "corpus", .module = corpus }, }, }); 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 nothing to // steer by: a bounded run ends with "pcs_len was zero". .use_llvm = true, }); // 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: bounded // by a count or a clock, repeatable from a seed, and able to replay one // input, which is what CI wants. Optimized, 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-dns-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); // Nothing else builds it, so without this it could stop compiling and // `zig build test` would not notice. check_step.dependOn(&fuzz_run.step); // -- the differential check ---------------------------------------------- // // Everything above is this library agreeing with itself, which cannot // catch the decoder and the encoder being wrong the same way. The check // for that is to put the same bytes to a decoder written by somebody else // and compare the verdicts. It is a step of its own rather than part of // `test` because the other decoder is dnspython, which is a Python package // rather than anything Zig can fetch: run it from inside `nix develop`, // where flake.nix puts one on the path. const verdict = b.addExecutable(.{ .name = "zig-dns-verdict", .root_module = b.createModule(.{ .root_source_file = b.path("tools/verdict.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{ .{ .name = "dns", .module = mod }, .{ .name = "corpus", .module = corpus }, }, }), }); const differential = b.addSystemCommand(&.{"python3"}); differential.addFileArg(b.path("tests/differential.py")); differential.addArtifactArg(verdict); differential.addPassthruArgs(); // It prints a table and runs for a while, so its output has to reach the // terminal rather than being captured by the build runner. differential.stdio = .inherit; const differential_step = b.step( "differential", "Compare the decoder against dnspython (needs the devshell)", ); differential_step.dependOn(&differential.step); test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = verdict.root_module }), ).step); check_step.dependOn(&verdict.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 = "dns", .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); }