// 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 docs_port = b.option(u16, "docs-port", "Port for `zig build docs-serve` (default 8000)") orelse 8000; const mdbus = b.dependency("mdbus", .{ .target = target, .optimize = optimize }); const dbus_service = b.dependency("dbus_service", .{ .target = target, .optimize = optimize }); const mod = b.addModule("mpris", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "mdbus", .module = mdbus.module("mdbus") }, .{ .name = "dbus_service", .module = dbus_service.module("dbus_service") }, }, }); const test_step = b.step("test", "Run the tests"); const check_step = b.step("check", "Compile everything without running it"); const mod_tests = b.addTest(.{ .root_module = mod }); test_step.dependOn(&b.addRunArtifact(mod_tests).step); check_step.dependOn(&mod_tests.step); // A pretend player on the session bus, for trying the library against // real peers -- playerctl, gdbus, a desktop shell's media controls. // Nothing runs it but a person or the bus test, so `check` is what keeps // it compiling. const example = b.addExecutable(.{ .name = "mpris-example", .root_module = b.createModule(.{ .root_source_file = b.path("tools/example.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "mpris", .module = mod }, .{ .name = "mdbus", .module = mdbus.module("mdbus") }, }, }), }); b.installArtifact(example); check_step.dependOn(&example.step); // The fuzz target: what an MPRIS server must do with whatever a peer // calls it with. Its seeds are calls to every method and property of the // four interfaces, made at build time -- see tools/fuzz_corpus.zig. const corpus_gen = b.addExecutable(.{ .name = "fuzz-corpus", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz_corpus.zig"), .target = b.graph.host, .optimize = .Debug, }), }); const corpus_mod = b.createModule(.{ .root_source_file = b.addRunArtifact(corpus_gen).addOutputFileArg("fuzz_corpus.zig"), }); const fuzz_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "mpris", .module = mod }, .{ .name = "mdbus", .module = mdbus.module("mdbus") }, .{ .name = "dbus_service", .module = dbus_service.module("dbus_service") }, .{ .name = "fuzz_corpus", .module = corpus_mod }, }, }) }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); check_step.dependOn(&fuzz_tests.step); // A step of its own for `zig build fuzz --fuzz`, which takes over the // terminal and runs until stopped, so `zig build test` must not reach // it. On Zig 0.16.0 it finds no coverage; tools/fuzz.zig says more. b.step("fuzz", "The fuzz target: add --fuzz to fuzz it").dependOn(&b.addRunArtifact(fuzz_tests).step); // The loop that drives the same target without Zig's fuzzer. Optimized, // since a fuzzer's whole job is how many inputs it gets through, and // ReleaseSafe keeps every check that makes a failure a failure; Debug, // with -Dfuzz-optimize=Debug, for a stack trace to read when running one // finding again. const fuzz_optimize = b.option(std.builtin.OptimizeMode, "fuzz-optimize", "How to build `fuzz-run` (default ReleaseSafe)") orelse .ReleaseSafe; const host_opts = .{ .target = b.graph.host, .optimize = fuzz_optimize }; const host_mdbus = b.dependency("mdbus", host_opts).module("mdbus"); const host_dbus_service = b.dependency("dbus_service", host_opts).module("dbus_service"); const fuzz_run = b.addExecutable(.{ .name = "zig-mpris-fuzz", .root_module = b.createModule(.{ .root_source_file = b.path("tools/fuzz.zig"), .target = b.graph.host, .optimize = fuzz_optimize, .imports = &.{.{ .name = "fuzz_targets", .module = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = b.graph.host, .optimize = fuzz_optimize, .imports = &.{ .{ .name = "mpris", .module = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = b.graph.host, .optimize = fuzz_optimize, .imports = &.{ .{ .name = "mdbus", .module = host_mdbus }, .{ .name = "dbus_service", .module = host_dbus_service }, }, }) }, .{ .name = "mdbus", .module = host_mdbus }, .{ .name = "dbus_service", .module = host_dbus_service }, .{ .name = "fuzz_corpus", .module = corpus_mod }, }, }) }}, }), }); const run_fuzz = b.addRunArtifact(fuzz_run); run_fuzz.stdio = .inherit; if (b.args) |a| run_fuzz.addArgs(a); b.step("fuzz-run", "Fuzz the target with a loop of our own").dependOn(&run_fuzz.step); check_step.dependOn(&fuzz_run.step); // The documentation is a side effect of compiling, so the module is built // as a library purely to get at it. const library = b.addLibrary(.{ .name = "mpris", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.getEmittedDocs(), .install_dir = .prefix, .install_subdir = "docs", }); b.step("docs", "Build the API documentation into zig-out/docs").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 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; b.step("docs-serve", "Serve the API documentation over HTTP").dependOn(&run_docs_server.step); // The docs server has a test of its own, and nothing else builds it. test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = docs_server.root_module })).step); check_step.dependOn(&docs_server.step); }