// 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("git", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); // Dates and time zones: git's date parsers, the time zone database and // strftime, none of them ported from git or glibc (ADR 0004). const datetime = b.dependency("datetime", .{ .target = target, .optimize = optimize }); mod.addImport("datetime", datetime.module("datetime")); // Charsets as glibc's iconv converts them, byte for byte, for commits // re-encoded for output. const charset = b.dependency("charset", .{ .target = target, .optimize = optimize }); mod.addImport("charset", charset.module("charset")); const test_filter = b.option( []const u8, "test-filter", "Run only the tests whose name contains this", ); const filters: []const []const u8 = if (test_filter) |f| &.{f} else &.{}; // LLVM, because the self-hosted backend that Debug uses otherwise emits // no coverage instrumentation: kcov reports nothing and Zig's fuzzer ends // with "corrupted coverage file: pcs_len was zero". const tests = b.addTest(.{ .name = "zig-git-test", .root_module = mod, .filters = filters, .use_llvm = true, }); const test_step = b.step("test", "Run tests"); test_step.dependOn(&b.addRunArtifact(tests).step); // Again, with the repositories the tests make using SHA-256 object IDs // rather than SHA-1's. `zig build test --fuzz` would fuzz both runs of // the one binary at once, over the same corpus and coverage, and fail // for it; the fuzz tests do not read repositories, so this run only // replays their corpus (`ZIG_GIT_TEST_NO_FUZZ`, src/testing/fuzz.zig) // and the first is the one fuzzed. const run_tests_sha256 = b.addRunArtifact(tests); run_tests_sha256.setEnvironmentVariable("ZIG_GIT_TEST_OBJECT_FORMAT", "sha256"); run_tests_sha256.setEnvironmentVariable("ZIG_GIT_TEST_NO_FUZZ", "1"); test_step.dependOn(&run_tests_sha256.step); // A step of its own for `zig build fuzz --fuzz`, which takes over the // terminal and runs until stopped, so it must not be reached by // `zig build test`. Without `--fuzz` it runs each fuzz test once on its // seeds, as `zig build test` already does. const fuzz_step = b.step("fuzz", "The tests, fuzz tests included: add --fuzz to fuzz them"); fuzz_step.dependOn(&b.addRunArtifact(tests).step); // Compiles everything without running it. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&tests.step); // -- zig-git ------------------------------------------------------------- // // A few of git's plumbing commands answered by the library, printing // what git prints, for comparing the two on any repository. const tool = b.addExecutable(.{ .name = "zig-git", .root_module = b.createModule(.{ .root_source_file = b.path("tools/zig_git.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "git", .module = mod }}, }), }); b.installArtifact(tool); check_step.dependOn(&tool.step); const run_tool = b.addRunArtifact(tool); run_tool.addPassthruArgs(); const run_step = b.step("run", "Run zig-git: zig build run -- [...]"); run_step.dependOn(&run_tool.step); // Its tests compare it with git, under both object formats. const tool_tests = b.addTest(.{ .name = "zig-git-tool-test", .root_module = tool.root_module, .filters = filters, .use_llvm = true, }); check_step.dependOn(&tool_tests.step); test_step.dependOn(&b.addRunArtifact(tool_tests).step); const run_tool_tests_sha256 = b.addRunArtifact(tool_tests); run_tool_tests_sha256.setEnvironmentVariable("ZIG_GIT_TEST_OBJECT_FORMAT", "sha256"); run_tool_tests_sha256.setEnvironmentVariable("ZIG_GIT_TEST_NO_FUZZ", "1"); test_step.dependOn(&run_tool_tests_sha256.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 = "git", .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.addDirectoryArg(library.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); // The server has a test of its own; without this it would never run. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); // The generator of src/width_tables.zig is run by hand with `zig run`; // its test keeps it compiling and correct in the meantime. const gen_width_tests = b.addTest(.{ .name = "gen-width-test", .root_module = b.createModule(.{ .root_source_file = b.path("tools/gen_width.zig"), .target = b.graph.host, .optimize = .Debug, }), }); test_step.dependOn(&b.addRunArtifact(gen_width_tests).step); check_step.dependOn(&gen_width_tests.step); }