Skip to content

Commit 3cf6f0d

Browse files
bootstrap: Link LLVM tools dynamically in order to save time in ThinLTO builds.
1 parent f4b8451 commit 3cf6f0d

File tree

3 files changed

+46
-16
lines changed

3 files changed

+46
-16
lines changed

src/bootstrap/dist.rs

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,6 +1885,34 @@ impl Step for HashSign {
18851885
}
18861886
}
18871887

1888+
// Maybe add libLLVM.so to the lib-dir. It will only have been built if
1889+
// LLVM tools are linked dynamically.
1890+
// Note: This function does no yet support Windows but we also don't support
1891+
// linking LLVM tools dynamically on Windows yet.
1892+
fn maybe_install_llvm_dylib(builder: &Builder,
1893+
target: Interned<String>,
1894+
image: &Path) {
1895+
let src_libdir = builder
1896+
.llvm_out(target)
1897+
.join("lib");
1898+
1899+
// Usually libLLVM.so is a symlink to something like libLLVM-6.0.so.
1900+
// Since tools link to the latter rather than the former, we have to
1901+
// follow the symlink to find out what to distribute.
1902+
let llvm_dylib_path = src_libdir.join("libLLVM.so");
1903+
if llvm_dylib_path.exists() {
1904+
let llvm_dylib_path = llvm_dylib_path.canonicalize().unwrap_or_else(|e| {
1905+
panic!("dist: Error calling canonicalize path `{}`: {}",
1906+
llvm_dylib_path.display(), e);
1907+
});
1908+
1909+
let dst_libdir = image.join("lib");
1910+
t!(fs::create_dir_all(&dst_libdir));
1911+
1912+
builder.install(&llvm_dylib_path, &dst_libdir, 0o644);
1913+
}
1914+
}
1915+
18881916
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
18891917
pub struct LlvmTools {
18901918
pub stage: u32,
@@ -1929,18 +1957,18 @@ impl Step for LlvmTools {
19291957
drop(fs::remove_dir_all(&image));
19301958

19311959
// Prepare the image directory
1932-
let bindir = builder
1960+
let src_bindir = builder
19331961
.llvm_out(target)
19341962
.join("bin");
1935-
let dst = image.join("lib/rustlib")
1936-
.join(target)
1937-
.join("bin");
1938-
t!(fs::create_dir_all(&dst));
1963+
let dst_bindir = image.join("bin");
1964+
t!(fs::create_dir_all(&dst_bindir));
19391965
for tool in LLVM_TOOLS {
1940-
let exe = bindir.join(exe(tool, &target));
1941-
builder.install(&exe, &dst, 0o755);
1966+
let exe = src_bindir.join(exe(tool, &target));
1967+
builder.install(&exe, &dst_bindir, 0o755);
19421968
}
19431969

1970+
maybe_install_llvm_dylib(builder, target, &image);
1971+
19441972
// Prepare the overlay
19451973
let overlay = tmp.join("llvm-tools-overlay");
19461974
drop(fs::remove_dir_all(&overlay));
@@ -2025,7 +2053,6 @@ impl Step for Lldb {
20252053
let dst = image.join("lib");
20262054
t!(fs::create_dir_all(&dst));
20272055
for entry in t!(fs::read_dir(&libdir)) {
2028-
// let entry = t!(entry);
20292056
let entry = entry.unwrap();
20302057
if let Ok(name) = entry.file_name().into_string() {
20312058
if name.starts_with("liblldb.") && !name.ends_with(".a") {
@@ -2060,6 +2087,9 @@ impl Step for Lldb {
20602087
}
20612088
}
20622089

2090+
// Copy libLLVM.so to the lib dir as well, if needed.
2091+
maybe_install_llvm_dylib(builder, target, &image);
2092+
20632093
// Prepare the overlay
20642094
let overlay = tmp.join("lldb-overlay");
20652095
drop(fs::remove_dir_all(&overlay));

src/bootstrap/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,6 +1025,10 @@ impl Build {
10251025
self.rust_version()
10261026
}
10271027

1028+
fn llvm_link_tools_dynamically(&self, target: Interned<String>) -> bool {
1029+
(target.contains("linux-gnu") || target.contains("apple-darwin"))
1030+
}
1031+
10281032
/// Returns the `version` string associated with this compiler for Rust
10291033
/// itself.
10301034
///

src/bootstrap/native.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,10 @@ impl Step for Llvm {
171171

172172
// This setting makes the LLVM tools link to the dynamic LLVM library,
173173
// which saves both memory during parallel links and overall disk space
174-
// for the tools. We don't distribute any of those tools, so this is
175-
// just a local concern. However, it doesn't work well everywhere.
176-
//
177-
// If we are shipping llvm tools then we statically link them LLVM
178-
if (target.contains("linux-gnu") || target.contains("apple-darwin")) &&
179-
!builder.config.llvm_tools_enabled &&
180-
!want_lldb {
181-
cfg.define("LLVM_LINK_LLVM_DYLIB", "ON");
174+
// for the tools. We don't do this on every platform as it doesn't work
175+
// equally well everywhere.
176+
if builder.llvm_link_tools_dynamically(target) && !emscripten {
177+
cfg.define("LLVM_LINK_LLVM_DYLIB", "ON");
182178
}
183179

184180
// For distribution we want the LLVM tools to be *statically* linked to libstdc++

0 commit comments

Comments
 (0)