waratah_buildinfo/lib.rs
1//! Compile-time build provenance shared by every Waratah binary and process.
2//!
3//! `build.rs` runs vergen-gitcl at compile time and emits `VERGEN_*` cargo
4//! instructions; this crate surfaces them as consts plus the version surfaces
5//! used across the cluster:
6//!
7//! - [`PKG_VERSION`]: the workspace semver (clap `--version` one-liner).
8//! - [`version_string()`]: what Describe RPCs and `/waratah/cluster` carry —
9//! semver plus the `git describe --long` output, e.g.
10//! `0.2.0+v0.2.0-3-g1a2b3c4-dirty`.
11//! - [`build_info()`]: the multi-line provenance block for `--build-info`.
12//!
13//! Every git field tolerates absence. vergen runs with `fail_on_error` off,
14//! so a source tarball (no `.git`) or a repository without tags still builds.
15//! Note vergen's failure mode: it still *emits* the variable, set to the
16//! sentinel `VERGEN_IDEMPOTENT_OUTPUT` (plus a `cargo:warning`) — `option_env!`
17//! therefore never returns `None` for build-script-fed keys, and the consts
18//! map the sentinel to their per-field fallback (`"untagged"` / `"unknown"`)
19//! via [`real_or`] below. The hand-rolled `WARATAH_GIT_DESCRIBE` genuinely is
20//! unset on a tarball and falls back to `"untagged"`.
21//!
22//! Container/CI builds without `.git` can instead pin every git field from
23//! the environment: `WARATAH_GIT_DESCRIBE`, `VERGEN_GIT_SHA`,
24//! `VERGEN_GIT_BRANCH`, `VERGEN_GIT_COMMIT_TIMESTAMP`, and `VERGEN_GIT_DIRTY`.
25//! A non-empty value wins over git discovery (and over vergen's placeholder
26//! emission — the overridden field is withheld from vergen and hand-emitted
27//! verbatim; see `provenance_override` in `build.rs`). Docker builds feed
28//! these from `--build-arg` / `ARG` plumbing.
29
30/// vergen 9's fail-on-error placeholder value (see the module docs).
31const VERGEN_PLACEHOLDER: &str = "VERGEN_IDEMPOTENT_OUTPUT";
32
33/// Const-compatible `&str` equality: `str` patterns in `match` and const
34/// `PartialEq` are both unstable, so compare bytes in a loop.
35const fn str_eq(a: &str, b: &str) -> bool {
36 let (a, b) = (a.as_bytes(), b.as_bytes());
37 if a.len() != b.len() {
38 return false;
39 }
40 let mut i = 0;
41 while i < a.len() {
42 if a[i] != b[i] {
43 return false;
44 }
45 i += 1;
46 }
47 true
48}
49
50/// `value` when vergen really computed it; `fallback` when the variable is
51/// absent, holds vergen's placeholder sentinel, or is empty (a blank
52/// `rustc-env` value is never meaningful provenance).
53const fn real_or(value: Option<&'static str>, fallback: &'static str) -> &'static str {
54 match value {
55 Some(v) if str_eq(v, VERGEN_PLACEHOLDER) => fallback,
56 Some(v) if str_eq(v, "") => fallback,
57 Some(v) => v,
58 None => fallback,
59 }
60}
61
62/// Workspace package version (`version.workspace = true` in every member).
63pub const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
64
65/// `git describe --long --tags --dirty` for the checked-out commit (e.g.
66/// `v0.2.0-3-g1a2b3c4-dirty`), degrading to the abbreviated SHA on a
67/// repository without tags and `-dirty` appended when the tree had
68/// uncommitted changes at compile time. `"untagged"` only when the build
69/// had no git at all (source tarball) — or when the variable was present but
70/// empty, which is what a container build exports for an unset build-arg
71/// (`ENV WARATAH_GIT_DESCRIBE=`); an empty string is no more provenance than
72/// absence.
73///
74/// Emitted by a hand-rolled call in `build.rs` (vergen-gitcl 9.1.0 cannot
75/// produce `--long` output); see `git_describe_long` there.
76pub const GIT_DESCRIBE: &str = match option_env!("WARATAH_GIT_DESCRIBE") {
77 Some(v) if !str_eq(v, "") => v,
78 _ => "untagged",
79};
80
81/// Abbreviated commit hash at compile time (`"unknown"` without git).
82pub const GIT_SHA: &str = real_or(option_env!("VERGEN_GIT_SHA"), "unknown");
83
84/// Branch checked out at compile time (`"unknown"` without git).
85pub const GIT_BRANCH: &str = real_or(option_env!("VERGEN_GIT_BRANCH"), "unknown");
86
87/// Committer timestamp of HEAD at compile time, RFC 3339 with the commit's
88/// own UTC offset (`"unknown"` without git).
89pub const GIT_COMMIT_TIMESTAMP: &str =
90 real_or(option_env!("VERGEN_GIT_COMMIT_TIMESTAMP"), "unknown");
91
92/// `"true"` when the working tree had uncommitted changes at compile time.
93pub const GIT_DIRTY: &str = real_or(option_env!("VERGEN_GIT_DIRTY"), "unknown");
94
95/// Wall-clock time the build script ran (UTC).
96pub const BUILD_TIMESTAMP: &str = real_or(option_env!("VERGEN_BUILD_TIMESTAMP"), "unknown");
97
98/// rustc semver that produced the binary.
99pub const RUSTC_SEMVER: &str = real_or(option_env!("VERGEN_RUSTC_SEMVER"), "unknown");
100
101/// Target triple the binary was compiled for (plain `TARGET` build-script
102/// env, hand-emitted as `WARATAH_TARGET`).
103pub const TARGET: &str = match option_env!("WARATAH_TARGET") {
104 Some(v) => v,
105 None => "unknown",
106};
107
108/// Version string for Describe RPCs and `/waratah/cluster`: semver plus the
109/// git describe as semver build metadata (`0.2.0+v0.2.0-3-g1a2b3c4-dirty`),
110/// or the bare semver when no git info was baked in. The metadata is only
111/// semver-valid when the tag name is (a `+` in a tag would break it); the
112/// string is opaque provenance, not something the cluster parses as semver.
113pub fn version_string() -> String {
114 if GIT_DESCRIBE == "untagged" {
115 PKG_VERSION.to_owned()
116 } else {
117 format!("{PKG_VERSION}+{GIT_DESCRIBE}")
118 }
119}
120
121/// Multi-line provenance block printed by the binaries' `--build-info` flag.
122pub fn build_info() -> String {
123 format!(
124 "version: {}\n\
125 git-describe: {}\n\
126 commit: {}\n\
127 branch: {}\n\
128 commit-date: {}\n\
129 dirty: {}\n\
130 built: {}\n\
131 rustc: {}\n\
132 target: {}",
133 PKG_VERSION,
134 GIT_DESCRIBE,
135 GIT_SHA,
136 GIT_BRANCH,
137 GIT_COMMIT_TIMESTAMP,
138 GIT_DIRTY,
139 BUILD_TIMESTAMP,
140 RUSTC_SEMVER,
141 TARGET,
142 )
143}
144
145#[cfg(test)]
146mod tests {
147 use super::*;
148
149 #[test]
150 fn vergen_sentinel_maps_to_fallback() {
151 // vergen's fail-on-error path emits the sentinel instead of leaving
152 // the variable unset, so the mapping — not `option_env!` — is what
153 // produces the documented placeholders.
154 assert_eq!(real_or(Some(VERGEN_PLACEHOLDER), "unknown"), "unknown");
155 // A blank value is not meaningful provenance either: it is what an
156 // environment leaks when an empty VERGEN_* slips past build.rs.
157 assert_eq!(real_or(Some(""), "unknown"), "unknown");
158 assert_eq!(real_or(None, "unknown"), "unknown");
159 assert_eq!(real_or(Some("real value"), "unknown"), "real value");
160 }
161
162 #[test]
163 fn version_string_matches_describe_presence() {
164 // Depends on how the crate itself was compiled: with git info the
165 // describe rides along as build metadata; without it (tarball) the
166 // bare semver is all there is.
167 if GIT_DESCRIBE == "untagged" {
168 assert_eq!(version_string(), PKG_VERSION);
169 } else {
170 assert_eq!(version_string(), format!("{PKG_VERSION}+{GIT_DESCRIBE}"));
171 assert!(!version_string().ends_with("+untagged"));
172 }
173 }
174
175 #[test]
176 fn build_info_lists_every_field() {
177 let info = build_info();
178 for label in [
179 "version:",
180 "git-describe:",
181 "commit:",
182 "branch:",
183 "commit-date:",
184 "dirty:",
185 "built:",
186 "rustc:",
187 "target:",
188 ] {
189 assert!(info.contains(label), "missing {label} in:\n{info}");
190 }
191 }
192}