Skip to content

refactor bootstrap path resolution #135058

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ impl Step for Std {
const DEFAULT: bool = true;

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
run.crate_or_deps("sysroot").path("library")
run.crate_or_deps("sysroot").path("library").alias("core")
}

fn make_run(run: RunConfig<'_>) {
Expand Down
5 changes: 4 additions & 1 deletion src/bootstrap/src/core/build_steps/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,7 +574,10 @@ impl Step for Std {

fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
let builder = run.builder;
run.crate_or_deps("sysroot").path("library").default_condition(builder.config.docs)
run.crate_or_deps("sysroot")
.path("library")
.alias("core")
.default_condition(builder.config.docs)
}

fn make_run(run: RunConfig<'_>) {
Expand Down
64 changes: 43 additions & 21 deletions src/bootstrap/src/core/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod cargo;
use std::any::{Any, type_name};
use std::cell::{Cell, RefCell};
use std::collections::BTreeSet;
use std::fmt::{Debug, Write};
use std::fmt::{self, Debug, Write};
use std::hash::Hash;
use std::ops::Deref;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -271,16 +271,17 @@ impl PathSet {
/// This is used for `StepDescription::krate`, which passes all matching crates at once to
/// `Step::make_run`, rather than calling it many times with a single crate.
/// See `tests.rs` for examples.
fn intersection_removing_matches(&self, needles: &mut Vec<PathBuf>, module: Kind) -> PathSet {
fn intersection_removing_matches(&self, needles: &mut [CLIStepPath], module: Kind) -> PathSet {
let mut check = |p| {
for (i, n) in needles.iter().enumerate() {
let matched = Self::check(p, n, module);
let mut result = false;
for n in needles.iter_mut() {
let matched = Self::check(p, &n.path, module);
if matched {
needles.remove(i);
return true;
n.will_be_executed = true;
result = true;
}
}
false
result
};
match self {
PathSet::Set(set) => PathSet::Set(set.iter().filter(|&p| check(p)).cloned().collect()),
Expand Down Expand Up @@ -361,6 +362,32 @@ fn remap_paths(paths: &mut Vec<PathBuf>) {
paths.append(&mut add);
}

#[derive(Clone, PartialEq)]
struct CLIStepPath {
path: PathBuf,
will_be_executed: bool,
}

#[cfg(test)]
impl CLIStepPath {
fn will_be_executed(mut self, will_be_executed: bool) -> Self {
self.will_be_executed = will_be_executed;
self
}
}

impl Debug for CLIStepPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.path.display())
}
}

impl From<PathBuf> for CLIStepPath {
fn from(path: PathBuf) -> Self {
Self { path, will_be_executed: false }
}
}

impl StepDescription {
fn from<S: Step>(kind: Kind) -> StepDescription {
StepDescription {
Expand Down Expand Up @@ -478,7 +505,8 @@ impl StepDescription {
return;
}

let mut path_lookup: Vec<(PathBuf, bool)> =
let mut paths: Vec<CLIStepPath> = paths.into_iter().map(|p| p.into()).collect();
let mut path_lookup: Vec<(CLIStepPath, bool)> =
paths.clone().into_iter().map(|p| (p, false)).collect();

// List of `(usize, &StepDescription, Vec<PathSet>)` where `usize` is the closest index of a path
Expand Down Expand Up @@ -518,8 +546,10 @@ impl StepDescription {
}
}

paths.retain(|p| !p.will_be_executed);

if !paths.is_empty() {
eprintln!("ERROR: no `{}` rules matched {:?}", builder.kind.as_str(), paths,);
eprintln!("ERROR: no `{}` rules matched {:?}", builder.kind.as_str(), paths);
eprintln!(
"HELP: run `x.py {} --help --verbose` to show a list of available paths",
builder.kind.as_str()
Expand Down Expand Up @@ -682,7 +712,7 @@ impl<'a> ShouldRun<'a> {
/// (for now, just `all_krates` and `paths`, but we may want to add an `aliases` function in the future?)
fn pathset_for_paths_removing_matches(
&self,
paths: &mut Vec<PathBuf>,
paths: &mut [CLIStepPath],
kind: Kind,
) -> Vec<PathSet> {
let mut sets = vec![];
Expand Down Expand Up @@ -825,12 +855,8 @@ impl<'a> Builder<'a> {
match kind {
Kind::Build => describe!(
compile::Std,
// FIXME(#135022): `compile::Assemble` **must** come before `compile::Rustc` after
// `PathSet` also permits prefix-matching, because `compile::Rustc` can consume the
// `"compiler"` path filter first, causing `compile::Assemble` to no longer run when
// the user writes `./x build compiler --stage 0`.
compile::Assemble,
compile::Rustc,
compile::Assemble,
compile::CodegenBackend,
compile::StartupObjects,
tool::BuildManifest,
Expand Down Expand Up @@ -929,14 +955,10 @@ impl<'a> Builder<'a> {
test::Rustdoc,
test::CoverageRunRustdoc,
test::Pretty,
test::Crate,
test::CrateLibrustc,
// The cranelift and gcc tests need to be listed after the
// compiler unit tests (CrateLibrustc) so that they don't
// hijack the whole `compiler` directory during path matching.
// <https://github.com/rust-lang/rust/pull/134919>
test::CodegenCranelift,
test::CodegenGCC,
test::Crate,
test::CrateLibrustc,
test::CrateRustdoc,
test::CrateRustdocJsonTypes,
test::CrateBootstrap,
Expand Down
32 changes: 28 additions & 4 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,37 @@ fn test_intersection() {
};
let library_set = set(&["library/core", "library/alloc", "library/std"]);
let mut command_paths = vec![
PathBuf::from("library/core"),
PathBuf::from("library/alloc"),
PathBuf::from("library/stdarch"),
CLIStepPath::from(PathBuf::from("library/core")),
CLIStepPath::from(PathBuf::from("library/alloc")),
CLIStepPath::from(PathBuf::from("library/stdarch")),
];
let subset = library_set.intersection_removing_matches(&mut command_paths, Kind::Build);
assert_eq!(subset, set(&["library/core", "library/alloc"]),);
assert_eq!(command_paths, vec![PathBuf::from("library/stdarch")]);
assert_eq!(command_paths, vec![
CLIStepPath::from(PathBuf::from("library/core")).will_be_executed(true),
CLIStepPath::from(PathBuf::from("library/alloc")).will_be_executed(true),
CLIStepPath::from(PathBuf::from("library/stdarch")).will_be_executed(false),
]);
}

#[test]
fn test_resolve_parent_and_subpaths() {
let set = |paths: &[&str]| {
PathSet::Set(paths.into_iter().map(|p| TaskPath { path: p.into(), kind: None }).collect())
};

let mut command_paths = vec![
CLIStepPath::from(PathBuf::from("src/tools/miri")),
CLIStepPath::from(PathBuf::from("src/tools/miri/cargo-miri")),
];

let library_set = set(&["src/tools/miri", "src/tools/miri/cargo-miri"]);
library_set.intersection_removing_matches(&mut command_paths, Kind::Build);

assert_eq!(command_paths, vec![
CLIStepPath::from(PathBuf::from("src/tools/miri")).will_be_executed(true),
CLIStepPath::from(PathBuf::from("src/tools/miri/cargo-miri")).will_be_executed(true),
]);
}

#[test]
Expand Down
Loading