Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/korp-map

crates/korp-map-build-thing/src/recipe.rs

//! **The recipe** — korp-map's `impl BuildThing`, the ONE declaration of what this
//! repository builds. `.nornir/build-variants.toml` is emitted from
//! [`KorpMapBuildThing::variants`] (see [`crate::emit`]); nothing here reads it.
//!
//! | variant | kind | features | what it is |
//! |---|---|---|---|
//! | `korp-map-server` ⭐ | service | `server` | the map half on its own: the map wire (HTTP Arrow IPC + Flight) over the region exports it provisions. No warehouse, no control plane, no sibling compiled in and none reached; a korp UI or thin client reads its map here |
//! | `korp-map-lib` | lib | — | the map library korp links as its map half (`korp --features map`) |
//!
//! The row is the one korp declared for `korp-map-server` while the crate lived in
//! korp (`thin`, role server, suite ORIGINAL); it moved here with the crate.
//!
//! `korp-map-wire` and `korp-map-wire-roundtrip` are their own self-rooting
//! workspaces (`exclude`d at the root), as they were in korp: the wire is a contract
//! crate korp-map depends on by path, the roundtrip crate a test of the contract.

use std::collections::BTreeMap;

use nornir_build_thing::cargo_container::CargoToolchain;
use nornir_build_thing::contract::Coordinate;
use nornir_build_thing::plugin::{ABI_VERSION, BuildThing, Manifest, Role};
use nornir_build_thing::variant::{
    AllowEntry, ArtifactKind, BuildVariant, BuildVariantSet, Profile, ServiceShape,
};

/// The crate a receipt names as its recipe.
pub const KRATE: &str = "korp-map-build-thing";

/// The content hash over this crate's sources, computed by `build.rs`.
pub const CONTENT_HASH: &str = env!("KORP_MAP_BUILD_THING_CONTENT_HASH");

/// The delivery this repo ships in (the same one korp's rows name).
pub const SUITE: &str = "ORIGINAL";

/// The one package this repository builds.
pub const PACKAGE: &str = "korp-map";

/// The one shipped binary.
pub const SERVER_BIN: &str = "korp-map-server";

/// korp-map's build thing.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct KorpMapBuildThing;

impl KorpMapBuildThing {
    /// The coordinate a receipt names this recipe by: `krate:version:hash`.
    #[must_use]
    pub fn coordinate() -> Coordinate {
        Coordinate {
            krate: KRATE.to_string(),
            version: env!("CARGO_PKG_VERSION").to_string(),
            hash: Some(CONTENT_HASH.to_string()),
        }
    }
}

fn s(x: &str) -> String {
    x.to_string()
}

/// The rows, in declaration order.
pub fn variants() -> Vec<BuildVariant> {
    vec![
        BuildVariant {
            name: s("korp-map-server"),
            package: Some(s(PACKAGE)),
            bin: Some(s(SERVER_BIN)),
            member: Some(s(PACKAGE)),
            kind: ArtifactKind::Service,
            role: Some(Role::Server),
            shape: ServiceShape::ThinStandalone,
            suites: vec![s(SUITE)],
            features: vec![s("server")],
            profile: Profile::Release,
            is_default: true,
            ..BuildVariant::default()
        },
        BuildVariant {
            name: s("korp-map-lib"),
            package: Some(s(PACKAGE)),
            member: Some(s(PACKAGE)),
            kind: ArtifactKind::Lib,
            role: Some(Role::Library),
            shape: ServiceShape::NotApplicable,
            suites: vec![s(SUITE)],
            profile: Profile::Release,
            is_default: false,
            ..BuildVariant::default()
        },
    ]
}

/// Every feature the workspace's manifests declare, `default` excluded.
fn offers() -> Vec<&'static str> {
    vec![
        "server",
        "sweden-clip",
        "planet-download",
        "collectors-daemon",
        "wire-client",
        "testmatrix",
    ]
}

/// **The features no row builds, each with a written reason.**
pub fn allow_unbuilt() -> BTreeMap<String, String> {
    [
        ("sweden-clip", "the region clip through osm-katana (planet → region cut). Reached by `server`, which the korp-map-server row builds; no row names it alone because a clip with no server or host around it is not an artifact anybody ships. korp turns it on through its own `sweden-clip`."),
        ("planet-download", "the in-process librqbit planet TORRENT (forwards osm-katana/planet-fixture). Deliberately off every build point: provisioning and every test rely on a planet already on disk, and a BitTorrent client in a delivery is a network dependency nobody asked for. Mirrors korp's own `planet-download` gate, which forwards to it."),
        ("collectors-daemon", "the tile daemon path through korp-collectors' `osm-daemon`. A host's choice (korp's `collectors` atoms turn it on), not a shape this repository ships."),
        ("wire-client", "the map read over korp-map-wire's MapClient (`korp_map::wire_read`): the CLIENT side, turned on by korp's `client` together with `map` (korp-cli, korp-probe, korp-ui, korp-server). The server row serves the wire; it never reads it."),
        ("testmatrix", "turns the tests' `functional_status` self-reports into real nornir test-matrix rows. A TEST-HARNESS mode, never a delivery."),
    ]
    .into_iter()
    .map(|(k, v)| (s(k), s(v)))
    .collect()
}

