// SPDX-FileCopyrightText: © 2026 Tim Culverhouse // 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(.{}); // The live-bus tests need a session bus, which a normal build machine may // not have. Installing the test runner lets it be built here and executed // somewhere that does, such as the Nix flake's dbus-broker VM check. const install_test_runner = b.option( bool, "install-test-runner", "Install the test runner so it can be run outside of `zig build test`", ) orelse false; // The generated 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. This is the same reason `zig std` runs a // server rather than just opening a file. const docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000; // The protocol permits 128 MiB, but a client rarely needs anywhere near // that, and the limit is what keeps a hostile peer from forcing large // allocations. The default is deliberately well under the ceiling; raise // it only as far as the messages actually expected, and note that the // connection queues up to 256 messages, so the worst case scales with it. const max_message_size = b.option( usize, "max_message_size", "Largest message to encode or accept, in bytes (default 16 MiB, protocol maximum 128 MiB)", ) orelse 16 * 1024 * 1024; const options = b.addOptions(); options.addOption(usize, "max_message_size", max_message_size); const mod = b.addModule("mdbus", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, }); mod.addOptions("build_options", options); // Renders the module's doc comments into browsable HTML under // `zig-out/docs`. The documentation comes from an object file because that // is what emits it; the object is never linked or installed. const docs_obj = b.addObject(.{ .name = "mdbus", .root_module = mod, }); const install_docs = b.addInstallDirectory(.{ .source_dir = docs_obj.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); const docs_step = b.step("docs", "Generate API documentation"); docs_step.dependOn(&install_docs.step); 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); // Served straight from the emitted directory rather than from // `zig-out/docs`, because the build no longer knows the install prefix // while it is being configured. Depending on the install step keeps // `docs-serve` leaving the same `zig-out/docs` behind that `docs` does. run_docs_server.step.dependOn(&install_docs.step); run_docs_server.addDirectoryArg(docs_obj.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 benchmark imports `wire` directly rather than going through the // public module, because it measures the wire layer by itself and needs // `parseMessage` and `encodeMessage`, which are internals that // `Connection` drives. Compiling wire.zig as a second module root is // harmless; nothing links both into one binary. const wire_mod = b.createModule(.{ .root_source_file = b.path("src/wire.zig"), .target = target, .optimize = optimize, }); wire_mod.addOptions("build_options", options); const bench = b.addExecutable(.{ .name = "bench-wire", .root_module = b.createModule(.{ .root_source_file = b.path("tools/bench_wire.zig"), .target = target, .optimize = optimize, .imports = &.{.{ .name = "wire", .module = wire_mod }}, }), }); const run_bench = b.addRunArtifact(bench); // Timings go to the terminal as they are produced, not to a captured // buffer that only shows up if something fails. run_bench.stdio = .inherit; const bench_step = b.step("bench", "Run the decoder benchmarks"); bench_step.dependOn(&run_bench.step); // A test binary covers one module at a time, so the library and the // documentation server need one each. const mod_tests = b.addTest(.{ .name = "mdbus-tests", .root_module = mod, }); const docs_server_tests = b.addTest(.{ .name = "docs-server-tests", .root_module = docs_server.root_module, }); if (install_test_runner) b.installArtifact(mod_tests); const test_step = b.step("test", "Run tests"); for ([_]*std.Build.Step.Compile{ mod_tests, docs_server_tests }) |test_binary| { test_step.dependOn(&b.addRunArtifact(test_binary).step); } }