[libcxx] [libcxxabi] [libunwind] [llvm] [libcxxabi][libunwind] Support for using LLVM libc (PR #134893)

Petr Hosek via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 8 10:42:05 PDT 2025


https://github.com/petrhosek created https://github.com/llvm/llvm-project/pull/134893

This generalizes the support added in #99287 renaming the option to RUNTIMES_USE_LIBC and integrating the module into libc++abi and libunwind as well.

>From 1677ee5c547018472a1f6a29282d3af3500abcd7 Mon Sep 17 00:00:00 2001
From: Petr Hosek <phosek at google.com>
Date: Fri, 2 Aug 2024 08:25:53 -0700
Subject: [PATCH] [libcxxabi][libunwind] Support for using LLVM libc

This generalizes the support added in #99287 renaming the option to
RUNTIMES_USE_LIBC and integrating the module into libc++abi and
libunwind as well.
---
 libcxx/CMakeLists.txt                    |  8 -----
 libcxx/cmake/Modules/HandleLibC.cmake    | 39 --------------------
 libcxx/include/CMakeLists.txt            |  2 +-
 libcxx/src/CMakeLists.txt                |  4 +--
 libcxxabi/CMakeLists.txt                 |  2 ++
 libcxxabi/cmake/Modules/HandleLibC.cmake | 39 ++++++++++++++++++++
 libcxxabi/src/CMakeLists.txt             | 14 ++++----
 libunwind/CMakeLists.txt                 |  2 ++
 libunwind/cmake/Modules/HandleLibC.cmake | 39 ++++++++++++++++++++
 libunwind/src/CMakeLists.txt             | 14 ++++----
 runtimes/cmake/Modules/HandleLibC.cmake  | 46 ++++++++++++++++++++++++
 11 files changed, 147 insertions(+), 62 deletions(-)
 delete mode 100644 libcxx/cmake/Modules/HandleLibC.cmake
 create mode 100644 libcxxabi/cmake/Modules/HandleLibC.cmake
 create mode 100644 libunwind/cmake/Modules/HandleLibC.cmake
 create mode 100644 runtimes/cmake/Modules/HandleLibC.cmake

diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt
index ebaa6e9fd0e97..d3003cbec3889 100644
--- a/libcxx/CMakeLists.txt
+++ b/libcxx/CMakeLists.txt
@@ -225,14 +225,6 @@ set(LIBCXX_ABI_DEFINES "" CACHE STRING "A semicolon separated list of ABI macros
 set(LIBCXX_EXTRA_SITE_DEFINES "" CACHE STRING "Extra defines to add into __config_site")
 option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
 
-# C Library options -----------------------------------------------------------
-
-set(LIBCXX_SUPPORTED_C_LIBRARIES system llvm-libc)
-set(LIBCXX_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
-if (NOT "${LIBCXX_LIBC}" IN_LIST LIBCXX_SUPPORTED_C_LIBRARIES)
-  message(FATAL_ERROR "Unsupported C library: '${LIBCXX_CXX_ABI}'. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
-endif()
-
 # ABI Library options ---------------------------------------------------------
 if (MSVC)
   set(LIBCXX_DEFAULT_ABI_LIBRARY "vcruntime")
diff --git a/libcxx/cmake/Modules/HandleLibC.cmake b/libcxx/cmake/Modules/HandleLibC.cmake
deleted file mode 100644
index 1b0564ae6fcc6..0000000000000
--- a/libcxx/cmake/Modules/HandleLibC.cmake
+++ /dev/null
@@ -1,39 +0,0 @@
-#===============================================================================
-# Define targets for linking against the selected C library
-#
-# After including this file, the following targets are defined:
-# - libcxx-libc-headers: An interface target that allows getting access to the
-#                        headers of the selected C library.
-# - libcxx-libc-shared: A target representing the selected shared C library.
-# - libcxx-libc-static: A target representing the selected static C library.
-#===============================================================================
-
-# Link against a system-provided libc
-if (LIBCXX_LIBC STREQUAL "system")
-  add_library(libcxx-libc-headers INTERFACE)
-
-  add_library(libcxx-libc-static INTERFACE)
-  add_library(libcxx-libc-shared INTERFACE)
-
-# Link against the in-tree LLVM libc
-elseif (LIBCXX_LIBC STREQUAL "llvm-libc")
-  add_library(libcxx-libc-headers INTERFACE)
-  target_link_libraries(libcxx-libc-headers INTERFACE libc-headers)
-  if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
-    target_compile_options(libcxx-libc-headers INTERFACE "-nostdlibinc")
-  endif()
-
-  add_library(libcxx-libc-static INTERFACE)
-  if (TARGET libc)
-    target_link_libraries(libcxx-libc-static INTERFACE libc)
-  endif()
-  if (TARGET libm)
-    target_link_libraries(libcxx-libc-static INTERFACE libm)
-  endif()
-  if (CXX_SUPPORTS_NOLIBC_FLAG)
-    target_link_options(libcxx-libc-static INTERFACE "-nolibc")
-  endif()
-
-  # TODO: There's no support for building LLVM libc as a shared library yet.
-  add_library(libcxx-libc-shared INTERFACE)
-endif()
diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt
index a021b9bb44d67..9058a72ea75ab 100644
--- a/libcxx/include/CMakeLists.txt
+++ b/libcxx/include/CMakeLists.txt
@@ -2121,7 +2121,7 @@ list(APPEND _all_includes "${LIBCXX_GENERATED_INCLUDE_DIR}/libcxx.imp")
 add_custom_target(generate-cxx-headers ALL DEPENDS ${_all_includes})
 
 add_library(cxx-headers INTERFACE)
-target_link_libraries(cxx-headers INTERFACE libcxx-libc-headers libcxx-abi-headers)
+target_link_libraries(cxx-headers INTERFACE runtimes-libc-headers libcxx-abi-headers)
 add_dependencies(cxx-headers generate-cxx-headers)
 # It's important that the arch directory be included first so that its header files
 # which interpose on the default include dir be included instead of the default ones.
diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt
index 4e9bf900af4c5..97fe57a5f24f8 100644
--- a/libcxx/src/CMakeLists.txt
+++ b/libcxx/src/CMakeLists.txt
@@ -175,7 +175,7 @@ include(FindLibcCommonUtils)
 # Build the shared library.
 add_library(cxx_shared SHARED ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
 target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
-target_link_libraries(cxx_shared PUBLIC cxx-headers libcxx-libc-shared
+target_link_libraries(cxx_shared PUBLIC cxx-headers runtimes-libc-shared
                                   PRIVATE ${LIBCXX_LIBRARIES}
                                   PRIVATE llvm-libc-common-utilities)
 set_target_properties(cxx_shared
@@ -266,7 +266,7 @@ set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
 # Build the static library.
 add_library(cxx_static STATIC ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
 target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
-target_link_libraries(cxx_static PUBLIC cxx-headers libcxx-libc-static
+target_link_libraries(cxx_static PUBLIC cxx-headers runtimes-libc-static
                                   PRIVATE ${LIBCXX_LIBRARIES}
                                   PRIVATE libcxx-abi-static
                                   PRIVATE llvm-libc-common-utilities)
diff --git a/libcxxabi/CMakeLists.txt b/libcxxabi/CMakeLists.txt
index 6dcfc51e55321..1b1f25012f605 100644
--- a/libcxxabi/CMakeLists.txt
+++ b/libcxxabi/CMakeLists.txt
@@ -248,6 +248,8 @@ add_library_flags("${LIBCXXABI_ADDITIONAL_LIBRARIES}")
 # Configure compiler. Must happen after setting the target flags.
 include(config-ix)
 
+include(HandleLibC) # Setup the C library flags
+
 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
   list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
   # cmake 3.14 and above remove system include paths that are explicitly
diff --git a/libcxxabi/cmake/Modules/HandleLibC.cmake b/libcxxabi/cmake/Modules/HandleLibC.cmake
new file mode 100644
index 0000000000000..0dbf5125d0f9e
--- /dev/null
+++ b/libcxxabi/cmake/Modules/HandleLibC.cmake
@@ -0,0 +1,39 @@
+#===============================================================================
+# Define targets for linking against the selected C library
+#
+# After including this file, the following targets are defined:
+# - libcxxabi-libc-headers: An interface target that allows getting access to the
+#                        headers of the selected C library.
+# - libcxxabi-libc-shared: A target representing the selected shared C library.
+# - libcxxabi-libc-static: A target representing the selected static C library.
+#===============================================================================
+
+# Link against a system-provided libc
+if (LIBCXXABI_LIBC STREQUAL "system")
+  add_library(libcxxabi-libc-headers INTERFACE)
+
+  add_library(libcxxabi-libc-static INTERFACE)
+  add_library(libcxxabi-libc-shared INTERFACE)
+
+# Link against the in-tree LLVM libc
+elseif (LIBCXXABI_LIBC STREQUAL "llvm-libc")
+  add_library(libcxxabi-libc-headers INTERFACE)
+  target_link_libraries(libcxxabi-libc-headers INTERFACE libc-headers)
+  if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
+    target_compile_options(libcxxabi-libc-headers INTERFACE "-nostdlibinc")
+  endif()
+
+  add_library(libcxxabi-libc-static INTERFACE)
+  if (TARGET libc)
+    target_link_libraries(libcxxabi-libc-static INTERFACE libc)
+  endif()
+  if (TARGET libm)
+    target_link_libraries(libcxxabi-libc-static INTERFACE libm)
+  endif()
+  if (CXX_SUPPORTS_NOLIBC_FLAG)
+    target_link_options(libcxxabi-libc-static INTERFACE "-nolibc")
+  endif()
+
+  # TODO: There's no support for building LLVM libc as a shared library yet.
+  add_library(libcxxabi-libc-shared INTERFACE)
+endif()
diff --git a/libcxxabi/src/CMakeLists.txt b/libcxxabi/src/CMakeLists.txt
index 0a6fc892a4f69..0675577aed6a1 100644
--- a/libcxxabi/src/CMakeLists.txt
+++ b/libcxxabi/src/CMakeLists.txt
@@ -172,11 +172,12 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
     target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
   endif()
 endif()
-target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
+target_link_libraries(cxxabi_shared_objects
+  PUBLIC cxxabi-headers
+  PRIVATE cxx-headers runtimes-libc-headers ${LIBCXXABI_LIBRARIES})
 if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
   target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
 endif()
-target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
 set_target_properties(cxxabi_shared_objects
   PROPERTIES
     CXX_EXTENSIONS OFF
@@ -215,7 +216,7 @@ if (ZOS)
 endif ()
 
 target_link_libraries(cxxabi_shared
-  PUBLIC cxxabi_shared_objects
+  PUBLIC cxxabi_shared_objects runtimes-libc-shared
   PRIVATE ${LIBCXXABI_LIBRARIES})
 
 if (LIBCXXABI_ENABLE_SHARED)
@@ -274,8 +275,9 @@ if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC
   target_link_libraries(cxxabi_static_objects PUBLIC unwind_static_objects) # propagate usage requirements
   target_sources(cxxabi_static_objects PUBLIC $<TARGET_OBJECTS:unwind_static_objects>)
 endif()
-target_link_libraries(cxxabi_static_objects PRIVATE cxx-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
-target_link_libraries(cxxabi_static_objects PUBLIC cxxabi-headers)
+target_link_libraries(cxxabi_static_objects
+  PUBLIC cxxabi-headers
+  PRIVATE cxx-headers runtimes-libc-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
 set_target_properties(cxxabi_static_objects
   PROPERTIES
     CXX_EXTENSIONS OFF
@@ -311,7 +313,7 @@ endif()
 
 add_library(cxxabi_static STATIC)
 if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
-  target_link_libraries(cxxabi_static PUBLIC unwind_static)
+  target_link_libraries(cxxabi_static PUBLIC unwind_static runtimes-libc-static)
 endif()
 set_target_properties(cxxabi_static
   PROPERTIES
diff --git a/libunwind/CMakeLists.txt b/libunwind/CMakeLists.txt
index 3c8499fd33464..cf31ca2d8f52c 100644
--- a/libunwind/CMakeLists.txt
+++ b/libunwind/CMakeLists.txt
@@ -181,6 +181,8 @@ include(HandleLibunwindFlags)
 # Configure compiler.
 include(config-ix)
 
+include(HandleLibC) # Setup the C library flags
+
 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
   list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
 endif()
diff --git a/libunwind/cmake/Modules/HandleLibC.cmake b/libunwind/cmake/Modules/HandleLibC.cmake
new file mode 100644
index 0000000000000..62f8db17d1e7f
--- /dev/null
+++ b/libunwind/cmake/Modules/HandleLibC.cmake
@@ -0,0 +1,39 @@
+#===============================================================================
+# Define targets for linking against the selected C library
+#
+# After including this file, the following targets are defined:
+# - libunwind-libc-headers: An interface target that allows getting access to the
+#                        headers of the selected C library.
+# - libunwind-libc-shared: A target representing the selected shared C library.
+# - libunwind-libc-static: A target representing the selected static C library.
+#===============================================================================
+
+# Link against a system-provided libc
+if (LIBUNWIND_LIBC STREQUAL "system")
+  add_library(libunwind-libc-headers INTERFACE)
+
+  add_library(libunwind-libc-static INTERFACE)
+  add_library(libunwind-libc-shared INTERFACE)
+
+# Link against the in-tree LLVM libc
+elseif (LIBUNWIND_LIBC STREQUAL "llvm-libc")
+  add_library(libunwind-libc-headers INTERFACE)
+  target_link_libraries(libunwind-libc-headers INTERFACE libc-headers)
+  if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
+    target_compile_options(libunwind-libc-headers INTERFACE "-nostdlibinc")
+  endif()
+
+  add_library(libunwind-libc-static INTERFACE)
+  if (TARGET libc)
+    target_link_libraries(libunwind-libc-static INTERFACE libc)
+  endif()
+  if (TARGET libm)
+    target_link_libraries(libunwind-libc-static INTERFACE libm)
+  endif()
+  if (CXX_SUPPORTS_NOLIBC_FLAG)
+    target_link_options(libunwind-libc-static INTERFACE "-nolibc")
+  endif()
+
+  # TODO: There's no support for building LLVM libc as a shared library yet.
+  add_library(libunwind-libc-shared INTERFACE)
+endif()
diff --git a/libunwind/src/CMakeLists.txt b/libunwind/src/CMakeLists.txt
index d69013e5dace1..2c0cd7aa9e2d6 100644
--- a/libunwind/src/CMakeLists.txt
+++ b/libunwind/src/CMakeLists.txt
@@ -160,9 +160,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
 else()
   target_compile_options(unwind_shared_objects PRIVATE -fno-rtti)
 endif()
-target_link_libraries(unwind_shared_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
 target_compile_options(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
-target_link_libraries(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
+target_link_libraries(unwind_shared_objects
+  PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
+  PRIVATE unwind-headers runtimes-libc-headers ${LIBUNWIND_LIBRARIES})
 set_target_properties(unwind_shared_objects
   PROPERTIES
     CXX_EXTENSIONS OFF
@@ -175,7 +176,7 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
 endif()
 
 add_library(unwind_shared SHARED)
-target_link_libraries(unwind_shared PUBLIC unwind_shared_objects)
+target_link_libraries(unwind_shared PUBLIC unwind_shared_objects runtimes-libc-shared)
 set_target_properties(unwind_shared
   PROPERTIES
     EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_SHARED}>,FALSE,TRUE>"
@@ -201,9 +202,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
 else()
   target_compile_options(unwind_static_objects PRIVATE -fno-rtti)
 endif()
-target_link_libraries(unwind_static_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
 target_compile_options(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
-target_link_libraries(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
+target_link_libraries(unwind_static_objects
+  PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
+  PRIVATE unwind-headers runtimes-libc-headers ${LIBUNWIND_LIBRARIES})
 set_target_properties(unwind_static_objects
   PROPERTIES
     CXX_EXTENSIONS OFF
@@ -222,7 +224,7 @@ if(LIBUNWIND_HIDE_SYMBOLS)
 endif()
 
 add_library(unwind_static STATIC)
-target_link_libraries(unwind_static PUBLIC unwind_static_objects)
+target_link_libraries(unwind_static PUBLIC unwind_static_objects runtimes-libc-static)
 set_target_properties(unwind_static
   PROPERTIES
     EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_STATIC}>,FALSE,TRUE>"
diff --git a/runtimes/cmake/Modules/HandleLibC.cmake b/runtimes/cmake/Modules/HandleLibC.cmake
new file mode 100644
index 0000000000000..8307ef4708725
--- /dev/null
+++ b/runtimes/cmake/Modules/HandleLibC.cmake
@@ -0,0 +1,46 @@
+#===============================================================================
+# Define targets for linking against the selected C library
+#
+# After including this file, the following targets are defined:
+# - runtimes-libc-headers: An interface target that allows getting access to the
+#                          headers of the selected C library.
+# - runtimes-libc-shared: A target representing the selected shared C library.
+# - runtimes-libc-static: A target representing the selected static C library.
+#===============================================================================
+
+set(RUNTIMES_SUPPORTED_C_LIBRARIES system llvm-libc)
+set(RUNTIMES_USE_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
+if (NOT "${RUNTIMES_USE_LIBC}" IN_LIST RUNTIMES_SUPPORTED_C_LIBRARIES)
+  message(FATAL_ERROR "Unsupported C library: '${RUNTIMES_CXX_ABI}'. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
+endif()
+
+# Link against a system-provided libc
+if (RUNTIMES_USE_LIBC STREQUAL "system")
+  add_library(runtimes-libc-headers INTERFACE)
+
+  add_library(runtimes-libc-static INTERFACE)
+  add_library(runtimes-libc-shared INTERFACE)
+
+# Link against the in-tree LLVM libc
+elseif (RUNTIMES_USE_LIBC STREQUAL "llvm-libc")
+  add_library(runtimes-libc-headers INTERFACE)
+  target_link_libraries(runtimes-libc-headers INTERFACE libc-headers)
+  check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
+  if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
+    target_compile_options(runtimes-libc-headers INTERFACE "-nostdlibinc")
+  endif()
+
+  add_library(runtimes-libc-static INTERFACE)
+  if (TARGET libc)
+    target_link_libraries(runtimes-libc-static INTERFACE libc)
+  endif()
+  if (TARGET libm)
+    target_link_libraries(runtimes-libc-static INTERFACE libm)
+  endif()
+  if (CXX_SUPPORTS_NOLIBC_FLAG)
+    target_link_options(runtimes-libc-static INTERFACE "-nolibc")
+  endif()
+
+  # TODO: There's no support for building LLVM libc as a shared library yet.
+  add_library(runtimes-libc-shared INTERFACE)
+endif()



More information about the cfe-commits mailing list