Skip to content

Commit

Permalink
Move the target libLLVM to llvm-tools-preview
Browse files Browse the repository at this point in the history
For running the compiler, we usually only need LLVM from `$sysroot/lib`,
which rustup will make available with `LD_LIBRARY_PATH`. We've also been
shipping LLVM in the `$target/lib` directory, which bloats the download
and installed size. The only times we do need the latter are for the
RPATH of `llvm-tools-preview` binaries, and for linking `rustc-dev`
libraries. We'll move it to the `llvm-tools-preview` component directly,
and `rustc-dev` will have an implicit dependency on it.

Here are the dist sizes that I got before and after this change:

    llvm-tools-1.45.0-dev-x86_64-unknown-linux-gnu.tar.gz     1.3M   24M
    llvm-tools-1.45.0-dev-x86_64-unknown-linux-gnu.tar.xz     748K   17M
    rustc-1.45.0-dev-x86_64-unknown-linux-gnu.tar.gz          83M    61M
    rustc-1.45.0-dev-x86_64-unknown-linux-gnu.tar.xz          56M    41M

The installed size should reduce by exactly one `libLLVM.so` (~70-80M),
unless you also install `llvm-tools`, and then it should be identical.
  • Loading branch information
cuviper committed May 20, 2020
1 parent 0271499 commit 9c97b3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/bootstrap/compile.rs
Expand Up @@ -772,7 +772,8 @@ impl Step for Assemble {

// 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.
dist::maybe_install_llvm_dylib(builder, target_compiler.host, &sysroot);
dist::maybe_install_llvm_runtime(builder, target_compiler.host, &sysroot);
dist::maybe_install_llvm_target(builder, target_compiler.host, &sysroot);

// Link the compiler binary itself into place
let out_dir = builder.cargo_out(build_compiler, Mode::Rustc, host);
Expand Down
47 changes: 28 additions & 19 deletions src/bootstrap/dist.rs
Expand Up @@ -514,7 +514,7 @@ impl Step for Rustc {
// components like the llvm tools and LLD. LLD is included below and
// tools/LLDB come later, so let's just throw it in the rustc
// component for now.
maybe_install_llvm_dylib(builder, host, image);
maybe_install_llvm_runtime(builder, host, image);

// Copy over lld if it's there
if builder.config.lld_enabled {
Expand Down Expand Up @@ -2228,27 +2228,18 @@ impl Step for HashSign {
}
}

// Maybe add libLLVM.so to the lib-dir. It will only have been built if
// LLVM tools are linked dynamically.
//
// We add this to both the libdir of the rustc binary itself (for it to load at
// runtime) and also to the target directory so it can find it at link-time.
//
// Note: This function does no yet support Windows but we also don't support
// linking LLVM tools dynamically on Windows yet.
pub fn maybe_install_llvm_dylib(builder: &Builder<'_>, target: Interned<String>, sysroot: &Path) {
/// Maybe add libLLVM.so to the given destination lib-dir. It will only have
/// been built if LLVM tools are linked dynamically.
///
/// Note: This function does not yet support Windows, but we also don't support
/// linking LLVM tools dynamically on Windows yet.
fn maybe_install_llvm(builder: &Builder<'_>, target: Interned<String>, dst_libdir: &Path) {
let src_libdir = builder.llvm_out(target).join("lib");
let dst_libdir1 = sysroot.join("lib/rustlib").join(&*target).join("lib");
let dst_libdir2 =
sysroot.join(builder.sysroot_libdir_relative(Compiler { stage: 1, host: target }));
t!(fs::create_dir_all(&dst_libdir1));
t!(fs::create_dir_all(&dst_libdir2));

if target.contains("apple-darwin") {
let llvm_dylib_path = src_libdir.join("libLLVM.dylib");
if llvm_dylib_path.exists() {
builder.install(&llvm_dylib_path, &dst_libdir1, 0o644);
builder.install(&llvm_dylib_path, &dst_libdir2, 0o644);
builder.install(&llvm_dylib_path, dst_libdir, 0o644);
}
return;
}
Expand All @@ -2262,11 +2253,23 @@ pub fn maybe_install_llvm_dylib(builder: &Builder<'_>, target: Interned<String>,
panic!("dist: Error calling canonicalize path `{}`: {}", llvm_dylib_path.display(), e);
});

builder.install(&llvm_dylib_path, &dst_libdir1, 0o644);
builder.install(&llvm_dylib_path, &dst_libdir2, 0o644);
builder.install(&llvm_dylib_path, dst_libdir, 0o644);
}
}

/// Maybe add libLLVM.so to the target lib-dir for linking.
pub fn maybe_install_llvm_target(builder: &Builder<'_>, target: Interned<String>, sysroot: &Path) {
let dst_libdir = sysroot.join("lib/rustlib").join(&*target).join("lib");
maybe_install_llvm(builder, target, &dst_libdir);
}

/// Maybe add libLLVM.so to the runtime lib-dir for rustc itself.
pub fn maybe_install_llvm_runtime(builder: &Builder<'_>, target: Interned<String>, sysroot: &Path) {
let dst_libdir =
sysroot.join(builder.sysroot_libdir_relative(Compiler { stage: 1, host: target }));
maybe_install_llvm(builder, target, &dst_libdir);
}

#[derive(Clone, Debug, Eq, Hash, PartialEq)]
pub struct LlvmTools {
pub target: Interned<String>,
Expand Down Expand Up @@ -2314,6 +2317,12 @@ impl Step for LlvmTools {
builder.install(&exe, &dst_bindir, 0o755);
}

// Copy libLLVM.so to the target lib dir as well, so the RPATH like
// `$ORIGIN/../lib` can find it. It may also be used as a dependency
// of `rustc-dev` to support the inherited `-lLLVM` when using the
// compiler libraries.
maybe_install_llvm_target(builder, target, &image);

// Prepare the overlay
let overlay = tmp.join("llvm-tools-overlay");
drop(fs::remove_dir_all(&overlay));
Expand Down

0 comments on commit 9c97b3c

Please sign in to comment.