From 48d1b9b0221d12f87abb1bac37d7e160f5a11bd6 Mon Sep 17 00:00:00 2001 From: Nilstrieb <48135649+Nilstrieb@users.noreply.github.com> Date: Tue, 2 Apr 2024 21:04:43 +0200 Subject: [PATCH] Link rustc/std tools into the correct sysroot When copying tool binaries, we were linking them into the sysroot of the compiler that built the binaries. This makes no sense, the binaries are for the next sysroot. So when the stage0 compiler builds clippy, this clippy belongs into stage1, and when the stage1 compiler builds clippy, this clippy belongs into stage2. This puts it right next to the librustc_driver it actually links against. Additionally, we `ensure(Assemble)` of this librustc_driver such that the tool will be working as expected. To run the tool manually, we still need to set LD_LIBRARY_PATH, but now with this, setting the rpath to `$ORIGIN/../lib` (like the `rustc` and `rustdoc` binaries) should be possible as future work now. Rustdoc, with its special treatment, was already getting the correct behavior. --- src/bootstrap/src/core/build_steps/tool.rs | 31 +++++++++++++--------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/src/bootstrap/src/core/build_steps/tool.rs b/src/bootstrap/src/core/build_steps/tool.rs index 05b19c0a6e332..0c769c984dbe2 100644 --- a/src/bootstrap/src/core/build_steps/tool.rs +++ b/src/bootstrap/src/core/build_steps/tool.rs @@ -81,7 +81,11 @@ impl Step for ToolBuild { match self.mode { Mode::ToolRustc => { builder.ensure(compile::Std::new(compiler, compiler.host)); - builder.ensure(compile::Rustc::new(compiler, target)); + // When building a tool that links against rustc, + // we need the rustc to link against to be present and ready in the syroot. + builder.ensure(compile::Assemble { + target_compiler: compiler.with_stage(compiler.stage + 1), + }); } Mode::ToolStd => builder.ensure(compile::Std::new(compiler, target)), Mode::ToolBootstrap => {} // uses downloaded stage0 compiler libs @@ -863,8 +867,8 @@ macro_rules! tool_extended { $($name:ident, $path:expr, $tool_name:expr, + mode = $mode:expr, stable = $stable:expr - $(,tool_std = $tool_std:literal)? $(,allow_features = $allow_features:expr)? $(,add_bins_to_sysroot = $add_bins_to_sysroot:expr)? ;)+) => { @@ -913,15 +917,16 @@ macro_rules! tool_extended { compiler: $sel.compiler, target: $sel.target, tool: $tool_name, - mode: if false $(|| $tool_std)? { Mode::ToolStd } else { Mode::ToolRustc }, + mode: $mode, path: $path, extra_features: $sel.extra_features, source_type: SourceType::InTree, allow_features: concat!($($allow_features)*), }); - if (false $(|| !$add_bins_to_sysroot.is_empty())?) && $sel.compiler.stage > 0 { - let bindir = $builder.sysroot($sel.compiler).join("bin"); + if (false $(|| !$add_bins_to_sysroot.is_empty())?) { + // As usual, we copy the tool into the next sysroot, as it links against the compiler in that sysroot. + let bindir = $builder.sysroot($sel.compiler.with_stage($sel.compiler.stage + 1)).join("bin"); t!(fs::create_dir_all(&bindir)); #[allow(unused_variables)] @@ -950,17 +955,17 @@ macro_rules! tool_extended { // NOTE: Most submodule updates for tools are handled by bootstrap.py, since they're needed just to // invoke Cargo to build bootstrap. See the comment there for more details. tool_extended!((self, builder), - Cargofmt, "src/tools/rustfmt", "cargo-fmt", stable=true; - CargoClippy, "src/tools/clippy", "cargo-clippy", stable=true; - Clippy, "src/tools/clippy", "clippy-driver", stable=true, add_bins_to_sysroot = ["clippy-driver", "cargo-clippy"]; - Miri, "src/tools/miri", "miri", stable=false, add_bins_to_sysroot = ["miri"]; - CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", stable=true, add_bins_to_sysroot = ["cargo-miri"]; + Cargofmt, "src/tools/rustfmt", "cargo-fmt", mode=Mode::ToolRustc, stable=true; + CargoClippy, "src/tools/clippy", "cargo-clippy", mode= Mode::ToolRustc, stable=true; + Clippy, "src/tools/clippy", "clippy-driver", mode=Mode::ToolRustc, stable=true, add_bins_to_sysroot = ["clippy-driver", "cargo-clippy"]; + Miri, "src/tools/miri", "miri", mode= Mode::ToolRustc, stable=false, add_bins_to_sysroot = ["miri"]; + CargoMiri, "src/tools/miri/cargo-miri", "cargo-miri", mode=Mode::ToolRustc, stable=true, add_bins_to_sysroot = ["cargo-miri"]; // FIXME: tool_std is not quite right, we shouldn't allow nightly features. // But `builder.cargo` doesn't know how to handle ToolBootstrap in stages other than 0, // and this is close enough for now. - Rls, "src/tools/rls", "rls", stable=true, tool_std=true; - RustDemangler, "src/tools/rust-demangler", "rust-demangler", stable=false, tool_std=true; - Rustfmt, "src/tools/rustfmt", "rustfmt", stable=true, add_bins_to_sysroot = ["rustfmt", "cargo-fmt"]; + Rls, "src/tools/rls", "rls", mode=Mode::ToolStd, stable=true; + RustDemangler, "src/tools/rust-demangler", "rust-demangler", mode=Mode::ToolStd, stable=false; + Rustfmt, "src/tools/rustfmt", "rustfmt", mode=Mode::ToolRustc, stable=true, add_bins_to_sysroot = ["rustfmt", "cargo-fmt"]; ); impl<'a> Builder<'a> {