Cotton code
60 lines · 3.2 KB · RustRaw
//! Stamps the core with what it was built from: the commit, whether crates///! differed from it, and crates/' content as a tree hash//! (`scripts/lib/crates-tree.sh`). `cotton_ffi::build_commit()` answers it, the//! app logs it at launch, and `scripts/core-fresh.sh` reads it out of an//! xcframework to refuse a walk on a core older than the checkout: on 22 Oct//! 2026 a lane's deck screenshots ran on a core without `@card`, and nothing//! could tell.//!//! Outside a git checkout every part is empty and the core is unstamped.use std::path::{Path, PathBuf};use std::process::Command;fn main() {    let manifest = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").expect("cargo sets CARGO_MANIFEST_DIR"));    let crates = manifest.parent().expect("cotton-ffi sits in crates/");    let root = crates.parent().expect("crates/ sits in the checkout");    let commit = git(root, &["rev-parse", "HEAD"]).unwrap_or_default();    let tree = run(Command::new("bash").arg(root.join("scripts/lib/crates-tree.sh")).arg(root)).unwrap_or_default();    let committed = git(root, &["rev-parse", "HEAD:crates"]).unwrap_or_default();    let dirty = !tree.is_empty() && tree != committed;    println!("cargo:rustc-env=COTTON_BUILD_COMMIT={commit}");    // When that commit was made (seconds): the order of two cores, which a copy shared by several apps reads before    // an older one may remake or adopt it (copy_keep::core_built, owner 28 Nov 2026).    let at = git(root, &["log", "-1", "--format=%ct", "HEAD"]).unwrap_or_default();    println!("cargo:rustc-env=COTTON_BUILD_COMMIT_TIME={}", at.trim().parse::<i64>().unwrap_or(0));    println!("cargo:rustc-env=COTTON_BUILD_DIRTY={}", u8::from(dirty));    println!("cargo:rustc-env=COTTON_BUILD_CRATES_TREE={tree}");    // The kit this core carries the pictures of (`COTTON_CORE_KIT`,    // cotton-core's build.rs; none is the whole core), stamped beside the    // commit so `scripts/core-fresh.sh` refuses a walk of one app on a core    // built for another.    println!("cargo:rerun-if-env-changed=COTTON_CORE_KIT");    println!("cargo:rustc-env=COTTON_BUILD_CORE_KIT={}", std::env::var("COTTON_CORE_KIT").unwrap_or_default());    // Run again when HEAD moves or anything under crates/ changes; the build    // output (`target`) is never watched, or every build would run this again.    for path in ["HEAD", "logs/HEAD"] {        if let Some(path) = git(root, &["rev-parse", "--path-format=absolute", "--git-path", path]) {            println!("cargo:rerun-if-changed={path}");        }    }    println!("cargo:rerun-if-changed={}", root.join("scripts/lib/crates-tree.sh").display());    for entry in std::fs::read_dir(crates).into_iter().flatten().flatten() {        let name = entry.file_name();        let name = name.to_string_lossy();        if name != "target" && !name.starts_with('.') {            println!("cargo:rerun-if-changed={}", entry.path().display());        }    }}fn git(root: &Path, args: &[&str]) -> Option<String> {    run(Command::new("git").arg("-C").arg(root).args(args))}fn run(command: &mut Command) -> Option<String> {    let output = command.output().ok().filter(|output| output.status.success())?;    Some(String::from_utf8_lossy(&output.stdout).trim().to_string())}