[libcxxabi][libunwind] Support for using LLVM libc (#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.
This commit is contained in:
Petr Hosek 2025-07-09 00:48:50 -07:00 committed by GitHub
parent 2d424e6333
commit 9d3b60dea7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 71 additions and 62 deletions

View File

@ -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_LIBC}'. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
endif()
# ABI Library options ---------------------------------------------------------
if (MSVC)
set(LIBCXX_DEFAULT_ABI_LIBRARY "vcruntime")

View File

@ -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()

View File

@ -1708,7 +1708,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.

View File

@ -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)

View File

@ -253,6 +253,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

View File

@ -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

View File

@ -182,6 +182,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()

View File

@ -132,9 +132,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
@ -147,7 +148,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>"
@ -173,9 +174,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
@ -194,7 +196,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>"

View File

@ -0,0 +1,48 @@
#===============================================================================
# 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.
#===============================================================================
include_guard(GLOBAL)
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()