58 lines
1.8 KiB
Rust
58 lines
1.8 KiB
Rust
// 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);
|
||
|
||
// cross images contain rustc but need not contain the rustup manager.
|
||
let rustc_version = run_cmd(Command::new("rustc").args(["--version"]));
|
||
let toolchain = std::env::var("RUSTUP_TOOLCHAIN")
|
||
.map(|value| value.trim().to_owned())
|
||
.unwrap_or_else(|_| rustc_version.clone());
|
||
|
||
println!("cargo:rustc-env=TOOLCHAIN={}", toolchain);
|
||
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");
|
||
}
|