-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
42 lines (34 loc) · 1.2 KB
/
build.rs
File metadata and controls
42 lines (34 loc) · 1.2 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
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=build.rs");
let is_git_repo = Command::new("git")
.args(["rev-parse", "--is-inside-work-tree"])
.output()
.map(|output| output.status.success())
.unwrap_or(false);
if is_git_repo {
println!("cargo:rerun-if-changed=.git/HEAD");
if let Ok(head) = std::fs::read_to_string(".git/HEAD")
&& head.starts_with("ref: ")
{
let head_ref = head.trim_start_matches("ref: ").trim();
println!("cargo:rerun-if-changed=.git/{head_ref}");
println!(
"cargo:rustc-env=CARGO_GIT_BRANCH={}",
head_ref.rsplit('/').next().unwrap_or("unknown")
);
}
println!("cargo:rerun-if-changed=.git/index");
}
let mut git_hash = "unknown".to_string();
if is_git_repo
&& let Ok(output) = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
&& output.status.success()
&& let Ok(hash) = String::from_utf8(output.stdout)
{
git_hash = hash.trim().to_string();
}
println!("cargo:rustc-env=CARGO_GIT_COMMIT={git_hash}");
}