// 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(.{}); // SASL mechanisms live outside this library, shared with the SMTP and // POP3 clients rather than implemented once each. const sasl_dep = b.dependency("sasl", .{ .target = target, .optimize = optimize, }); const mod = b.addModule("imap", .{ .root_source_file = b.path("src/root.zig"), .target = target, .imports = &.{ .{ .name = "sasl", .module = sasl_dep.module("sasl") }, }, }); const exe = b.addExecutable(.{ .name = "imap", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "imap", .module = mod }, }, }), }); b.installArtifact(exe); const run_step = b.step("run", "Run the demo CLI"); const run_cmd = b.addRunArtifact(exe); run_step.dependOn(&run_cmd.step); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); // Compiled with LLVM: the self-hosted backend Debug otherwise uses emits // no coverage instrumentation, so kcov would report nothing and // `zig build test --fuzz` would have no program counters to steer by. const mod_tests = b.addTest(.{ .root_module = mod, .use_llvm = true }); const run_mod_tests = b.addRunArtifact(mod_tests); const exe_tests = b.addTest(.{ .root_module = exe.root_module, .use_llvm = true }); const run_exe_tests = b.addRunArtifact(exe_tests); const test_step = b.step("test", "Run tests"); test_step.dependOn(&run_mod_tests.step); test_step.dependOn(&run_exe_tests.step); const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&exe.step); // The client against a server over a loopback socket, which is the only // way to cover `Connection` -- the socket, the buffering, the flushes -- // rather than the protocol alone. const e2e_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tests/e2e.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "imap", .module = mod }, }, }), .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(e2e_tests).step); // The driver for the NixOS virtual machine test, which needs a Dovecot to // talk to and so cannot be a test block. Installed only when asked for -- // a consumer of this library does not want it in their prefix -- but // always compiled by `check`, so it cannot rot unnoticed. const integration = b.addExecutable(.{ .name = "imap-integration", .root_module = b.createModule(.{ .root_source_file = b.path("tests/integration.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "imap", .module = mod }, }, }), }); if (b.option(bool, "integration", "Install the integration driver for the NixOS test") orelse false) b.installArtifact(integration); // The fuzz targets, reachable three ways: as test blocks (so `zig build // test` keeps them compiling and running against their corpus of // regressions), through `zig build test --fuzz` with the compiler's own // fuzzer, and from the standalone driver below. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "imap", .module = mod }, }, }); const fuzz_tests = b.addTest(.{ .root_module = fuzz_mod, .use_llvm = true }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); // A loop of its own over `std.testing.Smith`, mutating a corpus of real // inputs; it needs no coverage feedback, and makes a quick gate in CI: // // zig build fuzz -- --iterations 100000 const fuzz_exe = b.addExecutable(.{ .name = "imap-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "fuzz_targets", .module = fuzz_mod }, }, }), }); const run_fuzz = b.addRunArtifact(fuzz_exe); run_fuzz.stdio = .inherit; run_fuzz.addPassthruArgs(); const fuzz_step = b.step("fuzz", "Fuzz the parsers with the standalone driver"); fuzz_step.dependOn(&run_fuzz.step); check_step.dependOn(&fuzz_exe.step); check_step.dependOn(&integration.step); // 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 = "imap", .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); }