Skip to content

Commit

Permalink
[libc][libcxx] Support for building libc++ against LLVM libc (#99287)
Browse files Browse the repository at this point in the history
Provide an option to build libc++ against LLVM libc and set the CMake
compile and link options appropriately when the option is enabled.
  • Loading branch information
petrhosek authored Jul 19, 2024
1 parent 2e5b451 commit 3b78dfa
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 3 deletions.
2 changes: 2 additions & 0 deletions clang/cmake/caches/Fuchsia-stage2.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ foreach(target armv6m-unknown-eabi;armv7m-unknown-eabi;armv8m.main-unknown-eabi)
set(RUNTIMES_${target}_LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC ON CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_LIBC "llvm-libc" CACHE STRING "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_FILESYSTEM OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_RANDOM_DEVICE OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_LOCALIZATION OFF CACHE BOOL "")
Expand Down Expand Up @@ -389,6 +390,7 @@ foreach(target riscv32-unknown-elf)
set(RUNTIMES_${target}_LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC ON CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_LIBC "llvm-libc" CACHE STRING "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_FILESYSTEM OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_RANDOM_DEVICE OFF CACHE BOOL "")
set(RUNTIMES_${target}_LIBCXX_ENABLE_LOCALIZATION OFF CACHE BOOL "")
Expand Down
9 changes: 9 additions & 0 deletions libcxx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,14 @@ 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")
Expand Down Expand Up @@ -495,6 +503,7 @@ endif()
# Setup Compiler Flags
#===============================================================================

include(HandleLibC) # Setup the C library flags
include(HandleLibCXXABI) # Setup the ABI library flags

# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
Expand Down
39 changes: 39 additions & 0 deletions libcxx/cmake/Modules/HandleLibC.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#===============================================================================
# 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()
3 changes: 3 additions & 0 deletions libcxx/cmake/config-ix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ if (NOT LIBCXX_USE_COMPILER_RT)
endif()
endif()

check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
check_cxx_compiler_flag(-nolibc CXX_SUPPORTS_NOLIBC_FLAG)

# libc++ is using -nostdlib++ at the link step when available,
# otherwise -nodefaultlibs is used. We want all our checks to also
# use one of these options, otherwise we may end up with an inconsistency between
Expand Down
2 changes: 1 addition & 1 deletion libcxx/include/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,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-abi-headers)
target_link_libraries(cxx-headers INTERFACE libcxx-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.
Expand Down
4 changes: 2 additions & 2 deletions libcxx/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ split_list(LIBCXX_LINK_FLAGS)
if (LIBCXX_ENABLE_SHARED)
add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(cxx_shared PUBLIC cxx-headers
target_link_libraries(cxx_shared PUBLIC cxx-headers libcxx-libc-shared
PRIVATE ${LIBCXX_LIBRARIES})
set_target_properties(cxx_shared
PROPERTIES
Expand Down Expand Up @@ -292,7 +292,7 @@ set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
if (LIBCXX_ENABLE_STATIC)
add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(cxx_static PUBLIC cxx-headers
target_link_libraries(cxx_static PUBLIC cxx-headers libcxx-libc-static
PRIVATE ${LIBCXX_LIBRARIES}
PRIVATE libcxx-abi-static)
set_target_properties(cxx_static
Expand Down

0 comments on commit 3b78dfa

Please sign in to comment.