Something went wrong. Try again.
Sign git commits and tags with an OpenPGP key kept in Bitwarden.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie <jeff@ocjtech.us>// SPDX-License-Identifier: MIT
const std = @import("std");
pub fn build(b: *std.Build) void { const target = b.standardTargetOptions(.{}); const optimize = b.standardOptimizeOption(.{});
const crypto_ext = b.dependency("std_crypto_ext", .{ .target = target, .optimize = optimize, }).module("std_crypto_ext"); const known_folders = b.dependency("known_folders", .{ .target = target, .optimize = optimize, }).module("known-folders");
// The version has one home, build.zig.zon; the program reads it from // here, package.nix reads it from there, and a release tag has to match // it. const options = b.addOptions(); options.addOption([]const u8, "version", @import("build.zig.zon").version);
const mod = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "std_crypto_ext", .module = crypto_ext }, .{ .name = "known-folders", .module = known_folders }, }, }); mod.addOptions("build_options", options);
const exe = b.addExecutable(.{ .name = "bwgpg", .root_module = mod, }); b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); run_cmd.addPassthruArgs(); const run_step = b.step("run", "Run bwgpg"); run_step.dependOn(&run_cmd.step);
const test_step = b.step("test", "Run the unit tests"); const unit_tests = b.addTest(.{ .root_module = mod }); test_step.dependOn(&b.addRunArtifact(unit_tests).step);
// Compile everything without running it, including what only a // particular step would otherwise build. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&exe.step); check_step.dependOn(&unit_tests.step);
// The same code built for the machine running the build, for the tools // below that exercise it. const host_mod = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = b.graph.host, .optimize = .Debug, .imports = &.{ .{ .name = "std_crypto_ext", .module = b.dependency("std_crypto_ext", .{ .target = b.graph.host, .optimize = .Debug, }).module("std_crypto_ext") }, .{ .name = "known-folders", .module = b.dependency("known_folders", .{ .target = b.graph.host, .optimize = .Debug, }).module("known-folders") }, }, }); host_mod.addOptions("build_options", options);
// -- interoperability with GnuPG ----------------------------------------- // // The unit tests check the signature code against golden bytes, which // says it is stable but not that it is right. This checks it against the // thing that has to accept it: keys generated by gpg, signatures made // here, verified by gpg. It needs gpg on PATH, which the devshell has. const sign_file = b.addExecutable(.{ .name = "bwgpg-sign-file", .root_module = b.createModule(.{ .root_source_file = b.path("tools/sign_file.zig"), .target = b.graph.host, .optimize = .Debug, .imports = &.{.{ .name = "bwgpg", .module = host_mod }}, }), }); check_step.dependOn(&sign_file.step); const run_interop = b.addSystemCommand(&.{ "sh", "tests/interop.sh" }); run_interop.addArtifactArg(sign_file); run_interop.has_side_effects = true; const interop_step = b.step("interop", "Check signatures against GnuPG"); interop_step.dependOn(&run_interop.step);
// -- a Vaultwarden to test against --------------------------------------- // // `bwgpg-fixture` registers an account on an empty Vaultwarden and stores // a key in it; see tests/fixture.zig. `-Dfixture` installs it beside // bwgpg, which is how the NixOS test gets it. const fixture = b.addExecutable(.{ .name = "bwgpg-fixture", .root_module = b.createModule(.{ .root_source_file = b.path("tests/fixture.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "bwgpg", .module = mod }}, }), }); check_step.dependOn(&fixture.step); if (b.option(bool, "fixture", "Also install bwgpg-fixture, for end-to-end tests") orelse false) { b.installArtifact(fixture); }
// -- documentation ------------------------------------------------------- // // Zig emits the API documentation as a side effect of compiling. 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 install_docs = b.addInstallDirectory(.{ .source_dir = exe.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 built for the machine running the build, never for // whatever -Dtarget bwgpg 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(exe.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);}