-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
252 lines (222 loc) · 7.67 KB
/
build.rs
File metadata and controls
252 lines (222 loc) · 7.67 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
// SPDX-FileCopyrightText: 2025 RAprogramm <andrey.rozanov.vl@gmail.com>
//
// SPDX-License-Identifier: MIT
use std::{
env, fs,
path::{Path, PathBuf},
process,
process::{Command, Stdio}
};
use crate::readme::{sync_readme, verify_readme_relaxed};
#[path = "build/readme.rs"]
mod readme;
fn main() {
if let Err(err) = run() {
eprintln!("error: {err}");
process::exit(1);
}
}
/// Runs build script with README sync and feature detection.
///
/// # Modes
///
/// - **Drift allowed**: Skip all checks if `MASTERROR_DISABLE_README_SYNC=1`
/// - **Packaged/no-git**: Relaxed verification (warning only, no failure)
/// - **Git working tree**: Strict sync mode
fn run() -> Result<(), Box<dyn std::error::Error>> {
println!("cargo:rustc-check-cfg=cfg(masterror_has_error_generic_member_access)");
println!("cargo:rustc-check-cfg=cfg(masterror_requires_error_generic_feature)");
println!("cargo:rerun-if-changed=Cargo.toml");
println!("cargo:rerun-if-changed=README.template.md");
println!("cargo:rerun-if-changed=build/readme.rs");
println!("cargo:rerun-if-env-changed=MASTERROR_DISABLE_ERROR_GENERIC_MEMBER_ACCESS");
let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR")?);
if allow_readme_drift() {
return Ok(());
}
if is_packaged_manifest(&manifest_dir) || !has_git_anywhere(&manifest_dir) {
if let Err(err) = verify_readme_relaxed(&manifest_dir) {
println!("cargo:warning={err}");
}
return Ok(());
}
sync_readme(&manifest_dir)?;
if let Some(support) = detect_error_generic_member_access()? {
if support.requires_feature_attr {
println!("cargo:rustc-cfg=masterror_requires_error_generic_feature");
}
println!("cargo:rustc-cfg=masterror_has_error_generic_member_access");
}
Ok(())
}
struct ErrorGenericSupport {
requires_feature_attr: bool
}
// Your previous heuristic: target/package/... => packaged
fn is_packaged_manifest(manifest_dir: &Path) -> bool {
let mut seen_target = false;
for comp in manifest_dir.components() {
match comp {
std::path::Component::Normal(name) => {
if seen_target && name == "package" {
return true;
}
seen_target = name == "target";
}
_ => {
seen_target = false;
}
}
}
false
}
// Check .git up the chain (workspace root is often above
// the crate directory)
fn has_git_anywhere(mut dir: &Path) -> bool {
loop {
if dir.join(".git").exists() {
return true;
}
match dir.parent() {
Some(p) => dir = p,
None => return false
}
}
}
fn allow_readme_drift() -> bool {
has_env("MASTERROR_ALLOW_README_DRIFT") || has_env("MASTERROR_SKIP_README_CHECK")
}
fn has_env(name: &str) -> bool {
env::var_os(name).map(|v| !v.is_empty()).unwrap_or(false)
}
fn detect_error_generic_member_access()
-> Result<Option<ErrorGenericSupport>, Box<dyn std::error::Error>> {
if has_env("MASTERROR_DISABLE_ERROR_GENERIC_MEMBER_ACCESS") {
return Ok(None);
}
let out_dir = PathBuf::from(env::var("OUT_DIR")?);
fs::create_dir_all(&out_dir)?;
let stable_check = out_dir.join("check_error_generic_stable.rs");
fs::write(&stable_check, STABLE_SNIPPET)?;
if compile_probe(&stable_check, &out_dir)?.success() {
return Ok(Some(ErrorGenericSupport {
requires_feature_attr: false
}));
}
let nightly_check = out_dir.join("check_error_generic_nightly.rs");
fs::write(&nightly_check, NIGHTLY_SNIPPET)?;
if compile_probe(&nightly_check, &out_dir)?.success() {
return Ok(Some(ErrorGenericSupport {
requires_feature_attr: true
}));
}
Ok(None)
}
fn compile_probe(
source: &Path,
out_dir: &Path
) -> Result<process::ExitStatus, Box<dyn std::error::Error>> {
let rustc = env::var("RUSTC")?;
let mut cmd = Command::new(rustc);
cmd.arg("--crate-type").arg("lib");
cmd.arg("--emit").arg("metadata");
cmd.arg(source);
cmd.arg("-o");
cmd.arg(out_dir.join("check_error_generic.rmeta"));
cmd.stdout(Stdio::null());
cmd.stderr(Stdio::null());
Ok(cmd.status()?)
}
const STABLE_SNIPPET: &str = r#"use std::error::{Error, Request};
pub fn probe(request: &mut Request<'_>, error: &(dyn Error + 'static)) {
let _ = request;
let _ = error;
}
"#;
const NIGHTLY_SNIPPET: &str = r#"#![feature(error_generic_member_access)]
use std::error::{Error, Request};
pub fn probe(request: &mut Request<'_>, error: &(dyn Error + 'static)) {
request.provide_ref::<&'static str>(&"marker");
request.provide_value::<usize>(0);
let _ = error;
}
"#;
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::*;
#[test]
fn is_packaged_manifest_detects_package_dir() {
let path = PathBuf::from("/home/user/project/target/package/masterror-0.1.0");
assert!(is_packaged_manifest(&path));
}
#[test]
fn is_packaged_manifest_rejects_normal_dir() {
let path = PathBuf::from("/home/user/project/src");
assert!(!is_packaged_manifest(&path));
}
#[test]
fn is_packaged_manifest_rejects_target_without_package() {
let path = PathBuf::from("/home/user/project/target/debug");
assert!(!is_packaged_manifest(&path));
}
#[test]
fn is_packaged_manifest_rejects_package_without_target() {
let path = PathBuf::from("/home/user/package/something");
assert!(!is_packaged_manifest(&path));
}
#[test]
fn has_env_returns_true_when_var_set() {
env::set_var("MASTERROR_TEST_VAR", "1");
assert!(has_env("MASTERROR_TEST_VAR"));
env::remove_var("MASTERROR_TEST_VAR");
}
#[test]
fn has_env_returns_false_when_var_not_set() {
env::remove_var("MASTERROR_TEST_VAR_NONEXISTENT");
assert!(!has_env("MASTERROR_TEST_VAR_NONEXISTENT"));
}
#[test]
fn has_env_returns_false_when_var_empty() {
env::set_var("MASTERROR_TEST_VAR_EMPTY", "");
assert!(!has_env("MASTERROR_TEST_VAR_EMPTY"));
env::remove_var("MASTERROR_TEST_VAR_EMPTY");
}
#[test]
fn allow_readme_drift_checks_both_vars() {
env::remove_var("MASTERROR_ALLOW_README_DRIFT");
env::remove_var("MASTERROR_SKIP_README_CHECK");
assert!(!allow_readme_drift());
env::set_var("MASTERROR_ALLOW_README_DRIFT", "1");
assert!(allow_readme_drift());
env::remove_var("MASTERROR_ALLOW_README_DRIFT");
env::set_var("MASTERROR_SKIP_README_CHECK", "1");
assert!(allow_readme_drift());
env::remove_var("MASTERROR_SKIP_README_CHECK");
}
#[test]
fn error_generic_support_struct_exists() {
let support = ErrorGenericSupport {
requires_feature_attr: true
};
assert!(support.requires_feature_attr);
let support = ErrorGenericSupport {
requires_feature_attr: false
};
assert!(!support.requires_feature_attr);
}
#[test]
fn stable_snippet_is_valid_rust() {
assert!(STABLE_SNIPPET.contains("use std::error::{Error, Request}"));
assert!(STABLE_SNIPPET.contains("pub fn probe"));
assert!(!STABLE_SNIPPET.contains("#![feature"));
}
#[test]
fn nightly_snippet_is_valid_rust_with_feature() {
assert!(NIGHTLY_SNIPPET.contains("use std::error::{Error, Request}"));
assert!(NIGHTLY_SNIPPET.contains("pub fn probe"));
assert!(NIGHTLY_SNIPPET.contains("#![feature(error_generic_member_access)]"));
assert!(NIGHTLY_SNIPPET.contains("provide_ref"));
assert!(NIGHTLY_SNIPPET.contains("provide_value"));
}
}