Skip to content

Commit f5caa26

Browse files
committed
Auto merge of rust-lang#134690 - clubby789:ci-clang-lto, r=<try>
CI: Add LTO support to clang in dist-x86_64-linux After rust-lang/cc-rs#1279, we attempt to pass `-flto=thin` to clang. In `dist-x86_64-linux`, we don't build clang with the `LLVMgold.so` library so this fails. This attempts to resolve this First, pass the binutils plugin include directory to Clang, [which will build the library](https://github.com/llvm/llvm-project/blob/2d6d723a85c2d007b0359c206d66cd2e5a9f00e1/llvm/docs/GoldPlugin.rst#how-to-build-it) Second, this library depends on the *version of libstdc++ that we built* specifically. However, despite both the RPATH and LD_LIBRARY_PATH pointing to `/rustroot/lib`, we incorrectly resolve to the system libstdc++, which doesn't load. ``` # LD_DEBUG=libs,files 2219: file=libstdc++.so.6 [0]; needed by /rustroot/bin/../lib/LLVMgold.so [0] 2219: find library=libstdc++.so.6 [0]; searching 2219: search path=/rustroot/bin/../lib/../lib (RPATH from file /rustroot/bin/../lib/LLVMgold.so) 2219: trying file=/rustroot/bin/../lib/../lib/libstdc++.so.6 2219: search path=/usr/lib64/tls:/usr/lib64 (system search path) 2219: trying file=/usr/lib64/tls/libstdc++.so.6 2219: trying file=/usr/lib64/libstdc++.so.6 ``` Using `LD_PRELOAD` causes it to correctly load the library I think this is probably not the most maintainable way to do this, so opening to see if this is desired and if there's a better way of doing this
2 parents 78af7da + cf1c38d commit f5caa26

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

src/bootstrap/src/core/build_steps/compile.rs

+13-2
Original file line numberDiff line numberDiff line change
@@ -989,14 +989,18 @@ impl Step for Rustc {
989989
// our LLVM wrapper. Unless we're explicitly requesting `librustc_driver` to be built with
990990
// debuginfo (via the debuginfo level of the executables using it): strip this debuginfo
991991
// away after the fact.
992+
let target_root_dir = stamp.parent().unwrap();
992993
if builder.config.rust_debuginfo_level_rustc == DebuginfoLevel::None
993994
&& builder.config.rust_debuginfo_level_tools == DebuginfoLevel::None
994995
{
995-
let target_root_dir = stamp.parent().unwrap();
996996
let rustc_driver = target_root_dir.join("librustc_driver.so");
997997
strip_debug(builder, target, &rustc_driver);
998998
}
999999

1000+
// Due to LTO a lot of debug info from C++ dependencies such as jemalloc can make it into
1001+
// our final binaries
1002+
strip_debug(builder, target, &target_root_dir.join("rustc-main"));
1003+
10001004
builder.ensure(RustcLink::from_rustc(
10011005
self,
10021006
builder.compiler(compiler.stage, builder.config.build),
@@ -2270,7 +2274,14 @@ pub fn strip_debug(builder: &Builder<'_>, target: TargetSelection, path: &Path)
22702274
}
22712275

22722276
let previous_mtime = t!(t!(path.metadata()).modified());
2273-
command("strip").arg("--strip-debug").arg(path).run_capture(builder);
2277+
let result = command("strip").arg("--strip-debug").arg(path).run_capture(builder);
2278+
if result.is_failure() {
2279+
eprintln!(
2280+
"warning: `strip --strip-debug {}` failed: `{}`",
2281+
path.display(),
2282+
result.stderr()
2283+
);
2284+
}
22742285

22752286
let file = t!(fs::File::open(path));
22762287

src/bootstrap/src/core/build_steps/tool.rs

+3
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,9 @@ impl Step for Rustdoc {
671671

672672
// don't create a stage0-sysroot/bin directory.
673673
if target_compiler.stage > 0 {
674+
// Due to LTO a lot of debug info from C++ dependencies such as jemalloc can make it into
675+
// our final binaries
676+
compile::strip_debug(builder, target, &tool_rustdoc);
674677
let bin_rustdoc = bin_rustdoc();
675678
builder.copy_link(&tool_rustdoc, &bin_rustdoc);
676679
bin_rustdoc

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

+6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ RUN mkdir -p /rustroot/bin
4444
ENV PATH=/rustroot/bin:$PATH
4545
ENV LD_LIBRARY_PATH=/rustroot/lib64:/rustroot/lib32:/rustroot/lib
4646
ENV PKG_CONFIG_PATH=/rustroot/lib/pkgconfig
47+
# Clang needs to access GCC headers to enable linker plugin LTO
48+
ENV GCC_VERSION=9.5.0
4749
WORKDIR /tmp
4850
RUN mkdir /home/user
4951
COPY host-x86_64/dist-x86_64-linux/shared.sh /tmp/
@@ -104,3 +106,7 @@ ENV DIST_SRC 1
104106
ENV LIBCURL_NO_PKG_CONFIG 1
105107

106108
ENV DIST_REQUIRE_ALL_TOOLS 1
109+
110+
# FIXME: Without this, LLVMgold.so incorrectly resolves to the system
111+
# libstdc++, instead of the one we build.
112+
ENV LD_PRELOAD=/rustroot/lib64/libstdc++.so.6

src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh

+1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ hide_output \
3939
-DLLVM_INCLUDE_TESTS=OFF \
4040
-DLLVM_INCLUDE_EXAMPLES=OFF \
4141
-DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt;bolt" \
42+
-DLLVM_BINUTILS_INCDIR="/rustroot/lib/gcc/x86_64-pc-linux-gnu/$GCC_VERSION/plugin/include/" \
4243
-DC_INCLUDE_DIRS="$INC"
4344

4445
hide_output make -j$(nproc)

src/ci/docker/host-x86_64/dist-x86_64-linux/build-gcc.sh

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#!/usr/bin/env bash
2-
set -ex
2+
set -eux
33

44
source shared.sh
55

66
# Note: in the future when bumping to version 10.1.0, also take care of the sed block below.
7-
GCC=9.5.0
7+
# This version is specified in the Dockerfile
8+
GCC=$GCC_VERSION
89

910
curl https://ftp.gnu.org/gnu/gcc/gcc-$GCC/gcc-$GCC.tar.xz | xzcat | tar xf -
1011
cd gcc-$GCC

0 commit comments

Comments
 (0)