-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuild.rs
158 lines (125 loc) · 4.13 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use std::env;
use std::ffi::OsStr;
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Write};
use std::process::Command;
use std::str::from_utf8;
use std::sync::LazyLock;
fn set_env(key: &str, value: &str) {
println!("cargo:rustc-env={key}={value}");
}
fn set_env_bool(key: &str, value: bool) {
set_env(key, if value { "1" } else { "0" });
}
fn rerun_if_changed(path: &str) {
println!("cargo:rustc-rerun-if-changed={path}");
}
fn run<S, I>(program: &str, args: I) -> Option<String>
where
I: IntoIterator<Item = S>,
S: AsRef<OsStr>,
{
let p = Command::new(program)
.args(args)
.output()
.unwrap_or_else(|_| panic!("missing tool '{program}'"));
if p.status.success() {
Some(from_utf8(&p.stdout).unwrap().trim().to_owned())
} else {
None
}
}
macro_rules! cached_getter {
($fn:ident, $cached_type:ty, $typ:ty, $init_fn:block) => {
pub fn $fn() -> &'static $typ {
static CACHE: LazyLock<$cached_type> = LazyLock::new(|| $init_fn);
&CACHE
}
};
}
cached_getter!(commit_hash, String, str, {
run("git", ["rev-parse", "--short", "HEAD"]).unwrap_or_else(|| "unknown".into())
});
cached_getter!(build_date, String, str, {
let date = chrono::Local::now();
date.format("%a %d %b %Y %H:%M:%S (UTC%:z)").to_string()
});
cached_getter!(build_user, String, str, {
let user = run("id", ["-un"]).unwrap_or_else(|| "unknown".into());
let host = run::<&str, _>("hostname", []).unwrap_or_else(|| "unknown".into());
format!("{user}@{host}")
});
cached_getter!(build_profile, String, str, { env::var("PROFILE").unwrap() });
cached_getter!(rustc, String, str, { env::var("RUSTC").unwrap() });
cached_getter!(rustc_version, String, str, {
run(rustc(), ["--version"]).unwrap_or_else(|| "<rustc unknown>".into())
});
cached_getter!(out_dir, String, str, { env::var("OUT_DIR").unwrap() });
pub fn is_vcs_dirty() -> bool {
static CACHE: LazyLock<bool> = LazyLock::new(|| {
let git = Command::new("git")
.args(["diff-index", "--quiet", "HEAD"])
.output()
.unwrap();
git.status.code().unwrap_or(1) == 1
});
*CACHE
}
pub fn build_is_debug() -> bool {
build_profile() == "debug"
}
fn write_quotes(input: &str) {
let input_file = BufReader::new(
File::options()
.read(true)
.open(input)
.expect("failed to open input quote file"),
);
let mut out = BufWriter::new(
File::options()
.write(true)
.truncate(true)
.create(true)
.open(format!("{}/quotes.rs", out_dir()))
.expect("failed to open quote output file"),
);
writeln!(
out,
r#"
struct BuiltInQuote {{
text: &'static [&'static str],
author: &'static str,
}}
const BUILT_IN_QUOTES: &[BuiltInQuote] = &["#
)
.unwrap();
input_file
.lines()
.map(|x| x.expect("failed to read quote"))
.filter(|x| !x.is_empty())
.filter(|x| !x.starts_with('#'))
.for_each(|line| {
let mut fields = line.split('|');
let raw_quote_text = fields.next().expect("missing quote text");
let quote_text_lines = raw_quote_text.split("\\n").map(|x| x.trim());
let author = fields.next().expect("missing author").trim();
writeln!(out, r#"BuiltInQuote {{"#).unwrap();
writeln!(out, r#"text: &["#).unwrap();
quote_text_lines.for_each(|line| writeln!(out, r#""{line}","#).unwrap());
writeln!(out, "],").unwrap();
writeln!(out, r#"author: "{author}","#).unwrap();
writeln!(out, r#"}},"#).unwrap();
});
writeln!(out, r#"];"#).unwrap();
}
fn main() {
rerun_if_changed(".git/HEAD");
set_env("GIT_COMMIT", commit_hash());
set_env("BUILD_DATE", build_date());
set_env("BUILD_USER", build_user());
set_env("BUILD_PROFILE", build_profile());
set_env("RUSTC_VERSION", rustc_version());
set_env_bool("IS_VCS_DIRTY", is_vcs_dirty());
set_env_bool("BUILD_IS_DEBUG", build_is_debug());
write_quotes("src/quotes.txt");
}