Repository navigation
Expand file tree
/
Copy pathbuild.rs
More file actions
137 lines (119 loc) · 5.15 KB
/
Copy pathbuild.rs
File metadata and controls
137 lines (119 loc) · 5.15 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
use std::collections::HashSet;
use std::fs;
use std::path::Path;
/// Reserve a larger main-thread stack for Windows targets.
///
/// Clap + the full command graph can exceed the default 1 MiB Windows stack
/// during process startup, so `rtk.exe --version`, `--help`, and the hook entry
/// points need the headroom to start reliably without ad-hoc RUSTFLAGS.
///
/// Two things here are deliberately keyed off the **target**, not the host:
/// `CARGO_CFG_TARGET_OS` (a `#[cfg(windows)]` here would describe the machine
/// running the build, so cross-compiling to Windows from Linux would silently
/// skip the flag), and the flag syntax itself — `link.exe` takes `/STACK:`
/// while the MinGW `ld` behind `x86_64-pc-windows-gnu` only understands
/// `--stack`, and feeding it `/STACK:` makes it look for a file by that name.
/// Only `msvc` needs its own spelling: `gnu` (gnullvm reports it too) links
/// through a gcc or clang driver that takes `-Wl,--stack`, and that is also what
/// an environment the match does not name gets.
fn set_windows_stack_size() {
if std::env::var("CARGO_CFG_TARGET_OS").as_deref() != Ok("windows") {
return;
}
const STACK_BYTES: usize = 8 * 1024 * 1024;
let arg = match std::env::var("CARGO_CFG_TARGET_ENV").as_deref() {
Ok("msvc") => format!("/STACK:{STACK_BYTES}"),
_ => format!("-Wl,--stack,{STACK_BYTES}"),
};
println!("cargo:rustc-link-arg={arg}");
}
fn main() {
set_windows_stack_size();
check_cmds_modules_declared();
let filters_dir = Path::new("src/filters");
let out_dir = std::env::var("OUT_DIR").expect("OUT_DIR must be set by Cargo");
let dest = Path::new(&out_dir).join("builtin_filters.toml");
// Rebuild when any file in src/filters/ changes
println!("cargo:rerun-if-changed=src/filters");
let mut files: Vec<_> = fs::read_dir(filters_dir)
.expect("src/filters/ directory must exist")
.filter_map(|e| e.ok())
.filter(|e| e.path().extension().is_some_and(|ext| ext == "toml"))
.collect();
// Sort alphabetically for deterministic filter ordering
files.sort_by_key(|e| e.file_name());
let mut combined = String::from("schema_version = 1\n\n");
for entry in &files {
let content = fs::read_to_string(entry.path())
.unwrap_or_else(|e| panic!("Failed to read {:?}: {}", entry.path(), e));
combined.push_str(&format!(
"# --- {} ---\n",
entry.file_name().to_string_lossy()
));
combined.push_str(&content);
combined.push_str("\n\n");
}
// Validate: parse the combined TOML to catch errors at build time
let parsed: toml::Value = combined.parse().unwrap_or_else(|e| {
panic!(
"TOML validation failed for combined filters:\n{}\n\nCheck src/filters/*.toml files",
e
)
});
// Detect duplicate filter names across files
if let Some(filters) = parsed.get("filters").and_then(|f| f.as_table()) {
let mut seen: HashSet<String> = HashSet::new();
for key in filters.keys() {
if !seen.insert(key.clone()) {
panic!(
"Duplicate filter name '{}' found across src/filters/*.toml files",
key
);
}
}
}
fs::write(&dest, combined).expect("Failed to write combined builtin_filters.toml");
}
/// Every `.rs` under `src/cmds/<ecosystem>/` must be declared in that folder's
/// `mod.rs`. An undeclared file is not part of the crate: it is never compiled,
/// never linted, and its tests never run, all without any diagnostic.
fn check_cmds_modules_declared() {
println!("cargo:rerun-if-changed=src/cmds");
let cmds = Path::new("src/cmds");
let ecosystems = fs::read_dir(cmds).expect("src/cmds/ directory must exist");
for ecosystem in ecosystems {
let dir = ecosystem.expect("failed to read src/cmds/ entry").path();
let mod_rs = dir.join("mod.rs");
if !dir.is_dir() || !mod_rs.exists() {
continue;
}
let declared = fs::read_to_string(&mod_rs)
.unwrap_or_else(|e| panic!("Failed to read {}: {}", mod_rs.display(), e));
let entries = fs::read_dir(&dir)
.unwrap_or_else(|e| panic!("Failed to read {}: {}", dir.display(), e));
for entry in entries {
let path = entry.expect("failed to read directory entry").path();
if path.extension().is_none_or(|ext| ext != "rs") {
continue;
}
let stem = path
.file_stem()
.expect("a .rs path always has a stem")
.to_string_lossy()
.to_string();
if stem == "mod" {
continue;
}
if !declared.contains(&format!("mod {stem};")) {
panic!(
"{} is not declared in {}.\n\
Add `pub mod {};` there, keeping the list alphabetical.\n\
An undeclared module is never compiled, linted, or tested.",
path.display(),
mod_rs.display(),
stem
);
}
}
}
}