build.rs

cdk-rsx/crates/cdk-rsx-wire/build.rs

build.rs101 lines← SDK documentation
1//! Source-qualified Wire interpretation. All filesystem work runs on the build host.2use cdk_rsx_compat as compatibility;3use sha2::{Digest, Sha256};4use std::{5    env, fs,6    path::{Path, PathBuf},7};89fn rust_sources(directory: &Path, paths: &mut Vec<PathBuf>) {10    for entry in fs::read_dir(directory).expect("Wire policy source directory") {11        let path = entry.expect("Wire policy source entry").path();12        if path.is_dir() {13            rust_sources(&path, paths);14        } else if path.extension().is_some_and(|extension| extension == "rs") {15            paths.push(path);16        }17    }18}1920fn field(hash: &mut Sha256, name: &str, bytes: &[u8]) {21    hash.update((name.len() as u64).to_le_bytes());22    hash.update(name.as_bytes());23    hash.update((bytes.len() as u64).to_le_bytes());24    hash.update(bytes);25}2627fn main() {28    for name in [29        compatibility::CONTEXT_ENV,30        compatibility::ALIASES_ENV,31        compatibility::PACKAGE_ROOTS_ENV,32    ] {33        println!("cargo:rerun-if-env-changed={name}");34    }35    let context = env::var(compatibility::CONTEXT_ENV).ok();36    let aliases = env::var_os(compatibility::ALIASES_ENV).map(PathBuf::from);37    if let Some(path) = &aliases {38        println!("cargo:rerun-if-changed={}", path.display());39    }40    let resolver = compatibility::Resolver::load(context.as_deref(), aliases.as_deref())41        .unwrap_or_else(|error| panic!("Wire compatibility: {error}"));42    let roots = compatibility::PackageRoots::parse(43        env::var(compatibility::PACKAGE_ROOTS_ENV).ok().as_deref(),44    )45    .unwrap_or_else(|error| panic!("Wire compatibility: {error}"));46    let mut hash = Sha256::new();47    hash.update(b"cdk-rsx-wire-source-policy-v2\0");48    // The manifest pins the published codec exactly. The canonical builder's49    // tooling contract also includes resolved registry checksums/features and50    // dependency edges; no vendored codec sources belong to this package.51    let mut paths = vec![PathBuf::from("Cargo.toml"), PathBuf::from("build.rs")];52    for directory in ["src"] {53        // Watching directories also catches newly added Rust implementation files.54        println!("cargo:rerun-if-changed={directory}");55        rust_sources(Path::new(directory), &mut paths);56    }57    paths.sort();58    for path in paths {59        let name = path.to_str().expect("Wire source path");60        println!("cargo:rerun-if-changed={name}");61        let bytes =62            fs::read(&path).unwrap_or_else(|error| panic!("Wire policy source {name}: {error}"));63        field(&mut hash, name, &bytes);64    }65    // Freeze selected cfg/feature branches as well as their source expressions.66    // No host paths, environment secrets or runtime filesystem inputs enter the67    // policy. Cargo supplies this configuration for the compiled Wire crate.68    let mut configuration: Vec<_> = env::vars()69        .filter(|(name, _)| name.starts_with("CARGO_CFG_") || name.starts_with("CARGO_FEATURE_"))70        .collect();71    configuration.sort();72    for (name, value) in configuration {73        field(&mut hash, &name, value.as_bytes());74    }75    let (digest, record) = compatibility::package_recipe(76        &resolver,77        &roots,78        Path::new(env!("CARGO_MANIFEST_DIR")),79        env!("CARGO_PKG_NAME"),80        include_str!("Cargo.toml"),81        hash.finalize().into(),82    );83    let record = compatibility::wasm_record(&record);84    fs::write(85        PathBuf::from(env::var_os("OUT_DIR").unwrap()).join("source_policy.rs"),86        format!(87            r#"#[doc(hidden)]88#[macro_export]89macro_rules! __wire_policy_recipe {{90    () => {{{{91        {record}92        {digest:?}93    }}}};94}}95/// Standalone Wire source, selected target and dependency interpretation policy.96pub const WIRE_POLICY: [u8; 32] = __wire_policy_recipe!();97"#98        ),99    )100    .unwrap();101}