// build.rs use std::process::Command; use std::str; /// Helper that runs a command and returns its stdout as a trimmed UTF‑8 string. fn run_cmd(cmd: &mut Command) -> String { let out = cmd.output().expect("failed to run command"); if !out.status.success() { panic!( "command {:?} failed with status {:?}\nstderr: {}", cmd, out.status, String::from_utf8_lossy(&out.stderr) ); } str::from_utf8(&out.stdout) .expect("command produced non‑UTF8 output") .trim() .to_owned() } fn main() { let git_hash = run_cmd(Command::new("git").args(["rev-parse", "--short", "HEAD"])); println!("cargo:rustc-env=GIT_HASH={}", git_hash); let args = &["rev-parse", "--abbrev-ref", "HEAD"]; let git_branch = run_cmd(Command::new("git").args(args)); println!("cargo:rustc-env=GIT_BRANCH={}", git_branch); // Prefer the explicit env var – it mirrors the real user choice let toolchain = std::env::var("RUSTUP_TOOLCHAIN") .map(|v| v.trim().to_owned()) .unwrap_or_else(|_| { // Try rustup first; if that fails, fall back to rustc let raw = run_cmd(Command::new("rustup").args(["show", "active-toolchain"])); // rustup output may look like: // stable-x86_64-unknown-linux-gnu (overridden by environment variable) // We only want the part before the first '(' raw.split('(').next().unwrap().trim().to_owned() }); println!("cargo:rustc-env=TOOLCHAIN={}", toolchain); let rustc_version = run_cmd(Command::new("rustc").args(["--version"])); println!("cargo:rustc-env=RUSTC_VERSION={}", rustc_version); println!( "cargo:rustc-env=EFO_SHORT_VERSION={} ({}{})", env!("CARGO_PKG_VERSION"), git_branch, git_hash, ); println!( "cargo:rustc-env=EFO_FULL_VERSION={} ({}{})  {}  {}", env!("CARGO_PKG_VERSION"), git_branch, git_hash, toolchain, rustc_version, ); // println!("cargo:rerun-if-changed=.git/HEAD"); // println!("cargo:rerun-if-changed=.git/refs/heads"); // println!("cargo:rerun-if-changed=.git/refs/remotes"); }