forked from Treeniks/chameleos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
74 lines (64 loc) · 2.13 KB
/
build.rs
File metadata and controls
74 lines (64 loc) · 2.13 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
use std::path::Path;
use std::process::Command;
use std::process::ExitStatus;
fn run_command<const N: usize>(command: [&str; N]) -> (ExitStatus, String) {
let s = Command::new(command[0])
.args(&command[1..])
.output()
.unwrap();
(
s.status,
String::from_utf8(s.stdout).unwrap().trim().to_owned(),
)
}
fn main() {
println!("cargo::rerun-if-changed=.git");
println!("cargo::rerun-if-changed=src");
let version = {
let (status, describe) = run_command(["git", "describe", "--exact-match", "--tags"]);
status
.success()
.then_some(describe)
.or_else(|| {
let (status, describe) = run_command(["git", "describe", "--long", "--tags"]);
status.success().then_some(describe)
})
.unwrap_or_else(|| format!("v{}", std::env::var("CARGO_PKG_VERSION").unwrap()))
};
let commit_hash = run_command(["git", "rev-parse", "HEAD"]).1;
let rustc_version = run_command([&std::env::var("RUSTC").unwrap(), "--version"]).1;
let build_time = format!("{}", chrono::Local::now());
let target = {
let mut s = format!(
"{}-{}-{}",
std::env::var("CARGO_CFG_TARGET_ARCH").unwrap(),
std::env::var("CARGO_CFG_TARGET_VENDOR").unwrap(),
std::env::var("CARGO_CFG_TARGET_OS").unwrap(),
);
let env = std::env::var("CARGO_CFG_TARGET_ENV").unwrap();
if !env.is_empty() {
s.push('-');
s.push_str(&env);
}
s
};
let mut long_version = format!("{version}",);
if !commit_hash.is_empty() {
long_version.push_str(&format!("\ncommit hash: {commit_hash}"));
}
long_version.push_str(&format!(
"\nbuild time: {build_time}\n{rustc_version}\n{target}"
));
let out_dir = std::env::var("OUT_DIR").unwrap();
let dest_path = Path::new(&out_dir).join("metadata.rs");
std::fs::write(
dest_path,
format!(
r#"
pub const VERSION: &str = "{version}";
pub const LONG_VERSION: &str = "{long_version}";
"#
),
)
.unwrap();
}