// 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 = passlibModule(b, target, optimize, true); // 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"); test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = mod })).step); // passlib's own known-answer tables and the hashes passlib 1.7.4 made for // `tools/gen_vectors.py`, run against every scheme through the registry. // `-Dvector-scheme=NAME` narrows them to one scheme. const vector_options = b.addOptions(); vector_options.addOption(?[]const u8, "scheme", b.option( []const u8, "vector-scheme", "Run only this scheme's rows of the passlib vectors", )); // Always at least ReleaseSafe: the tables include Argon2 at 64 MiB and // bcrypt at cost 12, which take most of a minute in a Debug build, and // ReleaseSafe keeps every check that makes a failure a failure. const vector_optimize: std.builtin.OptimizeMode = if (optimize == .Debug) .ReleaseSafe else optimize; const vector_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tests/vectors.zig"), .target = target, .optimize = vector_optimize, .imports = &.{ .{ .name = "passlib", .module = passlibModule(b, target, vector_optimize, false) }, .{ .name = "vector_options", .module = vector_options.createModule() }, }, }), }); test_step.dependOn(&b.addRunArtifact(vector_tests).step); const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&vector_tests.step); // The fuzz targets: what every scheme must do with a hash string nobody // chose. They are ordinary tests as well as fuzz tests, so `zig build // test` exercises the same properties on their seeds. const fuzz_mod = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "passlib", .module = mod }}, }); 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 &.{}, }); 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, which // this toolchain cannot usefully run: `tools/fuzz.zig` says why. // 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-passlib-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "fuzz_targets", .module = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{.{ .name = "passlib", .module = passlibModule(b, b.graph.host, .ReleaseSafe, false) }}, }) }}, }), }); // The converse of the vectors: hashes made here, checked by passlib // 1.7.4 itself. It needs the devshell's python, so it is a step of its // own rather than part of `test`. const crosscheck = b.addExecutable(.{ .name = "zig-passlib-crosscheck", .root_module = b.createModule(.{ .root_source_file = b.path("tools/crosscheck.zig"), .target = b.graph.host, .optimize = .ReleaseSafe, .imports = &.{ .{ .name = "passlib", .module = fuzz_run.root_module.import_table.get("fuzz_targets").?.import_table.get("passlib").? }, .{ .name = "fuzz_targets", .module = fuzz_run.root_module.import_table.get("fuzz_targets").? }, }, }), }); const run_crosscheck = b.addRunArtifact(crosscheck); const hashes = run_crosscheck.addOutputFileArg("hashes.tsv"); const run_passlib = b.addSystemCommand(&.{"python3"}); run_passlib.addFileArg(b.path("tools/crosscheck.py")); run_passlib.addFileArg(hashes); run_passlib.has_side_effects = true; const crosscheck_step = b.step("crosscheck", "Have passlib 1.7.4 verify hashes made here (needs the devshell)"); crosscheck_step.dependOn(&run_passlib.step); check_step.dependOn(&crosscheck.step); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; if (b.args) |a| run_fuzz.addArgs(a); 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); // -- 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 = "passlib", .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. 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.addArg(b.getInstallPath(.prefix, "docs")); 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); // It has a test of its own; without this it would never run. And nothing // else builds it, so it belongs in the check step too. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); } /// The library module and its two dependencies, built for `target` at /// `optimize`. `public` makes it the module this package exports; the fuzz /// driver builds a private one for the host. fn passlibModule(b: *std.Build, target: std.Build.ResolvedTarget, optimize: std.builtin.OptimizeMode, public: bool) *std.Build.Module { // MD4, crypt(3) DES, DES and Argon2 version 1.0 come from here: the // primitives that `std.crypto` leaves out. const crypto_ext = b.dependency("std_crypto_ext", .{ .target = target, .optimize = optimize, }); // SASLprep, which passlib's `scram` scheme runs over a password before // hashing it. It brings the Unicode tables for NFKC with it. const scram = b.dependency("scram", .{ .target = target, .optimize = optimize, }); const options: std.Build.Module.CreateOptions = .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "std_crypto_ext", .module = crypto_ext.module("std_crypto_ext") }, .{ .name = "scram", .module = scram.module("scram") }, }, }; return if (public) b.addModule("passlib", options) else b.createModule(options); }