// 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(.{}); // Only the generator uses it, and the generator runs during the build. const openapi2zig_dep = b.dependency("openapi2zig", .{ .target = b.graph.host, .optimize = .ReleaseFast, }); // `zig build update-spec` replaces api/swagger.v1.json with the API // description from a Forgejo release. It reaches the network, so it is a // step of its own that nothing else depends on -- an ordinary build never // runs it, and never needs to. { const forgejo_version = b.option( []const u8, "forgejo-version", "Forgejo release tag to take the API description from (default: the latest release)", ); const update_exe = b.addExecutable(.{ .name = "update-spec", .root_module = b.createModule(.{ .root_source_file = b.path("src/update-spec.zig"), .target = b.graph.host, .optimize = optimize, }), }); const update_cmd = b.addRunArtifact(update_exe); if (forgejo_version) |version| update_cmd.addArg(version); // What the network answers is not a function of the command line, so // the run cannot be cached on its arguments the way the generator is. update_cmd.has_side_effects = true; const fetched_spec = update_cmd.captureStdOut(.{ .basename = "swagger.v1.json" }); const write_spec = b.addUpdateSourceFiles(); write_spec.addCopyFileToSource(fetched_spec, "api/swagger.v1.json"); const update_step = b.step("update-spec", "Update api/swagger.v1.json from a Forgejo release"); update_step.dependOn(&write_spec.step); } // The bindings are generated during the build rather than checked in, so // the only thing this repository holds for them is the specification they // are derived from. const generate_exe = b.addExecutable(.{ .name = "generate", .root_module = b.createModule(.{ .root_source_file = b.path("src/generate.zig"), // It runs during the build, so it is built for the machine doing // the building, whatever the bindings are being built for. .target = b.graph.host, // The generator runs once per build of a 850 KB document; the time // it takes to optimize is repaid the first time it runs. .optimize = .ReleaseFast, .imports = &.{ .{ .name = "openapi2zig", .module = openapi2zig_dep.module("openapi2zig"), }, }, }), .use_lld = false, .use_llvm = false, }); const generate_cmd = b.addRunArtifact(generate_exe); generate_cmd.setStdIn(.{ .lazy_path = b.path("api/swagger.v1.json") }); const api_zig = generate_cmd.captureStdOut(.{ .basename = "api.zig" }); const zfj_mod = b.addModule("zfj", .{ .root_source_file = api_zig, .target = target, .optimize = optimize, }); // Installed so that `zig build` leaves the generated source somewhere it // can be read: it is the reference for what the library actually exposes. const install_api_zig = b.addInstallFile(api_zig, "api.zig"); b.getInstallStep().dependOn(&install_api_zig.step); const zfj_lib = b.addLibrary(.{ .name = "zfj", .root_module = zfj_mod, }); const install_docs = b.addInstallDirectory(.{ .source_dir = zfj_lib.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Build the API documentation"); docs_step.dependOn(&install_docs.step); // Compiling the generated module is the test that matters most: 506 // operations and 246 models either type check against each other or they // do not. The tests in src/ check what that cannot -- that the bindings // are shaped the way a caller expects. const zfj_tests = b.addTest(.{ .root_module = zfj_mod, }); const run_zfj_tests = b.addRunArtifact(zfj_tests); const shape_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/tests.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "zfj", .module = zfj_mod }, }, }), }); const run_shape_tests = b.addRunArtifact(shape_tests); // The command-line tool. A consumer of the `zfj` module never compiles // it; see docs/adr/0002-the-cli-lives-beside-the-bindings.md. const strip = b.option(bool, "strip", "Leave the debug information out of the zfj executable"); // Reads the timestamps Forgejo gives, which it writes in the // instance's own offset rather than in UTC. const datetime_mod = b.dependency("datetime", .{ .target = target, .optimize = optimize }).module("datetime"); const cli_options = b.addOptions(); cli_options.addOption([]const u8, "version", @import("build.zig.zon").version); const cli_mod = b.createModule(.{ .root_source_file = b.path("src/cli/main.zig"), .target = target, .optimize = optimize, .strip = strip, .imports = &.{ .{ .name = "zfj", .module = zfj_mod }, .{ .name = "datetime", .module = datetime_mod }, }, }); cli_mod.addOptions("build_options", cli_options); // `issue view --web` opens the browser through openuri on Linux and // Windows. It is lazy, and not asked for on macOS, where it would link // Launch Services and need Apple's SDK to cross-build. const openuri_mod: ?*std.Build.Module = switch (target.result.os.tag) { .linux, .windows => if (b.lazyDependency("openuri", .{ .target = target, .optimize = optimize })) |openuri| openuri.module("openuri") else return, else => null, }; if (openuri_mod) |m| cli_mod.addImport("openuri", m); const cli_exe = b.addExecutable(.{ .name = "zfj", .root_module = cli_mod, }); b.installArtifact(cli_exe); const run_cli = b.addRunArtifact(cli_exe); run_cli.addPassthruArgs(); const run_step = b.step("run", "Run the zfj command-line tool"); run_step.dependOn(&run_cli.step); // The tests count the operations in the specification itself, so they // are handed it to read. const cli_test_mod = b.createModule(.{ .root_source_file = b.path("src/cli/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "zfj", .module = zfj_mod }, .{ .name = "datetime", .module = datetime_mod }, }, }); cli_test_mod.addAnonymousImport("swagger.v1.json", .{ .root_source_file = b.path("api/swagger.v1.json") }); cli_test_mod.addAnonymousImport("build.zig.zon", .{ .root_source_file = b.path("build.zig.zon") }); cli_test_mod.addOptions("build_options", cli_options); if (openuri_mod) |m| cli_test_mod.addImport("openuri", m); const cli_tests = b.addTest(.{ .name = "zfj-cli-test", .root_module = cli_test_mod, }); const run_cli_tests = b.addRunArtifact(cli_tests); const test_step = b.step("test", "Run the tests"); test_step.dependOn(&run_zfj_tests.step); test_step.dependOn(&run_shape_tests.step); test_step.dependOn(&run_cli_tests.step); }