Skip to content

Commit a705acf

Browse files
committed
[CMake][Release] Statically link clang with stage1 runtimes
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 6ff8a06 commit a705acf

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

clang/cmake/caches/Release.cmake

Lines changed: 16 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,18 @@ 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+
set(RUNTIMES_CMAKE_ARGS "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON" CACHE STRING "")
96+
set(LLVM_ENABLE_LLD ON CACHE STRING "")
97+
endif()
98+
9399
# Stage 1 Common Config
94100
set(LLVM_ENABLE_RUNTIMES ${STAGE1_RUNTIMES} CACHE STRING "")
95101
set(LLVM_ENABLE_PROJECTS ${STAGE1_PROJECTS} CACHE STRING "")
102+
set(LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY ON CACHE STRING "")
96103

97104
# stage2-instrumented and Final Stage Config:
98105
# Options that need to be set in both the instrumented stage (if we are doing
@@ -102,6 +109,11 @@ set_instrument_and_final_stage_var(LLVM_ENABLE_LTO "${LLVM_RELEASE_ENABLE_LTO}"
102109
if (LLVM_RELEASE_ENABLE_LTO)
103110
set_instrument_and_final_stage_var(LLVM_ENABLE_LLD "ON" BOOL)
104111
endif()
112+
set_instrument_and_final_stage_var(LLVM_ENABLE_LIBCXX "ON" BOOL)
113+
set_instrument_and_final_stage_var(LLVM_STATIC_LINK_CXX_STDLIB "ON" BOOL)
114+
set_instrument_and_final_stage_var(CMAKE_EXE_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" STRING)
115+
set_instrument_and_final_stage_var(CMAKE_SHARED_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" STRING)
116+
set_instrument_and_final_stage_var(CMAKE_MODULE_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" STRING)
105117

106118
# Final Stage Config (stage2)
107119
set_final_stage_var(LLVM_ENABLE_RUNTIMES "${LLVM_RELEASE_ENABLE_RUNTIMES}" STRING)

0 commit comments

Comments
 (0)