Skip to content

Commit 7222038

Browse files
committed
Support static linking LLVM with ThinLTO
1 parent a29424a commit 7222038

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

src/bootstrap/builder.rs

+27
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,33 @@ impl<'a> Builder<'a> {
787787
}
788788
}
789789

790+
// If we're statically linking in an LLVM compiled for ThinLTO,
791+
// configure the linker accordingly.
792+
if Mode::Rustc == mode && self.config.llvm_thin_lto && !self.config.llvm_link_shared {
793+
if target.contains("msvc") {
794+
// Here we assume the linker is lld-link.exe.
795+
rustflags.arg(&format!("-Clink-arg=/opt:lldtojobs={}", self.jobs()));
796+
} else {
797+
// Here we assume the linker is clang and that lld is available.
798+
// If not, there'll be linker errors.
799+
rustflags.arg("-Clink-arg=-fuse-ld=lld");
800+
rustflags.arg("-Clink-arg=-flto=thin");
801+
802+
// Copy the optimization flags LLVM uses for its Release and
803+
// RelWithDebugInfo builds.
804+
if self.config.llvm_optimize {
805+
if self.config.llvm_release_debuginfo {
806+
rustflags.arg("-Clink-arg=-O2");
807+
} else {
808+
rustflags.arg("-Clink-arg=-O3");
809+
}
810+
}
811+
812+
// Make LLD respect the `-j` option.
813+
rustflags.arg(&format!("-Clink-arg=-Wl,--thinlto-jobs={}", self.jobs()));
814+
}
815+
}
816+
790817
// This tells Cargo (and in turn, rustc) to output more complete
791818
// dependency information. Most importantly for rustbuild, this
792819
// includes sysroot artifacts, like libstd, which means that we don't

src/bootstrap/compile.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -567,9 +567,12 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: Interne
567567
let file = compiler_file(builder, builder.cxx(target).unwrap(), target, "libstdc++.a");
568568
cargo.env("LLVM_STATIC_STDCPP", file);
569569
}
570-
if builder.config.llvm_link_shared || builder.config.llvm_thin_lto {
570+
if builder.config.llvm_link_shared {
571571
cargo.env("LLVM_LINK_SHARED", "1");
572572
}
573+
if builder.config.llvm_thin_lto {
574+
cargo.env("LLVM_LTO", "thin");
575+
}
573576
if builder.config.llvm_use_libcxx {
574577
cargo.env("LLVM_USE_LIBCXX", "1");
575578
}

src/ci/docker/dist-x86_64-linux/Dockerfile

+1
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ ENV RUST_CONFIGURE_ARGS \
101101
--set target.x86_64-unknown-linux-gnu.linker=clang \
102102
--set target.x86_64-unknown-linux-gnu.ar=/rustroot/bin/llvm-ar \
103103
--set target.x86_64-unknown-linux-gnu.ranlib=/rustroot/bin/llvm-ranlib \
104+
--set llvm.link-shared=true \
104105
--set llvm.thin-lto=true \
105106
--set rust.jemalloc
106107
ENV SCRIPT python2.7 ../x.py dist --host $HOSTS --target $HOSTS

src/ci/docker/x86_64-gnu-llvm-7/Dockerfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
2121
COPY scripts/sccache.sh /scripts/
2222
RUN sh /scripts/sccache.sh
2323

24-
# using llvm-link-shared due to libffi issues -- see #34486
24+
# using llvm.link-shared due to libffi issues -- see #34486
2525
ENV RUST_CONFIGURE_ARGS \
2626
--build=x86_64-unknown-linux-gnu \
2727
--llvm-root=/usr/lib/llvm-7 \
28-
--enable-llvm-link-shared \
28+
--set llvm.link-shared=true \
2929
--set rust.thin-lto-import-instr-limit=10
3030

3131
ENV SCRIPT python2.7 ../x.py test src/tools/tidy && python2.7 ../x.py test

src/librustc_llvm/build.rs

+5
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ fn main() {
127127
}
128128

129129
if flag.starts_with("-flto") {
130+
// Handled below.
130131
continue;
131132
}
132133

@@ -153,6 +154,10 @@ fn main() {
153154
cfg.define("NDEBUG", None);
154155
}
155156

157+
if let Ok(kind) = env::var("LLVM_LTO") {
158+
cfg.flag(&format!("-flto={}", kind));
159+
}
160+
156161
build_helper::rerun_if_changed_anything_in_dir(Path::new("../rustllvm"));
157162
cfg.file("../rustllvm/PassWrapper.cpp")
158163
.file("../rustllvm/RustWrapper.cpp")

0 commit comments

Comments
 (0)