build.rs 595 B

123456789101112131415161718192021222324
  1. use std::process::{Command, ExitStatus, Output};
  2. #[cfg(not(target_os = "windows"))]
  3. use std::os::unix::process::ExitStatusExt;
  4. #[cfg(target_os = "windows")]
  5. use std::os::windows::process::ExitStatusExt;
  6. fn main() {
  7. let output = String::from_utf8(
  8. Command::new("git")
  9. .args(["rev-parse", "HEAD"])
  10. .output()
  11. .unwrap_or(Output {
  12. stdout: vec![],
  13. stderr: vec![],
  14. status: ExitStatus::from_raw(0),
  15. })
  16. .stdout,
  17. )
  18. .unwrap_or_default();
  19. let git_hash = if output == String::default() { "dev".into() } else { output };
  20. println!("cargo:rustc-env=GIT_HASH={git_hash}");
  21. }