[clang] [CMake][Release] Statically link clang with stage1 runtimes (PR #127268)

Tom Stellard via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 14 13:59:13 PST 2025


https://github.com/tstellar created https://github.com/llvm/llvm-project/pull/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++.

>From a705acfd439e6310648b0ed1ad612eaffc408ad5 Mon Sep 17 00:00:00 2001
From: Tom Stellard <tstellar at redhat.com>
Date: Fri, 14 Feb 2025 17:46:25 +0000
Subject: [PATCH] [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++.
---
 clang/cmake/caches/Release.cmake | 20 ++++++++++++++++----
 1 file changed, 16 insertions(+), 4 deletions(-)

diff --git a/clang/cmake/caches/Release.cmake b/clang/cmake/caches/Release.cmake
index 23e99493087ff..290e186baf6fe 100644
--- a/clang/cmake/caches/Release.cmake
+++ b/clang/cmake/caches/Release.cmake
@@ -48,10 +48,8 @@ set(CLANG_ENABLE_BOOTSTRAP ON CACHE BOOL "")
 
 set(STAGE1_PROJECTS "clang")
 
-# Building Flang on Windows requires compiler-rt, so we need to build it in
-# stage1.  compiler-rt is also required for building the Flang tests on
-# macOS.
-set(STAGE1_RUNTIMES "compiler-rt")
+# Build all runtimes so we can statically link them into the stage2 compiler.
+set(STAGE1_RUNTIMES "compiler-rt;libcxx;libcxxabi;libunwind")
 
 if (LLVM_RELEASE_ENABLE_PGO)
   list(APPEND STAGE1_PROJECTS "lld")
@@ -90,9 +88,18 @@ else()
   set(CLANG_BOOTSTRAP_TARGETS ${LLVM_RELEASE_FINAL_STAGE_TARGETS} CACHE STRING "")
 endif()
 
+if (LLVM_RELEASE_ENABLE_LTO)
+  # Enable LTO for the runtimes.  We need to configure stage1 clang to default
+  # to using lld as the linker because the stage1 toolchain will be used to
+  # build and link the runtimes.
+  set(RUNTIMES_CMAKE_ARGS "-DCMAKE_INTERPROCEDURAL_OPTIMIZATION=ON" CACHE STRING "")
+  set(LLVM_ENABLE_LLD ON CACHE STRING "")
+endif()
+
 # Stage 1 Common Config
 set(LLVM_ENABLE_RUNTIMES ${STAGE1_RUNTIMES} CACHE STRING "")
 set(LLVM_ENABLE_PROJECTS ${STAGE1_PROJECTS} CACHE STRING "")
+set(LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY ON CACHE STRING "")
 
 # stage2-instrumented and Final Stage Config:
 # 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}"
 if (LLVM_RELEASE_ENABLE_LTO)
   set_instrument_and_final_stage_var(LLVM_ENABLE_LLD "ON" BOOL)
 endif()
+set_instrument_and_final_stage_var(LLVM_ENABLE_LIBCXX "ON" BOOL)
+set_instrument_and_final_stage_var(LLVM_STATIC_LINK_CXX_STDLIB "ON" BOOL)
+set_instrument_and_final_stage_var(CMAKE_EXE_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" STRING)
+set_instrument_and_final_stage_var(CMAKE_SHARED_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" STRING)
+set_instrument_and_final_stage_var(CMAKE_MODULE_LINKER_FLAGS "-rtlib=compiler-rt --unwindlib=libunwind -static-libgcc" STRING)
 
 # Final Stage Config (stage2)
 set_final_stage_var(LLVM_ENABLE_RUNTIMES "${LLVM_RELEASE_ENABLE_RUNTIMES}" STRING)



More information about the cfe-commits mailing list