/// **The `[[bin]]` no delivery builds**, with its reason.
fn allow_unbuilt_bins() -> BTreeMap<String, AllowEntry> {
    [(
        "korp-map-build-thing",
        "this recipe crate's own emit door (`cargo run -p korp-map-build-thing -- emit` regenerates .nornir/build-variants.toml); the forge loads the crate as wasm, the bin is repository tooling, never shipped.",
    )]
    .into_iter()
    .map(|(k, v)| (s(k), AllowEntry::Reason(s(v))))
    .collect()
}

/// **The pinned build image every cargo variant compiles in** — `[build.toolchain]`,
/// the same image korp and korp-collectors pin (edda's
/// `nornir_build_thing::CARGO_TOOLCHAIN_PIN`, never typed here).
fn toolchain() -> Option<CargoToolchain> {
    Some(CargoToolchain {
        image: s("nordisk-cargo-toolchain"),
        image_sha256: s(nornir_build_thing::CARGO_TOOLCHAIN_PIN.digest),
        rust_version: s("1.97.1"),
        // What the pinned image carries std for; wasm32 is the target this crate's own
        // plugin module is built for.
        targets: vec![
            s("x86_64-unknown-linux-gnu"),
            s("x86_64-unknown-linux-musl"),
            s("wasm32-unknown-unknown"),
        ],
        timeout_secs: 3600,
        mem_limit_mb: 8192,
        cpus: 8,
        network: false,
        remap_paths: true,
        // The sibling checkouts the default build reaches, transitively (the path deps,
        // the root `[patch.crates-io]` table, and what those reach in turn).
        // `tests/siblings.rs` walks the closure and fails on one missing here; a sibling
        // added or removed moves this list in the SAME commit.
        siblings: vec![
            s("Skidbladnir"),
            s("draupnir"),
            s("edda"),
            s("facett"),
            s("korp-collectors"),
            s("znippy-zoomies"),
        ],
    })
}

impl BuildThing for KorpMapBuildThing {
    fn manifest(&self) -> Manifest {
        Manifest {
            abi: ABI_VERSION,
            repo: s("korp-map"),
            source_crate: s(KRATE),
            summary: "korp-map — the map half: region cuts from the OSM planet, the map \
                      registry, the packed-cut read, and korp-map-server serving the map \
                      wire (HTTP Arrow IPC + Flight). One server binary and the library \
                      korp links."
                .to_string(),
        }
    }

    fn variants(&self) -> BuildVariantSet {
        BuildVariantSet {
            base_suite: Some(s(SUITE)),
            offers: offers().into_iter().map(s).collect(),
            allow_unbuilt: allow_unbuilt(),
            allow_unbuilt_bins: allow_unbuilt_bins(),
            variants: variants(),
            toolchain: toolchain(),
            ..BuildVariantSet::default()
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use nornir_build_thing::feature_axis::FeatureAxis;

    /// **The feature axis does not rot.** Every feature offered is built by a row or
    /// excused by name, and no excuse is stale.
    #[test]
    fn the_feature_axis_gate_is_clean() {
        let set = KorpMapBuildThing.variants();
        let axis = FeatureAxis::of("korp-map", &set);
        if let Err(rot) = axis.gate() {
            panic!("the feature axis is rotting: {rot:#?}");
        }
    }

    /// Every excuse names a feature this repo offers, in words.
    #[test]
    fn every_excuse_names_a_feature_this_repo_offers() {
        let set = KorpMapBuildThing.variants();
        for (f, why) in &set.allow_unbuilt {
            assert!(
                set.offers.contains(f),
                "'{f}' is excused as unbuilt and is not a feature this repo offers"
            );
            assert!(!why.trim().is_empty(), "'{f}' is excused with no words");
        }
    }

    /// The only binary a row names is the one that exists.
    #[test]
    fn the_only_binary_a_row_names_is_the_server() {
        for v in KorpMapBuildThing.variants().variants {
            if let Some(bin) = &v.bin {
                assert_eq!(
                    bin, SERVER_BIN,
                    "variant '{}' names a binary this crate does not ship",
                    v.name
                );
            }
        }
    }
}