Skip to content

Commit 235d45e

Browse files
author
Kjetil Kjeka
committed
Distribute LLVM bitcode linker as a preview component
1 parent d493fd1 commit 235d45e

File tree

7 files changed

+84
-7
lines changed

7 files changed

+84
-7
lines changed

Diff for: src/bootstrap/src/core/build_steps/dist.rs

+48
Original file line numberDiff line numberDiff line change
@@ -1548,6 +1548,7 @@ impl Step for Extended {
15481548
compiler: builder.compiler(stage, target),
15491549
backend: "cranelift".to_string(),
15501550
});
1551+
add_component!("llvm-bitcode-linker" => LlvmBitcodeLinker {compiler, target});
15511552

15521553
let etc = builder.src.join("src/etc/installer");
15531554

@@ -2224,6 +2225,53 @@ impl Step for LlvmTools {
22242225
}
22252226
}
22262227

2228+
#[derive(Debug, PartialOrd, Ord, Clone, Hash, PartialEq, Eq)]
2229+
pub struct LlvmBitcodeLinker {
2230+
pub compiler: Compiler,
2231+
pub target: TargetSelection,
2232+
}
2233+
2234+
impl Step for LlvmBitcodeLinker {
2235+
type Output = Option<GeneratedTarball>;
2236+
const DEFAULT: bool = true;
2237+
const ONLY_HOSTS: bool = true;
2238+
2239+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2240+
let default = should_build_extended_tool(run.builder, "llvm-bitcode-linker");
2241+
run.alias("llvm-bitcode-linker").default_condition(default)
2242+
}
2243+
2244+
fn make_run(run: RunConfig<'_>) {
2245+
run.builder.ensure(LlvmBitcodeLinker {
2246+
compiler: run.builder.compiler_for(
2247+
run.builder.top_stage,
2248+
run.builder.config.build,
2249+
run.target,
2250+
),
2251+
target: run.target,
2252+
});
2253+
}
2254+
2255+
fn run(self, builder: &Builder<'_>) -> Option<GeneratedTarball> {
2256+
let compiler = self.compiler;
2257+
let target = self.target;
2258+
2259+
let llbc_linker =
2260+
builder.ensure(tool::LlvmBitcodeLinker { compiler, target, extra_features: vec![] });
2261+
2262+
let self_contained_bin_dir = format!("lib/rustlib/{}/bin/self-contained", target.triple);
2263+
2264+
// Prepare the image directory
2265+
let mut tarball = Tarball::new(builder, "llvm-bitcode-linker", &target.triple);
2266+
tarball.set_overlay(OverlayKind::LlvmBitcodeLinker);
2267+
tarball.is_preview(true);
2268+
2269+
tarball.add_file(llbc_linker, self_contained_bin_dir, 0o755);
2270+
2271+
Some(tarball.generate())
2272+
}
2273+
}
2274+
22272275
// Tarball intended for internal consumption to ease rustc/std development.
22282276
//
22292277
// Should not be considered stable by end users.

Diff for: src/bootstrap/src/core/build_steps/install.rs

+9
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,15 @@ install!((self, builder, _config),
300300
);
301301
}
302302
};
303+
LlvmBitcodeLinker, alias = "llvm-bitcode-linker", Self::should_build(_config), only_hosts: true, {
304+
if let Some(tarball) = builder.ensure(dist::LlvmBitcodeLinker { compiler: self.compiler, target: self.target }) {
305+
install_sh(builder, "llvm-bitcode-linker", self.compiler.stage, Some(self.target), &tarball);
306+
} else {
307+
builder.info(
308+
&format!("skipping llvm-bitcode-linker stage{} ({})", self.compiler.stage, self.target),
309+
);
310+
}
311+
};
303312
);
304313

305314
#[derive(Debug, Clone, Hash, PartialEq, Eq)]

