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