Skip to content

Commit

Permalink
unify llvm-bitcode-linker, wasm-component-ld and llvm-tools logics
Browse files Browse the repository at this point in the history
Signed-off-by: onur-ozkan <[email protected]>
  • Loading branch information
onur-ozkan committed Sep 7, 2024
1 parent a3af208 commit b6fdb74
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 51 deletions.
60 changes: 14 additions & 46 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use crate::utils::helpers::{
self, exe, get_clang_cl_resource_dir, get_closest_merge_base_commit, is_debug_info, is_dylib,
symlink_dir, t, up_to_date,
};
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode, LLVM_TOOLS};
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Std {
Expand Down Expand Up @@ -1890,52 +1890,20 @@ impl Step for Assemble {
// delegates to the `rust-lld` binary for linking and then runs
// logic to create the final binary. This is used by the
// `wasm32-wasip2` target of Rust.
if builder.build_wasm_component_ld() {
let wasm_component_ld_exe =
builder.ensure(crate::core::build_steps::tool::WasmComponentLd {
compiler: build_compiler,
target: target_compiler.host,
});
builder.copy_link(
&wasm_component_ld_exe,
&libdir_bin.join(wasm_component_ld_exe.file_name().unwrap()),
);
}

if builder.config.llvm_enabled(target_compiler.host) {
let llvm::LlvmResult { llvm_config, .. } =
builder.ensure(llvm::Llvm { target: target_compiler.host });
if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
let llvm_bin_dir =
command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
let llvm_bin_dir = Path::new(llvm_bin_dir.trim());

// Since we've already built the LLVM tools, install them to the sysroot.
// This is the equivalent of installing the `llvm-tools-preview` component via
// rustup, and lets developers use a locally built toolchain to
// build projects that expect llvm tools to be present in the sysroot
// (e.g. the `bootimage` crate).
for tool in LLVM_TOOLS {
let tool_exe = exe(tool, target_compiler.host);
let src_path = llvm_bin_dir.join(&tool_exe);
// When using `download-ci-llvm`, some of the tools
// may not exist, so skip trying to copy them.
if src_path.exists() {
builder.copy_link(&src_path, &libdir_bin.join(&tool_exe));
}
}
}
}
dist::maybe_install_wasm_component_ld(
builder,
build_compiler,
target_compiler.host,
&libdir_bin,
);

if builder.config.llvm_bitcode_linker_enabled {
let src_path = builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
compiler: build_compiler,
target: target_compiler.host,
extra_features: vec![],
});
let tool_exe = exe("llvm-bitcode-linker", target_compiler.host);
builder.copy_link(&src_path, &libdir_bin.join(tool_exe));
}
dist::maybe_install_llvm_tools(builder, target_compiler.host, &libdir_bin);
dist::maybe_install_llvm_bitcode_linker(
builder,
build_compiler,
target_compiler.host,
&libdir_bin,
);

// Ensure that `libLLVM.so` ends up in the newly build compiler directory,
// so that it can be found when the newly built `rustc` is run.
Expand Down
77 changes: 72 additions & 5 deletions src/bootstrap/src/core/build_steps/dist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,10 @@ impl Step for Rustc {
);
}
}
if builder.build_wasm_component_ld() {
let src_dir = builder.sysroot_libdir(compiler, host).parent().unwrap().join("bin");
let ld = exe("wasm-component-ld", compiler.host);
builder.copy_link(&src_dir.join(&ld), &dst_dir.join(&ld));
}

maybe_install_wasm_component_ld(builder, compiler, compiler.host, &dst_dir);
maybe_install_llvm_tools(builder, compiler.host, &dst_dir);
maybe_install_llvm_bitcode_linker(builder, compiler, compiler.host, &dst_dir);

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

/// Maybe add LLVM tools to the rustc sysroot.
pub fn maybe_install_llvm_tools(builder: &Builder<'_>, target: TargetSelection, dst_dir: &Path) {
if builder.config.llvm_enabled(target) {
let llvm::LlvmResult { llvm_config, .. } = builder.ensure(llvm::Llvm { target });
if !builder.config.dry_run() && builder.config.llvm_tools_enabled {
let llvm_bin_dir =
command(llvm_config).arg("--bindir").run_capture_stdout(builder).stdout();
let llvm_bin_dir = Path::new(llvm_bin_dir.trim());

// Since we've already built the LLVM tools, install them to the sysroot.
// This is the equivalent of installing the `llvm-tools-preview` component via
// rustup, and lets developers use a locally built toolchain to
// build projects that expect llvm tools to be present in the sysroot
// (e.g. the `bootimage` crate).
for tool in LLVM_TOOLS {
let tool_exe = exe(tool, target);
let src_path = llvm_bin_dir.join(&tool_exe);
// When using `download-ci-llvm`, some of the tools
// may not exist, so skip trying to copy them.
if src_path.exists() {
builder.copy_link(&src_path, &dst_dir.join(&tool_exe));
}
}
}
}
}

/// Maybe add `llvm-bitcode-linker` to the rustc sysroot.
pub fn maybe_install_llvm_bitcode_linker(
builder: &Builder<'_>,
compiler: Compiler,
target: TargetSelection,
dst_dir: &Path,
) {
if builder.config.llvm_bitcode_linker_enabled {
let dst_dir = dst_dir.join("self-contained");
t!(std::fs::create_dir_all(&dst_dir));

let src_path = builder.ensure(crate::core::build_steps::tool::LlvmBitcodeLinker {
compiler,
target,
extra_features: vec![],
});

let tool_exe = exe("llvm-bitcode-linker", target);

builder.copy_link(&src_path, &dst_dir.join(tool_exe));
}
}

/// Maybe add `wasm-component-ld` to the rustc sysroot.
pub fn maybe_install_wasm_component_ld(
builder: &Builder<'_>,
compiler: Compiler,
target: TargetSelection,
dst_dir: &Path,
) {
if builder.build_wasm_component_ld() {
let wasm_component_ld_exe =
builder.ensure(crate::core::build_steps::tool::WasmComponentLd { compiler, target });

builder.copy_link(
&wasm_component_ld_exe,
&dst_dir.join(wasm_component_ld_exe.file_name().unwrap()),
);
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct LlvmTools {
pub target: TargetSelection,
Expand Down

0 comments on commit b6fdb74

Please sign in to comment.