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