Skip to content

Commit 49205a1

Browse files
committed
Extract parts of bootstrap::core::builder into a cli_paths module
1 parent d5419f1 commit 49205a1

File tree

3 files changed

+234
-223
lines changed

3 files changed

+234
-223
lines changed
Lines changed: 229 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,229 @@
1+
//! Various pieces of code for dealing with "paths" passed to bootstrap on the
2+
//! command-line, extracted from `core/builder/mod.rs` because that file is
3+
//! large and hard to navigate.
4+
5+
use std::fmt::{self, Debug};
6+
use std::path::PathBuf;
7+
8+
use crate::core::builder::{Builder, Kind, ShouldRun, StepDescription};
9+
10+
pub(crate) const PATH_REMAP: &[(&str, &[&str])] = &[
11+
// bootstrap.toml uses `rust-analyzer-proc-macro-srv`, but the
12+
// actual path is `proc-macro-srv-cli`
13+
("rust-analyzer-proc-macro-srv", &["src/tools/rust-analyzer/crates/proc-macro-srv-cli"]),
14+
// Make `x test tests` function the same as `x t tests/*`
15+
(
16+
"tests",
17+
&[
18+
// tidy-alphabetical-start
19+
"tests/assembly-llvm",
20+
"tests/codegen-llvm",
21+
"tests/codegen-units",
22+
"tests/coverage",
23+
"tests/coverage-run-rustdoc",
24+
"tests/crashes",
25+
"tests/debuginfo",
26+
"tests/incremental",
27+
"tests/mir-opt",
28+
"tests/pretty",
29+
"tests/run-make",
30+
"tests/run-make-cargo",
31+
"tests/rustdoc",
32+
"tests/rustdoc-gui",
33+
"tests/rustdoc-js",
34+
"tests/rustdoc-js-std",
35+
"tests/rustdoc-json",
36+
"tests/rustdoc-ui",
37+
"tests/ui",
38+
"tests/ui-fulldeps",
39+
// tidy-alphabetical-end
40+
],
41+
),
42+
];
43+
44+
pub(crate) fn remap_paths(paths: &mut Vec<PathBuf>) {
45+
let mut remove = vec![];
46+
let mut add = vec![];
47+
for (i, path) in paths.iter().enumerate().filter_map(|(i, path)| path.to_str().map(|s| (i, s)))
48+
{
49+
for &(search, replace) in PATH_REMAP {
50+
// Remove leading and trailing slashes so `tests/` and `tests` are equivalent
51+
if path.trim_matches(std::path::is_separator) == search {
52+
remove.push(i);
53+
add.extend(replace.iter().map(PathBuf::from));
54+
break;
55+
}
56+
}
57+
}
58+
remove.sort();
59+
remove.dedup();
60+
for idx in remove.into_iter().rev() {
61+
paths.remove(idx);
62+
}
63+
paths.append(&mut add);
64+
}
65+
66+
#[derive(Clone, PartialEq)]
67+
pub(crate) struct CLIStepPath {
68+
pub(crate) path: PathBuf,
69+
pub(crate) will_be_executed: bool,
70+
}
71+
72+
#[cfg(test)]
73+
impl CLIStepPath {
74+
pub(crate) fn will_be_executed(mut self, will_be_executed: bool) -> Self {
75+
self.will_be_executed = will_be_executed;
76+
self
77+
}
78+
}
79+
80+
impl Debug for CLIStepPath {
81+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
82+
write!(f, "{}", self.path.display())
83+
}
84+
}
85+
86+
impl From<PathBuf> for CLIStepPath {
87+
fn from(path: PathBuf) -> Self {
88+
Self { path, will_be_executed: false }
89+
}
90+
}
91+
92+
pub(crate) fn match_paths_to_steps_and_run(
93+
builder: &Builder<'_>,
94+
v: &[StepDescription],
95+
paths: &[PathBuf],
96+
) {
97+
let should_runs = v
98+
.iter()
99+
.map(|desc| (desc.should_run)(ShouldRun::new(builder, desc.kind)))
100+
.collect::<Vec<_>>();
101+
102+
// FIXME(Zalathar): This particular check isn't related to path-to-step
103+
// matching, and should probably be hoisted to somewhere much earlier.
104+
if builder.download_rustc() && (builder.kind == Kind::Dist || builder.kind == Kind::Install) {
105+
eprintln!(
106+
"ERROR: '{}' subcommand is incompatible with `rust.download-rustc`.",
107+
builder.kind.as_str()
108+
);
109+
crate::exit!(1);
110+
}
111+
112+
// sanity checks on rules
113+
for (desc, should_run) in v.iter().zip(&should_runs) {
114+
assert!(!should_run.paths.is_empty(), "{:?} should have at least one pathset", desc.name);
115+
}
116+
117+
if paths.is_empty() || builder.config.include_default_paths {
118+
for (desc, should_run) in v.iter().zip(&should_runs) {
119+
if desc.default && should_run.is_really_default() {
120+
desc.maybe_run(builder, should_run.paths.iter().cloned().collect());
121+
}
122+
}
123+
}
124+
125+
// Attempt to resolve paths to be relative to the builder source directory.
126+
let mut paths: Vec<PathBuf> = paths
127+
.iter()
128+
.map(|original_path| {
129+
let mut path = original_path.clone();
130+
131+
// Someone could run `x <cmd> <path>` from a different repository than the source
132+
// directory.
133+
// In that case, we should not try to resolve the paths relative to the working
134+
// directory, but rather relative to the source directory.
135+
// So we forcefully "relocate" the path to the source directory here.
136+
if !path.is_absolute() {
137+
path = builder.src.join(path);
138+
}
139+
140+
// If the path does not exist, it may represent the name of a Step, such as `tidy` in `x test tidy`
141+
if !path.exists() {
142+
// Use the original path here
143+
return original_path.clone();
144+
}
145+
146+
// Make the path absolute, strip the prefix, and convert to a PathBuf.
147+
match std::path::absolute(&path) {
148+
Ok(p) => p.strip_prefix(&builder.src).unwrap_or(&p).to_path_buf(),
149+
Err(e) => {
150+
eprintln!("ERROR: {e:?}");
151+
panic!("Due to the above error, failed to resolve path: {path:?}");
152+
}
153+
}
154+
})
155+
.collect();
156+
157+
remap_paths(&mut paths);
158+
159+
// Handle all test suite paths.
160+
// (This is separate from the loop below to avoid having to handle multiple paths in `is_suite_path` somehow.)
161+
paths.retain(|path| {
162+
for (desc, should_run) in v.iter().zip(&should_runs) {
163+
if let Some(suite) = should_run.is_suite_path(path) {
164+
desc.maybe_run(builder, vec![suite.clone()]);
165+
return false;
166+
}
167+
}
168+
true
169+
});
170+
171+
if paths.is_empty() {
172+
return;
173+
}
174+
175+
let mut paths: Vec<CLIStepPath> = paths.into_iter().map(|p| p.into()).collect();
176+
let mut path_lookup: Vec<(CLIStepPath, bool)> =
177+
paths.clone().into_iter().map(|p| (p, false)).collect();
178+
179+
// List of `(usize, &StepDescription, Vec<PathSet>)` where `usize` is the closest index of a path
180+
// compared to the given CLI paths. So we can respect to the CLI order by using this value to sort
181+
// the steps.
182+
let mut steps_to_run = vec![];
183+
184+
for (desc, should_run) in v.iter().zip(&should_runs) {
185+
let pathsets = should_run.pathset_for_paths_removing_matches(&mut paths, desc.kind);
186+
187+
// This value is used for sorting the step execution order.
188+
// By default, `usize::MAX` is used as the index for steps to assign them the lowest priority.
189+
//
190+
// If we resolve the step's path from the given CLI input, this value will be updated with
191+
// the step's actual index.
192+
let mut closest_index = usize::MAX;
193+
194+
// Find the closest index from the original list of paths given by the CLI input.
195+
for (index, (path, is_used)) in path_lookup.iter_mut().enumerate() {
196+
if !*is_used && !paths.contains(path) {
197+
closest_index = index;
198+
*is_used = true;
199+
break;
200+
}
201+
}
202+
203+
steps_to_run.push((closest_index, desc, pathsets));
204+
}
205+
206+
// Sort the steps before running them to respect the CLI order.
207+
steps_to_run.sort_by_key(|(index, _, _)| *index);
208+
209+
// Handle all PathSets.
210+
for (_index, desc, pathsets) in steps_to_run {
211+
if !pathsets.is_empty() {
212+
desc.maybe_run(builder, pathsets);
213+
}
214+
}
215+
216+
paths.retain(|p| !p.will_be_executed);
217+
218+
if !paths.is_empty() {
219+
eprintln!("ERROR: no `{}` rules matched {:?}", builder.kind.as_str(), paths);
220+
eprintln!(
221+
"HELP: run `x.py {} --help --verbose` to show a list of available paths",
222+
builder.kind.as_str()
223+
);
224+
eprintln!(
225+
"NOTE: if you are adding a new Step to bootstrap itself, make sure you register it with `describe!`"
226+
);
227+
crate::exit!(1);
228+
}
229+
}

0 commit comments

Comments
 (0)