Skip to content

Commit 4ace433

Browse files
committed
unify llvm-bitcode-linker, wasm-component-ld and llvm-tools logics
Signed-off-by: onur-ozkan <[email protected]>
1 parent a3af208 commit 4ace433

File tree

2 files changed

+86
-52
lines changed

2 files changed

+86
-52
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 {
@@ -1890,52 +1890,20 @@ impl Step for Assemble {
18901890
// delegates to the `rust-lld` binary for linking and then runs
18911891
// logic to create the final binary. This is used by the
18921892
// `wasm32-wasip2` target of Rust.
1893-
if builder.build_wasm_component_ld() {
1894-
let wasm_component_ld_exe =
1895-
builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
1896-
compiler: build_compiler,
1897-
target: target_compiler.host,
1898-
});
1899-
builder.copy_link(
1900-
&wasm_component_ld_exe,
1901-
&libdir_bin.join(wasm_component_ld_exe.file_name().unwrap()),
1902-
);
1903-
}
1904-
1905-
if builder.config.llvm_enabled(target_compiler.host) {
1906-
let llvm::LlvmResult { llvm_config, .. } =
1907-
builder.ensure(llvm::Llvm { target: target_compiler.host });
1908-
if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
1909-
let llvm_bin_dir =
1910-
command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
1911-
let llvm_bin_dir = Path::new(llvm_bin_dir.trim());
1912-
1913-
// Since we've already built the LLVM tools, install them to the sysroot.
1914-
// This is the equivalent of installing the `llvm-tools-preview` component via
1915-
// rustup, and lets developers use a locally built toolchain to
1916-
// build projects that expect llvm tools to be present in the sysroot
1917-
// (e.g. the `bootimage` crate).
1918-
for tool in LLVM_TOOLS {
1919-
let tool_exe = exe(tool, target_compiler.host);
1920-
let src_path = llvm_bin_dir.join(&tool_exe);
1921-
// When using `download-ci-llvm`, some of the tools
1922-
// may not exist, so skip trying to copy them.
1923-
if src_path.exists() {
1924-
builder.copy_link(&src_path, &libdir_bin.join(&tool_exe));
1925-
}
1926-
}
1927-
}
1928-
}
1893+
dist::maybe_install_wasm_component_ld(
1894+
builder,
1895+
build_compiler,
1896+
target_compiler.host,
1897+
&libdir_bin,
1898+
);
19291899

1930-
if builder.config.llvm_bitcode_linker_enabled {
1931-
let src_path = builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
1932-
compiler: build_compiler,
1933-
target: target_compiler.host,
1934-
extra_features: vec![],
1935-
});
1936-
let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
1937-
builder.copy_link(&src_path, &libdir_bin.join(tool_exe));
1938-
}
1900+
dist::maybe_install_llvm_tools(builder, target_compiler.host, &libdir_bin);
1901+
dist::maybe_install_llvm_bitcode_linker(
1902+
builder,
1903+
build_compiler,
1904+
target_compiler.host,
1905+
&libdir_bin,
1906+
);
19391907

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

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

+72-6
Original file line numberDiff line numberDiff line change
@@ -457,9 +457,10 @@ impl Step for Rustc {
457457
let dst_dir = image.join("lib/rustlib").join(host).join("bin");
458458
t!(fs::create_dir_all(&dst_dir));
459459

460+
let src_dir = builder.sysroot_libdir(compiler, host).parent().unwrap().join("bin");
461+
460462
// Copy over lld if it's there
461463
if builder.config.lld_enabled {
462-
let src_dir = builder.sysroot_libdir(compiler, host).parent().unwrap().join("bin");
463464
let rust_lld = exe("rust-lld", compiler.host);
464465
builder.copy_link(&src_dir.join(&rust_lld), &dst_dir.join(&rust_lld));
465466
let self_contained_lld_src_dir = src_dir.join("gcc-ld");
@@ -473,11 +474,10 @@ impl Step for Rustc {
473474
);
474475
}
475476
}
476-
if builder.build_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-
}
477+
478+
maybe_install_wasm_component_ld(builder, compiler, compiler.host, &src_dir);
479+
maybe_install_llvm_tools(builder, compiler.host, &src_dir);
480+
maybe_install_llvm_bitcode_linker(builder, compiler, compiler.host, &src_dir);
481481

482482
// Man pages
483483
t!(fs::create_dir_all(image.join("share/man/man1")));
@@ -2095,6 +2095,72 @@ pub fn maybe_install_llvm_runtime(builder: &Builder<'_>, target: TargetSelection
20952095
}
20962096
}
20972097

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

0 commit comments

Comments
 (0)