Diff for: src/bootstrap/src/core/build_steps/tool.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -746,9 +746,11 @@ impl Step for LlvmBitcodeLinker {
746746
.join(exe(bin_name, self.compiler.host));
747747

748748
if self.compiler.stage > 0 {
749-
let bindir = builder.sysroot(self.compiler).join("bin");
750-
t!(fs::create_dir_all(&bindir));
751-
let bin_destination = bindir.join(exe(bin_name, self.compiler.host));
749+
let bindir_self_contained = builder
750+
.sysroot(self.compiler)
751+
.join(format!("lib/rustlib/{}/bin/self-contained", self.target.triple));
752+
t!(fs::create_dir_all(&bindir_self_contained));
753+
let bin_destination = bindir_self_contained.join(exe(bin_name, self.compiler.host));
752754
builder.copy_link(&tool_out, &bin_destination);
753755
bin_destination
754756
} else {

Diff for: src/bootstrap/src/core/builder.rs

+1
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,7 @@ impl<'a> Builder<'a> {
853853
dist::Clippy,
854854
dist::Miri,
855855
dist::LlvmTools,
856+
dist::LlvmBitcodeLinker,
856857
dist::RustDev,
857858
dist::Bootstrap,
858859
dist::Extended,

Diff for: src/bootstrap/src/utils/tarball.rs

+8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub(crate) enum OverlayKind {
2020
RLS,
2121
RustAnalyzer,
2222
RustcCodegenCranelift,
23+
LlvmBitcodeLinker,
2324
}
2425

2526
impl OverlayKind {
@@ -64,6 +65,12 @@ impl OverlayKind {
6465
"compiler/rustc_codegen_cranelift/LICENSE-APACHE",
6566
"compiler/rustc_codegen_cranelift/LICENSE-MIT",
6667
],
68+
OverlayKind::LlvmBitcodeLinker => &[
69+
"COPYRIGHT",
70+
"LICENSE-APACHE",
71+
"LICENSE-MIT",
72+
"src/tools/llvm-bitcode-linker/README.md",
73+
],
6774
}
6875
}
6976

@@ -87,6 +94,7 @@ impl OverlayKind {
8794
.rust_analyzer_info
8895
.version(builder, &builder.release_num("rust-analyzer/crates/rust-analyzer")),
8996
OverlayKind::RustcCodegenCranelift => builder.rust_version(),
97+
OverlayKind::LlvmBitcodeLinker => builder.rust_version(),
9098
}
9199
}
92100
}

Diff for: src/doc/unstable-book/src/compiler-flags/codegen-options.md

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ In addition to the stable set of linker flavors, the following unstable values a
1010
- `ptx`: use [`rust-ptx-linker`](https://github.com/denzp/rust-ptx-linker)
1111
for Nvidia NVPTX GPGPU support.
1212
- `bpf`: use [`bpf-linker`](https://github.com/alessandrod/bpf-linker) for eBPF support.
13+
- `llbc`: for linking in llvm bitcode. Install the preview rustup components`llvm-bitcode-linker`
14+
and `llvm-tools` to use as a self-contained linker by passing
15+
`-Zunstable-options -Clink-self-contained=+linker` together with `-Clinker-flavor=llbc`.
16+
Can currently only be used for Nvidia NVPTX targets (`nvptx64-nvidia-cuda`).
1317

1418
Additionally, a set of more precise linker flavors also exists, for example allowing targets to
1519
declare that they use the LLD linker by default. The following values are currently unstable, and

Diff for: src/tools/llvm-bitcode-linker/src/linker.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl Session {
6262
.arg("-o")
6363
.arg(&self.link_path)
6464
.output()
65-
.unwrap();
65+
.context("An error occured when calling llvm-link. Make sure the llvm-tools component is installed.")?;
6666

6767
if !llvm_link_output.status.success() {
6868
tracing::error!(
@@ -108,7 +108,9 @@ impl Session {
108108
opt_cmd.arg("--strip-debug");
109109
}
110110

111-
let opt_output = opt_cmd.output().unwrap();
111+
let opt_output = opt_cmd.output().context(
112+
"An error occured when calling opt. Make sure the llvm-tools component is installed.",
113+
)?;
112114

113115
if !opt_output.status.success() {
114116
tracing::error!(
@@ -133,8 +135,11 @@ impl Session {
133135
lcc_command.arg("--mcpu").arg(mcpu);
134136
}
135137

136-
let lcc_output =
137-
lcc_command.arg(&self.opt_path).arg("-o").arg(&self.out_path).output().unwrap();
138+
let lcc_output = lcc_command
139+
.arg(&self.opt_path)
140+
.arg("-o").arg(&self.out_path)
141+
.output()
142+
.context("An error occured when calling llc. Make sure the llvm-tools component is installed.")?;
138143

139144
if !lcc_output.status.success() {
140145
tracing::error!(

0 commit comments

Comments
 (0)