-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
34 lines (30 loc) · 1.32 KB
/
Copy pathbuild.rs
File metadata and controls
34 lines (30 loc) · 1.32 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
use std::{env, process::Command};
fn main() {
println!("cargo:rerun-if-env-changed=PD_BUILD_GIT_TAG");
println!("cargo:rerun-if-env-changed=PD_BUILD_GIT_COMMIT");
println!("cargo:rerun-if-env-changed=PD_BUILD_GIT_DIRTY");
let git_tag = env::var("PD_BUILD_GIT_TAG").unwrap_or_else(|_| {
run_git(["describe", "--tags", "--exact-match"]).unwrap_or_else(|| "untagged".to_string())
});
let git_commit = env::var("PD_BUILD_GIT_COMMIT").unwrap_or_else(|_| {
run_git(["rev-parse", "--short=12", "HEAD"]).unwrap_or_else(|| "unknown".to_string())
});
let git_dirty = env::var("PD_BUILD_GIT_DIRTY").unwrap_or_else(|_| {
match run_git(["status", "--porcelain", "--untracked-files=no"]) {
Some(output) if !output.trim().is_empty() => "true".to_string(),
_ => "false".to_string(),
}
});
println!("cargo:rustc-env=PD_BUILD_GIT_TAG={git_tag}");
println!("cargo:rustc-env=PD_BUILD_GIT_COMMIT={git_commit}");
println!("cargo:rustc-env=PD_BUILD_GIT_DIRTY={git_dirty}");
}
fn run_git<const N: usize>(args: [&str; N]) -> Option<String> {
let output = Command::new("git").args(args).output().ok()?;
if !output.status.success() {
return None;
}
String::from_utf8(output.stdout)
.ok()
.map(|value| value.trim().to_string())
}