-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
52 lines (48 loc) · 1.86 KB
/
Copy pathbuild.rs
File metadata and controls
52 lines (48 loc) · 1.86 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
use std::env;
use std::fs;
use std::path::Path;
use std::process::Command;
fn valid_sha(value: &str) -> Option<String> {
let trimmed = value.trim();
((trimmed.len() == 40 || trimmed.len() == 64)
&& trimmed.bytes().all(|byte| byte.is_ascii_hexdigit()))
.then(|| trimmed.to_ascii_lowercase())
}
fn cargo_vcs_sha(manifest_dir: &Path) -> Option<String> {
let path = manifest_dir.join(".cargo_vcs_info.json");
let text = fs::read_to_string(path).ok()?;
let marker = "\"sha1\"";
let start = text.find(marker)? + marker.len();
let value = text[start..].split(':').nth(1)?.split('"').nth(1)?;
valid_sha(value)
}
fn git_sha(manifest_dir: &Path) -> Option<String> {
let output = Command::new("git")
.args(["rev-parse", "HEAD"])
.current_dir(manifest_dir)
.output()
.ok()?;
output
.status
.success()
.then(|| String::from_utf8_lossy(&output.stdout).to_string())
.and_then(|value| valid_sha(&value))
}
fn main() {
println!("cargo:rerun-if-env-changed=HARDGATE_BUILD_GIT_SHA");
println!("cargo:rerun-if-env-changed=TARGET");
println!("cargo:rerun-if-changed=.cargo_vcs_info.json");
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
let manifest_path = env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR");
let manifest_dir = Path::new(&manifest_path);
let sha = env::var("HARDGATE_BUILD_GIT_SHA")
.ok()
.and_then(|value| valid_sha(&value))
.or_else(|| cargo_vcs_sha(manifest_dir))
.or_else(|| git_sha(manifest_dir))
.unwrap_or_else(|| "unknown".to_string());
println!("cargo:rustc-env=HARDGATE_BUILD_GIT_SHA={sha}");
let target = env::var("TARGET").expect("Cargo must provide TARGET to build.rs");
println!("cargo:rustc-env=HARDGATE_BUILD_TARGET={target}");
}