Skip to content

Commit 3b78dfa

Browse files
authored
[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.
1 parent 2e5b451 commit 3b78dfa

File tree

6 files changed

+56
-3
lines changed

6 files changed

+56
-3
lines changed

clang/cmake/caches/Fuchsia-stage2.cmake

+2
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ foreach(target armv6m-unknown-eabi;armv7m-unknown-eabi;armv8m.main-unknown-eabi)
339339
set(RUNTIMES_${target}_LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
340340
set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC ON CACHE BOOL "")
341341
set(RUNTIMES_${target}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "")
342+
set(RUNTIMES_${target}_LIBCXX_LIBC "llvm-libc" CACHE STRING "")
342343
set(RUNTIMES_${target}_LIBCXX_ENABLE_FILESYSTEM OFF CACHE BOOL "")
343344
set(RUNTIMES_${target}_LIBCXX_ENABLE_RANDOM_DEVICE OFF CACHE BOOL "")
344345
set(RUNTIMES_${target}_LIBCXX_ENABLE_LOCALIZATION OFF CACHE BOOL "")
@@ -389,6 +390,7 @@ foreach(target riscv32-unknown-elf)
389390
set(RUNTIMES_${target}_LIBCXX_ENABLE_SHARED OFF CACHE BOOL "")
390391
set(RUNTIMES_${target}_LIBCXX_ENABLE_STATIC ON CACHE BOOL "")
391392
set(RUNTIMES_${target}_LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS ON CACHE BOOL "")
393+
set(RUNTIMES_${target}_LIBCXX_LIBC "llvm-libc" CACHE STRING "")
392394
set(RUNTIMES_${target}_LIBCXX_ENABLE_FILESYSTEM OFF CACHE BOOL "")
393395
set(RUNTIMES_${target}_LIBCXX_ENABLE_RANDOM_DEVICE OFF CACHE BOOL "")
394396
set(RUNTIMES_${target}_LIBCXX_ENABLE_LOCALIZATION OFF CACHE BOOL "")

libcxx/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,14 @@ set(LIBCXX_ABI_DEFINES "" CACHE STRING "A semicolon separated list of ABI macros
212212
set(LIBCXX_EXTRA_SITE_DEFINES "" CACHE STRING "Extra defines to add into __config_site")
213213
option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
214214

215+
# C Library options -----------------------------------------------------------
216+
217+
set(LIBCXX_SUPPORTED_C_LIBRARIES system llvm-libc)
218+
set(LIBCXX_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
219+
if (NOT "${LIBCXX_LIBC}" IN_LIST LIBCXX_SUPPORTED_C_LIBRARIES)
220+
message(FATAL_ERROR "Unsupported C library: '${LIBCXX_CXX_ABI}'. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
221+
endif()
222+
215223
# ABI Library options ---------------------------------------------------------
216224
if (MSVC)
217225
set(LIBCXX_DEFAULT_ABI_LIBRARY "vcruntime")
@@ -495,6 +503,7 @@ endif()
495503
# Setup Compiler Flags
496504
#===============================================================================
497505

506+
include(HandleLibC) # Setup the C library flags
498507
include(HandleLibCXXABI) # Setup the ABI library flags
499508

500509
# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.

libcxx/cmake/Modules/HandleLibC.cmake

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#===============================================================================
2+
# Define targets for linking against the selected C library
3+
#
4+
# After including this file, the following targets are defined:
5+
# - libcxx-libc-headers: An interface target that allows getting access to the
6+
# headers of the selected C library.
7+
# - libcxx-libc-shared: A target representing the selected shared C library.
8+
# - libcxx-libc-static: A target representing the selected static C library.
9+
#===============================================================================
10+
11+
# Link against a system-provided libc
12+
if (LIBCXX_LIBC STREQUAL "system")
13+
add_library(libcxx-libc-headers INTERFACE)
14+
15+
add_library(libcxx-libc-static INTERFACE)
16+
add_library(libcxx-libc-shared INTERFACE)
17+
18+
# Link against the in-tree LLVM libc
19+
elseif (LIBCXX_LIBC STREQUAL "llvm-libc")
20+
add_library(libcxx-libc-headers INTERFACE)
21+
target_link_libraries(libcxx-libc-headers INTERFACE libc-headers)
22+
if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
23+
target_compile_options(libcxx-libc-headers INTERFACE "-nostdlibinc")
24+
endif()
25+
26+
add_library(libcxx-libc-static INTERFACE)
27+
if (TARGET libc)
28+
target_link_libraries(libcxx-libc-static INTERFACE libc)
29+
endif()
30+
if (TARGET libm)
31+
target_link_libraries(libcxx-libc-static INTERFACE libm)
32+
endif()
33+
if (CXX_SUPPORTS_NOLIBC_FLAG)
34+
target_link_options(libcxx-libc-static INTERFACE "-nolibc")
35+
endif()
36+
37+
# TODO: There's no support for building LLVM libc as a shared library yet.
38+
add_library(libcxx-libc-shared INTERFACE)
39+
endif()

libcxx/cmake/config-ix.cmake

+3
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ if (NOT LIBCXX_USE_COMPILER_RT)
2727
endif()
2828
endif()
2929

30+
check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
31+
check_cxx_compiler_flag(-nolibc CXX_SUPPORTS_NOLIBC_FLAG)
32+
3033
# libc++ is using -nostdlib++ at the link step when available,
3134
# otherwise -nodefaultlibs is used. We want all our checks to also
3235
# use one of these options, otherwise we may end up with an inconsistency between

libcxx/include/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@ list(APPEND _all_includes "${LIBCXX_GENERATED_INCLUDE_DIR}/libcxx.imp")
10431043
add_custom_target(generate-cxx-headers ALL DEPENDS ${_all_includes})
10441044

10451045
add_library(cxx-headers INTERFACE)
1046-
target_link_libraries(cxx-headers INTERFACE libcxx-abi-headers)
1046+
target_link_libraries(cxx-headers INTERFACE libcxx-libc-headers libcxx-abi-headers)
10471047
add_dependencies(cxx-headers generate-cxx-headers)
10481048
# It's important that the arch directory be included first so that its header files
10491049
# which interpose on the default include dir be included instead of the default ones.

libcxx/src/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ split_list(LIBCXX_LINK_FLAGS)
199199
if (LIBCXX_ENABLE_SHARED)
200200
add_library(cxx_shared SHARED ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
201201
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
202-
target_link_libraries(cxx_shared PUBLIC cxx-headers
202+
target_link_libraries(cxx_shared PUBLIC cxx-headers libcxx-libc-shared
203203
PRIVATE ${LIBCXX_LIBRARIES})
204204
set_target_properties(cxx_shared
205205
PROPERTIES
@@ -292,7 +292,7 @@ set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
292292
if (LIBCXX_ENABLE_STATIC)
293293
add_library(cxx_static STATIC ${exclude_from_all} ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
294294
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
295-
target_link_libraries(cxx_static PUBLIC cxx-headers
295+
target_link_libraries(cxx_static PUBLIC cxx-headers libcxx-libc-static
296296
PRIVATE ${LIBCXX_LIBRARIES}
297297
PRIVATE libcxx-abi-static)
298298
set_target_properties(cxx_static

0 commit comments

Comments
 (0)