-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
78 lines (73 loc) · 2.14 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
fn main() {
// Make the current git hash available to the build.
let maybe_rev = git_revision_hash();
if let Some(rev) = maybe_rev.as_deref() {
println!("cargo:rustc-env=YADF_BUILD_GIT_HASH={}", rev);
}
let long_version = long_version(maybe_rev);
println!("cargo:rustc-env=YADF_BUILD_VERSION={}", long_version);
}
fn git_revision_hash() -> Option<String> {
let result = std::process::Command::new("git")
.args(&["rev-parse", "--short=10", "HEAD"])
.output();
result.ok().and_then(|output| {
let v = String::from_utf8_lossy(&output.stdout).trim().to_string();
if v.is_empty() {
None
} else {
Some(v)
}
})
}
fn long_version(rev: Option<String>) -> String {
// Do we have a git hash?
// (Yes, if ripgrep was built on a machine with `git` installed.)
let hash = match rev {
None => String::new(),
Some(githash) => format!(" (rev {})", githash),
};
let runtime = runtime_cpu_features();
if runtime.is_empty() {
format!(
"{}{}|{} (compiled)",
env!("CARGO_PKG_VERSION"),
hash,
compile_cpu_features().join(" ")
)
} else {
format!(
"{}{}|{} (compiled)|{} (runtime)",
env!("CARGO_PKG_VERSION"),
hash,
compile_cpu_features().join(" "),
runtime.join(" ")
)
}
}
/// Returns the relevant CPU features enabled at compile time.
fn compile_cpu_features() -> Vec<&'static str> {
let mut features = vec![];
if cfg!(feature = "avx2") {
features.push("+AVX");
} else {
features.push("-AVX");
}
features
}
/// Returns the relevant CPU features enabled at runtime.
#[cfg(target_arch = "x86_64")]
fn runtime_cpu_features() -> Vec<&'static str> {
let mut features = vec![];
if is_x86_feature_detected!("avx2") {
features.push("+AVX");
} else {
features.push("-AVX");
}
features
}
/// Returns the relevant CPU features enabled at runtime.
#[cfg(not(target_arch = "x86_64"))]
fn runtime_cpu_features() -> Vec<&'static str> {
vec![]
}