51 lines
1.5 KiB
Rust
51 lines
1.5 KiB
Rust
use std::{env, path::Path, process::Command};
|
|
|
|
/// Paths that change when `HEAD`, a branch, or a tag moves. `HEAD` itself is only rewritten on
|
|
/// checkout, so commits are tracked through the reflog and the ref directories.
|
|
const GIT_WATCH: [&str; 5] = [
|
|
"../.git/HEAD",
|
|
"../.git/logs/HEAD",
|
|
"../.git/refs/heads",
|
|
"../.git/refs/tags",
|
|
"../.git/packed-refs",
|
|
];
|
|
|
|
fn git_output(args: &[&str], dir: &str) -> Option<String> {
|
|
let output = Command::new("git")
|
|
.args(["-C", dir])
|
|
.args(args)
|
|
.output()
|
|
.ok()?;
|
|
|
|
if !output.status.success() {
|
|
return None;
|
|
}
|
|
|
|
let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
|
|
(!value.is_empty()).then_some(value)
|
|
}
|
|
|
|
fn firmware_version(dir: &str) -> String {
|
|
git_output(&["describe", "--tags", "--exact-match", "HEAD"], dir)
|
|
.or_else(|| git_output(&["rev-parse", "--short=12", "HEAD"], dir))
|
|
.unwrap_or_else(|| env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_string()))
|
|
}
|
|
|
|
fn main() {
|
|
if let Ok(manifest_dir) = env::var("CARGO_MANIFEST_DIR") {
|
|
println!("cargo:rustc-link-search={manifest_dir}");
|
|
println!(
|
|
"cargo:rustc-env=GUAC_VERSION={}",
|
|
firmware_version(&manifest_dir)
|
|
);
|
|
|
|
for path in GIT_WATCH {
|
|
let path = Path::new(&manifest_dir).join(path);
|
|
|
|
if path.exists() {
|
|
println!("cargo:rerun-if-changed={}", path.display());
|
|
}
|
|
}
|
|
}
|
|
}
|