Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
224 lines (206 loc) · 7.2 KB
/
Copy pathbuild.rs
File metadata and controls
224 lines (206 loc) · 7.2 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
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
//! Records a bounded source revision and cleanliness for compile-time identity.
//!
//! An explicit packager state (`MACO_SOURCE_STATE`) is used with
//! `MACO_SOURCE_REVISION` and skips git. Otherwise git is probed only at
//! `CARGO_MANIFEST_DIR`. A missing git binary, a failed probe, or a revision
//! that is not 40 hexadecimal digits records `unknown` and an empty revision.
//! A dirty packager revision may also use the Nix form `{40-hex}-dirty`; the
//! suffix is removed and the state stays dirty. Metadata gaps do not fail the
//! build.
//!
//! Rerun directives cover the package inputs plus the git HEAD, index, and
//! current branch ref resolved by git. That includes a linked worktree, where
//! `.git` is a gitfile. Emitting any `rerun-if-changed` path replaces Cargo's
//! default package scan, so those package inputs have to be listed here.
use std::path::Path;
use std::process::{Command, Stdio};
const REVISION_HEX_LEN: usize = 40;
const PACKAGE_INPUTS: &[&str] = &[
"build.rs",
"Cargo.toml",
"Cargo.lock",
"src",
"tests",
"benches",
"assets",
"schemas",
"docs",
"scripts",
"flake.nix",
"README.md",
];
fn main() {
println!("cargo:rerun-if-env-changed=MACO_SOURCE_REVISION");
println!("cargo:rerun-if-env-changed=MACO_SOURCE_STATE");
if let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") {
emit_package_rerun_directives(&manifest_dir);
emit_git_rerun_directives(&manifest_dir);
}
let (revision, state) = match packager_override() {
Some(recorded) => recorded,
None => probe_git(),
};
let (revision, state) = normalize_recorded(&revision, &state);
println!("cargo:rustc-env=MACO_SOURCE_REVISION={revision}");
println!("cargo:rustc-env=MACO_SOURCE_STATE={state}");
}
/// Non-empty `MACO_SOURCE_STATE` is an explicit packager input. Empty or
/// absent state falls through to the git probe.
fn packager_override() -> Option<(String, String)> {
let state = std::env::var_os("MACO_SOURCE_STATE")?;
if state.is_empty() {
return None;
}
let state = state.to_str().unwrap_or("").to_string();
let revision = std::env::var("MACO_SOURCE_REVISION").unwrap_or_default();
Some((revision, state))
}
fn probe_git() -> (String, String) {
let Ok(manifest_dir) = std::env::var("CARGO_MANIFEST_DIR") else {
return unknown_record();
};
let Some(head) = run_git(&manifest_dir, &["rev-parse", "HEAD"]) else {
return unknown_record();
};
let Some(revision) = git_stdout_line(&head) else {
return unknown_record();
};
if !is_lowercase_hex_revision(&revision) {
return unknown_record();
}
let Some(status) = run_git(
&manifest_dir,
&["status", "--porcelain", "--untracked-files=normal"],
) else {
return unknown_record();
};
let state = if status.is_empty() { "clean" } else { "dirty" };
(revision, state.to_string())
}
fn unknown_record() -> (String, String) {
(String::new(), "unknown".to_string())
}
/// `git -C <manifest> -c color.ui=false …`, with repository-location variables
/// removed so the probe cannot follow an ambient checkout.
fn run_git(manifest_dir: &str, args: &[&str]) -> Option<Vec<u8>> {
let output = Command::new("git")
.arg("-C")
.arg(manifest_dir)
.arg("-c")
.arg("color.ui=false")
.args(args)
.env_remove("GIT_DIR")
.env_remove("GIT_WORK_TREE")
.env_remove("GIT_INDEX_FILE")
.stdin(Stdio::null())
.stderr(Stdio::null())
.current_dir(manifest_dir)
.output()
.ok()?;
if output.status.success() {
Some(output.stdout)
} else {
None
}
}
fn git_stdout_line(stdout: &[u8]) -> Option<String> {
let mut text = String::from_utf8(stdout.to_vec()).ok()?;
if text.ends_with('\n') {
text.pop();
if text.ends_with('\r') {
text.pop();
}
}
Some(text)
}
fn is_lowercase_hex_revision(revision: &str) -> bool {
revision.len() == REVISION_HEX_LEN
&& revision
.bytes()
.all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f'))
}
fn canonical_revision(revision: &str) -> Option<String> {
if revision.len() != REVISION_HEX_LEN {
return None;
}
if !revision.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return None;
}
Some(revision.to_ascii_lowercase())
}
fn normalize_recorded(revision: &str, state: &str) -> (String, &'static str) {
let revision = canonical_revision(revision_for_state(revision, state));
match (state, revision) {
("clean", Some(revision)) => (revision, "clean"),
("dirty", Some(revision)) => (revision, "dirty"),
_ => (String::new(), "unknown"),
}
}
/// Nix `dirtyRev` is the commit plus a `-dirty` suffix. Only the dirty state
/// accepts that form. Keep this in step with `build_identity::normalize_recorded`.
fn revision_for_state<'a>(revision: &'a str, state: &str) -> &'a str {
if state == "dirty" {
revision.strip_suffix("-dirty").unwrap_or(revision)
} else {
revision
}
}
fn emit_package_rerun_directives(manifest_dir: &str) {
for relative in PACKAGE_INPUTS {
let path = Path::new(manifest_dir).join(relative);
if path.exists() {
if let Some(text) = path.to_str() {
println!("cargo:rerun-if-changed={text}");
}
}
}
}
/// Watch the git files that change the recorded revision or cleanliness.
/// `git rev-parse` follows a linked-worktree gitfile, so the directive is the
/// real HEAD, index, or branch ref rather than a missing `.git/HEAD`.
fn emit_git_rerun_directives(manifest_dir: &str) {
emit_existing_git_file(manifest_dir, "HEAD");
emit_existing_git_file(manifest_dir, "index");
let Some(stdout) = run_git(manifest_dir, &["symbolic-ref", "-q", "HEAD"]) else {
return;
};
let Some(refname) = git_stdout_line(&stdout) else {
return;
};
if refname.is_empty() || refname.contains(['\n', '\r']) || refname.starts_with('-') {
return;
}
emit_existing_git_file(manifest_dir, &refname);
}
fn emit_existing_git_file(manifest_dir: &str, spec: &str) {
let Some(path) = absolute_git_path(manifest_dir, spec) else {
return;
};
if std::fs::metadata(&path).is_ok_and(|metadata| metadata.is_file()) {
println!("cargo:rerun-if-changed={path}");
}
}
fn absolute_git_path(manifest_dir: &str, spec: &str) -> Option<String> {
if let Some(stdout) = run_git(
manifest_dir,
&["rev-parse", "--path-format=absolute", "--git-path", spec],
) {
if let Some(path) = git_stdout_line(&stdout) {
if !path.is_empty() {
return Some(path);
}
}
}
let git_dir = git_stdout_line(&run_git(
manifest_dir,
&["rev-parse", "--absolute-git-dir"],
)?)?;
let relative = git_stdout_line(&run_git(manifest_dir, &["rev-parse", "--git-path", spec])?)?;
let relative_path = Path::new(&relative);
let resolved = if relative_path.is_absolute() {
relative_path.to_path_buf()
} else {
Path::new(&git_dir).join(relative_path)
};
resolved.to_str().map(ToString::to_string)
}