use std::{env, process::Command}; fn git_output(args: &[&str], dir: &str) -> Option { 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) ); println!("cargo:rerun-if-changed=../.git/HEAD"); } }