Skip to content

Commit 0e59075

Browse files
committed
unify llvm-bitcode-linker, wasm-component-ld and llvm-tools logics
Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 38e3a57 commit 0e59075

File tree

2 files changed

+86
-51
lines changed

2 files changed

+86
-51
lines changed

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

+14-46
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ use crate::utils::helpers::{
2929
self, exe, get_clang_cl_resource_dir, get_closest_merge_base_commit, is_debug_info, is_dylib,
3030
symlink_dir, t, up_to_date,
3131
};
32-
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode, LLVM_TOOLS};
32+
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode};
3333

3434
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
3535
pub struct Std {
@@ -1912,52 +1912,20 @@ impl Step for Assemble {
19121912
// delegates to the `rust-lld` binary for linking and then runs
19131913
// logic to create the final binary. This is used by the
19141914
// `wasm32-wasip2` target of Rust.
1915-
if builder.tool_enabled("wasm-component-ld") {
1916-
let wasm_component_ld_exe =
1917-
builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
1918-
compiler: build_compiler,
1919-
target: target_compiler.host,
1920-
});
1921-
builder.copy_link(
1922-
&wasm_component_ld_exe,
1923-
&libdir_bin.join(wasm_component_ld_exe.file_name().unwrap()),
1924-
);
1925-
}
1926-
1927-
if builder.config.llvm_enabled(target_compiler.host) {
1928-
let llvm::LlvmResult { llvm_config, .. } =
1929-
builder.ensure(llvm::Llvm { target: target_compiler.host });
1930-
if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
1931-
let llvm_bin_dir =
1932-
command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
1933-
let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
1934-
1935-
// Since we've already built the LLVM tools, install them to the sysroot.
1936-
// This is the equivalent of installing the `llvm-tools-preview` component via
1937-
// rustup, and lets developers use a locally built toolchain to
1938-
// build projects that expect llvm tools to be present in the sysroot
1939-
// (e.g. the `bootimage` crate).
1940-
for tool in LLVM_TOOLS {
1941-
let tool_exe = exe(tool, target_compiler.host);
1942-
let src_path = llvm_bin_dir.join(&tool_exe);
1943-
// When using `download-ci-llvm`, some of the tools
1944-
// may not exist, so skip trying to copy them.
1945-
if src_path.exists() {
1946-
builder.copy_link(&src_path, &libdir_bin.join(&tool_exe));
1947-
}
1948-
}
1949-
}
1950-
}
1915+
dist::maybe_install_wasm_component_ld(
1916+
builder,
1917+
build_compiler,
1918+
target_compiler.host,
1919+
&libdir_bin,
1920+
);
19511921

1952-
if builder.config.llvm_bitcode_linker_enabled {
1953-
let src_path = builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
1954-
compiler: build_compiler,
1955-
target: target_compiler.host,
1956-
extra_features: vec![],
1957-
});
1958-
let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
1959-
builder.copy_link(&src_path, &libdir_bin.join(tool_exe));
1960-
}
1922+
dist::maybe_install_llvm_tools(builder, target_compiler.host, &libdir_bin);
1923+
dist::maybe_install_llvm_bitcode_linker(
1924+
builder,
1925+
build_compiler,
1926+
target_compiler.host,
1927+
&libdir_bin,
1928+
);
19611929

19621930
// Ensure that `libLLVM.so` ends up in the newly build compiler directory,
19631931
// so that it can be found when the newly built `rustc` is run.

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

+72-5
Original file line numberDiff line numberDiff line change
@@ -473,11 +473,10 @@ impl Step for Rustc {
473473
);
474474
}
475475
}
476-
if builder.tool_enabled("wasm-component-ld") {
477-
let src_dir = builder.sysroot_libdir(compiler, host).parent().unwrap().join("bin");
478-
let ld = exe("wasm-component-ld", compiler.host);
479-
builder.copy_link(&src_dir.join(&ld), &dst_dir.join(&ld));
480-
}
476+
477+
maybe_install_wasm_component_ld(builder, compiler, compiler.host, &dst_dir);
478+
maybe_install_llvm_tools(builder, compiler.host, &dst_dir);
479+
maybe_install_llvm_bitcode_linker(builder, compiler, compiler.host, &dst_dir);
481480

482481
// Man pages
483482
t!(fs::create_dir_all(image.join("share/man/man1")));
@@ -2095,6 +2094,74 @@ pub fn maybe_install_llvm_runtime(builder: &Builder<'_>, target: TargetSelection
20952094
}
20962095
}
20972096

2097+
/// Maybe add LLVM tools to the rustc sysroot.
2098+
pub fn maybe_install_llvm_tools(builder: &Builder<'_>, target: TargetSelection, dst_dir: &Path) {
2099+
if builder.config.llvm_enabled(target) {
2100+
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
2101+
if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
2102+
let llvm_bin_dir =
2103+
command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
2104+
let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
2105+
2106+
// Since we've already built the LLVM tools, install them to the sysroot.
2107+
// This is the equivalent of installing the `llvm-tools-preview` component via
2108+
// rustup, and lets developers use a locally built toolchain to
2109+
// build projects that expect llvm tools to be present in the sysroot
2110+
// (e.g. the `bootimage` crate).
2111+
for tool in LLVM_TOOLS {
2112+
let tool_exe = exe(tool, target);
2113+
let src_path = llvm_bin_dir.join(&tool_exe);
2114+
// When using `download-ci-llvm`, some of the tools
2115+
// may not exist, so skip trying to copy them.
2116+
if src_path.exists() {
2117+
builder.copy_link(&src_path, &dst_dir.join(&tool_exe));
2118+
}
2119+
}
2120+
}
2121+
}
2122+
}
2123+
2124+
/// Maybe add `llvm-bitcode-linker` to the rustc sysroot.
2125+
pub fn maybe_install_llvm_bitcode_linker(
2126+
builder: &Builder<'_>,
2127+
compiler: Compiler,
2128+
target: TargetSelection,
2129+
dst_dir: &Path,
2130+
) {
2131+
if builder.config.llvm_bitcode_linker_enabled {
2132+
let dst_dir = dst_dir.join("self-contained");
2133+
t!(std::fs::create_dir_all(&dst_dir));
2134+
2135+
let src_path = builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
2136+
compiler,
2137+
target,
2138+
extra_features: vec![],
2139+
});
2140+
2141+
let tool_exe = exe("llvm-bitcode-linker", target);
2142+
2143+
builder.copy_link(&src_path, &dst_dir.join(tool_exe));
2144+
}
2145+
}
2146+
2147+
/// Maybe add `wasm-component-ld` to the rustc sysroot.
2148+
pub fn maybe_install_wasm_component_ld(
2149+
builder: &Builder<'_>,
2150+
compiler: Compiler,
2151+
target: TargetSelection,
2152+
dst_dir: &Path,
2153+
) {
2154+
if builder.tool_enabled("wasm-component-ld") {
2155+
let wasm_component_ld_exe =
2156+
builder.ensure(crate::core::build_steps::tool::WasmComponentLd { compiler, target });
2157+
2158+
builder.copy_link(
2159+
&wasm_component_ld_exe,
2160+
&dst_dir.join(wasm_component_ld_exe.file_name().unwrap()),
2161+
);
2162+
}
2163+
}
2164+
20982165
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
20992166
pub struct LlvmTools {
21002167
pub target: TargetSelection,

0 commit comments

Comments
 (0)