[lld][WebAssembly] Reinstate mistakenly disabled test. NFC
[llvm-project.git] / libcxxabi / src / CMakeLists.txt
blob762649a965ca2e183fb175fcfc2a6e6be49c6771
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 (APPLE)
71   add_library_flags_if(LIBCXXABI_HAS_SYSTEM_LIB System)
72 else()
73   if (LIBCXXABI_ENABLE_THREADS)
74     add_library_flags_if(LIBCXXABI_HAS_PTHREAD_LIB pthread)
75   endif()
77   add_library_flags_if(LIBCXXABI_HAS_C_LIB c)
78 endif()
80 if (LIBCXXABI_USE_COMPILER_RT)
81   find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
82   list(APPEND LIBCXXABI_SHARED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}")
83 endif ()
85 if (LIBCXXABI_USE_LLVM_UNWINDER)
86   # Prefer using the in-tree version of libunwind, either shared or static. If
87   # none are found fall back to using -lunwind.
88   # FIXME: Is it correct to prefer the static version of libunwind?
89   if (NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
90     list(APPEND LIBCXXABI_SHARED_LIBRARIES unwind_shared)
91   elseif (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY AND (TARGET unwind_static OR HAVE_LIBUNWIND))
92     list(APPEND LIBCXXABI_SHARED_LIBRARIES unwind_static)
93   else()
94     list(APPEND LIBCXXABI_SHARED_LIBRARIES unwind)
95   endif()
96   if (NOT LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY AND (TARGET unwind_shared OR HAVE_LIBUNWIND))
97     list(APPEND LIBCXXABI_STATIC_LIBRARIES unwind_shared)
98   elseif (LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY AND (TARGET unwind_static OR HAVE_LIBUNWIND))
99       # We handle this by directly merging libunwind objects into libc++abi.
100   else()
101     list(APPEND LIBCXXABI_STATIC_LIBRARIES unwind)
102   endif()
103 else()
104   add_library_flags_if(LIBCXXABI_HAS_GCC_S_LIB gcc_s)
105 endif()
106 if (MINGW)
107   # MINGW_LIBRARIES is defined in config-ix.cmake
108   list(APPEND LIBCXXABI_LIBRARIES ${MINGW_LIBRARIES})
109 endif()
110 if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
111   list(APPEND LIBCXXABI_LIBRARIES android_support)
112 endif()
114 if (NOT LIBCXXABI_USE_COMPILER_RT)
115   add_library_flags_if(LIBCXXABI_HAS_GCC_LIB gcc)
116 endif ()
118 # Setup flags.
119 if (LIBCXXABI_SUPPORTS_NOSTDLIBXX_FLAG)
120   add_link_flags_if_supported(-nostdlib++)
121 else()
122   add_link_flags_if_supported(-nodefaultlibs)
123 endif()
125 if ( APPLE )
126   if (LLVM_USE_SANITIZER)
127     if (("${LLVM_USE_SANITIZER}" STREQUAL "Address") OR
128         ("${LLVM_USE_SANITIZER}" STREQUAL "Address;Undefined") OR
129         ("${LLVM_USE_SANITIZER}" STREQUAL "Undefined;Address"))
130       set(LIBFILE "libclang_rt.asan_osx_dynamic.dylib")
131     elseif("${LLVM_USE_SANITIZER}" STREQUAL "Undefined")
132       set(LIBFILE "libclang_rt.ubsan_osx_dynamic.dylib")
133     elseif("${LLVM_USE_SANITIZER}" STREQUAL "Thread")
134       set(LIBFILE "libclang_rt.tsan_osx_dynamic.dylib")
135     else()
136       message(WARNING "LLVM_USE_SANITIZER=${LLVM_USE_SANITIZER} is not supported on OS X")
137     endif()
138     if (LIBFILE)
139       find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY)
140       get_filename_component(LIBDIR "${LIBCXXABI_BUILTINS_LIBRARY}" DIRECTORY)
141       if (NOT IS_DIRECTORY "${LIBDIR}")
142         message(FATAL_ERROR "Cannot find compiler-rt directory on OS X required for LLVM_USE_SANITIZER")
143       endif()
144       set(LIBCXXABI_SANITIZER_LIBRARY "${LIBDIR}/${LIBFILE}")
145       set(LIBCXXABI_SANITIZER_LIBRARY "${LIBCXXABI_SANITIZER_LIBRARY}" PARENT_SCOPE)
146       message(STATUS "Manually linking compiler-rt library: ${LIBCXXABI_SANITIZER_LIBRARY}")
147       add_library_flags("${LIBCXXABI_SANITIZER_LIBRARY}")
148       add_link_flags("-Wl,-rpath,${LIBDIR}")
149     endif()
150   endif()
152   # Make sure we link in CrashReporterClient if we find it -- it's used by
153   # abort() on Apple platforms when building the system dylib.
154   find_library(CrashReporterClient NAMES libCrashReporterClient.a
155                                    PATHS "${CMAKE_OSX_SYSROOT}/usr/local/lib")
156   if (CrashReporterClient)
157     message(STATUS "Linking with CrashReporterClient at ${CrashReporterClient}")
158     add_library_flags("${CrashReporterClient}")
159   else()
160     message(STATUS "Could not find CrashReporterClient, not linking against it")
161   endif()
162 endif()
164 split_list(LIBCXXABI_COMPILE_FLAGS)
165 split_list(LIBCXXABI_LINK_FLAGS)
167 # FIXME: libc++abi.so will not link when modules are enabled because it depends
168 # on symbols defined in libc++.so which has not yet been built.
169 if (LLVM_ENABLE_MODULES)
170   string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
171 endif()
173 if (NOT TARGET pstl::ParallelSTL)
174   message(STATUS "Could not find ParallelSTL, libc++abi will not attempt to use it but the build may fail if the libc++ in use needs it to be available.")
175 endif()
177 # Build the shared library.
178 if (LIBCXXABI_ENABLE_SHARED)
179   add_library(cxxabi_shared SHARED ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
180   target_link_libraries(cxxabi_shared PRIVATE cxx-headers ${LIBCXXABI_SHARED_LIBRARIES} ${LIBCXXABI_LIBRARIES})
181   if (TARGET pstl::ParallelSTL)
182     target_link_libraries(cxxabi_shared PUBLIC pstl::ParallelSTL)
183   endif()
184   set_target_properties(cxxabi_shared
185     PROPERTIES
186       CXX_EXTENSIONS OFF
187       CXX_STANDARD 20
188       CXX_STANDARD_REQUIRED OFF
189       COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
190       LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
191       OUTPUT_NAME "c++abi"
192       SOVERSION "1"
193       VERSION "${LIBCXXABI_LIBRARY_VERSION}"
194       DEFINE_SYMBOL ""
195   )
197   list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_shared")
198   if (LIBCXXABI_INSTALL_SHARED_LIBRARY)
199     list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_shared")
200   endif()
202   add_library(cxxabi-reexports INTERFACE)
204   # -exported_symbols_list is only available on Apple platforms
205   if (APPLE)
206     function(export_symbols file)
207       target_link_libraries(cxxabi_shared PRIVATE "-Wl,-exported_symbols_list,${file}")
208     endfunction()
209     function(reexport_symbols file)
210       export_symbols("${file}")
211       target_link_libraries(cxxabi-reexports INTERFACE "-Wl,-reexported_symbols_list,${file}")
212     endfunction()
214     export_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/itanium-base.exp")
216     if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
217       reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/new-delete.exp")
218     endif()
220     if (LIBCXXABI_ENABLE_EXCEPTIONS)
221       reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/exceptions.exp")
223       if ("${CMAKE_OSX_ARCHITECTURES}" MATCHES "^(armv6|armv7|armv7s)$")
224         reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-sjlj.exp")
225       else()
226         reexport_symbols("${CMAKE_CURRENT_SOURCE_DIR}/../lib/personality-v0.exp")
227       endif()
228     endif()
229   endif()
230 endif()
232 # Build the static library.
233 if (LIBCXXABI_ENABLE_STATIC)
234   add_library(cxxabi_static STATIC ${LIBCXXABI_SOURCES} ${LIBCXXABI_HEADERS})
235   target_link_libraries(cxxabi_static PRIVATE cxx-headers ${LIBCXXABI_STATIC_LIBRARIES} ${LIBCXXABI_LIBRARIES})
236   if (TARGET pstl::ParallelSTL)
237     target_link_libraries(cxxabi_static PUBLIC pstl::ParallelSTL)
238   endif()
239   set_target_properties(cxxabi_static
240     PROPERTIES
241       CXX_EXTENSIONS OFF
242       CXX_STANDARD 20
243       CXX_STANDARD_REQUIRED OFF
244       COMPILE_FLAGS "${LIBCXXABI_COMPILE_FLAGS}"
245       LINK_FLAGS "${LIBCXXABI_LINK_FLAGS}"
246       OUTPUT_NAME "c++abi"
247     )
249   if(LIBCXXABI_HERMETIC_STATIC_LIBRARY)
250     append_flags_if_supported(CXXABI_STATIC_LIBRARY_FLAGS -fvisibility=hidden)
251     # If the hermetic library doesn't define the operator new/delete functions
252     # then its code shouldn't declare them with hidden visibility.  They might
253     # actually be provided by a shared library at link time.
254     if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
255       append_flags_if_supported(CXXABI_STATIC_LIBRARY_FLAGS -fvisibility-global-new-delete-hidden)
256     endif()
257     target_compile_options(cxxabi_static PRIVATE ${CXXABI_STATIC_LIBRARY_FLAGS})
258     target_compile_definitions(cxxabi_static
259       PRIVATE
260         _LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS
261         _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
262   endif()
264   list(APPEND LIBCXXABI_BUILD_TARGETS "cxxabi_static")
265   if (LIBCXXABI_INSTALL_STATIC_LIBRARY)
266     list(APPEND LIBCXXABI_INSTALL_TARGETS "cxxabi_static")
267   endif()
269   if (APPLE)
270     set(MERGE_ARCHIVES_LIBTOOL "--use-libtool" "--libtool" "${CMAKE_LIBTOOL}")
271   endif()
273   # Merge the libc++abi.a and libunwind.a into one.
274   if(LIBCXXABI_USE_LLVM_UNWINDER AND LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY)
275     add_custom_command(TARGET cxxabi_static POST_BUILD
276       COMMAND ${Python3_EXECUTABLE} ${LIBCXXABI_LIBCXX_PATH}/utils/merge_archives.py
277       ARGS
278         -o "$<TARGET_LINKER_FILE:cxxabi_static>"
279         --ar "${CMAKE_AR}"
280         ${MERGE_ARCHIVES_LIBTOOL}
281         "$<TARGET_LINKER_FILE:cxxabi_static>"
282         "$<TARGET_LINKER_FILE:unwind_static>"
283       WORKING_DIRECTORY ${LIBCXXABI_BUILD_DIR}
284       DEPENDS unwind_static
285     )
286   endif()
287 endif()
289 # Add a meta-target for both libraries.
290 add_custom_target(cxxabi DEPENDS ${LIBCXXABI_BUILD_TARGETS})
292 if (LIBCXXABI_INSTALL_LIBRARY)
293   install(TARGETS ${LIBCXXABI_INSTALL_TARGETS}
294     LIBRARY DESTINATION ${LIBCXXABI_INSTALL_LIBRARY_DIR} COMPONENT cxxabi
295     ARCHIVE DESTINATION ${LIBCXXABI_INSTALL_LIBRARY_DIR} COMPONENT cxxabi
296     RUNTIME DESTINATION ${LIBCXXABI_INSTALL_RUNTIME_DIR} COMPONENT cxxabi
297     )
298 endif()
300 if (NOT CMAKE_CONFIGURATION_TYPES AND LIBCXXABI_INSTALL_LIBRARY)
301   add_custom_target(install-cxxabi
302     DEPENDS cxxabi
303     COMMAND "${CMAKE_COMMAND}"
304             -DCMAKE_INSTALL_COMPONENT=cxxabi
305             -P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
306   add_custom_target(install-cxxabi-stripped
307     DEPENDS cxxabi
308     COMMAND "${CMAKE_COMMAND}"
309             -DCMAKE_INSTALL_COMPONENT=cxxabi
310             -DCMAKE_INSTALL_DO_STRIP=1
311             -P "${LIBCXXABI_BINARY_DIR}/cmake_install.cmake")
312 endif()