// 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 websocket = b.dependency("websocket", .{ .target = target, .optimize = optimize }); // With no options, as zig-http asks for it: the dependency cache is keyed // on them, and the same tls asked for two ways would be two modules over // the same files, which a compilation with the server in it refuses. const tls = b.dependency("tls", .{}); const flac = b.dependency("flac", .{ .target = target, .optimize = optimize }); const noise = b.dependency("noise", .{ .target = target, .optimize = optimize }); const opus = b.dependency("opus", .{ .target = target, .optimize = optimize }); const std_crypto_ext = b.dependency("std_crypto_ext", .{ .target = target, .optimize = optimize }); const resample = b.dependency("resample", .{ .target = target, .optimize = optimize }); const mod = b.addModule("sendspin", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "websocket", .module = websocket.module("websocket") }, .{ .name = "tls", .module = tls.module("tls") }, .{ .name = "flac", .module = flac.module("flac") }, .{ .name = "noise", .module = noise.module("noise") }, .{ .name = "opus", .module = opus.module("opus") }, .{ .name = "std_crypto_ext", .module = std_crypto_ext.module("std_crypto_ext") }, }, }); // Where a server connects to the player, in a module of its own: it is // an HTTP server, zig-http's, and a player that only ever dials out // should not have to take one -- nor the second zig-uri and z46 it // brings, which a program with its own would have in two modules. So // `sendspin` itself is only ever a client, as zig-websocket's // `websocket` is, with `websocket_server` beside it. // // zig-http is the very one zig-websocket's server half is built against, // asked of it with the arguments it asks with, so that the two agree on // what a request is. const http = websocket.builder.dependency("http", .{ .target = target, .optimize = optimize }); const listener_mod = b.addModule("sendspin_listener", .{ .root_source_file = b.path("src/Listener.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "websocket", .module = websocket.module("websocket") }, .{ .name = "websocket_server", .module = websocket.module("websocket_server") }, .{ .name = "http", .module = http.module("http") }, .{ .name = "http_io", .module = http.module("http_io") }, }, }); // The server, in a module of its own for the same reason: a player has // no use for it. const server_mod = b.addModule("sendspin_server", .{ .root_source_file = b.path("src/server/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "noise", .module = noise.module("noise") }, .{ .name = "websocket", .module = websocket.module("websocket") }, .{ .name = "websocket_server", .module = websocket.module("websocket_server") }, .{ .name = "http", .module = http.module("http") }, .{ .name = "http_io", .module = http.module("http_io") }, .{ .name = "flac", .module = flac.module("flac") }, .{ .name = "opus", .module = opus.module("opus") }, .{ .name = "resample", .module = resample.module("resample") }, }, }); // Its tests meet the client, dialing and dialed. const server_test_mod = b.createModule(.{ .root_source_file = b.path("src/server/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "sendspin_listener", .module = listener_mod }, .{ .name = "noise", .module = noise.module("noise") }, .{ .name = "websocket", .module = websocket.module("websocket") }, .{ .name = "websocket_server", .module = websocket.module("websocket_server") }, .{ .name = "http", .module = http.module("http") }, .{ .name = "http_io", .module = http.module("http_io") }, .{ .name = "flac", .module = flac.module("flac") }, .{ .name = "opus", .module = opus.module("opus") }, .{ .name = "resample", .module = resample.module("resample") }, }, }); // Advertising a player or server by mDNS, and finding players: through // the system's Avahi on the system bus, or zig-dns-client's own // responder and monitor where Avahi is not there or will not publish. // A module of its own for the same reason again: it brings D-Bus and a // DNS library, which a player that is only ever given a URL has no use // for. const dns_client = b.dependency("dns_client", .{ .target = target, .optimize = optimize }); const avahi = b.dependency("avahi", .{ .target = target, .optimize = optimize }); const discovery_mod = b.addModule("sendspin_discovery", .{ .root_source_file = b.path("src/discovery/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "dns_client", .module = dns_client.module("dns_client") }, .{ .name = "avahi", .module = avahi.module("avahi") }, }, }); const test_step = b.step("test", "Run the tests"); const check_step = b.step("check", "Compile everything without running it"); const discovery_tests = b.addTest(.{ .root_module = discovery_mod }); test_step.dependOn(&b.addRunArtifact(discovery_tests).step); check_step.dependOn(&discovery_tests.step); const server_tests = b.addTest(.{ .root_module = server_test_mod }); test_step.dependOn(&b.addRunArtifact(server_tests).step); check_step.dependOn(&server_tests.step); const listener_tests = b.addTest(.{ .root_module = listener_mod }); test_step.dependOn(&b.addRunArtifact(listener_tests).step); check_step.dependOn(&listener_tests.step); const mod_tests = b.addTest(.{ .root_module = mod }); test_step.dependOn(&b.addRunArtifact(mod_tests).step); check_step.dependOn(&mod_tests.step); // A client that connects, says what it hears, and plays nothing: for // trying the library against a real server. Nothing runs it but a person, // so `check` is what keeps it compiling. const example = b.addExecutable(.{ .name = "sendspin-probe", .root_module = b.createModule(.{ .root_source_file = b.path("tools/probe.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "sendspin_listener", .module = listener_mod }, .{ .name = "sendspin_discovery", .module = discovery_mod }, .{ .name = "noise", .module = noise.module("noise") }, }, }), }); b.installArtifact(example); check_step.dependOn(&example.step); // A server that pairs players by dynamic PIN, the PIN typed on standard // input: the other side of the probe, and what tests/nixos/ // pair-server.nix pairs aiosendspin's client with. const pair_server = b.addExecutable(.{ .name = "sendspin-pair-server", .root_module = b.createModule(.{ .root_source_file = b.path("tools/pair_server.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "sendspin_server", .module = server_mod }, .{ .name = "sendspin_discovery", .module = discovery_mod }, .{ .name = "noise", .module = noise.module("noise") }, }, }), }); b.installArtifact(pair_server); check_step.dependOn(&pair_server.step); // The client adapter for the Sendspin conformance harness, which // tests/nixos/conformance.nix runs against the harness's servers. // zig-uri with the options zig-http asks for it with, since the // dependency cache is keyed on them: asked for any other way, zig-uri // and the z46 it brings would be second modules over the same files, // which a compilation refuses. const uri = b.dependency("uri", .{ .target = target, .optimize = optimize }); const conformance_mod = b.createModule(.{ .root_source_file = b.path("tools/conformance.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "sendspin_listener", .module = listener_mod }, .{ .name = "noise", .module = noise.module("noise") }, .{ .name = "uri", .module = uri.module("uri") }, }, }); const conformance = b.addExecutable(.{ .name = "sendspin-conformance", .root_module = conformance_mod }); b.installArtifact(conformance); check_step.dependOn(&conformance.step); const conformance_tests = b.addTest(.{ .root_module = conformance_mod }); test_step.dependOn(&b.addRunArtifact(conformance_tests).step); check_step.dependOn(&conformance_tests.step); // The server adapter for the same harness, with z2dimg for the // reference picture it draws -- and z2d from z2dimg itself, asked for // with the same options, so that the two are one module. const z2dimg = b.dependency("z2dimg", .{ .target = target, .optimize = optimize }); const z2d = z2dimg.builder.dependency("z2d", .{ .target = target, .optimize = optimize }); const conformance_server_mod = b.createModule(.{ .root_source_file = b.path("tools/conformance_server.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "sendspin_server", .module = server_mod }, .{ .name = "noise", .module = noise.module("noise") }, .{ .name = "flac", .module = flac.module("flac") }, .{ .name = "uri", .module = uri.module("uri") }, .{ .name = "z2d", .module = z2d.module("z2d") }, .{ .name = "z2dimg", .module = z2dimg.module("z2dimg") }, }, }); const conformance_server = b.addExecutable(.{ .name = "sendspin-conformance-server", .root_module = conformance_server_mod }); b.installArtifact(conformance_server); check_step.dependOn(&conformance_server.step); const conformance_server_tests = b.addTest(.{ .root_module = conformance_server_mod }); test_step.dependOn(&b.addRunArtifact(conformance_server_tests).step); check_step.dependOn(&conformance_server_tests.step); // The fuzz targets: what a player must do with whatever a server sends // it, and a server with whatever a client sends it. Their seeds are real sessions and handshakes, made at build time by // tools/fuzz_corpus.zig -- in a build for the machine running the build, // optimized, since it encodes FLAC and Opus every time the seeds change. const corpus_deps = hostDependencies(b, .safe); 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 = .safe, .imports = &.{ .{ .name = "flac", .module = corpus_deps.flac }, .{ .name = "opus", .module = corpus_deps.opus }, .{ .name = "noise", .module = corpus_deps.noise }, .{ .name = "sendspin", .module = corpus_deps.sendspin }, }, }), }); const corpus_file = b.addRunArtifact(corpus_gen).addOutputFileArg("fuzz_corpus.zig"); const corpus_mod = b.createModule(.{ .root_source_file = corpus_file }); // Zig's fuzzer takes one test at a time, so naming a target is what is // wanted when a finding is being chased. const fuzz_filter = b.option([]const u8, "fuzz-filter", "Fuzz or test only the targets whose name contains this"); const fuzz_tests = b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("tests/fuzz.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = mod }, .{ .name = "sendspin_server", .module = server_mod }, .{ .name = "noise", .module = noise.module("noise") }, .{ .name = "fuzz_corpus", .module = corpus_mod }, }, }), .filters = if (fuzz_filter) |f| &.{f} else &.{}, // The self-hosted backend, which Debug uses otherwise, emits no // coverage instrumentation, and without it the fuzzer has nothing // to steer by. .use_llvm = true, }); 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. b.step("fuzz", "The fuzz targets: add --fuzz to fuzz them").dependOn(&b.addRunArtifact(fuzz_tests).step); // The loop that drives the same targets 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_deps = hostDependencies(b, fuzz_optimize); const fuzz_run = b.addExecutable(.{ .name = "zig-sendspin-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 = "sendspin", .module = host_deps.sendspin }, .{ .name = "sendspin_server", .module = host_deps.server }, .{ .name = "noise", .module = host_deps.noise }, .{ .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 targets 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 each module is // built as a library purely to get at it. There are three, split so that // a player carries no server and no HTTP server it does not use, and a // reader arriving at the docs wants to be told which is which: each // bundle goes in a subdirectory of its own, under an index that says // what the module is for and what it costs to depend on. const documented = [_]struct { module: *std.Build.Module, name: []const u8, needs: []const u8, what: []const u8, }{ .{ .module = mod, .name = "sendspin", .needs = "zig-websocket, tls.zig, zig-flac, zig-opus, zig-noise, zig-std-crypto-ext", .what = "The player client: the connection, the messages, the clock, the scheduler, pairing.", }, .{ .module = listener_mod, .name = "sendspin_listener", .needs = "sendspin, and zig-http", .what = "Where a server connects to a player, for the server-initiated direction.", }, .{ .module = server_mod, .name = "sendspin_server", .needs = "sendspin, zig-http, zig-resample", .what = "The server: connections, groups, streams in every format, metadata, artwork, pairing.", }, .{ .module = discovery_mod, .name = "sendspin_discovery", .needs = "sendspin, zig-avahi, zig-dns-client", .what = "Advertising a player or server by mDNS, through Avahi or on its own, and finding players.", }, }; const docs_step = b.step("docs", "Build the API documentation into zig-out/docs"); // Every bundle and the index in one generated directory: installed as // zig-out/docs, and handed to the docs server as it is, since the // install path cannot be asked for while configuring. const docs_dir = b.addWriteFiles(); var index: std.ArrayList(u8) = .empty; index.appendSlice(b.allocator, \\ \\ \\ \\zig-sendspin API documentation \\ \\

