Skip to content

Commit 6d949a7

Browse files
committed
Build llvm-bitcode-linker as a HostRustc tool
This tool can be built with any compiler that can produce code for the host target of the compiler for which the linker should be installed. This change saves one rebuild of the tool in stage 2 build.
1 parent 6178c6d commit 6d949a7

File tree

5 files changed

+30
-26
lines changed

5 files changed

+30
-26
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,20 +2024,20 @@ impl Step for Assemble {
20242024
}
20252025
}
20262026

2027-
let maybe_install_llvm_bitcode_linker = |compiler| {
2027+
// Build llvm-bitcode-linker if it is enabled and install it into the sysroot of `compiler`
2028+
let maybe_install_llvm_bitcode_linker = |compiler: Compiler| {
20282029
if builder.config.llvm_bitcode_linker_enabled {
20292030
trace!("llvm-bitcode-linker enabled, installing");
20302031
let llvm_bitcode_linker =
20312032
builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
2032-
compiler,
2033-
target: target_compiler.host,
2033+
target: compiler.host,
20342034
});
20352035

20362036
// Copy the llvm-bitcode-linker to the self-contained binary directory
20372037
let bindir_self_contained = builder
20382038
.sysroot(compiler)
20392039
.join(format!("lib/rustlib/{}/bin/self-contained", compiler.host));
2040-
let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
2040+
let tool_exe = exe("llvm-bitcode-linker", compiler.host);
20412041

20422042
t!(fs::create_dir_all(&bindir_self_contained));
20432043
builder.copy_link(

src/bootstrap/src/core/build_steps/dist.rs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1555,7 +1555,7 @@ impl Step for Extended {
15551555
compiler: builder.compiler(stage, target),
15561556
backend: "cranelift".to_string(),
15571557
});
1558-
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {compiler, target});
1558+
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {target});
15591559

15601560
let etc = builder.src.join("src/etc/installer");
15611561

@@ -2323,7 +2323,6 @@ impl Step for LlvmTools {
23232323

23242324
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
23252325
pub struct LlvmBitcodeLinker {
2326-
pub compiler: Compiler,
23272326
pub target: TargetSelection,
23282327
}
23292328

@@ -2338,23 +2337,12 @@ impl Step for LlvmBitcodeLinker {
23382337
}
23392338

23402339
fn make_run(run: RunConfig<'_>) {
2341-
run.builder.ensure(LlvmBitcodeLinker {
2342-
compiler: run.builder.compiler_for(
2343-
run.builder.top_stage,
2344-
run.builder.config.build,
2345-
run.target,
2346-
),
2347-
target: run.target,
2348-
});
2340+
run.builder.ensure(LlvmBitcodeLinker { target: run.target });
23492341
}
23502342

23512343
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
2352-
let compiler = self.compiler;
23532344
let target = self.target;
2354-
2355-
builder.ensure(compile::Rustc::new(compiler, target));
2356-
2357-
let llbc_linker = builder.ensure(tool::LlvmBitcodeLinker { compiler, target });
2345+
let llbc_linker = builder.ensure(tool::LlvmBitcodeLinker { target });
23582346

23592347
let self_contained_bin_dir = format!("lib/rustlib/{}/bin/self-contained", target.triple);
23602348

src/bootstrap/src/core/build_steps/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ install!((self, builder, _config),
287287
}
288288
};
289289
LlvmBitcodeLinker, alias = "llvm-bitcode-linker", Self::should_build(_config), only_hosts: true, {
290-
if let Some(tarball) = builder.ensure(dist::LlvmBitcodeLinker { compiler: self.compiler, target: self.target }) {
290+
if let Some(tarball) = builder.ensure(dist::LlvmBitcodeLinker { target: self.target }) {
291291
install_sh(builder, "llvm-bitcode-linker", self.compiler.stage, Some(self.target), &tarball);
292292
} else {
293293
builder.info(

src/bootstrap/src/core/build_steps/tool.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -983,9 +983,11 @@ impl Step for RustAnalyzerProcMacroSrv {
983983
}
984984
}
985985

986+
/// Compile the `llvm-bitcode-linker` tool for `target`.
987+
/// It is a compiler host tool used to link specific targets using LLVM.
988+
/// It is used by `rustc` at runtime.
986989
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
987990
pub struct LlvmBitcodeLinker {
988-
pub compiler: Compiler,
989991
pub target: TargetSelection,
990992
}
991993

@@ -1001,19 +1003,18 @@ impl Step for LlvmBitcodeLinker {
10011003
}
10021004

10031005
fn make_run(run: RunConfig<'_>) {
1004-
run.builder.ensure(LlvmBitcodeLinker {
1005-
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.build),
1006-
target: run.target,
1007-
});
1006+
run.builder.ensure(LlvmBitcodeLinker { target: run.target });
10081007
}
10091008

10101009
#[cfg_attr(
10111010
feature = "tracing",
10121011
instrument(level = "debug", name = "LlvmBitcodeLinker::run", skip_all)
10131012
)]
10141013
fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
1014+
let compiler = builder.compiler_for_target(self.target);
1015+
10151016
builder.ensure(ToolBuild {
1016-
compiler: self.compiler,
1017+
compiler,
10171018
target: self.target,
10181019
tool: "llvm-bitcode-linker",
10191020
mode: Mode::ToolRustc,

src/bootstrap/src/core/builder/mod.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use tracing::instrument;
1515

1616
pub use self::cargo::{Cargo, cargo_profile_var};
1717
pub use crate::Compiler;
18+
use crate::core::build_steps::compile::Std;
1819
use crate::core::build_steps::{
1920
check, clean, clippy, compile, dist, doc, gcc, install, llvm, run, setup, test, tool, vendor,
2021
};
@@ -1314,6 +1315,20 @@ impl<'a> Builder<'a> {
13141315
resolved_compiler
13151316
}
13161317

1318+
/// Return the lowest stage compiler that can compile code for the given `target`.
1319+
pub fn compiler_for_target(&self, target: TargetSelection) -> Compiler {
1320+
// If we're not cross-compiling, we can always use the stage0 compiler
1321+
if self.config.build == target {
1322+
self.compiler(0, target)
1323+
} else {
1324+
// Otherwise, we have to build a stage 1 compiler that can compile code for `target`.
1325+
let compiler = self.compiler(1, self.config.build);
1326+
// FIXME(kobzol): get rid of this nonsense and create something like `RustcWithStdForTarget`
1327+
self.ensure(Std::new(compiler, target));
1328+
compiler
1329+
}
1330+
}
1331+
13171332
pub fn sysroot(&self, compiler: Compiler) -> PathBuf {
13181333
self.ensure(compile::Sysroot::new(compiler))
13191334
}

0 commit comments

Comments
 (0)