Skip to content

Simplify LLVM bitcode linker in bootstrap and add tests for it #142357

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 5 commits into from
Jul 9, 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
12 changes: 9 additions & 3 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2059,14 +2059,20 @@ impl Step for Assemble {
trace!("llvm-bitcode-linker enabled, installing");
let llvm_bitcode_linker =
builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
compiler,
build_compiler: compiler,
target: target_compiler.host,
extra_features: vec![],
});

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

t!(fs::create_dir_all(&bindir_self_contained));
builder.copy_link(
&llvm_bitcode_linker.tool_path,
&libdir_bin.join(tool_exe),
&bindir_self_contained.join(tool_exe),
FileType::Executable,
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2375,7 +2375,7 @@ impl Step for LlvmBitcodeLinker {
builder.ensure(compile::Rustc::new(compiler, target));

let llbc_linker =
builder.ensure(tool::LlvmBitcodeLinker { compiler, target, extra_features: vec![] });
builder.ensure(tool::LlvmBitcodeLinker { build_compiler: compiler, target });

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

Expand Down
42 changes: 18 additions & 24 deletions src/bootstrap/src/core/build_steps/tool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -910,6 +910,13 @@ impl Step for LldWrapper {

tool_result
}

fn metadata(&self) -> Option<StepMetadata> {
Some(
StepMetadata::build("LldWrapper", self.target_compiler.host)
.built_by(self.build_compiler),
)
}
}

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
Expand Down Expand Up @@ -1014,9 +1021,8 @@ impl Step for RustAnalyzerProcMacroSrv {

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct LlvmBitcodeLinker {
pub compiler: Compiler,
pub build_compiler: Compiler,
pub target: TargetSelection,
pub extra_features: Vec<String>,
}

impl Step for LlvmBitcodeLinker {
Expand All @@ -1032,8 +1038,9 @@ impl Step for LlvmBitcodeLinker {

fn make_run(run: RunConfig<'_>) {
run.builder.ensure(LlvmBitcodeLinker {
compiler: run.builder.compiler(run.builder.top_stage, run.builder.config.host_target),
extra_features: Vec::new(),
build_compiler: run
.builder
.compiler(run.builder.top_stage, run.builder.config.host_target),
target: run.target,
});
}
Expand All @@ -1043,35 +1050,22 @@ impl Step for LlvmBitcodeLinker {
instrument(level = "debug", name = "LlvmBitcodeLinker::run", skip_all)
)]
fn run(self, builder: &Builder<'_>) -> ToolBuildResult {
let tool_result = builder.ensure(ToolBuild {
compiler: self.compiler,
builder.ensure(ToolBuild {
compiler: self.build_compiler,
target: self.target,
tool: "llvm-bitcode-linker",
mode: Mode::ToolRustc,
path: "src/tools/llvm-bitcode-linker",
source_type: SourceType::InTree,
extra_features: self.extra_features,
extra_features: vec![],
allow_features: "",
cargo_args: Vec::new(),
artifact_kind: ToolArtifactKind::Binary,
});
})
}

if tool_result.target_compiler.stage > 0 {
let bindir_self_contained = builder
.sysroot(tool_result.target_compiler)
.join(format!("lib/rustlib/{}/bin/self-contained", self.target.triple));
t!(fs::create_dir_all(&bindir_self_contained));
let bin_destination = bindir_self_contained
.join(exe("llvm-bitcode-linker", tool_result.target_compiler.host));
builder.copy_link(&tool_result.tool_path, &bin_destination, FileType::Executable);
ToolBuildResult {
tool_path: bin_destination,
build_compiler: tool_result.build_compiler,
target_compiler: tool_result.target_compiler,
}
} else {
tool_result
}
fn metadata(&self) -> Option<StepMetadata> {
Some(StepMetadata::build("LlvmBitcodeLinker", self.target).built_by(self.build_compiler))
}
}

Expand Down
54 changes: 54 additions & 0 deletions src/bootstrap/src/core/builder/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,58 @@ mod snapshot {
");
}

#[test]
fn build_compiler_tools() {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx
.config("build")
.stage(2)
.args(&["--set", "rust.lld=true", "--set", "rust.llvm-bitcode-linker=true"])
.render_steps(), @r"
[build] llvm <host>
[build] rustc 0 <host> -> rustc 1 <host>
[build] rustc 0 <host> -> LldWrapper 1 <host>
[build] rustc 1 <host> -> LlvmBitcodeLinker 2 <host>
[build] rustc 1 <host> -> std 1 <host>
[build] rustc 1 <host> -> rustc 2 <host>
[build] rustc 1 <host> -> LldWrapper 2 <host>
[build] rustc 2 <host> -> LlvmBitcodeLinker 3 <host>
[build] rustc 2 <host> -> std 2 <host>
[build] rustdoc 1 <host>
"
);
}

#[test]
fn build_compiler_tools_cross() {
let ctx = TestCtx::new();
insta::assert_snapshot!(
ctx
.config("build")
.stage(2)
.args(&["--set", "rust.lld=true", "--set", "rust.llvm-bitcode-linker=true"])
.hosts(&[TEST_TRIPLE_1])
.render_steps(), @r"
[build] llvm <host>
[build] rustc 0 <host> -> rustc 1 <host>
[build] rustc 0 <host> -> LldWrapper 1 <host>
[build] rustc 1 <host> -> LlvmBitcodeLinker 2 <host>
[build] rustc 1 <host> -> std 1 <host>
[build] rustc 1 <host> -> rustc 2 <host>
[build] rustc 1 <host> -> LldWrapper 2 <host>
[build] rustc 2 <host> -> LlvmBitcodeLinker 3 <host>
[build] rustc 1 <host> -> std 1 <target1>
[build] rustc 2 <host> -> std 2 <target1>
[build] llvm <target1>
[build] rustc 1 <host> -> rustc 2 <target1>
[build] rustc 1 <host> -> LldWrapper 2 <target1>
[build] rustc 2 <target1> -> LlvmBitcodeLinker 3 <target1>
[build] rustdoc 1 <target1>
"
);
}

#[test]
fn build_library_no_explicit_stage() {
let ctx = TestCtx::new();
Expand Down Expand Up @@ -1040,6 +1092,7 @@ mod snapshot {
[build] rustc 0 <host> -> cargo-clippy 1 <host>
[build] rustc 0 <host> -> miri 1 <host>
[build] rustc 0 <host> -> cargo-miri 1 <host>
[build] rustc 1 <host> -> LlvmBitcodeLinker 2 <host>
");
}

Expand Down Expand Up @@ -1230,6 +1283,7 @@ mod snapshot {
[build] rustc 0 <host> -> cargo-clippy 1 <target1>
[build] rustc 0 <host> -> miri 1 <target1>
[build] rustc 0 <host> -> cargo-miri 1 <target1>
[build] rustc 1 <host> -> LlvmBitcodeLinker 2 <target1>
");
}

Expand Down
2 changes: 0 additions & 2 deletions src/bootstrap/src/utils/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ impl ConfigBuilder {
// in-tree LLVM from sources.
self.args.push("--set".to_string());
self.args.push("llvm.download-ci-llvm=false".to_string());
self.args.push("--set".to_string());
self.args.push(format!("target.'{}'.llvm-config=false", get_host_target()));

// Do not mess with the local rustc checkout build directory
self.args.push("--build-dir".to_string());
Expand Down
Loading