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