// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: GPL-2.0-or-later 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; // The RTL-SDR input is Linux-only: the driver underneath it talks to // usbfs directly. A build without it still compiles — the input is // simply not there, and says so if asked for. const rtlsdr = b.option(bool, "rtlsdr", "Build the RTL-SDR input (default: on for Linux)") orelse (target.result.os.tag == .linux); // Off by default because it needs the network and reaches FlightAware's // real server. It sends nothing: see the test it guards. const tls_check = b.option( bool, "tls-check", "Also check the pinned certificate against FlightAware's live server (needs network)", ) orelse false; const options = b.addOptions(); options.addOption(bool, "rtlsdr", rtlsdr); options.addOption(bool, "tls_check", tls_check); // One module, shared: two `createModule` calls on the same options make // two modules out of the same file, which the compiler refuses. const options_module = options.createModule(); // Dates, times and timezones. The standard library has no timezone // database and its calendar only decodes, so this is not something to // write again. const datetime = b.dependency("datetime", .{ .target = target, .optimize = optimize, }); // Positions on the earth: the great-circle distance behind the range and // speed checks, and the position type the decoder hands back. const geo = b.dependency("geo", .{ .target = target, .optimize = optimize, }); // TLS for the FlightAware uplink. Not the standard library's, which // cannot talk to a server that asks for a client certificate: see the // note in the manifest. const tls = b.dependency("tls", .{ .target = target, .optimize = optimize, }); const mod = b.addModule("dump1090", .{ .root_source_file = b.path("src/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "datetime", .module = datetime.module("datetime") }, .{ .name = "build_options", .module = options_module }, .{ .name = "geo", .module = geo.module("geo") }, .{ .name = "tls", .module = tls.module("tls") }, }, }); // FlightAware's uplink presents a certificate from their own private CA, // so the root is pinned rather than taken from the system store. It is // embedded straight out of PiAware at the commit named in the manifest, // which keeps the trusted bytes checkable against their published source // instead of being a certificate pasted into this repository. const piaware = b.dependency("piaware", .{}); mod.addAnonymousImport("flightaware_root_ca", .{ .root_source_file = piaware.path("package/ca/flightaware-root.cert.pem"), }); // The RTL2832U driver, in Zig: no libusb, no C, and nothing to link. // It is only imported when the input is being built, so that a build // without it does not pull a Linux-only module into the graph. if (rtlsdr) { const rtlsdr_dep = b.dependency("rtlsdr", .{ .target = target, .optimize = optimize, }); mod.addImport("rtlsdr", rtlsdr_dep.module("rtlsdr")); } const exe = b.addExecutable(.{ .name = "dump1090", .root_module = b.createModule(.{ .root_source_file = b.path("src/main.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "dump1090", .module = mod }, .{ .name = "datetime", .module = datetime.module("datetime") }, .{ .name = "build_options", .module = options_module }, }, }), }); b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); run_cmd.step.dependOn(b.getInstallStep()); if (b.args) |args| run_cmd.addArgs(args); const run_step = b.step("run", "Run dump1090"); run_step.dependOn(&run_cmd.step); const test_step = b.step("test", "Run tests"); const mod_tests = b.addTest(.{ .root_module = mod }); test_step.dependOn(&b.addRunArtifact(mod_tests).step); const exe_tests = b.addTest(.{ .root_module = exe.root_module }); test_step.dependOn(&b.addRunArtifact(exe_tests).step); // Compile everything without running it, for editors and for a quick // "does it still build" that skips the test binaries' run step. const check_step = b.step("check", "Compile everything without running it"); check_step.dependOn(&exe.step); check_step.dependOn(&mod_tests.step); // The documentation comes out as a side effect of compiling the module, so // it is built as a library purely to get at it. const library = b.addLibrary(.{ .name = "dump1090", .root_module = mod }); const install_docs = b.addInstallDirectory(.{ .source_dir = library.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); 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 rest of the project 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; const docs_serve_step = b.step("docs-serve", "Serve the API documentation over HTTP"); docs_serve_step.dependOn(&run_docs_server.step); // Nothing else builds the documentation server, so without these it could // stop compiling and no test would notice. test_step.dependOn(&b.addRunArtifact( b.addTest(.{ .root_module = docs_server.root_module }), ).step); check_step.dependOn(&docs_server.step); }