Skip to content

Commit

Permalink
Rollup merge of rust-lang#132782 - onur-ozkan:cleanup, r=jieyouxu
Browse files Browse the repository at this point in the history
improvements on initial sysroot and libdir finding logics

Stabilized initial sysroot and libdir path resolution logic to work without dry-run conditions and utilized initial sysroot more broadly.
  • Loading branch information
matthiaskrgr authored Nov 29, 2024
2 parents cb2bd2b + 27323aa commit 45fd6b4
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 54 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,7 +1064,7 @@ impl Step for Tidy {
if builder.config.channel == "dev" || builder.config.channel == "nightly" {
builder.info("fmt check");
if builder.initial_rustfmt().is_none() {
let inferred_rustfmt_dir = builder.initial_rustc.parent().unwrap();
let inferred_rustfmt_dir = builder.initial_sysroot.join("bin");
eprintln!(
"\
ERROR: no `rustfmt` binary found in {PATH}
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ impl<'a> Builder<'a> {
pub fn sysroot_libdir_relative(&self, compiler: Compiler) -> &Path {
match self.config.libdir_relative() {
Some(relative_libdir) if compiler.stage >= 1 => relative_libdir,
_ if compiler.stage == 0 => &self.build.initial_libdir,
_ if compiler.stage == 0 => &self.build.initial_relative_libdir,
_ => Path::new("lib"),
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/bootstrap/src/core/config/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ pub struct Config {
pub initial_cargo: PathBuf,
pub initial_rustc: PathBuf,
pub initial_cargo_clippy: Option<PathBuf>,
pub initial_sysroot: PathBuf,

#[cfg(not(test))]
initial_rustfmt: RefCell<RustfmtState>,
Expand Down Expand Up @@ -1563,8 +1564,6 @@ impl Config {
);
}

config.initial_cargo_clippy = cargo_clippy;

config.initial_rustc = if let Some(rustc) = rustc {
if !flags.skip_stage0_validation {
config.check_stage0_version(&rustc, "rustc");
Expand All @@ -1580,19 +1579,18 @@ impl Config {
.join(exe("rustc", config.build))
};

config.initial_sysroot = config.initial_rustc.ancestors().nth(2).unwrap().into();

config.initial_cargo_clippy = cargo_clippy;

config.initial_cargo = if let Some(cargo) = cargo {
if !flags.skip_stage0_validation {
config.check_stage0_version(&cargo, "cargo");
}
cargo
} else {
config.download_beta_toolchain();
config
.out
.join(config.build)
.join("stage0")
.join("bin")
.join(exe("cargo", config.build))
config.initial_sysroot.join("bin").join(exe("cargo", config.build))
};

// NOTE: it's important this comes *after* we set `initial_rustc` just above.
Expand Down
5 changes: 2 additions & 3 deletions src/bootstrap/src/core/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,8 @@ impl Config {
let version = &self.stage0_metadata.compiler.version;
let host = self.build;

let bin_root = self.out.join(host).join("stage0");
let clippy_stamp = bin_root.join(".clippy-stamp");
let cargo_clippy = bin_root.join("bin").join(exe("cargo-clippy", host));
let clippy_stamp = self.initial_sysroot.join(".clippy-stamp");
let cargo_clippy = self.initial_sysroot.join("bin").join(exe("cargo-clippy", host));
if cargo_clippy.exists() && !program_out_of_date(&clippy_stamp, date) {
return cargo_clippy;
}
Expand Down
55 changes: 14 additions & 41 deletions src/bootstrap/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ pub struct Build {
initial_rustc: PathBuf,
initial_cargo: PathBuf,
initial_lld: PathBuf,
initial_libdir: PathBuf,
initial_relative_libdir: PathBuf,
initial_sysroot: PathBuf,

// Runtime state filled in later on
Expand Down Expand Up @@ -312,46 +312,19 @@ impl Build {
let in_tree_llvm_info = config.in_tree_llvm_info.clone();
let in_tree_gcc_info = config.in_tree_gcc_info.clone();

let initial_target_libdir_str = if config.dry_run() {
"/dummy/lib/path/to/lib/".to_string()
} else {
output(
Command::new(&config.initial_rustc)
.arg("--target")
.arg(config.build.rustc_target_arg())
.arg("--print")
.arg("target-libdir"),
)
};
let initial_target_libdir_str =
config.initial_sysroot.join("lib/rustlib").join(config.build).join("lib");

let initial_target_dir = Path::new(&initial_target_libdir_str).parent().unwrap();
let initial_lld = initial_target_dir.join("bin").join("rust-lld");

let initial_sysroot = if config.dry_run() {
"/dummy".to_string()
} else {
output(Command::new(&config.initial_rustc).arg("--print").arg("sysroot"))
}
.trim()
.to_string();

// FIXME(Zalathar): Determining this path occasionally fails locally for
// unknown reasons, so we print some extra context to help track down why.
let find_initial_libdir = || {
let initial_libdir =
initial_target_dir.parent()?.parent()?.strip_prefix(&initial_sysroot).ok()?;
Some(initial_libdir.to_path_buf())
};
let Some(initial_libdir) = find_initial_libdir() else {
panic!(
"couldn't determine `initial_libdir`:
- config.initial_rustc: {rustc:?}
- initial_target_libdir_str: {initial_target_libdir_str:?}
- initial_target_dir: {initial_target_dir:?}
- initial_sysroot: {initial_sysroot:?}
",
rustc = config.initial_rustc,
);
};
let initial_relative_libdir = initial_target_dir
.ancestors()
.nth(2)
.unwrap()
.strip_prefix(&config.initial_sysroot)
.expect("Couldn’t determine initial relative libdir.")
.to_path_buf();

let version = std::fs::read_to_string(src.join("src").join("version"))
.expect("failed to read src/version");
Expand Down Expand Up @@ -380,11 +353,11 @@ impl Build {
}

let mut build = Build {
initial_lld,
initial_relative_libdir,
initial_rustc: config.initial_rustc.clone(),
initial_cargo: config.initial_cargo.clone(),
initial_lld,
initial_libdir,
initial_sysroot: initial_sysroot.into(),
initial_sysroot: config.initial_sysroot.clone(),
local_rebuild: config.local_rebuild,
fail_fast: config.cmd.fail_fast(),
doc_tests: config.cmd.doc_tests(),
Expand Down

0 comments on commit 45fd6b4

Please sign in to comment.