-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
27 lines (22 loc) · 928 Bytes
/
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
// Include the GIT_HASH, in `GIT_HASH` environment variable at build
// time, panic'ing if it can not be found
//
// https://stackoverflow.com/questions/43753491/include-git-commit-hash-as-string-into-rust-program
use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rerun-if-env-changed=GIT_HASH");
// Populate env!(GIT_HASH) with the current git commit
println!("cargo:rustc-env=GIT_HASH={}", get_git_hash());
Ok(())
}
fn get_git_hash() -> String {
let git_hash = {
let output = Command::new("git")
.args(["describe", "--always", "--dirty", "--abbrev=64"])
.output()
.expect("failed to execute git rev-parse to read the current git hash");
String::from_utf8(output.stdout).expect("non-utf8 found in git hash")
};
assert!(!git_hash.is_empty(), "attempting to embed empty git hash");
git_hash
}