source · Rust

Build

crates/vak/build.rs
Raw
1.5 KB34 linesSnapshot ed4ab258
  1. 1//! Keeps the build stamp honest, and composes the version string.
  2. 2//!
  3. 3//! Two jobs, both about the same fact — which commit this binary is.
  4. 4//!
  5. 5//! 1. `VAK_GIT_SHA` is read with `option_env!`, which is baked in at COMPILE
  6. 6//! time. Cargo does not know an arbitrary environment variable is a build
  7. 7//! input, so without `rerun-if-env-changed` it happily reuses a cached
  8. 8//! object compiled against an older value, and `install.json` then records
  9. 9//! the commit of whenever that object was last built. A manifest naming
  10. 10//! the wrong commit is worse than one saying nothing: it is what an
  11. 11//! operator reads to answer "what is deployed here", and a confidently
  12. 12//! stale answer sends them debugging code that was never running.
  13. 13//!
  14. 14//! 2. `VAK_VERSION` is the string `--version` prints. Composing it here
  15. 15//! rather than in the CLI because `concat!` takes only literals, so
  16. 16//! "release, plus a sha if there is one" cannot be expressed inline.
  17. 17//!
  18. 18//! `scripts/build.sh` and `scripts/release.sh` set the sha; a plain
  19. 19//! `cargo build` leaves it unset and both the manifest and `--version`
  20. 20//! honestly report just the release.
  21. 21
  22. 22fn main() {
  23. 23 println!("cargo:rerun-if-env-changed=VAK_GIT_SHA");
  24. 24
  25. 25 let version = std::env::var("CARGO_PKG_VERSION").unwrap_or_default();
  26. 26 let stamped = match std::env::var("VAK_GIT_SHA") {
  27. 27 Ok(sha) if !sha.trim().is_empty() && sha != "unknown" => {
  28. 28 format!("{version} ({})", sha.trim())
  29. 29 }
  30. 30 _ => version,
  31. 31 };
  32. 32 println!("cargo:rustc-env=VAK_VERSION={stamped}");
  33. 33}
  34. 34