Bump version to 19.1.0 (final)
[llvm-project.git] / libcxxabi / src / CMakeLists.txt
blobc1a7bcb14eb199d5d67889d9d5f8894845c42a87
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 (NOT LIBCXXABI_USE_COMPILER_RT)
79   add_library_flags_if(LIBCXXABI_HAS_GCC_LIB gcc)
80 endif()
81 if (NOT LIBCXXABI_USE_LLVM_UNWINDER)
82   add_library_flags_if(LIBCXXABI_HAS_GCC_S_LIB gcc_s)
83 endif()
85 if (MINGW)
86   # MINGW_LIBRARIES is defined in config-ix.cmake
87   list(APPEND LIBCXXABI_LIBRARIES ${MINGW_LIBRARIES})
88 endif()
89 if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
90   list(APPEND LIBCXXABI_LIBRARIES android_support)
91 endif()
93 # Setup flags.
94 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
95   add_link_flags_if_supported(-nostdlib++)
96 else()
97   add_link_flags_if_supported(-nodefaultlibs)
98 endif()
100 if (CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG AND LIBCXXABI_USE_LLVM_UNWINDER)
101   # If we're linking directly against the libunwind that we're building
102   # in the same invocation, don't try to link in the toolchain's
103   # default libunwind (which may be missing still).
104   add_link_flags_if_supported(--unwindlib=none)
105 endif()
107 if ( APPLE )
108   if (LLVM_USE_SANITIZER)
109     if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
110         ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
111         ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
112       set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
113     elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
114       set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
115     elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
116       set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
117     else()
118       message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
119     endif()
120     if (LIBFILE)
121       find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
122       get_filename_component(LIBDIR "${LIBCXXABI_BUILTINS_LIBRARY}" DIRECTORY)
123       if (NOT IS_DIRECTORY "${LIBDIR}")
124         message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
125       endif()
126       set(LIBCXXABI_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
127       set(LIBCXXABI_SANITIZER_LIBRARY "${LIBCXXABI_SANITIZER_LIBRARY}" PARENT_SCOPE)
128       message(STATUS "Manually linking compiler-rt library: ${LIBCXXABI_SANITIZER_LIBRARY}")
129       add_library_flags("${LIBCXXABI_SANITIZER_LIBRARY}")
130       add_link_flags("-Wl,-rpath,${LIBDIR}")
131     endif()
132   endif()
134   # Make sure we link in CrashReporterClient if we find it -- it's used by
135   # abort() on Apple platforms when building the system dylib.
136   find_library(CrashReporterClient NAMES libCrashReporterClient.a
137                                    PATHS "${CMAKE_OSX_SYSROOT}/usr/local/lib")
138   if (CrashReporterClient)
139     message(STATUS "Linking with CrashReporterClient at ${CrashReporterClient}")
140     add_library_flags("${CrashReporterClient}")
141   else()
142     message(STATUS "Could not find CrashReporterClient, not linking against it")
143   endif()
144 endif()
146 split_list(LIBCXXABI_COMPILE_FLAGS)
147 split_list(LIBCXXABI_LINK_FLAGS)
149 # FIXME: libc++abi.so will not link when modules are enabled because it depends
150 # on symbols defined in libc++.so which has not yet been built.
151 if (LLVM_ENABLE_MODULES)
152   string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
153 endif()
155 include(WarningFlags)
157 # Build the shared library.
158 add_library(cxxabi_shared_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
159 cxx_add_warning_flags(cxxabi_shared_objects ${LIBCXXABI_ENABLE_WERROR} ${LIBCXXABI_ENABLE_PEDANTIC})
160 if (LIBCXXABI_USE_LLVM_UNWINDER)
161   if (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY OR
162       (DEFINED LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_SHARED))
163     target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared_objects) # propagate usage requirements
164     target_sources(cxxabi_shared_objects PUBLIC $<TARGET_OBJECTS:unwind_shared_objects>)
165   else()
166     target_link_libraries(cxxabi_shared_objects PUBLIC unwind_shared)
167   endif()
168 endif()
169 target_link_libraries(cxxabi_shared_objects PRIVATE cxx-headers ${LIBCXXABI_LIBRARIES})
170 if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG)
171   target_link_libraries(cxxabi_shared_objects PRIVATE ${LIBCXXABI_BUILTINS_LIBRARY})
172 endif()
173 target_link_libraries(cxxabi_shared_objects PUBLIC cxxabi-headers)
174 set_target_properties(cxxabi_shared_objects
175   PROPERTIES
176     CXX_EXTENSIONS OFF
177     CXX_STANDARD 23
178     CXX_STANDARD_REQUIRED OFF # TODO: Make this REQUIRED once we don't need to accommodate the LLVM documentation builders using an ancient CMake
179     COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
180     DEFINE_SYMBOL ""
182 if (CMAKE_POSITION_INDEPENDENT_CODE OR NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
183   set_target_properties(cxxabi_shared_objects PROPERTIES POSITION_INDEPENDENT_CODE ON) # must set manually because it's an object library
184 endif()
185 target_compile_options(cxxabi_shared_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
187 if (LIBCXXABI_ENABLE_SHARED)
188   add_library(cxxabi_shared SHARED)
189   set_target_properties(cxxabi_shared
190     PROPERTIES
191       LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
192       OUTPUT_NAME "${LIBCXXABI_SHARED_OUTPUT_NAME}"
193       SOVERSION "1"
194       VERSION "${LIBCXXABI_LIBRARY_VERSION}"
195   )
197   if (ZOS)
198     add_custom_command(TARGET cxxabi_shared POST_BUILD
199       COMMAND
200         ${LIBCXXABI_LIBCXX_PATH}/utils/zos_rename_dll_side_deck.sh
201         $<TARGET_LINKER_FILE_NAME:cxxabi_shared> $<TARGET_FILE_NAME:cxxabi_shared> "${LIBCXXABI_DLL_NAME}"
202       COMMENT "Rename dll name inside the side deck file"
203       WORKING_DIRECTORY $<TARGET_FILE_DIR:cxxabi_shared>
204     )
205   endif ()
207   target_link_libraries(cxxabi_shared
208     PUBLIC cxxabi_shared_objects
209     PRIVATE ${LIBCXXABI_LIBRARIES})
211   list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_shared")
212   if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
213     list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
214   endif()
216   add_library(cxxabi-reexports INTERFACE)
217   function(reexport_symbols file)
218     # -exported_symbols_list is only available on Apple platforms
219     if (APPLE)
220       target_link_libraries(cxxabi_shared PRIVATE "-Wl,-exported_symbols_list,${file}")
221       target_link_libraries(cxxabi-reexports INTERFACE "-Wl,-reexported_symbols_list,${file}")
222     endif()
223   endfunction()
225   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/cxxabiv1.exp")
226   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/fundamental-types.exp")
227   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/itanium-base.exp")
228   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/std-misc.exp")
230   if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
231     reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/new-delete.exp")
232   endif()
234   # Note that std:: exception types are always defined by the library regardless of
235   # whether the exception runtime machinery is provided.
236   reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/std-exceptions.exp")
238   if (LIBCXXABI_ENABLE_EXCEPTIONS)
239     reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/itanium-exceptions.exp")
241     if ("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^(armv6|armv7|armv7s)$")
242       reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-sjlj.exp")
243     else()
244       reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-v0.exp")
245     endif()
246   endif()
247 endif()
249 # Build the static library.
250 add_library(cxxabi_static_objects OBJECT EXCLUDE_FROM_ALL ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
251 cxx_add_warning_flags(cxxabi_static_objects ${LIBCXXABI_ENABLE_WERROR} ${LIBCXXABI_ENABLE_PEDANTIC})
252 if (LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
253   target_link_libraries(cxxabi_static_objects PUBLIC unwind_static_objects) # propagate usage requirements
254   target_sources(cxxabi_static_objects PUBLIC $<TARGET_OBJECTS:unwind_static_objects>)
255 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)
258 set_target_properties(cxxabi_static_objects
259   PROPERTIES
260     CXX_EXTENSIONS OFF
261     CXX_STANDARD 23
262     CXX_STANDARD_REQUIRED OFF # TODO: Make this REQUIRED once we don't need to accommodate the LLVM documentation builders using an ancient CMake
263     COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
265 target_compile_options(cxxabi_static_objects PRIVATE "${LIBCXXABI_ADDITIONAL_COMPILE_FLAGS}")
267 if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
268   target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility=hidden)
269   # If the hermetic library doesn't define the operator new/delete functions
270   # then its code shouldn't declare them with hidden visibility.  They might
271   # actually be provided by a shared library at link time.
272   if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
273     target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility-global-new-delete=force-hidden)
274     if (NOT CXX_SUPPORTS_FVISIBILITY_GLOBAL_NEW_DELETE_EQ_FORCE_HIDDEN_FLAG)
275       target_add_compile_flags_if_supported(cxxabi_static_objects PRIVATE -fvisibility-global-new-delete-hidden)
276     endif()
277   endif()
278   # _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS can be defined in libcxx's
279   # __config_site too. Define it in the same way here, to avoid redefinition
280   # conflicts.
281   target_compile_definitions(cxxabi_static_objects
282     PRIVATE
283       _LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS
284       _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS=)
285 endif()
287 if (LIBCXXABI_ENABLE_STATIC)
288   add_library(cxxabi_static STATIC)
289   if (LIBCXXABI_USE_LLVM_UNWINDER AND NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
290     target_link_libraries(cxxabi_static PUBLIC unwind_static)
291   endif()
292   set_target_properties(cxxabi_static
293     PROPERTIES
294       LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
295       OUTPUT_NAME "${LIBCXXABI_STATIC_OUTPUT_NAME}"
296     )
297   target_link_libraries(cxxabi_static
298     PUBLIC cxxabi_static_objects
299     PRIVATE ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
301   list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_static")
302   if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
303     list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
304   endif()
305 endif()
307 # Add a meta-target for both libraries.
308 add_custom_target(cxxabi DEPENDS ${LIBCXXABI_BUILD_TARGETS})
309 add_dependencies(cxxabi-test-depends cxxabi cxx)
311 if (LIBCXXABI_INSTALL_LIBRARY)
312   install(TARGETS ${LIBCXXABI_INSTALL_TARGETS}
313     LIBRARY DESTINATION ${LIBCXXABI_INSTALL_LIBRARY_DIR} COMPONENT cxxabi
314     ARCHIVE DESTINATION ${LIBCXXABI_INSTALL_LIBRARY_DIR} COMPONENT cxxabi
315     RUNTIME DESTINATION ${LIBCXXABI_INSTALL_RUNTIME_DIR} COMPONENT cxxabi
316     )
317 endif()
319 if (NOT CMAKE_CONFIGURATION_TYPES AND LIBCXXABI_INSTALL_LIBRARY)
320   add_custom_target(install-cxxabi
321     DEPENDS cxxabi install-cxxabi-headers
322     COMMAND "${CMAKE_COMMAND}"
323             -DCMAKE_INSTALL_COMPONENT=cxxabi
324             -P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
325   add_custom_target(install-cxxabi-stripped
326     DEPENDS cxxabi
327     COMMAND "${CMAKE_COMMAND}"
328             -DCMAKE_INSTALL_COMPONENT=cxxabi
329             -DCMAKE_INSTALL_DO_STRIP=1
330             -P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
331 endif()