-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
120 lines (101 loc) · 3.33 KB
/
build.rs
File metadata and controls
120 lines (101 loc) · 3.33 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
use std::env;
use std::process::Command;
fn report_build_profile() {
println!(
"cargo:rustc-env=BUILD_PROFILE={}",
env::var("PROFILE").unwrap_or_else(|_| "unknown".to_string())
);
}
fn report_enabled_features() {
let mut enabled_features: Vec<&str> = Vec::new();
#[cfg(feature = "serde")]
enabled_features.push("serde");
#[cfg(feature = "async")]
enabled_features.push("async");
#[cfg(feature = "tokio")]
enabled_features.push("tokio");
#[cfg(feature = "tracing")]
enabled_features.push("tracing");
#[cfg(feature = "http")]
enabled_features.push("http");
#[cfg(feature = "cli")]
enabled_features.push("cli");
if enabled_features.is_empty() {
enabled_features.push("none");
}
println!(
"cargo:rustc-env=BUILD_FEATURES={}",
enabled_features.join(",")
);
}
fn report_repository_version() {
let version = match env::var("CI_BUILD_REF") {
Ok(val) if !val.is_empty() => val,
_ => {
match Command::new("git")
.args(["describe", "--always", "--dirty", "--long", "--tags"])
.output()
{
Ok(output) if output.status.success() => {
String::from_utf8(output.stdout)
.unwrap_or_else(|_| "unknown".to_string())
.trim()
.to_string()
}
_ => {
match Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
{
Ok(output) if output.status.success() => {
String::from_utf8(output.stdout)
.unwrap_or_else(|_| "unknown".to_string())
.trim()
.to_string()
}
_ => env::var("CARGO_PKG_VERSION").unwrap_or_else(|_| "unknown".to_string())
}
}
}
}
};
println!("cargo:rustc-env=REPO_VERSION={}", version);
}
fn report_build_timestamp() {
let timestamp = chrono::Utc::now().to_rfc3339();
println!("cargo:rustc-env=BUILD_TIMESTAMP={}", timestamp);
}
fn report_rust_version() {
let rust_version = match Command::new("rustc")
.args(["--version"])
.output()
{
Ok(output) if output.status.success() => {
String::from_utf8(output.stdout)
.unwrap_or_else(|_| "unknown".to_string())
.trim()
.to_string()
}
_ => "unknown".to_string()
};
println!("cargo:rustc-env=RUST_VERSION={}", rust_version);
}
fn report_target_info() {
if let Ok(target) = env::var("TARGET") {
println!("cargo:rustc-env=BUILD_TARGET={}", target);
}
if let Ok(host) = env::var("HOST") {
println!("cargo:rustc-env=BUILD_HOST={}", host);
}
}
fn main() {
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=.git/HEAD");
println!("cargo:rerun-if-changed=.git/refs/heads");
report_build_profile();
report_enabled_features();
report_repository_version();
report_build_timestamp();
report_rust_version();
report_target_info();
}