-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
79 lines (71 loc) · 2.86 KB
/
build.rs
File metadata and controls
79 lines (71 loc) · 2.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
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 std::fs;
use std::path::PathBuf;
use std::process::Command;
fn main() {
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/index");
println!("cargo:rerun-if-env-changed=CORE_OPS_BUILD_REVISION");
println!("cargo:rerun-if-env-changed=CORE_OPS_BUILD_TIME");
println!("cargo:rerun-if-env-changed=CORE_OPS_TREE_STATE");
println!("cargo:rerun-if-env-changed=CORE_OPS_BUILD_SPEC_CONTEXT");
let revision = env_or_git_revision();
let build_time = env_or_build_time();
let tree_state = env_or_tree_state().unwrap_or_else(|| "unknown".to_string());
let spec_context = std::env::var("CORE_OPS_BUILD_SPEC_CONTEXT")
.ok()
.filter(|value| !value.trim().is_empty());
let out_dir = PathBuf::from(std::env::var("OUT_DIR").expect("OUT_DIR"));
let contents = format!(
"pub const BUILD_REVISION: Option<&str> = {revision};\n\
pub const BUILD_TIME: Option<&str> = {build_time};\n\
pub const BUILD_TREE_STATE: &str = {tree_state:?};\n\
pub const BUILD_SPEC_CONTEXT: Option<&str> = {spec_context};\n",
revision = option_literal(revision.as_deref()),
build_time = option_literal(build_time.as_deref()),
tree_state = tree_state,
spec_context = option_literal(spec_context.as_deref()),
);
fs::write(out_dir.join("build_info.rs"), contents).expect("write build_info.rs");
}
fn env_or_git_revision() -> Option<String> {
std::env::var("CORE_OPS_BUILD_REVISION")
.ok()
.filter(|value| !value.trim().is_empty())
.or_else(|| run_command(&["git", "rev-parse", "--short", "HEAD"]))
}
fn env_or_build_time() -> Option<String> {
std::env::var("CORE_OPS_BUILD_TIME")
.ok()
.filter(|value| !value.trim().is_empty())
.or_else(|| run_command(&["date", "-u", "+%Y-%m-%dT%H:%M:%SZ"]))
}
fn env_or_tree_state() -> Option<String> {
std::env::var("CORE_OPS_TREE_STATE")
.ok()
.filter(|value| !value.trim().is_empty())
.or_else(|| {
match Command::new("git")
.args(["diff", "--quiet", "--ignore-submodules=dirty"])
.status()
{
Ok(status) if status.success() => Some("clean".to_string()),
Ok(status) if status.code() == Some(1) => Some("dirty".to_string()),
_ => None,
}
})
}
fn run_command(args: &[&str]) -> Option<String> {
let (program, rest) = args.split_first()?;
let output = Command::new(program).args(rest).output().ok()?;
if !output.status.success() {
return None;
}
let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
(!value.is_empty()).then_some(value)
}
fn option_literal(value: Option<&str>) -> String {
match value {
Some(value) => format!("Some({value:?})"),
None => "None".to_string(),
}
}