#[allow(dead_code)] mod build_id {
include!("src/build_id.rs");
}
use build_id::{
BUILD_ID_SRC_DEGRADED, BUILD_ID_SRC_TREE, content_id, degraded_build_id, workspace_root,
};
fn git(args: &[&str]) -> Option<String> {
std::process::Command::new("git")
.args(args)
.output()
.ok()
.filter(|o| o.status.success())
.map(|o| String::from_utf8_lossy(&o.stdout).trim().to_string())
.filter(|s| !s.is_empty())
}
fn watch_git_head() {
if let Some(head) = git(&["rev-parse", "--git-path", "HEAD"]) {
println!("cargo:rerun-if-changed={head}");
}
if let Some(refname) = git(&["symbolic-ref", "-q", "HEAD"])
&& let Some(refpath) = git(&["rev-parse", "--git-path", &refname])
{
println!("cargo:rerun-if-changed={refpath}");
}
}
fn main() {
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default();
let pkg_name = std::env::var("CARGO_PKG_NAME").unwrap_or_default();
let pkg_version = std::env::var("CARGO_PKG_VERSION").unwrap_or_default();
watch_git_head();
let scan = workspace_root(&manifest_dir)
.as_deref()
.and_then(content_id);
let (build_id, id_src, note) = match scan {
Some(scan) => {
for dir in &scan.dirs {
println!("cargo:rerun-if-changed={}", dir.display());
}
for file in &scan.files {
println!("cargo:rerun-if-changed={}", file.display());
}
(scan.id, BUILD_ID_SRC_TREE, String::new())
}
None => {
let reason = format!(
"workspace source tree not readable from CARGO_MANIFEST_DIR={manifest_dir} \
(expected <root>/crates alongside <root>/Cargo.toml)"
);
println!(
"cargo:warning=memra build identity DEGRADED: {reason}. system_fingerprint \
will carry a version-only id that does not identify this build's source; \
published performance pins cannot be verified against it."
);
(
degraded_build_id(&pkg_name, &pkg_version),
BUILD_ID_SRC_DEGRADED,
reason,
)
}
};
println!("cargo:rustc-env=MEMRA_BUILD_ID={build_id}");
println!("cargo:rustc-env=MEMRA_BUILD_ID_SRC={id_src}");
println!("cargo:rustc-env=MEMRA_BUILD_ID_NOTE={note}");
let sha = git(&["rev-parse", "--short=12", "HEAD"]).unwrap_or_else(|| "unknown".into());
println!("cargo:rustc-env=MEMRA_BUILD_SHA={sha}");
}