fn main() {
println!("cargo:rerun-if-env-changed=RUSTC");
println!("cargo:rerun-if-env-changed=RUSTUP_TOOLCHAIN");
println!("cargo:rerun-if-env-changed={ALLOC_MAX_DEPTH_VAR}");
let rustc = std::env::var("RUSTC").unwrap_or_else(|_| "rustc".to_string());
let version = std::process::Command::new(rustc)
.arg("--version")
.output()
.ok()
.and_then(|out| String::from_utf8(out.stdout).ok())
.and_then(|s| s.split_whitespace().nth(1).map(str::to_string))
.unwrap_or_default();
println!("cargo:rustc-env=HOTPATH_RUSTC_VERSION={version}");
println!("cargo:rustc-env=HOTPATH_CARGO_PROFILE={}", cargo_profile());
println!(
"cargo:rustc-env={ALLOC_MAX_DEPTH_VAR}={}",
alloc_max_depth()
);
}
const ALLOC_MAX_DEPTH_VAR: &str = "HOTPATH_ALLOC_MAX_DEPTH";
const DEFAULT_ALLOC_MAX_DEPTH: usize = 64;
const ALLOC_MAX_DEPTH_RANGE: std::ops::RangeInclusive<usize> = 1..=4096;
fn alloc_max_depth() -> usize {
let Ok(raw) = std::env::var(ALLOC_MAX_DEPTH_VAR) else {
return DEFAULT_ALLOC_MAX_DEPTH;
};
match raw.trim().parse::<usize>() {
Ok(depth) if ALLOC_MAX_DEPTH_RANGE.contains(&depth) => depth,
_ => panic!(
"Invalid {ALLOC_MAX_DEPTH_VAR} value: '{raw}'. Expected an integer in {}..={}.",
ALLOC_MAX_DEPTH_RANGE.start(),
ALLOC_MAX_DEPTH_RANGE.end()
),
}
}
fn cargo_profile() -> String {
let from_out_dir = std::env::var("OUT_DIR").ok().and_then(|out_dir| {
let path = std::path::PathBuf::from(out_dir);
let components: Vec<_> = path
.components()
.map(|c| c.as_os_str().to_string_lossy().into_owned())
.collect();
let build_idx = components.iter().rposition(|c| c == "build")?;
components.get(build_idx.checked_sub(1)?).cloned()
});
from_out_dir
.or_else(|| std::env::var("PROFILE").ok())
.unwrap_or_default()
}