inky-engine 3.0.1

A hand-fused Metal inference engine for Thinking Machines' Inkling 2 Mini, for Apple M5 Macs
//! Embeds the commit a binary was built from as `INKY_BUILD_SHA` (reported by `/health` as `build_sha`).
//!
//! An `INKY_BUILD_SHA` already in the build environment wins, so a release or packager can pin it, or set it
//! empty to embed nothing. Unset, the checkout's `git rev-parse HEAD` is used when there is a git checkout and
//! a `git` to ask; a crates.io source tree has neither, so the commit `cargo package` recorded in its
//! `.cargo_vcs_info.json` is used, and with none of these the binary reports `unknown`. The build never fails
//! for want of git.

use std::path::{Path, PathBuf};
use std::process::Command;

#[path = "src/build_info.rs"]
mod build_info;

fn git(dir: &Path, args: &[&str]) -> Option<String> {
    let out = Command::new("git").arg("-C").arg(dir).args(args).output().ok()?;
    if !out.status.success() {
        return None;
    }
    let text = String::from_utf8(out.stdout).ok()?;
    let text = text.trim();
    (!text.is_empty()).then(|| text.to_string())
}

fn main() {
    println!("cargo:rerun-if-env-changed=INKY_BUILD_SHA");
    println!("cargo:rerun-if-changed=build.rs");
    if std::env::var_os("INKY_BUILD_SHA").is_some() {
        return;
    }
    let Some(dir) = std::env::var_os("CARGO_MANIFEST_DIR").map(PathBuf::from) else { return };
    // Only the checkout this crate is the top of: a packaged crate unpacked inside some other repository must
    // not report that repository's commit.
    let top = git(&dir, &["rev-parse", "--show-toplevel"]).map(PathBuf::from);
    let same = |a: &Path, b: &Path| a.canonicalize().ok().zip(b.canonicalize().ok()).is_some_and(|(a, b)| a == b);
    if !top.is_some_and(|t| same(&t, &dir)) {
        // A crates.io source tree: the commit the package was cut from.
        let info = dir.join(".cargo_vcs_info.json");
        if info.exists() {
            println!("cargo:rerun-if-changed={}", info.display());
        }
        if let Some(sha) = std::fs::read_to_string(&info).ok().as_deref().and_then(build_info::vcs_info_sha) {
            println!("cargo:rustc-env=INKY_BUILD_SHA={sha}");
        }
        return;
    }
    let Some(sha) = git(&dir, &["rev-parse", "--verify", "HEAD"]) else { return };
    if !sha.bytes().all(|b| b.is_ascii_hexdigit()) {
        return;
    }
    // Rebuild when HEAD moves: HEAD itself (a checkout), the branch it names (a commit), and packed-refs. Only paths
    // that exist are watched: Cargo reruns a build script on every build while a watched path is missing, and a
    // CI checkout (detached HEAD, no packed-refs) then recompiled the whole crate on each cargo command. A loose
    // branch ref that gets packed is a change to a watched path, so nothing is missed.
    let watch = |p: String| {
        let p = dir.join(p);
        if p.exists() {
            println!("cargo:rerun-if-changed={}", p.display());
        }
    };
    for path in ["HEAD", "packed-refs"] {
        if let Some(p) = git(&dir, &["rev-parse", "--git-path", path]) {
            watch(p);
        }
    }
    if let Some(branch) = git(&dir, &["symbolic-ref", "-q", "HEAD"]) {
        if let Some(p) = git(&dir, &["rev-parse", "--git-path", &branch]) {
            watch(p);
        }
    }
    println!("cargo:rustc-env=INKY_BUILD_SHA={sha}");
}