1
0

build.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. if option_env!("CFG_RELEASE").is_none() {
  7. println!("cargo:rustc-env=POKE_RA_DEVS=1");
  8. }
  9. }
  10. fn set_rerun() {
  11. println!("cargo:rerun-if-env-changed=CFG_RELEASE");
  12. let mut manifest_dir = PathBuf::from(
  13. env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."),
  14. );
  15. while manifest_dir.parent().is_some() {
  16. let head_ref = manifest_dir.join(".git/HEAD");
  17. if head_ref.exists() {
  18. println!("cargo:rerun-if-changed={}", head_ref.display());
  19. return;
  20. }
  21. manifest_dir.pop();
  22. }
  23. println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!");
  24. }
  25. fn set_commit_info() {
  26. let output = match Command::new("git")
  27. .arg("log")
  28. .arg("-1")
  29. .arg("--date=short")
  30. .arg("--format=%H %h %cd")
  31. .output()
  32. {
  33. Ok(output) if output.status.success() => output,
  34. _ => return,
  35. };
  36. let stdout = String::from_utf8(output.stdout).unwrap();
  37. let mut parts = stdout.split_whitespace();
  38. let mut next = || parts.next().unwrap();
  39. println!("cargo:rustc-env=RA_COMMIT_HASH={}", next());
  40. println!("cargo:rustc-env=RA_COMMIT_SHORT_HASH={}", next());
  41. println!("cargo:rustc-env=RA_COMMIT_DATE={}", next())
  42. }