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