Author: Petr Hosek Date: 2024-07-18T22:17:42-07:00 New Revision: 3b78dfa10c4b77581cc29c4510aefe919ae660ba
URL: https://github.com/llvm/llvm-project/commit/3b78dfa10c4b77581cc29c4510aefe919ae660ba DIFF: https://github.com/llvm/llvm-project/commit/3b78dfa10c4b77581cc29c4510aefe919ae660ba.diff LOG: [libc][libcxx] Support for building libc++ against LLVM libc (#99287) Provide an option to build libc++ against LLVM libc and set the CMake compile and link options appropriately when the option is enabled. Added: libcxx/cmake/Modules/HandleLibC.cmake Modified: clang/cmake/caches/Fuchsia-stage2.cmake libcxx/CMakeLists.txt libcxx/cmake/config-ix.cmake libcxx/include/CMakeLists.txt libcxx/src/CMakeLists.txt Removed: ################################################################################ diff --git a/clang/cmake/caches/Fuchsia-stage2.cmake b/clang/cmake/caches/Fuchsia-stage2.cmake index 73ebd36c28496..0218f0b21eb28 100644 --- a/clang/cmake/caches/Fuchsia-stage2.cmake +++ b/clang/cmake/caches/Fuchsia-stage2.cmake @@ -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 "") @@ -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 "") diff --git a/libcxx/CMakeLists.txt b/libcxx/CMakeLists.txt index 155f81a74a974..332816b15260a 100644 --- a/libcxx/CMakeLists.txt +++ b/libcxx/CMakeLists.txt @@ -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") @@ -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. diff --git a/libcxx/cmake/Modules/HandleLibC.cmake b/libcxx/cmake/Modules/HandleLibC.cmake new file mode 100644 index 0000000000000..1b0564ae6fcc6 --- /dev/null +++ b/libcxx/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: +# - 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/cmake/config-ix.cmake b/libcxx/cmake/config-ix.cmake index 7406fba482e69..998819604de28 100644 --- a/libcxx/cmake/config-ix.cmake +++ b/libcxx/cmake/config-ix.cmake @@ -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 diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 4b036ec639507..fa6736dc11e66 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -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. diff --git a/libcxx/src/CMakeLists.txt b/libcxx/src/CMakeLists.txt index 0ae58a10c879c..0dfc9647558d4 100644 --- a/libcxx/src/CMakeLists.txt +++ b/libcxx/src/CMakeLists.txt @@ -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 @@ -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 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits