Skip to content

Commit 9d3b60d

Browse files
authored
[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.
1 parent 2d424e6 commit 9d3b60d

File tree

9 files changed

+71
-62
lines changed

9 files changed

+71
-62
lines changed

libcxx/CMakeLists.txt

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,6 @@ set(LIBCXX_ABI_DEFINES "" CACHE STRING "A semicolon separated list of ABI macros
225225
set(LIBCXX_EXTRA_SITE_DEFINES "" CACHE STRING "Extra defines to add into __config_site")
226226
option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
227227

228-
# C Library options -----------------------------------------------------------
229-
230-
set(LIBCXX_SUPPORTED_C_LIBRARIES system llvm-libc)
231-
set(LIBCXX_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
232-
if (NOT "${LIBCXX_LIBC}" IN_LIST LIBCXX_SUPPORTED_C_LIBRARIES)
233-
message(FATAL_ERROR "Unsupported C library: '${LIBCXX_LIBC}'. Supported values are ${LIBCXX_SUPPORTED_C_LIBRARIES}.")
234-
endif()
235-
236228
# ABI Library options ---------------------------------------------------------
237229
if (MSVC)
238230
set(LIBCXX_DEFAULT_ABI_LIBRARY "vcruntime")

libcxx/cmake/Modules/HandleLibC.cmake

Lines changed: 0 additions & 39 deletions
This file was deleted.

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ list(APPEND _all_includes "${LIBCXX_GENERATED_INCLUDE_DIR}/libcxx.imp")
17081708
add_custom_target(generate-cxx-headers ALL DEPENDS ${_all_includes})
17091709

17101710
add_library(cxx-headers INTERFACE)
1711-
target_link_libraries(cxx-headers INTERFACE libcxx-libc-headers libcxx-abi-headers)
1711+
target_link_libraries(cxx-headers INTERFACE runtimes-libc-headers libcxx-abi-headers)
17121712
add_dependencies(cxx-headers generate-cxx-headers)
17131713
# It's important that the arch directory be included first so that its header files
17141714
# which interpose on the default include dir be included instead of the default ones.

libcxx/src/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ include(FindLibcCommonUtils)
175175
# Build the shared library.
176176
add_library(cxx_shared SHARED ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
177177
target_include_directories(cxx_shared PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
178-
target_link_libraries(cxx_shared PUBLIC cxx-headers libcxx-libc-shared
178+
target_link_libraries(cxx_shared PUBLIC cxx-headers runtimes-libc-shared
179179
PRIVATE ${LIBCXX_LIBRARIES}
180180
PRIVATE llvm-libc-common-utilities)
181181
set_target_properties(cxx_shared
@@ -266,7 +266,7 @@ set(CMAKE_STATIC_LIBRARY_PREFIX "lib")
266266
# Build the static library.
267267
add_library(cxx_static STATIC ${LIBCXX_SOURCES} ${LIBCXX_HEADERS})
268268
target_include_directories(cxx_static PRIVATE ${CMAKE_CURRENT_SOURCE_DIR})
269-
target_link_libraries(cxx_static PUBLIC cxx-headers libcxx-libc-static
269+
target_link_libraries(cxx_static PUBLIC cxx-headers runtimes-libc-static
270270
PRIVATE ${LIBCXX_LIBRARIES}
271271
PRIVATE libcxx-abi-static
272272
PRIVATE llvm-libc-common-utilities)

libcxxabi/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,8 @@ add_library_flags("${LIBCXXABI_ADDITIONAL_LIBRARIES}")
253253
# Configure compiler. Must happen after setting the target flags.
254254
include(config-ix)
255255

256+
include(HandleLibC) # Setup the C library flags
257+
256258
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
257259
list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
258260
# cmake 3.14 and above remove system include paths that are explicitly

libcxxabi/src/CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,12 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
172172
target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
173173
endif()
174174
endif()
175-
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
175+
target_link_libraries(cxxabi_shared_objects
176+
PUBLIC cxxabi-headers
177+
PRIVATE cxx-headers runtimes-libc-headers ${LIBCXXABI_LIBRARIES})
176178
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
177179
target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
178180
endif()
179-
target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
180181
set_target_properties(cxxabi_shared_objects
181182
PROPERTIES
182183
CXX_EXTENSIONS OFF
@@ -215,7 +216,7 @@ if (ZOS)
215216
endif ()
216217

217218
target_link_libraries(cxxabi_shared
218-
PUBLIC cxxabi_shared_objects
219+
PUBLIC cxxabi_shared_objects runtimes-libc-shared
219220
PRIVATE ${LIBCXXABI_LIBRARIES})
220221

221222
if (LIBCXXABI_ENABLE_SHARED)
@@ -274,8 +275,9 @@ if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC
274275
target_link_libraries(cxxabi_static_objects PUBLIC unwind_static_objects) # propagate usage requirements
275276
target_sources(cxxabi_static_objects PUBLIC $<TARGET_OBJECTS:unwind_static_objects>)
276277
endif()
277-
target_link_libraries(cxxabi_static_objects PRIVATE cxx-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
278-
target_link_libraries(cxxabi_static_objects PUBLIC cxxabi-headers)
278+
target_link_libraries(cxxabi_static_objects
279+
PUBLIC cxxabi-headers
280+
PRIVATE cxx-headers runtimes-libc-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
279281
set_target_properties(cxxabi_static_objects
280282
PROPERTIES
281283
CXX_EXTENSIONS OFF
@@ -311,7 +313,7 @@ endif()
311313

312314
add_library(cxxabi_static STATIC)
313315
if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
314-
target_link_libraries(cxxabi_static PUBLIC unwind_static)
316+
target_link_libraries(cxxabi_static PUBLIC unwind_static runtimes-libc-static)
315317
endif()
316318
set_target_properties(cxxabi_static
317319
PROPERTIES

libunwind/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ include(HandleLibunwindFlags)
182182
# Configure compiler.
183183
include(config-ix)
184184

185+
include(HandleLibC) # Setup the C library flags
186+
185187
if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
186188
list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
187189
endif()

libunwind/src/CMakeLists.txt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
132132
else()
133133
target_compile_options(unwind_shared_objects PRIVATE -fno-rtti)
134134
endif()
135-
target_link_libraries(unwind_shared_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
136135
target_compile_options(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
137-
target_link_libraries(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
136+
target_link_libraries(unwind_shared_objects
137+
PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
138+
PRIVATE unwind-headers runtimes-libc-headers ${LIBUNWIND_LIBRARIES})
138139
set_target_properties(unwind_shared_objects
139140
PROPERTIES
140141
CXX_EXTENSIONS OFF
@@ -147,7 +148,7 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
147148
endif()
148149

149150
add_library(unwind_shared SHARED)
150-
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects)
151+
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects runtimes-libc-shared)
151152
set_target_properties(unwind_shared
152153
PROPERTIES
153154
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_SHARED}>,FALSE,TRUE>"
@@ -173,9 +174,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
173174
else()
174175
target_compile_options(unwind_static_objects PRIVATE -fno-rtti)
175176
endif()
176-
target_link_libraries(unwind_static_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
177177
target_compile_options(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
178-
target_link_libraries(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
178+
target_link_libraries(unwind_static_objects
179+
PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
180+
PRIVATE unwind-headers runtimes-libc-headers ${LIBUNWIND_LIBRARIES})
179181
set_target_properties(unwind_static_objects
180182
PROPERTIES
181183
CXX_EXTENSIONS OFF
@@ -194,7 +196,7 @@ if(LIBUNWIND_HIDE_SYMBOLS)
194196
endif()
195197

196198
add_library(unwind_static STATIC)
197-
target_link_libraries(unwind_static PUBLIC unwind_static_objects)
199+
target_link_libraries(unwind_static PUBLIC unwind_static_objects runtimes-libc-static)
198200
set_target_properties(unwind_static
199201
PROPERTIES
200202
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_STATIC}>,FALSE,TRUE>"
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#===============================================================================
2+
# Define targets for linking against the selected C library
3+
#
4+
# After including this file, the following targets are defined:
5+
# - runtimes-libc-headers: An interface target that allows getting access to the
6+
# headers of the selected C library.
7+
# - runtimes-libc-shared: A target representing the selected shared C library.
8+
# - runtimes-libc-static: A target representing the selected static C library.
9+
#===============================================================================
10+
11+
include_guard(GLOBAL)
12+
13+
set(RUNTIMES_SUPPORTED_C_LIBRARIES system llvm-libc)
14+
set(RUNTIMES_USE_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
15+
if (NOT "${RUNTIMES_USE_LIBC}" IN_LIST RUNTIMES_SUPPORTED_C_LIBRARIES)
16+
message(FATAL_ERROR "Unsupported C library: '${RUNTIMES_CXX_ABI}'. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
17+
endif()
18+
19+
# Link against a system-provided libc
20+
if (RUNTIMES_USE_LIBC STREQUAL "system")
21+
add_library(runtimes-libc-headers INTERFACE)
22+
23+
add_library(runtimes-libc-static INTERFACE)
24+
add_library(runtimes-libc-shared INTERFACE)
25+
26+
# Link against the in-tree LLVM libc
27+
elseif (RUNTIMES_USE_LIBC STREQUAL "llvm-libc")
28+
add_library(runtimes-libc-headers INTERFACE)
29+
target_link_libraries(runtimes-libc-headers INTERFACE libc-headers)
30+
check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
31+
if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
32+
target_compile_options(runtimes-libc-headers INTERFACE "-nostdlibinc")
33+
endif()
34+
35+
add_library(runtimes-libc-static INTERFACE)
36+
if (TARGET libc)
37+
target_link_libraries(runtimes-libc-static INTERFACE libc)
38+
endif()
39+
if (TARGET libm)
40+
target_link_libraries(runtimes-libc-static INTERFACE libm)
41+
endif()
42+
if (CXX_SUPPORTS_NOLIBC_FLAG)
43+
target_link_options(runtimes-libc-static INTERFACE "-nolibc")
44+
endif()
45+
46+
# TODO: There's no support for building LLVM libc as a shared library yet.
47+
add_library(runtimes-libc-shared INTERFACE)
48+
endif()

0 commit comments

Comments
 (0)