Skip to content

Commit eb619c8

Browse files
committed
[libcxxabi][libunwind] Support for using LLVM libc
This is analogous to llvm#99287 and provides an option to build libc++abi and libunwind against LLVM libc when selected.
1 parent 129a8e1 commit eb619c8

File tree

6 files changed

+104
-12
lines changed

6 files changed

+104
-12
lines changed

libcxxabi/CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ message(STATUS "Using libc++abi testing configuration: ${LIBCXXABI_TEST_CONFIG}"
168168
set(LIBCXXABI_TEST_PARAMS "" CACHE STRING
169169
"A list of parameters to run the Lit test suite with.")
170170

171+
set(LIBCXXABI_SUPPORTED_C_LIBRARIES system llvm-libc)
172+
set(LIBCXXABI_LIBC "system" CACHE STRING "Specify C library to use. Supported values are ${LIBCXXABI_SUPPORTED_C_LIBRARIES}.")
173+
if (NOT "${LIBCXXABI_LIBC}" IN_LIST LIBCXXABI_SUPPORTED_C_LIBRARIES)
174+
message(FATAL_ERROR "Unsupported C library: '${LIBCXXABI_CXX_ABI}'. Supported values are ${LIBCXXABI_SUPPORTED_C_LIBRARIES}.")
175+
endif()
176+
171177
#===============================================================================
172178
# Configure System
173179
#===============================================================================
@@ -249,6 +255,8 @@ add_library_flags("${LIBCXXABI_ADDITIONAL_LIBRARIES}")
249255
# Configure compiler. Must happen after setting the target flags.
250256
include(config-ix)
251257

258+
include(HandleLibC) # Setup the C library flags
259+
252260
if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
253261
list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
254262
# 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
@@ -166,11 +166,12 @@ if (LIBCXXABI_USE_LLVM_UNWINDER)
166166
target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
167167
endif()
168168
endif()
169-
target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
169+
target_link_libraries(cxxabi_shared_objects
170+
PUBLIC cxxabi-headers
171+
PRIVATE cxx-headers libcxxabi-libc-headers ${LIBCXXABI_LIBRARIES})
170172
if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
171173
target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
172174
endif()
173-
target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
174175
set_target_properties(cxxabi_shared_objects
175176
PROPERTIES
176177
CXX_EXTENSIONS OFF
@@ -205,7 +206,7 @@ if (LIBCXXABI_ENABLE_SHARED)
205206
endif ()
206207

207208
target_link_libraries(cxxabi_shared
208-
PUBLIC cxxabi_shared_objects
209+
PUBLIC cxxabi_shared_objects libcxxabi-libc-shared
209210
PRIVATE ${LIBCXXABI_LIBRARIES})
210211

211212
list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_shared")
@@ -253,8 +254,9 @@ if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC
253254
target_link_libraries(cxxabi_static_objects PUBLIC unwind_static_objects) # propagate usage requirements
254255
target_sources(cxxabi_static_objects PUBLIC $<TARGET_OBJECTS:unwind_static_objects>)
255256
endif()
256-
target_link_libraries(cxxabi_static_objects PRIVATE cxx-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
257-
target_link_libraries(cxxabi_static_objects PUBLIC cxxabi-headers)
257+
target_link_libraries(cxxabi_static_objects
258+
PUBLIC cxxabi-headers
259+
PRIVATE cxx-headers libcxxabi-libc-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
258260
set_target_properties(cxxabi_static_objects
259261
PROPERTIES
260262
CXX_EXTENSIONS OFF
@@ -287,7 +289,7 @@ endif()
287289
if (LIBCXXABI_ENABLE_STATIC)
288290
add_library(cxxabi_static STATIC)
289291
if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
290-
target_link_libraries(cxxabi_static PUBLIC unwind_static)
292+
target_link_libraries(cxxabi_static PUBLIC unwind_static libcxxabi-libc-static)
291293
endif()
292294
set_target_properties(cxxabi_static
293295
PROPERTIES

libunwind/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,8 @@ include(HandleLibunwindFlags)
171171
# Configure compiler.
172172
include(config-ix)
173173

174+
include(HandleLibC) # Setup the C library flags
175+
174176
if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
175177
list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
176178
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
@@ -148,9 +148,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
148148
else()
149149
target_compile_options(unwind_shared_objects PRIVATE -fno-rtti)
150150
endif()
151-
target_link_libraries(unwind_shared_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
152151
target_compile_options(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
153-
target_link_libraries(unwind_shared_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
152+
target_link_libraries(unwind_shared_objects
153+
PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
154+
PRIVATE unwind-headers libunwind-libc-headers ${LIBUNWIND_LIBRARIES})
154155
set_target_properties(unwind_shared_objects
155156
PROPERTIES
156157
CXX_EXTENSIONS OFF
@@ -164,7 +165,7 @@ endif()
164165

165166
if (LIBUNWIND_ENABLE_SHARED)
166167
add_library(unwind_shared SHARED)
167-
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects)
168+
target_link_libraries(unwind_shared PUBLIC unwind_shared_objects libunwind-libc-shared)
168169
set_target_properties(unwind_shared
169170
PROPERTIES
170171
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"
@@ -188,9 +189,10 @@ if(CMAKE_C_COMPILER_ID STREQUAL MSVC)
188189
else()
189190
target_compile_options(unwind_static_objects PRIVATE -fno-rtti)
190191
endif()
191-
target_link_libraries(unwind_static_objects PRIVATE unwind-headers ${LIBUNWIND_LIBRARIES})
192192
target_compile_options(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_COMPILE_FLAGS}")
193-
target_link_libraries(unwind_static_objects PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}")
193+
target_link_libraries(unwind_static_objects
194+
PUBLIC "${LIBUNWIND_ADDITIONAL_LIBRARIES}"
195+
PRIVATE unwind-headers libunwind-libc-headers ${LIBUNWIND_LIBRARIES})
194196
set_target_properties(unwind_static_objects
195197
PROPERTIES
196198
CXX_EXTENSIONS OFF
@@ -210,7 +212,7 @@ endif()
210212

211213
if (LIBUNWIND_ENABLE_STATIC)
212214
add_library(unwind_static STATIC)
213-
target_link_libraries(unwind_static PUBLIC unwind_static_objects)
215+
target_link_libraries(unwind_static PUBLIC unwind_static_objects libunwind-libc-static)
214216
set_target_properties(unwind_static
215217
PROPERTIES
216218
LINK_FLAGS "${LIBUNWIND_LINK_FLAGS}"

0 commit comments

Comments
 (0)