-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
106 lines (95 loc) · 3.04 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
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
use std::{env, path::Path, process::Command};
use sysinfo::{CpuRefreshKind, RefreshKind, System};
use tera::{Context, Tera};
fn main() {
built::write_built_file().expect("built failed");
gen_ch4_version();
export_sysinfo();
write_main();
}
fn export_sysinfo() {
let na = Some("n/a".to_string());
let mut name = na.clone();
let mut os_version = na.clone();
let mut cpu_vendor = na.clone();
let mut cpu_brand = na.clone();
if sysinfo::IS_SUPPORTED_SYSTEM {
let system = System::new_with_specifics(
RefreshKind::nothing().with_cpu(CpuRefreshKind::everything()),
);
name = System::name().or_else(|| na.clone());
os_version = System::long_os_version().or_else(|| na.clone());
let cpu = system.cpus().first();
cpu_vendor = cpu
.map(|cpu| cpu.vendor_id().to_string())
.or_else(|| na.clone());
cpu_brand = cpu
.map(|cpu| cpu.brand().to_string())
.or_else(|| na.clone());
}
println!("cargo:rustc-env=CH4_SYSINFO_NAME={}", name.unwrap());
println!(
"cargo:rustc-env=CH4_SYSINFO_OS_VERSION={}",
os_version.unwrap()
);
println!(
"cargo:rustc-env=CH4_SYSINFO_CPU_VENDOR={}",
cpu_vendor.unwrap()
);
println!(
"cargo:rustc-env=CH4_SYSINFO_CPU_BRAND={}",
cpu_brand.unwrap()
);
}
fn gen_ch4_version() {
let mut ch4_version = env::var("CARGO_PKG_VERSION").unwrap();
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let path = Path::new(&manifest_dir);
match built::util::get_repo_head(path) {
Ok(Some((_, _, short_hash))) => {
ch4_version = format!("{ch4_version} git:{short_hash}");
}
Ok(None) => {}
Err(_) => {}
}
println!("cargo:rustc-env=CH4_VERSION={ch4_version}");
}
fn format_file(path: &std::path::Path) -> bool {
let path_str = path.to_str().unwrap();
Command::new("rustfmt")
.args(["--edition", "2021"])
.arg(path_str)
.output()
.map(|o| o.status.success())
.unwrap_or(false)
}
fn write_file(tera: &Tera, context: &Context, file_name: &str) {
let out_dir = std::env::var_os("OUT_DIR").unwrap();
let file_data = tera
.render("main_template.rs", context)
.expect("failed to render template");
let file_path = std::path::Path::new(&out_dir).join(file_name);
std::fs::write(&file_path, file_data).expect("failed to write file");
format_file(&file_path);
}
fn write_main() {
let tera = match Tera::new("templates/*.rs") {
Ok(t) => t,
Err(e) => {
panic!("Tera parsing error(s): {e}");
}
};
for async_value in &["true", "false"] {
let mut context = Context::new();
context.insert("async", async_value);
let file_name = format!(
"{}_main.rs",
if *async_value == "true" {
"async"
} else {
"std"
}
);
write_file(&tera, &context, &file_name);
}
}