// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT const std = @import("std"); /// The MPRIS interfaces, as introspection XML: dbus-codegen makes the /// bindings `src/Server.zig` serves them through. fn interfaceFiles(b: *std.Build) [4]std.Build.LazyPath { return .{ b.path("dbus/org.mpris.MediaPlayer2.xml"), b.path("dbus/org.mpris.MediaPlayer2.Player.xml"), b.path("dbus/org.mpris.MediaPlayer2.TrackList.xml"), b.path("dbus/org.mpris.MediaPlayer2.Playlists.xml"), }; } 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 interface_files = interfaceFiles(b); const interfaces = @import("dbus_service").addInterfaces(b, dbus_service, &interface_files); 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") }, .{ .name = "mpris_interfaces", .module = interfaces }, }, }); 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 }, }, }), // The self-hosted backend, which Debug uses by default, emits no // coverage instrumentation, and without it `zig build fuzz --fuzz` // ends with "corrupted coverage file: pcs_len was zero". LLVM costs a // few seconds of compile and gives the fuzzer something to steer by. .use_llvm = true, }); test_step.dependOn(&b.addRunArtifact(fuzz_tests).step); check_step.dependOn(&fuzz_tests.step); // How much of the library the fuzz target's seeds reach, which is what // the fuzzers start from: kcov runs the seeds under a tracer and writes // an HTML report to `zig-out/coverage`, restricted to `src` so that the // standard library and the dependencies stay out of the numbers. It // needs the LLVM backend the fuzz binary already has; under the // self-hosted one the report is 0.00% of zero lines rather than an // error. const run_kcov = b.addSystemCommand(&.{ "kcov", "--include-path=src" }); const coverage_output = run_kcov.addOutputDirectoryArg("coverage"); run_kcov.addArtifactArg(fuzz_tests); // The test runner reports on stderr, which a Run step otherwise takes for // an error message and fails on. _ = run_kcov.captureStdErr(.{}); b.step("coverage", "Report what the fuzz seeds reach, with kcov").dependOn(&b.addInstallDirectory(.{ .source_dir = coverage_output, .install_dir = .prefix, .install_subdir = "coverage", }).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. 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 // `safe` 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.lang.Optimize, "fuzz-optimize", "How to build `fuzz-run` (default safe)") orelse .safe; const host_opts = .{ .target = b.graph.host, .optimize = fuzz_optimize }; const host_mdbus = b.dependency("mdbus", host_opts).module("mdbus"); const host_dbus_service_dep = b.dependency("dbus_service", host_opts); const host_dbus_service = host_dbus_service_dep.module("dbus_service"); const host_interfaces = @import("dbus_service").addInterfaces(b, host_dbus_service_dep, &interface_files); 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 = "mpris_interfaces", .module = host_interfaces }, }, }) }, .{ .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; run_fuzz.addPassthruArgs(); 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); // 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(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; 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); }