-
-
Notifications
You must be signed in to change notification settings - Fork 99
/
build.rs
82 lines (69 loc) · 2.23 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use camino::Utf8Path;
use std::process::Command;
fn main() {
commit_info();
let target = std::env::var("TARGET").unwrap();
println!("cargo:rustc-env=NEXTEST_BUILD_HOST_TARGET={target}");
}
fn commit_info() {
println!("cargo:rerun-if-env-changed=CFG_OMIT_COMMIT_HASH");
if std::env::var_os("CFG_OMIT_COMMIT_HASH").is_some() {
return;
}
if let Some(info) = CommitInfo::get() {
println!("cargo:rustc-env=NEXTEST_BUILD_COMMIT_HASH={}", info.hash);
println!(
"cargo:rustc-env=NEXTEST_BUILD_COMMIT_SHORT_HASH={}",
info.short_hash,
);
println!("cargo:rustc-env=NEXTEST_BUILD_COMMIT_DATE={}", info.date);
}
}
struct CommitInfo {
hash: String,
short_hash: String,
date: String,
}
impl CommitInfo {
fn get() -> Option<Self> {
// Prefer git info over crate metadata to match what Cargo and rustc do.
Self::from_git().or_else(Self::from_metadata)
}
fn from_git() -> Option<Self> {
// cargo-nextest is one level down from the root of the repository.
if !Utf8Path::new("../.git").exists() {
return None;
}
let output = match Command::new("git")
.arg("log")
.arg("-1")
.arg("--date=short")
.arg("--format=%H %h %cd")
.arg("--abbrev=9")
.output()
{
Ok(output) if output.status.success() => output,
_ => return None,
};
let stdout = String::from_utf8(output.stdout).expect("git output is ASCII");
Self::from_string(&stdout)
}
fn from_metadata() -> Option<Self> {
// This file is generated by scripts/cargo-release-publish.sh.
let path = Utf8Path::new("nextest-commit-info");
if !path.exists() {
return None;
}
println!("cargo:rerun-if-changed={}", path);
let content = std::fs::read_to_string(path).ok()?;
Self::from_string(&content)
}
fn from_string(s: &str) -> Option<Self> {
let mut parts = s.split_whitespace().map(|s| s.to_string());
Some(CommitInfo {
hash: parts.next()?,
short_hash: parts.next()?,
date: parts.next()?,
})
}
}