[gn build] Port 077cc3deeebe
[llvm-project.git] / libcxxabi / src / CMakeLists.txt
blob21dda44e09976acff62885dba9fe3230ed584f17
1 # Get sources
2 set(LIBCXXABI_SOURCES
3   # C++ABI files
4   cxa_aux_runtime.cpp
5   cxa_default_handlers.cpp
6   cxa_demangle.cpp
7   cxa_exception_storage.cpp
8   cxa_guard.cpp
9   cxa_handlers.cpp
10   cxa_vector.cpp
11   cxa_virtual.cpp
12   # C++ STL files
13   stdlib_exception.cpp
14   stdlib_stdexcept.cpp
15   stdlib_typeinfo.cpp
16   # Internal files
17   abort_message.cpp
18   fallback_malloc.cpp
19   private_typeinfo.cpp
22 if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
23   list(APPEND LIBCXXABI_SOURCES
24     stdlib_new_delete.cpp
25   )
26 endif()
28 if (LIBCXXABI_ENABLE_EXCEPTIONS)
29   list(APPEND LIBCXXABI_SOURCES
30     cxa_exception.cpp
31     cxa_personality.cpp
32   )
33 else()
34   list(APPEND LIBCXXABI_SOURCES
35     cxa_noexception.cpp
36   )
37 endif()
39 if (LIBCXXABI_ENABLE_THREADS AND (UNIX OR FUCHSIA) AND NOT (APPLE OR CYGWIN)
40     AND NOT (${CMAKE_SYSTEM_NAME} MATCHES "AIX"))
41   list(APPEND LIBCXXABI_SOURCES
42     cxa_thread_atexit.cpp
43   )
44 endif()
46 set(LIBCXXABI_HEADERS
47   ../include/cxxabi.h
50 # Add all the headers to the project for IDEs.
51 if (MSVC_IDE OR XCODE)
52   # Force them all into the headers dir on MSVC, otherwise they end up at
53   # project scope because they don't have extensions.
54   if (MSVC_IDE)
55     source_group("Header Files" FILES ${LIBCXXABI_HEADERS})
56   endif()
57 endif()
59 # Some files depend on libc++ internals.
60 include_directories("${LIBCXXABI_LIBCXX_PATH}/src")
62 if (LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
63   add_definitions(-DHAVE___CXA_THREAD_ATEXIT_IMPL)
64 endif()
66 if (LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST)
67   add_definitions(-D_LIBCXXABI_FORGIVING_DYNAMIC_CAST)
68 endif()
70 if (NOT APPLE) # On Apple platforms, we always use -nostdlib++ so we don't need to re-add other libraries
71   if (LIBCXXABI_ENABLE_THREADS)
72     add_library_flags_if(LIBCXXABI_HAS_PTHREAD_LIB pthread)
73   endif()
75   add_library_flags_if(LIBCXXABI_HAS_C_LIB c)
76 endif()
78 if (FUCHSIA)
79     # TODO: Use CMAKE_LINK_LIBRARY_USING_FEATURE once our minimum CMake is at least 3.24
80     # https://cmake.org/cmake/help/latest/variable/CMAKE_LINK_LIBRARY_USING_FEATURE.html 
81     add_link_flags("-Wl,--push-state,--as-needed,-lzircon,--pop-state")
82 endif()
84 if (NOT LIBCXXABI_USE_COMPILER_RT)
85   add_library_flags_if(LIBCXXABI_HAS_GCC_LIB gcc)
86 endif()
87 if (NOT LIBCXXABI_USE_LLVM_UNWINDER)
88   add_library_flags_if(LIBCXXABI_HAS_GCC_S_LIB gcc_s)
89 endif()
91 if (MINGW)
92   # MINGW_LIBRARIES is defined in config-ix.cmake
93   list(APPEND LIBCXXABI_LIBRARIES ${MINGW_LIBRARIES})
94 endif()
95 if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
96   list(APPEND LIBCXXABI_LIBRARIES android_support)
97 endif()
99 # Setup flags.
100 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
101   add_link_flags_if_supported(-nostdlib++)
102 else()
103   add_link_flags_if_supported(-nodefaultlibs)
104 endif()
106 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG AND LIBCXXABI_USE_LLVM_UNWINDER)
107   # If we're linking directly against the libunwind that we're building
108   # in the same invocation, don't try to link in the toolchain's
109   # default libunwind (which may be missing still).
110   add_link_flags_if_supported(--unwindlib=none)
111 endif()
113 if ( APPLE )
114   if (LLVM_USE_SANITIZER)
115     if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
116         ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
117         ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
118       set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
119     elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
120       set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
121     elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
122       set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
123     else()
124       message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
125     endif()
126     if (LIBFILE)
127       find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
128       get_filename_component(LIBDIR "${LIBCXXABI_BUILTINS_LIBRARY}" DIRECTORY)
129       if (NOT IS_DIRECTORY "${LIBDIR}")
130         message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
131       endif()
132       set(LIBCXXABI_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
133       set(LIBCXXABI_SANITIZER_LIBRARY "${LIBCXXABI_SANITIZER_LIBRARY}" PARENT_SCOPE)
134       message(STATUS "Manually linking compiler-rt library: ${LIBCXXABI_SANITIZER_LIBRARY}")
135       add_library_flags("${LIBCXXABI_SANITIZER_LIBRARY}")
136       add_link_flags("-Wl,-rpath,${LIBDIR}")
137     endif()
138   endif()
140   # Make sure we link in CrashReporterClient if we find it -- it's used by
141   # abort() on Apple platforms when building the system dylib.
142   find_library(CrashReporterClient NAMES libCrashReporterClient.a
143                                    PATHS "${CMAKE_OSX_SYSROOT}/usr/local/lib")
144   if (CrashReporterClient)
145     message(STATUS "Linking with CrashReporterClient at ${CrashReporterClient}")
146     add_library_flags("${CrashReporterClient}")
147   else()
148     message(STATUS "Could not find CrashReporterClient, not linking against it")
149   endif()
150 endif()
152 split_list(LIBCXXABI_COMPILE_FLAGS)
153 split_list(LIBCXXABI_LINK_FLAGS)
155 # FIXME: libc++abi.so will not link when modules are enabled because it depends
156 # on symbols defined in libc++.so which has not yet been built.
157 if (LLVM_ENABLE_MODULES)
158   string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
159 endif()
161 include(WarningFlags)
163 # Build the shared library.
164 add_library(cxxabi_shared_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
165 cxx_add_warning_flags(cxxabi_shared_objects ${LIBCXXABI_ENABLE_WERROR} ${LIBCXXABI_ENABLE_PEDANTIC})
166 if (LIBCXXABI_USE_LLVM_UNWINDER)
167   if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY OR
168       (DEFINED LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_SHARED))
169     target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared_objects) # propagate usage requirements
170     target_sources(cxxabi_shared_objects PUBLIC $<TARGET_OBJECTS:unwind_shared_objects>)
171   else()
172     target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
173   endif()
174 endif()
175 target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
176 if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
177   target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
178 endif()
179 target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
180 set_target_properties(cxxabi_shared_objects
181   PROPERTIES
182     CXX_EXTENSIONS OFF
183     CXX_STANDARD 23
184     CXX_STANDARD_REQUIRED OFF # TODO: Make this REQUIRED once we don't need to accommodate the LLVM documentation builders using an ancient CMake
185     COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
186     DEFINE_SYMBOL ""
188 if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
189   set_target_properties(cxxabi_shared_objects PROPERTIES POSITION_INDEPENDENT_CODE ON) # must set manually because it's an object library
190 endif()
191 target_compile_options(cxxabi_shared_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
193 add_library(cxxabi_shared SHARED)
194 set_target_properties(cxxabi_shared
195   PROPERTIES
196     EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBCXXABI_ENABLE_SHARED}>,FALSE,TRUE>"
197     LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
198     OUTPUT_NAME "${LIBCXXABI_SHARED_OUTPUT_NAME}"
199     SOVERSION "1"
200     VERSION "${LIBCXXABI_LIBRARY_VERSION}"
203 if (ZOS)
204   add_custom_command(TARGET cxxabi_shared POST_BUILD
205     COMMAND
206       ${LIBCXXABI_LIBCXX_PATH}/utils/zos_rename_dll_side_deck.sh
207       $<TARGET_LINKER_FILE_NAME:cxxabi_shared> $<TARGET_FILE_NAME:cxxabi_shared> "${LIBCXXABI_DLL_NAME}"
208     COMMENT "Rename dll name inside the side deck file"
209     WORKING_DIRECTORY $<TARGET_FILE_DIR:cxxabi_shared>
210   )
211 endif ()
213 target_link_libraries(cxxabi_shared
214   PUBLIC cxxabi_shared_objects
215   PRIVATE ${LIBCXXABI_LIBRARIES})
217 if (LIBCXXABI_ENABLE_SHARED)
218 list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_shared")
219 endif()
220 if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
221 list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
222 endif()
224 # TODO: Move this to libc++'s HandleLibCXXABI.cmake since this is effectively trying to control
225 #       what libc++ re-exports.
226 add_library(cxxabi-reexports INTERFACE)
227 function(export_symbols file)
228   # -exported_symbols_list is only available on Apple platforms
229   if (APPLE)
230     target_link_libraries(cxxabi_shared PRIVATE "-Wl,-exported_symbols_list,${file}")
231   endif()
232 endfunction()
234 function(reexport_symbols file)
235   export_symbols("${file}")
236   # -reexported_symbols_list is only available on Apple platforms
237   if (APPLE)
238     target_link_libraries(cxxabi-reexports INTERFACE "-Wl,-reexported_symbols_list,${file}")
239   endif()
240 endfunction()
242 export_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/symbols-not-reexported.exp")
243 reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/cxxabiv1.exp")
244 reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/fundamental-types.exp")
245 reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/itanium-base.exp")
246 reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/std-misc.exp")
248 if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
249   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/new-delete.exp")
250 endif()
252 # Note that std:: exception types are always defined by the library regardless of
253 # whether the exception runtime machinery is provided.
254 reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/std-exceptions.exp")
256 if (LIBCXXABI_ENABLE_EXCEPTIONS)
257   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/itanium-exceptions.exp")
259   if ("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^(armv6|armv7|armv7s)$")
260     reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-sjlj.exp")
261   else()
262     reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-v0.exp")
263   endif()
264 endif()
266 # Build the static library.
267 add_library(cxxabi_static_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
268 cxx_add_warning_flags(cxxabi_static_objects ${LIBCXXABI_ENABLE_WERROR} ${LIBCXXABI_ENABLE_PEDANTIC})
269 if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
270   target_link_libraries(cxxabi_static_objects PUBLIC unwind_static_objects) # propagate usage requirements
271   target_sources(cxxabi_static_objects PUBLIC $<TARGET_OBJECTS:unwind_static_objects>)
272 endif()
273 target_link_libraries(cxxabi_static_objects PRIVATE cxx-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
274 target_link_libraries(cxxabi_static_objects PUBLIC cxxabi-headers)
275 set_target_properties(cxxabi_static_objects
276   PROPERTIES
277     CXX_EXTENSIONS OFF
278     CXX_STANDARD 23
279     CXX_STANDARD_REQUIRED OFF # TODO: Make this REQUIRED once we don't need to accommodate the LLVM documentation builders using an ancient CMake
280     COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
282 target_compile_options(cxxabi_static_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
284 if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
285   target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility=hidden)
286   # If the hermetic library doesn't define the operator new/delete functions
287   # then its code shouldn't declare them with hidden visibility.  They might
288   # actually be provided by a shared library at link time.
289   if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
290     target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility-global-new-delete=force-hidden)
291     if (NOT CXX_SUPPORTS_FVISIBILITY_GLOBAL_NEW_DELETE_EQ_FORCE_HIDDEN_FLAG)
292       target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility-global-new-delete-hidden)
293     endif()
294   endif()
295   # _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in libcxx's
296   # __config_site too. Define it in the same way here, to avoid redefinition
297   # conflicts.
298   target_compile_definitions(cxxabi_static_objects
299     PRIVATE
300       _LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS
301       _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
302 endif()
304 add_library(cxxabi_static STATIC)
305 if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
306   target_link_libraries(cxxabi_static PUBLIC unwind_static)
307 endif()
308 set_target_properties(cxxabi_static
309   PROPERTIES
310     EXCLUDE_FROM_ALL "$<IF:$<BOOL:${LIBCXXABI_ENABLE_STATIC}>,FALSE,TRUE>"
311     LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
312     OUTPUT_NAME "${LIBCXXABI_STATIC_OUTPUT_NAME}"
313   )
314 target_link_libraries(cxxabi_static
315   PUBLIC cxxabi_static_objects
316   PRIVATE ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
318 if (LIBCXXABI_ENABLE_STATIC)
319   list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_static")
320 endif()
321 if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
322   list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
323 endif()
325 # Add a meta-target for both libraries.
326 add_custom_target(cxxabi DEPENDS ${LIBCXXABI_BUILD_TARGETS})
328 if (LIBCXXABI_INSTALL_LIBRARY)
329   install(TARGETS ${LIBCXXABI_INSTALL_TARGETS}
330     LIBRARY DESTINATION ${LIBCXXABI_INSTALL_LIBRARY_DIR} COMPONENT cxxabi
331     ARCHIVE DESTINATION ${LIBCXXABI_INSTALL_LIBRARY_DIR} COMPONENT cxxabi
332     RUNTIME DESTINATION ${LIBCXXABI_INSTALL_RUNTIME_DIR} COMPONENT cxxabi
333     )
334 endif()
336 if (NOT CMAKE_CONFIGURATION_TYPES AND LIBCXXABI_INSTALL_LIBRARY)
337   add_custom_target(install-cxxabi
338     DEPENDS cxxabi install-cxxabi-headers
339     COMMAND "${CMAKE_COMMAND}"
340             -DCMAKE_INSTALL_COMPONENT=cxxabi
341             -P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
342   add_custom_target(install-cxxabi-stripped
343     DEPENDS cxxabi
344     COMMAND "${CMAKE_COMMAND}"
345             -DCMAKE_INSTALL_COMPONENT=cxxabi
346             -DCMAKE_INSTALL_DO_STRIP=1
347             -P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
348 endif()