Skip to content

Commit f5b311e

Browse files
authored
[CMake][Release] Statically link clang with stage1 runtimes (llvm#127268)
This change will cause clang and the other tools to statically link against the runtimes built in stage1. This will make the built binaries more portable by eliminating dependencies on system libraries like libgcc and libstdc++.
1 parent 5d4998b commit f5b311e

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

clang/cmake/caches/Release.cmake

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,8 @@ set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
4848

4949
set(STAGE1_PROJECTS "clang")
5050

51-
# Building Flang on Windows requires compiler-rt, so we need to build it in
52-
# stage1. compiler-rt is also required for building the Flang tests on
53-
# macOS.
54-
set(STAGE1_RUNTIMES "compiler-rt")
51+
# Build all runtimes so we can statically link them into the stage2 compiler.
52+
set(STAGE1_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind")
5553

5654
if (LLVM_RELEASE_ENABLE_PGO)
5755
list(APPEND STAGE1_PROJECTS "lld")
@@ -90,9 +88,20 @@ else()
9088
set(CLANG_BOOTSTRAP_TARGETS ${LLVM_RELEASE_FINAL_STAGE_TARGETS} CACHE STRING "")
9189
endif()
9290

91+
if (LLVM_RELEASE_ENABLE_LTO)
92+
# Enable LTO for the runtimes. We need to configure stage1 clang to default
93+
# to using lld as the linker because the stage1 toolchain will be used to
94+
# build and link the runtimes.
95+
# FIXME: We can't use LLVM_ENABLE_LTO=Thin here, because it causes the CMake
96+
# step for the libcxx build to fail. CMAKE_INTERPROCEDURAL_OPTIMIZATION does
97+
# enable ThinLTO, though.
98+
set(RUNTIMES_CMAKE_ARGS "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON -DLLVM_ENABLE_LLD=ON" CACHE STRING "")
99+
endif()
100+
93101
# Stage 1 Common Config
94102
set(LLVM_ENABLE_RUNTIMES ${STAGE1_RUNTIMES} CACHE STRING "")
95103
set(LLVM_ENABLE_PROJECTS ${STAGE1_PROJECTS} CACHE STRING "")
104+
set(LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY ON CACHE STRING "")
96105

97106
# stage2-instrumented and Final Stage Config:
98107
# Options that need to be set in both the instrumented stage (if we are doing
@@ -102,6 +111,16 @@ set_instrument_and_final_stage_var(LLVM_ENABLE_LTO "${LLVM_RELEASE_ENABLE_LTO}"
102111
if (LLVM_RELEASE_ENABLE_LTO)
103112
set_instrument_and_final_stage_var(LLVM_ENABLE_LLD "ON" BOOL)
104113
endif()
114+
set_instrument_and_final_stage_var(LLVM_ENABLE_LIBCXX "ON" BOOL)
115+
set_instrument_and_final_stage_var(LLVM_STATIC_LINK_CXX_STDLIB "ON" BOOL)
116+
set(RELEASE_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind")
117+
if(NOT ${CMAKE_HOST_SYSTEM_NAME} MATCHES "Darwin")
118+
set(RELEASE_LINKER_FLAGS "${RELEASE_LINKER_FLAGS} -static-libgcc")
119+
endif()
120+
121+
set_instrument_and_final_stage_var(CMAKE_EXE_LINKER_FLAGS ${RELEASE_LINKER_FLAGS} STRING)
122+
set_instrument_and_final_stage_var(CMAKE_SHARED_LINKER_FLAGS ${RELEASE_LINKER_FLAGS} STRING)
123+
set_instrument_and_final_stage_var(CMAKE_MODULE_LINKER_FLAGS ${RELEASE_LINKER_FLAGS} STRING)
105124

106125
# Final Stage Config (stage2)
107126
set_final_stage_var(LLVM_ENABLE_RUNTIMES "${LLVM_RELEASE_ENABLE_RUNTIMES}" STRING)

0 commit comments

Comments
 (0)