-
Notifications
You must be signed in to change notification settings - Fork 91
/
build.rs
35 lines (28 loc) · 1.03 KB
/
build.rs
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
use std::process::Command;
fn cmd(args: &[&str]) -> Option<String> {
if let Ok(out) = Command::new(args[0]).args(&args[1..]).output() {
if out.status.success() {
return Some(String::from_utf8_lossy(&out.stdout).trim().to_string());
}
}
None
}
fn get_git_sha() -> Option<String> {
if let Some(sha) = cmd(&["git", "rev-parse", "--short", "HEAD"]) {
let symbolic = cmd(&["git", "rev-parse", "--symbolic", "HEAD"]).unwrap();
let symbolic_full = cmd(&["git", "rev-parse", "--symbolic-full-name", "HEAD"]).unwrap();
println!("cargo:rerun-if-changed=.git/{symbolic}");
if symbolic != symbolic_full {
println!("cargo:rerun-if-changed=.git/{symbolic_full}");
}
Some(sha)
} else {
println!("cargo:warning=failed to get crater sha");
None
}
}
fn main() {
let sha = format!("{:?}", get_git_sha());
let output = std::env::var("OUT_DIR").unwrap();
::std::fs::write(format!("{output}/sha"), sha.as_bytes()).unwrap();
}