zig-sendspin

\\

Sendspin in Zig, both sides of it: a player client, and a server \\that streams to players in step. \\

Four modules, so that a player carries no server, and no mDNS, it does \\not use. \\ \\
ModuleDepends on \\ ) catch @panic("OOM"); for (documented) |d| { const library = b.addLibrary(.{ .name = d.name, .root_module = d.module }); _ = docs_dir.addCopyDirectory(library.getEmittedDocs(), d.name, .{}); index.print(b.allocator, "
{s}{s}{s}\n", .{ d.name, d.name, d.needs, d.what, }) catch @panic("OOM"); } index.appendSlice(b.allocator, "
\n") catch @panic("OOM"); _ = docs_dir.add("index.html", index.items); docs_step.dependOn(&b.addInstallDirectory(.{ .source_dir = docs_dir.getDirectory(), .install_dir = .prefix, .install_subdir = "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.addDirectoryArg(docs_dir.getDirectory()); 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); } /// The library and what it depends on, built for the machine running the /// build: for the programs the build runs, which must not follow `-Dtarget` /// or be slowed by a Debug `-Doptimize`. fn hostDependencies(b: *std.Build, optimize: std.lang.Optimize) struct { sendspin: *std.Build.Module, server: *std.Build.Module, flac: *std.Build.Module, opus: *std.Build.Module, noise: *std.Build.Module } { const opts = .{ .target = b.graph.host, .optimize = optimize }; const flac = b.dependency("flac", opts).module("flac"); const opus = b.dependency("opus", opts).module("opus"); const noise = b.dependency("noise", opts).module("noise"); const websocket = b.dependency("websocket", opts); // As the server module proper has it: zig-websocket's own zig-http. const http = websocket.builder.dependency("http", opts); const sendspin = b.createModule(.{ .root_source_file = b.path("src/root.zig"), .target = b.graph.host, .optimize = optimize, .imports = &.{ .{ .name = "websocket", .module = websocket.module("websocket") }, // As in `build`, the way zig-http asks for it. .{ .name = "tls", .module = b.dependency("tls", .{}).module("tls") }, .{ .name = "flac", .module = flac }, .{ .name = "noise", .module = noise }, .{ .name = "opus", .module = opus }, .{ .name = "std_crypto_ext", .module = b.dependency("std_crypto_ext", opts).module("std_crypto_ext") }, }, }); const server = b.createModule(.{ .root_source_file = b.path("src/server/root.zig"), .target = b.graph.host, .optimize = optimize, .imports = &.{ .{ .name = "sendspin", .module = sendspin }, .{ .name = "noise", .module = noise }, .{ .name = "websocket", .module = websocket.module("websocket") }, .{ .name = "websocket_server", .module = websocket.module("websocket_server") }, .{ .name = "http", .module = http.module("http") }, .{ .name = "http_io", .module = http.module("http_io") }, .{ .name = "flac", .module = flac }, .{ .name = "opus", .module = opus }, .{ .name = "resample", .module = b.dependency("resample", opts).module("resample") }, }, }); return .{ .sendspin = sendspin, .server = server, .flac = flac, .opus = opus, .noise = noise }; }