[BuildLibcalls, Attrs] Support more variants of C++'s new, add attributes for C+...
[lldb.git] / libcxxabi / CMakeLists.txt
1 # See www/CMake.html for instructions on how to build libcxxabi with CMake.
2
3 if (NOT IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libcxx")
4   message(FATAL_ERROR "libc++abi now requires being built in a monorepo layout with libcxx available")
5 endif()
6
7 #===============================================================================
8 # Setup Project
9 #===============================================================================
10
11 cmake_minimum_required(VERSION 3.13.4)
12
13 # Add path for custom modules
14 set(CMAKE_MODULE_PATH
15   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
16   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
17   ${CMAKE_MODULE_PATH}
18   )
19
20 set(LIBCXXABI_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
21 set(LIBCXXABI_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
22 set(LIBCXXABI_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
23         "Specify path to libc++ source.")
24
25 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBCXXABI_STANDALONE_BUILD)
26   project(libcxxabi CXX C)
27
28   set(PACKAGE_NAME libcxxabi)
29   set(PACKAGE_VERSION 11.0.0git)
30   set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
31   set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
32
33   # Add the CMake module path of libcxx so we can reuse HandleOutOfTreeLLVM.cmake
34   set(LIBCXXABI_LIBCXX_CMAKE_PATH "${LIBCXXABI_LIBCXX_PATH}/cmake/Modules")
35   list(APPEND CMAKE_MODULE_PATH "${LIBCXXABI_LIBCXX_CMAKE_PATH}")
36
37   # In a standalone build, we don't have llvm to automatically generate the
38   # llvm-lit script for us.  So we need to provide an explicit directory that
39   # the configurator should write the script into.
40   set(LIBCXXABI_STANDALONE_BUILD 1)
41   set(LLVM_LIT_OUTPUT_DIR "${LIBCXXABI_BINARY_DIR}/bin")
42
43   # Find the LLVM sources and simulate LLVM CMake options.
44   include(HandleOutOfTreeLLVM)
45 endif()
46
47 if (LIBCXXABI_STANDALONE_BUILD)
48   find_package(Python3 COMPONENTS Interpreter)
49   if(NOT Python3_Interpreter_FOUND)
50     message(WARNING "Python3 not found, using python2 as a fallback")
51     find_package(Python2 COMPONENTS Interpreter REQUIRED)
52     if(Python2_VERSION VERSION_LESS 2.7)
53       message(SEND_ERROR "Python 2.7 or newer is required")
54     endif()
55
56     # Treat python2 as python3
57     add_executable(Python3::Interpreter IMPORTED)
58     set_target_properties(Python3::Interpreter PROPERTIES
59       IMPORTED_LOCATION ${Python2_EXECUTABLE})
60     set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
61   endif()
62 endif()
63
64 # Require out of source build.
65 include(MacroEnsureOutOfSourceBuild)
66 MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
67  "${PROJECT_NAME} requires an out of source build. Please create a separate
68  build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
69  )
70
71 #===============================================================================
72 # Setup CMake Options
73 #===============================================================================
74 include(CMakeDependentOption)
75 include(HandleCompilerRT)
76
77 # Define options.
78 option(LIBCXXABI_ENABLE_EXCEPTIONS
79   "Provide support for exceptions in the runtime.
80   When disabled, libc++abi does not support stack unwinding and other exceptions-related features." ON)
81 option(LIBCXXABI_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
82 option(LIBCXXABI_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
83 option(LIBCXXABI_ENABLE_PIC "Build Position-Independent Code, even in static library" ON)
84 option(LIBCXXABI_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
85 option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF)
86 option(LIBCXXABI_ENABLE_STATIC_UNWINDER "Statically link the LLVM unwinder." OFF)
87 option(LIBCXXABI_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
88 option(LIBCXXABI_ENABLE_THREADS "Build with threads enabled" ON)
89 option(LIBCXXABI_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
90 option(LIBCXXABI_HAS_EXTERNAL_THREAD_API
91   "Build libc++abi with an externalized threading API.
92   This option may only be set to ON when LIBCXXABI_ENABLE_THREADS=ON." OFF)
93 option(LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY
94   "Build libc++abi with an externalized threading library.
95    This option may only be set to ON when LIBCXXABI_ENABLE_THREADS=ON" OFF)
96 option(LIBCXXABI_ENABLE_FORGIVING_DYNAMIC_CAST
97 "Make dynamic_cast more forgiving when type_info's mistakenly have hidden \
98 visibility, and thus multiple type_infos can exist for a single type. \
99 When the dynamic_cast would normally fail, this option will cause the \
100 library to try comparing the type_info names to see if they are equal \
101 instead." OFF)
102
103 option(LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS
104   "Build libc++abi with definitions for operator new/delete. These are normally
105    defined in libc++abi, but it is also possible to define them in libc++, in
106    which case the definition in libc++abi should be turned off." ON)
107 option(LIBCXXABI_BUILD_32_BITS "Build 32 bit libc++abi." ${LLVM_BUILD_32_BITS})
108 option(LIBCXXABI_INCLUDE_TESTS "Generate build targets for the libc++abi unit tests." ${LLVM_INCLUDE_TESTS})
109 set(LIBCXXABI_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
110     "Define suffix of library directory name (32/64)")
111 option(LIBCXXABI_INSTALL_LIBRARY "Install the libc++abi library." ON)
112 set(LIBCXXABI_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling.")
113 set(LIBCXXABI_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
114 set(LIBCXXABI_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
115 set(LIBCXXABI_LIBCXX_LIBRARY_PATH "" CACHE PATH "The path to libc++ library.")
116 set(LIBCXXABI_LIBRARY_VERSION "1.0" CACHE STRING
117 "Version of libc++abi. This will be reflected in the name of the shared \
118 library produced. For example, -DLIBCXXABI_LIBRARY_VERSION=x.y will \
119 result in the library being named libc++abi.x.y.dylib, along with the \
120 usual symlinks pointing to that.")
121
122 # Default to building a shared library so that the default options still test
123 # the libc++abi that is being built. There are two problems with testing a
124 # static libc++abi. In the case of a standalone build, the tests will link the
125 # system's libc++, which might not have been built against our libc++abi. In the
126 # case of an in tree build, libc++ will prefer a dynamic libc++abi from the
127 # system over a static libc++abi from the output directory.
128 option(LIBCXXABI_ENABLE_SHARED "Build libc++abi as a shared library." ON)
129 option(LIBCXXABI_ENABLE_STATIC "Build libc++abi as a static library." ON)
130
131 cmake_dependent_option(LIBCXXABI_INSTALL_STATIC_LIBRARY
132   "Install the static libc++abi library." ON
133   "LIBCXXABI_ENABLE_STATIC;LIBCXXABI_INSTALL_LIBRARY" OFF)
134 cmake_dependent_option(LIBCXXABI_INSTALL_SHARED_LIBRARY
135   "Install the shared libc++abi library." ON
136   "LIBCXXABI_ENABLE_SHARED;LIBCXXABI_INSTALL_LIBRARY" OFF)
137
138 cmake_dependent_option(LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_STATIC_LIBRARY
139   "Statically link the LLVM unwinder to static library" ON
140   "LIBCXXABI_ENABLE_STATIC_UNWINDER;LIBCXXABI_ENABLE_STATIC" OFF)
141 cmake_dependent_option(LIBCXXABI_STATICALLY_LINK_UNWINDER_IN_SHARED_LIBRARY
142   "Statically link the LLVM unwinder to shared library" ON
143   "LIBCXXABI_ENABLE_STATIC_UNWINDER;LIBCXXABI_ENABLE_SHARED" OFF)
144
145 option(LIBCXXABI_BAREMETAL "Build libc++abi for baremetal targets." OFF)
146 # The default terminate handler attempts to demangle uncaught exceptions, which
147 # causes extra I/O and demangling code to be pulled in.
148 option(LIBCXXABI_SILENT_TERMINATE "Set this to make the terminate handler default to a silent alternative" OFF)
149
150 if (NOT LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_STATIC)
151   message(FATAL_ERROR "libc++abi must be built as either a shared or static library.")
152 endif()
153
154 set(LIBCXXABI_LIBCXX_INCLUDES "${LIBCXXABI_LIBCXX_PATH}/include" CACHE PATH
155     "Specify path to libc++ includes.")
156 message(STATUS "Libc++abi will be using libc++ includes from ${LIBCXXABI_LIBCXX_INCLUDES}")
157
158 option(LIBCXXABI_HERMETIC_STATIC_LIBRARY
159   "Do not export any symbols from the static library." OFF)
160
161 set(LIBCXXABI_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/lit.site.cfg.in" CACHE STRING
162     "The Lit testing configuration to use when running the tests.")
163 set(LIBCXXABI_TEST_PARAMS "" CACHE STRING
164     "A list of parameters to run the Lit test suite with.")
165
166 #===============================================================================
167 # Configure System
168 #===============================================================================
169
170 # Add path for custom modules
171 set(CMAKE_MODULE_PATH
172   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
173   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
174   ${CMAKE_MODULE_PATH}
175   )
176
177 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
178   set(LIBCXXABI_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++)
179   set(LIBCXXABI_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++)
180   if(LIBCXX_LIBDIR_SUBDIR)
181     string(APPEND LIBCXXABI_LIBRARY_DIR /${LIBCXXABI_LIBDIR_SUBDIR})
182     string(APPEND LIBCXXABI_INSTALL_LIBRARY_DIR /${LIBCXXABI_LIBDIR_SUBDIR})
183   endif()
184 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
185   set(LIBCXXABI_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
186   set(LIBCXXABI_INSTALL_LIBRARY_DIR lib${LIBCXXABI_LIBDIR_SUFFIX})
187 else()
188   set(LIBCXXABI_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXXABI_LIBDIR_SUFFIX})
189   set(LIBCXXABI_INSTALL_LIBRARY_DIR lib${LIBCXXABI_LIBDIR_SUFFIX})
190 endif()
191
192 set(LIBCXXABI_INSTALL_PREFIX "" CACHE STRING "Define libc++abi destination prefix.")
193
194 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXXABI_LIBRARY_DIR})
195 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXXABI_LIBRARY_DIR})
196 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBCXXABI_LIBRARY_DIR})
197
198 # By default, for non-standalone builds, libcxx and libcxxabi share a library
199 # directory.
200 if (NOT LIBCXXABI_LIBCXX_LIBRARY_PATH)
201   set(LIBCXXABI_LIBCXX_LIBRARY_PATH "${LIBCXXABI_LIBRARY_DIR}" CACHE PATH
202       "The path to libc++ library." FORCE)
203 endif()
204
205 # Check that we can build with 32 bits if requested.
206 if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
207   if (LIBCXXABI_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
208     message(STATUS "Building 32 bits executables and libraries.")
209   endif()
210 elseif(LIBCXXABI_BUILD_32_BITS)
211   message(FATAL_ERROR "LIBCXXABI_BUILD_32_BITS=ON is not supported on this platform.")
212 endif()
213
214 # Declare libc++abi configuration variables.
215 # They are intended for use as follows:
216 # LIBCXXABI_C_FLAGS: General flags for both the c++ compiler and linker.
217 # LIBCXXABI_CXX_FLAGS: General flags for both the c++ compiler and linker.
218 # LIBCXXABI_COMPILE_FLAGS: Compile only flags.
219 # LIBCXXABI_LINK_FLAGS: Linker only flags.
220 # LIBCXXABI_LIBRARIES: libraries libc++abi is linked to.
221
222 set(LIBCXXABI_C_FLAGS "")
223 set(LIBCXXABI_CXX_FLAGS "")
224 set(LIBCXXABI_COMPILE_FLAGS "")
225 set(LIBCXXABI_LINK_FLAGS "")
226 set(LIBCXXABI_LIBRARIES "")
227
228 # Include macros for adding and removing libc++abi flags.
229 include(HandleLibcxxabiFlags)
230
231 #===============================================================================
232 # Setup Compiler Flags
233 #===============================================================================
234
235 # Configure target flags
236 add_target_flags_if(LIBCXXABI_BUILD_32_BITS "-m32")
237
238 if(LIBCXXABI_TARGET_TRIPLE)
239   add_target_flags("--target=${LIBCXXABI_TARGET_TRIPLE}")
240 elseif(CMAKE_CXX_COMPILER_TARGET)
241   set(LIBCXXABI_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
242 endif()
243 if(LIBCXX_GCC_TOOLCHAIN)
244   add_target_flags("--gcc-toolchain=${LIBCXXABI_GCC_TOOLCHAIN}")
245 elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
246   set(LIBCXXABI_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
247 endif()
248 if(LIBCXXABI_SYSROOT)
249   add_target_flags("--sysroot=${LIBCXXABI_SYSROOT}")
250 elseif(CMAKE_SYSROOT)
251   set(LIBCXXABI_SYSROOT "${CMAKE_SYSROOT}")
252 endif()
253
254 if (LIBCXXABI_TARGET_TRIPLE)
255   set(TARGET_TRIPLE "${LIBCXXABI_TARGET_TRIPLE}")
256 endif()
257
258 # Configure compiler. Must happen after setting the target flags.
259 include(config-ix)
260
261 if (LIBCXXABI_HAS_NOSTDINCXX_FLAG)
262   list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
263   # cmake 3.14 and above remove system include paths that are explicitly
264   # passed on the command line.  We build with -nostdinc++ and explicitly add
265   # just the libcxx system include paths with -I on the command line.
266   # Setting CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES effectively prevents cmake
267   # from removing these.
268   # See: https://gitlab.kitware.com/cmake/cmake/issues/19227
269   set(CMAKE_CXX_IMPLICIT_INCLUDE_DIRECTORIES "")
270   # Remove -stdlib flags to prevent them from causing an unused flag warning.
271   string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
272   string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
273   string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
274   string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
275 endif()
276
277 # Let the library headers know they are currently being used to build the
278 # library.
279 add_definitions(-D_LIBCXXABI_BUILDING_LIBRARY)
280
281 # libcxxabi needs to, for various reasons, include the libcpp headers as if
282 # it is being built as part of libcxx.
283 add_definitions(-D_LIBCPP_BUILDING_LIBRARY)
284
285 # Disable DLL annotations on Windows for static builds.
286 if (WIN32 AND LIBCXXABI_ENABLE_STATIC AND NOT LIBCXXABI_ENABLE_SHARED)
287   if (LIBCXX_ENABLE_SHARED AND LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
288     # Building libcxxabi statically, but intending for it to be statically
289     # linked into a shared libcxx; keep dllexport enabled within libcxxabi,
290     # as the symbols will need to be exported from libcxx.
291   else()
292     # Regular static build; disable dllexports.
293     add_definitions(-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS)
294   endif()
295 endif()
296
297 add_compile_flags_if_supported(-Werror=return-type)
298
299 # Get warning flags
300 add_compile_flags_if_supported(-W)
301 add_compile_flags_if_supported(-Wall)
302 add_compile_flags_if_supported(-Wchar-subscripts)
303 add_compile_flags_if_supported(-Wconversion)
304 add_compile_flags_if_supported(-Wmismatched-tags)
305 add_compile_flags_if_supported(-Wmissing-braces)
306 add_compile_flags_if_supported(-Wnewline-eof)
307 add_compile_flags_if_supported(-Wunused-function)
308 add_compile_flags_if_supported(-Wshadow)
309 add_compile_flags_if_supported(-Wshorten-64-to-32)
310 add_compile_flags_if_supported(-Wsign-compare)
311 add_compile_flags_if_supported(-Wsign-conversion)
312 add_compile_flags_if_supported(-Wstrict-aliasing=2)
313 add_compile_flags_if_supported(-Wstrict-overflow=4)
314 add_compile_flags_if_supported(-Wunused-parameter)
315 add_compile_flags_if_supported(-Wunused-variable)
316 add_compile_flags_if_supported(-Wwrite-strings)
317 add_compile_flags_if_supported(-Wundef)
318
319 add_compile_flags_if_supported(-Wno-suggest-override)
320
321 if (LIBCXXABI_ENABLE_WERROR)
322   add_compile_flags_if_supported(-Werror)
323   add_compile_flags_if_supported(-WX)
324 else()
325   add_compile_flags_if_supported(-Wno-error)
326   add_compile_flags_if_supported(-WX-)
327 endif()
328 if (LIBCXXABI_ENABLE_PEDANTIC)
329   add_compile_flags_if_supported(-pedantic)
330 endif()
331
332 # Get feature flags.
333 add_compile_flags_if_supported(-fstrict-aliasing)
334
335 # Exceptions
336 if (LIBCXXABI_ENABLE_EXCEPTIONS)
337   # Catches C++ exceptions only and tells the compiler to assume that extern C
338   # functions never throw a C++ exception.
339   add_compile_flags_if_supported(-EHsc)
340   # Do we really need to be run through the C compiler ?
341   add_c_compile_flags_if_supported(-funwind-tables)
342 else()
343   add_compile_flags_if_supported(-fno-exceptions)
344   add_compile_flags_if_supported(-EHs-)
345   add_compile_flags_if_supported(-EHa-)
346 endif()
347
348 # Assert
349 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
350 if (LIBCXXABI_ENABLE_ASSERTIONS)
351   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
352   if (NOT MSVC)
353     list(APPEND LIBCXXABI_COMPILE_FLAGS -D_DEBUG)
354   endif()
355   # On Release builds cmake automatically defines NDEBUG, so we
356   # explicitly undefine it:
357   if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
358     list(APPEND LIBCXXABI_COMPILE_FLAGS -UNDEBUG)
359   endif()
360 else()
361   if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
362     list(APPEND LIBCXXABI_COMPILE_FLAGS -DNDEBUG)
363   endif()
364 endif()
365
366 # Threading
367 if (NOT LIBCXXABI_ENABLE_THREADS)
368   if (LIBCXXABI_HAS_PTHREAD_API)
369     message(FATAL_ERROR "LIBCXXABI_HAS_PTHREAD_API can only"
370                         " be set to ON when LIBCXXABI_ENABLE_THREADS"
371                         " is also set to ON.")
372   endif()
373   if (LIBCXXABI_HAS_EXTERNAL_THREAD_API)
374     message(FATAL_ERROR "LIBCXXABI_HAS_EXTERNAL_THREAD_API can only"
375                         " be set to ON when LIBCXXABI_ENABLE_THREADS"
376                         " is also set to ON.")
377   endif()
378   if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
379     message(FATAL_ERROR "LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY can only"
380                         " be set to ON when LIBCXXABI_ENABLE_THREADS"
381                         " is also set to ON.")
382   endif()
383   add_definitions(-D_LIBCXXABI_HAS_NO_THREADS)
384   add_definitions(-D_LIBCPP_HAS_NO_THREADS)
385 endif()
386
387 if (LIBCXXABI_HAS_EXTERNAL_THREAD_API)
388   if (LIBCXXABI_HAS_PTHREAD_API)
389     message(FATAL_ERROR "The options LIBCXXABI_HAS_EXTERNAL_THREAD_API"
390                         " and LIBCXXABI_HAS_PTHREAD_API cannot be both"
391                         " set to ON at the same time.")
392   endif()
393   if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
394     message(FATAL_ERROR "The options LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY"
395                         " and LIBCXXABI_HAS_EXTERNAL_THREAD_API cannot be both"
396                         " set to ON at the same time.")
397   endif()
398 endif()
399
400 if (LLVM_ENABLE_MODULES)
401   # Ignore that the rest of the modules flags are now unused.
402   add_compile_flags_if_supported(-Wno-unused-command-line-argument)
403   add_compile_flags(-fno-modules)
404 endif()
405
406 set(LIBCXXABI_HAS_UNDEFINED_SYMBOLS OFF)
407 if ((NOT LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS)
408     OR (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY AND LIBCXXABI_ENABLE_SHARED)
409     OR MINGW)
410   set(LIBCXXABI_HAS_UNDEFINED_SYMBOLS ON)
411 endif()
412
413 if (LIBCXXABI_HAS_UNDEFINED_SYMBOLS)
414   # Need to allow unresolved symbols if this is to work with shared library builds
415   if (APPLE)
416     list(APPEND LIBCXXABI_LINK_FLAGS "-undefined dynamic_lookup")
417   else()
418     # Relax this restriction from HandleLLVMOptions
419     string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
420   endif()
421 endif()
422
423 if (LIBCXXABI_HAS_PTHREAD_API)
424   add_definitions(-D_LIBCPP_HAS_THREAD_API_PTHREAD)
425 endif()
426
427 if (LIBCXXABI_HAS_EXTERNAL_THREAD_API)
428   add_definitions(-D_LIBCPP_HAS_THREAD_API_EXTERNAL)
429 endif()
430
431 if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
432   add_definitions(-D_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
433 endif()
434
435 # Prevent libc++abi from having library dependencies on libc++
436 add_definitions(-D_LIBCPP_DISABLE_EXTERN_TEMPLATE)
437
438 # Bring back `std::unexpected`, which is removed in C++17, to support
439 # pre-C++17.
440 add_definitions(-D_LIBCPP_ENABLE_CXX17_REMOVED_UNEXPECTED_FUNCTIONS)
441
442 if (MSVC)
443   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
444 endif()
445
446 # Define LIBCXXABI_USE_LLVM_UNWINDER for conditional compilation.
447 if (LIBCXXABI_USE_LLVM_UNWINDER)
448   add_definitions(-DLIBCXXABI_USE_LLVM_UNWINDER)
449 endif()
450
451 if (LIBCXXABI_SILENT_TERMINATE)
452   add_definitions(-DLIBCXXABI_SILENT_TERMINATE)
453 endif()
454
455 if (LIBCXXABI_BAREMETAL)
456     add_definitions(-DLIBCXXABI_BAREMETAL)
457 endif()
458
459 if (LIBCXXABI_HAS_COMMENT_LIB_PRAGMA)
460   if (LIBCXXABI_HAS_PTHREAD_LIB)
461     add_definitions(-D_LIBCXXABI_LINK_PTHREAD_LIB)
462   endif()
463 endif()
464
465 string(REPLACE ";" " " LIBCXXABI_CXX_FLAGS "${LIBCXXABI_CXX_FLAGS}")
466 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FLAGS}")
467 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBCXXABI_C_FLAGS}")
468
469 #===============================================================================
470 # Setup Source Code
471 #===============================================================================
472
473 set(LIBCXXABI_LIBUNWIND_INCLUDES "${LIBCXXABI_LIBUNWIND_INCLUDES}" CACHE PATH
474     "Specify path to libunwind includes." FORCE)
475 set(LIBCXXABI_LIBUNWIND_PATH "${LIBCXXABI_LIBUNWIND_PATH}" CACHE PATH
476     "Specify path to libunwind source." FORCE)
477
478 include_directories(include)
479 if (LIBCXXABI_USE_LLVM_UNWINDER OR LLVM_NATIVE_ARCH MATCHES ARM)
480   find_path(LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL libunwind.h
481     PATHS ${LIBCXXABI_LIBUNWIND_INCLUDES}
482           ${LIBCXXABI_LIBUNWIND_PATH}/include
483           ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBUNWIND_INCLUDES}
484           ${LLVM_MAIN_SRC_DIR}/projects/libunwind/include
485           ${LLVM_MAIN_SRC_DIR}/runtimes/libunwind/include
486           ${LLVM_MAIN_SRC_DIR}/../libunwind/include
487     NO_DEFAULT_PATH
488     NO_CMAKE_FIND_ROOT_PATH
489   )
490
491   if (LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL STREQUAL "LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL-NOTFOUND")
492     set(LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL "")
493   endif()
494
495   if (NOT LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL STREQUAL "")
496     include_directories("${LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL}")
497   endif()
498 endif()
499
500 # Add source code. This also contains all of the logic for deciding linker flags
501 # soname, etc...
502 add_subdirectory(src)
503
504 if (LIBCXXABI_INCLUDE_TESTS)
505   add_subdirectory(test)
506   add_subdirectory(fuzz)
507 endif()