-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbuild.rs
More file actions
79 lines (69 loc) · 2.19 KB
/
build.rs
File metadata and controls
79 lines (69 loc) · 2.19 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
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
use cfg_aliases::cfg_aliases;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::Command;
fn main() {
cfg_aliases! {
// Single source of truth for feature aliases used in cfg attributes
tablet: { feature = "tablet-input" },
}
println!("cargo:rerun-if-env-changed=WAYSCRIBER_RELEASE_VERSION");
match env::var("WAYSCRIBER_RELEASE_VERSION") {
Ok(release_version) if !release_version.is_empty() => {
println!("cargo:rustc-env=WAYSCRIBER_RELEASE_VERSION={release_version}");
}
_ => {}
}
let hash = Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|output| {
if output.status.success() {
Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
} else {
None
}
})
.filter(|s| !s.is_empty())
.unwrap_or_else(|| "unknown".into());
println!("cargo:rustc-env=WAYSCRIBER_GIT_HASH={hash}");
if let Some(git_dir) = resolve_git_dir() {
emit_rerun(&git_dir.join("HEAD"));
emit_rerun(&git_dir.join("refs"));
emit_rerun(&git_dir.join("packed-refs"));
}
}
#[allow(clippy::collapsible_if)]
fn resolve_git_dir() -> Option<PathBuf> {
if let Some(from_env) = env::var_os("GIT_DIR") {
return Some(PathBuf::from(from_env));
}
let dot_git = PathBuf::from(".git");
if dot_git.is_dir() {
return Some(dot_git);
}
if dot_git.is_file() {
if let Ok(contents) = fs::read_to_string(&dot_git) {
if let Some(rest) = contents.strip_prefix("gitdir:") {
let mut resolved = PathBuf::from(rest.trim());
if resolved.is_relative() {
if let Some(parent) = dot_git.parent() {
resolved = parent.join(resolved);
}
}
return Some(resolved);
}
}
}
None
}
#[allow(clippy::collapsible_if)]
fn emit_rerun(path: &Path) {
if path.exists() {
if let Some(display) = path.to_str() {
println!("cargo:rerun-if-changed={display}");
}
}
}