1
0

build.rs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. //! Construct version in the `commit-hash date channel` format
  2. use std::{env, path::PathBuf, process::Command};
  3. fn main() {
  4. set_rerun();
  5. set_commit_info();
  6. }
  7. fn set_rerun() {
  8. let mut manifest_dir = PathBuf::from(
  9. env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
  10. );
  11. while manifest_dir.parent().is_some() {
  12. let head_ref = manifest_dir.join(".git/HEAD");
  13. if head_ref.exists() {
  14. println!("cargo:rerun-if-changed={}", head_ref.display());
  15. return;
  16. }
  17. manifest_dir.pop();
  18. }
  19. println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
  20. }
  21. fn set_commit_info() {
  22. let output = match Command::new("git")
  23. .arg("log")
  24. .arg("-1")
  25. .arg("--date=short")
  26. .arg("--format=%H %h %cd")
  27. .output()
  28. {
  29. Ok(output) if output.status.success() => output,
  30. _ => return,
  31. };
  32. let stdout = String::from_utf8(output.stdout).unwrap();
  33. let mut parts = stdout.split_whitespace();
  34. let mut next = || parts.next().unwrap();
  35. println!("cargo:rustc-env=RA_COMMIT_HASH={}", next());
  36. println!("cargo:rustc-env=RA_COMMIT_SHORT_HASH={}", next());
  37. println!("cargo:rustc-env=RA_COMMIT_DATE={}", next())
  38. }