Skip to content

Commit 1677ee5

Browse files
committed
[libcxxabi][libunwind] Support for using LLVM libc
This generalizes the support added in llvm#99287 renaming the option to RUNTIMES_USE_LIBC and integrating the module into libc++abi and libunwind as well.
1 parent 8a691cc commit 1677ee5

File tree

11 files changed

+147
-62
lines changed

11 files changed

+147
-62
lines changed

libcxx/CMakeLists.txt

-8
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_CXX_ABI}'. 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

-39
This file was deleted.

libcxx/include/CMakeLists.txt

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

21232123
add_library(cxx-headers INTERFACE)
2124-
target_link_libraries(cxx-headers INTERFACE libcxx-libc-headers libcxx-abi-headers)
2124+
target_link_libraries(cxx-headers INTERFACE runtimes-libc-headers libcxx-abi-headers)
21252125
add_dependencies(cxx-headers generate-cxx-headers)
21262126
# It's important that the arch directory be included first so that its header files
21272127
# 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
@@ -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

+2
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,8 @@ add_library_flags("${LIBCXXABI_ADDITIONAL_LIBRARIES}")
248248
# Configure compiler. Must happen after setting the target flags.
249249
include(config-ix)
250250

251+
include(HandleLibC) # Setup the C library flags
252+
251253
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
252254
list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
253255
# cmake 3.14 and above remove system include paths that are explicitly
+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+
# - libcxxabi-libc-headers: An interface target that allows getting access to the
6+
# headers of the selected C library.
7+
# - libcxxabi-libc-shared: A target representing the selected shared C library.
8+
# - libcxxabi-libc-static: A target representing the selected static C library.
9+
#===============================================================================
10+
11+
# Link against a system-provided libc
12+
if (LIBCXXABI_LIBC STREQUAL "system")
13+
add_library(libcxxabi-libc-headers INTERFACE)
14+
15+
add_library(libcxxabi-libc-static INTERFACE)
16+
add_library(libcxxabi-libc-shared INTERFACE)
17+
18+
# Link against the in-tree LLVM libc
19+
elseif (LIBCXXABI_LIBC STREQUAL "llvm-libc")
20+
add_library(libcxxabi-libc-headers INTERFACE)
21+
target_link_libraries(libcxxabi-libc-headers INTERFACE libc-headers)
22+
if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
23+
target_compile_options(libcxxabi-libc-headers INTERFACE "-nostdlibinc")
24+
endif()
25+
26+
add_library(libcxxabi-libc-static INTERFACE)
27+
if (TARGET libc)
28+
target_link_libraries(libcxxabi-libc-static INTERFACE libc)
29+
endif()
30+
if (TARGET libm)
31+
target_link_libraries(libcxxabi-libc-static INTERFACE libm)
32+
endif()
33+
if (CXX_SUPPORTS_NOLIBC_FLAG)
34+
target_link_options(libcxxabi-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(libcxxabi-libc-shared INTERFACE)
39+
endif()

libcxxabi/src/CMakeLists.txt

+8-6
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

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ include(HandleLibunwindFlags)
181181
# Configure compiler.
182182
include(config-ix)
183183

184+
include(HandleLibC) # Setup the C library flags
185+
184186
if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
185187
list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
186188
endif()
+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+
# - libunwind-libc-headers: An interface target that allows getting access to the
6+
# headers of the selected C library.
7+
# - libunwind-libc-shared: A target representing the selected shared C library.
8+
# - libunwind-libc-static: A target representing the selected static C library.
9+
#===============================================================================
10+
11+
# Link against a system-provided libc
12+
if (LIBUNWIND_LIBC STREQUAL "system")
13+
add_library(libunwind-libc-headers INTERFACE)
14+
15+
add_library(libunwind-libc-static INTERFACE)
16+
add_library(libunwind-libc-shared INTERFACE)
17+
18+
# Link against the in-tree LLVM libc
19+
elseif (LIBUNWIND_LIBC STREQUAL "llvm-libc")
20+
add_library(libunwind-libc-headers INTERFACE)
21+
target_link_libraries(libunwind-libc-headers INTERFACE libc-headers)
22+
if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
23+
target_compile_options(libunwind-libc-headers INTERFACE "-nostdlibinc")
24+
endif()
25+
26+
add_library(libunwind-libc-static INTERFACE)
27+
if (TARGET libc)
28+
target_link_libraries(libunwind-libc-static INTERFACE libc)
29+
endif()
30+
if (TARGET libm)
31+
target_link_libraries(libunwind-libc-static INTERFACE libm)
32+
endif()
33+
if (CXX_SUPPORTS_NOLIBC_FLAG)
34+
target_link_options(libunwind-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(libunwind-libc-shared INTERFACE)
39+
endif()

libunwind/src/CMakeLists.txt

+8-6
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
160160
else()
161161
target_compile_options(unwind_shared_objects PRIVATE -fno-rtti)
162162
endif()
163-
target_link_libraries(unwind_shared_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
164163
target_compile_options(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
165-
target_link_libraries(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
164+
target_link_libraries(unwind_shared_objects
165+
PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
166+
PRIVATE unwind-headers runtimes-libc-headers ${LIBUNWIND_LIBRARIES})
166167
set_target_properties(unwind_shared_objects
167168
PROPERTIES
168169
CXX_EXTENSIONS OFF
@@ -175,7 +176,7 @@ if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CO
175176
endif()
176177

177178
add_library(unwind_shared SHARED)
178-
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects)
179+
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects runtimes-libc-shared)
179180
set_target_properties(unwind_shared
180181
PROPERTIES
181182
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_SHARED}>,FALSE,TRUE>"
@@ -201,9 +202,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
201202
else()
202203
target_compile_options(unwind_static_objects PRIVATE -fno-rtti)
203204
endif()
204-
target_link_libraries(unwind_static_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
205205
target_compile_options(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
206-
target_link_libraries(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
206+
target_link_libraries(unwind_static_objects
207+
PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
208+
PRIVATE unwind-headers runtimes-libc-headers ${LIBUNWIND_LIBRARIES})
207209
set_target_properties(unwind_static_objects
208210
PROPERTIES
209211
CXX_EXTENSIONS OFF
@@ -222,7 +224,7 @@ if(LIBUNWIND_HIDE_SYMBOLS)
222224
endif()
223225

224226
add_library(unwind_static STATIC)
225-
target_link_libraries(unwind_static PUBLIC unwind_static_objects)
227+
target_link_libraries(unwind_static PUBLIC unwind_static_objects runtimes-libc-static)
226228
set_target_properties(unwind_static
227229
PROPERTIES
228230
EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBUNWIND_ENABLE_STATIC}>,FALSE,TRUE>"
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
set(RUNTIMES_SUPPORTED_C_LIBRARIES system llvm-libc)
12+
set(RUNTIMES_USE_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
13+
if (NOT "${RUNTIMES_USE_LIBC}" IN_LIST RUNTIMES_SUPPORTED_C_LIBRARIES)
14+
message(FATAL_ERROR "Unsupported C library: '${RUNTIMES_CXX_ABI}'. Supported values are ${RUNTIMES_SUPPORTED_C_LIBRARIES}.")
15+
endif()
16+
17+
# Link against a system-provided libc
18+
if (RUNTIMES_USE_LIBC STREQUAL "system")
19+
add_library(runtimes-libc-headers INTERFACE)
20+
21+
add_library(runtimes-libc-static INTERFACE)
22+
add_library(runtimes-libc-shared INTERFACE)
23+
24+
# Link against the in-tree LLVM libc
25+
elseif (RUNTIMES_USE_LIBC STREQUAL "llvm-libc")
26+
add_library(runtimes-libc-headers INTERFACE)
27+
target_link_libraries(runtimes-libc-headers INTERFACE libc-headers)
28+
check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
29+
if(CXX_SUPPORTS_NOSTDLIBINC_FLAG)
30+
target_compile_options(runtimes-libc-headers INTERFACE "-nostdlibinc")
31+
endif()
32+
33+
add_library(runtimes-libc-static INTERFACE)
34+
if (TARGET libc)
35+
target_link_libraries(runtimes-libc-static INTERFACE libc)
36+
endif()
37+
if (TARGET libm)
38+
target_link_libraries(runtimes-libc-static INTERFACE libm)
39+
endif()
40+
if (CXX_SUPPORTS_NOLIBC_FLAG)
41+
target_link_options(runtimes-libc-static INTERFACE "-nolibc")
42+
endif()
43+
44+
# TODO: There's no support for building LLVM libc as a shared library yet.
45+
add_library(runtimes-libc-shared INTERFACE)
46+
endif()

0 commit comments

Comments
 (0)