-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
48 lines (44 loc) · 1.87 KB
/
Copy pathbuild.rs
File metadata and controls
48 lines (44 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
fn main() {
emit_git_suffix();
}
/// Bake the autorip git short hash into the build as `GIT_SUFFIX` so the
/// RUNNING build is identifiable everywhere autorip reports its version (UI
/// footer, `/api/version`, startup log, `--version`) — e.g. `1.1.1 (g2014a41)`,
/// the same shape libfreemkv stamps into every MKV. This is what tells a
/// hand-deployed test build apart from the released image. `AUTORIP_BUILD_LABEL`
/// overrides the Cargo package version (when set non-empty) so a pre-release /
/// test build can stamp a label without bumping Cargo.toml. Empty suffix when
/// git or the repo is unavailable; always emitted so `env!("GIT_SUFFIX")`
/// resolves on every target.
fn emit_git_suffix() {
let version = std::env::var("AUTORIP_BUILD_LABEL")
.ok()
.filter(|s| !s.trim().is_empty())
.or_else(|| std::env::var("CARGO_PKG_VERSION").ok())
.unwrap_or_default();
println!("cargo:rustc-env=AUTORIP_VERSION={version}");
println!("cargo:rerun-if-env-changed=AUTORIP_BUILD_LABEL");
let suffix = git_short_hash()
.map(|h| format!(" (g{h})"))
.unwrap_or_default();
println!("cargo:rustc-env=GIT_SUFFIX={suffix}");
// Re-run when HEAD (or the branch it points at) moves so the stamp stays
// current without a clean rebuild.
println!("cargo:rerun-if-changed=.git/HEAD");
if let Ok(head) = std::fs::read_to_string(".git/HEAD")
&& let Some(ref_path) = head.strip_prefix("ref: ")
{
println!("cargo:rerun-if-changed=.git/{}", ref_path.trim());
}
}
fn git_short_hash() -> Option<String> {
let out = std::process::Command::new("git")
.args(["rev-parse", "--short=7", "HEAD"])
.output()
.ok()?;
if !out.status.success() {
return None;
}
let h = String::from_utf8(out.stdout).ok()?.trim().to_string();
if h.is_empty() { None } else { Some(h) }
}