Auto merge of #134690 - clubby789:ci-clang-lto, r=Kobzol

CI: Add LTO support to clang in dist-x86_64-linux

After https://github.com/rust-lang/cc-rs/pull/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](2d6d723a85/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
This commit is contained in:
bors 2024-12-27 23:22:08 +00:00
commit ecc189922d
4 changed files with 11 additions and 2 deletions

View File

@ -50,6 +50,7 @@ COPY host-x86_64/dist-x86_64-linux/shared.sh /tmp/
# Need at least GCC 5.1 to compile LLVM nowadays
COPY host-x86_64/dist-x86_64-linux/build-gcc.sh /tmp/
ENV GCC_VERSION=9.5.0
RUN ./build-gcc.sh && yum remove -y gcc gcc-c++
COPY scripts/cmake.sh /tmp/

View File

@ -44,12 +44,14 @@ RUN mkdir -p /rustroot/bin
ENV PATH=/rustroot/bin:$PATH
ENV LD_LIBRARY_PATH=/rustroot/lib64:/rustroot/lib32:/rustroot/lib
ENV PKG_CONFIG_PATH=/rustroot/lib/pkgconfig
# Clang needs to access GCC headers to enable linker plugin LTO
WORKDIR /tmp
RUN mkdir /home/user
COPY host-x86_64/dist-x86_64-linux/shared.sh /tmp/
# Need at least GCC 5.1 to compile LLVM nowadays
COPY host-x86_64/dist-x86_64-linux/build-gcc.sh /tmp/
ENV GCC_VERSION=9.5.0
RUN ./build-gcc.sh && yum remove -y gcc gcc-c++
# LLVM 17 needs cmake 3.20 or higher.
@ -105,3 +107,7 @@ ENV DIST_SRC 1
ENV LIBCURL_NO_PKG_CONFIG 1
ENV DIST_REQUIRE_ALL_TOOLS 1
# FIXME: Without this, LLVMgold.so incorrectly resolves to the system
# libstdc++, instead of the one we build.
ENV LD_PRELOAD=/rustroot/lib64/libstdc++.so.6

View File

@ -39,6 +39,7 @@ hide_output \
-DLLVM_INCLUDE_TESTS=OFF \
-DLLVM_INCLUDE_EXAMPLES=OFF \
-DLLVM_ENABLE_PROJECTS="clang;lld;compiler-rt;bolt" \
-DLLVM_BINUTILS_INCDIR="/rustroot/lib/gcc/x86_64-pc-linux-gnu/$GCC_VERSION/plugin/include/" \
-DC_INCLUDE_DIRS="$INC"
hide_output make -j$(nproc)

View File

@ -1,10 +1,11 @@
#!/usr/bin/env bash
set -ex
set -eux
source shared.sh
# Note: in the future when bumping to version 10.1.0, also take care of the sed block below.
GCC=9.5.0
# This version is specified in the Dockerfile
GCC=$GCC_VERSION
curl https://ftp.gnu.org/gnu/gcc/gcc-$GCC/gcc-$GCC.tar.xz | xzcat | tar xf -
cd gcc-$GCC