1#===============================================================================
2# Setup Project
3#===============================================================================
4
5cmake_minimum_required(VERSION 3.4.3)
6
7if(POLICY CMP0042)
8  cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
9endif()
10
11if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
12  project(libcxxabi)
13
14  # Rely on llvm-config.
15  set(CONFIG_OUTPUT)
16  if (NOT LLVM_CONFIG_PATH)
17    find_program(LLVM_CONFIG_PATH "llvm-config")
18  endif()
19  if(DEFINED LLVM_PATH)
20    set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIR} CACHE PATH "Path to llvm/include")
21    set(LLVM_PATH ${LLVM_PATH} CACHE PATH "Path to LLVM source tree")
22    set(LLVM_MAIN_SRC_DIR ${LLVM_PATH})
23    set(LLVM_CMAKE_PATH "${LLVM_PATH}/cmake/modules")
24  elseif(LLVM_CONFIG_PATH)
25    message(STATUS "Found LLVM_CONFIG_PATH as ${LLVM_CONFIG_PATH}")
26    set(CONFIG_COMMAND ${LLVM_CONFIG_PATH}
27      "--includedir"
28      "--prefix"
29      "--src-root")
30    execute_process(
31      COMMAND ${CONFIG_COMMAND}
32      RESULT_VARIABLE HAD_ERROR
33      OUTPUT_VARIABLE CONFIG_OUTPUT
34    )
35    if(NOT HAD_ERROR)
36      string(REGEX REPLACE
37        "[ \t]*[\r\n]+[ \t]*" ";"
38        CONFIG_OUTPUT ${CONFIG_OUTPUT})
39    else()
40      string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}")
41      message(STATUS "${CONFIG_COMMAND_STR}")
42      message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}")
43    endif()
44
45    list(GET CONFIG_OUTPUT 0 INCLUDE_DIR)
46    list(GET CONFIG_OUTPUT 1 LLVM_OBJ_ROOT)
47    list(GET CONFIG_OUTPUT 2 MAIN_SRC_DIR)
48
49    set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include")
50    set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree")
51    set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree")
52    set(LLVM_LIT_PATH "${LLVM_PATH}/utils/lit/lit.py")
53
54    # --cmakedir is supported since llvm r291218 (4.0 release)
55    execute_process(
56      COMMAND ${LLVM_CONFIG_PATH} --cmakedir
57      RESULT_VARIABLE HAD_ERROR
58      OUTPUT_VARIABLE CONFIG_OUTPUT
59      ERROR_QUIET)
60    if(NOT HAD_ERROR)
61      string(STRIP "${CONFIG_OUTPUT}" LLVM_CMAKE_PATH)
62    else()
63      set(LLVM_CMAKE_PATH
64          "${LLVM_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}/cmake/llvm")
65    endif()
66  else()
67    message(FATAL_ERROR "llvm-config not found and LLVM_MAIN_SRC_DIR not defined. "
68                        "Reconfigure with -DLLVM_CONFIG_PATH=path/to/llvm-config "
69                        "or -DLLVM_PATH=path/to/llvm-source-root.")
70  endif()
71
72  if(EXISTS ${LLVM_CMAKE_PATH})
73    list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}")
74    include("${LLVM_CMAKE_PATH}/AddLLVM.cmake")
75    include("${LLVM_CMAKE_PATH}/HandleLLVMOptions.cmake")
76  else()
77    message(FATAL_ERROR "Not found: ${LLVM_CMAKE_PATH}")
78  endif()
79
80  set(PACKAGE_NAME libcxxabi)
81  set(PACKAGE_VERSION 5.0.0svn)
82  set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
83  set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
84
85  if (NOT DEFINED LLVM_INCLUDE_TESTS)
86    set(LLVM_INCLUDE_TESTS ON)
87  endif()
88
89  if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
90    set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py)
91  else()
92    # Seek installed Lit.
93    find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit
94      DOC "Path to lit.py")
95  endif()
96
97  if(LLVM_LIT)
98    # Define the default arguments to use with 'lit', and an option for the user
99    # to override.
100    set(LIT_ARGS_DEFAULT "-sv")
101    if (MSVC OR XCODE)
102      set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar")
103    endif()
104    set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit")
105
106    # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools.
107    if( WIN32 AND NOT CYGWIN )
108      set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools")
109    endif()
110  else()
111    set(LLVM_INCLUDE_TESTS OFF)
112  endif()
113
114  set(LIBCXXABI_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
115      "Define suffix of library directory name (32/64)")
116
117  set(LIBCXXABI_STANDALONE_BUILD 1)
118else()
119  set(LLVM_MAIN_SRC_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "Path to LLVM source tree")
120  set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
121  set(LIBCXXABI_LIBDIR_SUFFIX ${LLVM_LIBDIR_SUFFIX})
122endif()
123
124#===============================================================================
125# Setup CMake Options
126#===============================================================================
127
128# Define options.
129option(LIBCXXABI_ENABLE_EXCEPTIONS "Use exceptions." ON)
130option(LIBCXXABI_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
131option(LIBCXXABI_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
132option(LIBCXXABI_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
133option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF)
134option(LIBCXXABI_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
135option(LIBCXXABI_ENABLE_THREADS "Build with threads enabled" ON)
136option(LIBCXXABI_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
137option(LIBCXXABI_HAS_EXTERNAL_THREAD_API
138  "Build libc++abi with an externalized threading API.
139  This option may only be set to ON when LIBCXXABI_ENABLE_THREADS=ON." OFF)
140option(LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY
141  "Build libc++abi with an externalized threading library.
142   This option may only be set to ON when LIBCXXABI_ENABLE_THREADS=ON" OFF)
143option(LIBCXXABI_BUILD_32_BITS "Build 32 bit libc++abi." ${LLVM_BUILD_32_BITS})
144option(LIBCXXABI_INCLUDE_TESTS "Generate build targets for the libc++abi unit tests." ${LLVM_INCLUDE_TESTS})
145set(LIBCXXABI_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling.")
146set(LIBCXXABI_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
147set(LIBCXXABI_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
148set(LIBCXXABI_LIBCXX_LIBRARY_PATH "" CACHE PATH "The path to libc++ library.")
149
150# Default to building a shared library so that the default options still test
151# the libc++abi that is being built. There are two problems with testing a
152# static libc++abi. In the case of a standalone build, the tests will link the
153# system's libc++, which might not have been built against our libc++abi. In the
154# case of an in tree build, libc++ will prefer a dynamic libc++abi from the
155# system over a static libc++abi from the output directory.
156option(LIBCXXABI_ENABLE_SHARED "Build libc++abi as a shared library." ON)
157option(LIBCXXABI_ENABLE_STATIC "Build libc++abi as a static library." ON)
158
159if (NOT LIBCXXABI_ENABLE_SHARED AND NOT LIBCXXABI_ENABLE_STATIC)
160  message(FATAL_ERROR "libc++abi must be built as either a shared or static library.")
161endif()
162
163if (LLVM_EXTERNAL_LIBCXX_SOURCE_DIR)
164  set(LIBCXXABI_LIBCXX_SRC_DIRS ${LLVM_EXTERNAL_LIBCXX_SOURCE_DIR})
165else()
166  set(LIBCXXABI_LIBCXX_SRC_DIRS
167    "${LLVM_MAIN_SRC_DIR}/projects/libcxx"
168    "${LLVM_MAIN_SRC_DIR}/runtimes/libcxx"
169    )
170endif()
171
172set(LIBCXXABI_LIBCXX_INCLUDE_DIRS "")
173foreach(dir ${LIBCXXABI_LIBCXX_SRC_DIRS})
174  list(APPEND LIBCXXABI_LIBCXX_INCLUDE_DIRS "${dir}/include")
175endforeach()
176
177find_path(
178  LIBCXXABI_LIBCXX_INCLUDES
179  vector
180  PATHS ${LIBCXXABI_LIBCXX_INCLUDES}
181        ${LIBCXXABI_LIBCXX_PATH}/include
182        ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBCXX_INCLUDES}
183        ${LIBCXXABI_LIBCXX_INCLUDE_DIRS}
184        ${LLVM_INCLUDE_DIR}/c++/v1
185  )
186
187set(LIBCXXABI_LIBCXX_INCLUDES "${LIBCXXABI_LIBCXX_INCLUDES}" CACHE PATH
188    "Specify path to libc++ includes." FORCE)
189
190find_path(
191  LIBCXXABI_LIBCXX_PATH
192  test/libcxx/__init__.py
193  PATHS ${LIBCXXABI_LIBCXX_PATH}
194        ${LIBCXXABI_LIBCXX_INCLUDES}/../
195        ${LIBCXXABI_LIBCXX_SRC_DIRS}
196  NO_DEFAULT_PATH
197  )
198
199if (LIBCXXABI_LIBCXX_PATH STREQUAL "LIBCXXABI_LIBCXX_PATH-NOTFOUND")
200  message(WARNING "LIBCXXABI_LIBCXX_PATH was not specified and couldn't be infered.")
201  set(LIBCXXABI_LIBCXX_PATH "")
202endif()
203
204set(LIBCXXABI_LIBCXX_PATH "${LIBCXXABI_LIBCXX_PATH}" CACHE PATH
205    "Specify path to libc++ source." FORCE)
206
207#===============================================================================
208# Configure System
209#===============================================================================
210
211# Add path for custom modules
212set(CMAKE_MODULE_PATH
213  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
214  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
215  ${CMAKE_MODULE_PATH}
216  )
217
218set(LIBCXXABI_COMPILER    ${CMAKE_CXX_COMPILER})
219set(LIBCXXABI_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
220set(LIBCXXABI_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
221set(LIBCXXABI_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXXABI_LIBDIR_SUFFIX})
222
223set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXXABI_LIBRARY_DIR})
224set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXXABI_LIBRARY_DIR})
225
226# By default, for non-standalone builds, libcxx and libcxxabi share a library
227# directory.
228if (NOT LIBCXXABI_LIBCXX_LIBRARY_PATH)
229  set(LIBCXXABI_LIBCXX_LIBRARY_PATH "${LIBCXXABI_LIBRARY_DIR}" CACHE PATH
230      "The path to libc++ library.")
231endif()
232
233# Check that we can build with 32 bits if requested.
234if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
235  if (LIBCXXABI_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
236    message(STATUS "Building 32 bits executables and libraries.")
237  endif()
238elseif(LIBCXXABI_BUILD_32_BITS)
239  message(FATAL_ERROR "LIBCXXABI_BUILD_32_BITS=ON is not supported on this platform.")
240endif()
241
242#===============================================================================
243# Setup Compiler Flags
244#===============================================================================
245
246# Get required flags.
247macro(append_if list condition var)
248  if (${condition})
249    list(APPEND ${list} ${var})
250  endif()
251endmacro()
252
253macro(add_target_flags_if condition var)
254  if (${condition})
255    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${var}")
256    set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${var}")
257    list(APPEND LIBCXXABI_LINK_FLAGS ${var})
258  endif()
259endmacro()
260
261set(LIBCXXABI_C_FLAGS "")
262set(LIBCXXABI_CXX_FLAGS "")
263set(LIBCXXABI_COMPILE_FLAGS "")
264set(LIBCXXABI_LINK_FLAGS "")
265
266# Configure target flags
267add_target_flags_if(LIBCXXABI_BUILD_32_BITS "-m32")
268add_target_flags_if(LIBCXXABI_TARGET_TRIPLE
269          "-target ${LIBCXXABI_TARGET_TRIPLE}")
270add_target_flags_if(LIBCXXABI_GCC_TOOLCHAIN
271         "-gcc-toolchain ${LIBCXXABI_GCC_TOOLCHAIN}")
272add_target_flags_if(LIBCXXABI_SYSROOT
273          "--sysroot=${LIBCXXABI_SYSROOT}")
274
275if (LIBCXXABI_TARGET_TRIPLE)
276  set(TARGET_TRIPLE "${LIBCXXABI_TARGET_TRIPLE}")
277endif()
278
279# Configure compiler. Must happen after setting the target flags.
280include(config-ix)
281
282if (LIBCXXABI_HAS_NOSTDINCXX_FLAG)
283  list(APPEND LIBCXXABI_COMPILE_FLAGS -nostdinc++)
284  # Remove -stdlib flags to prevent them from causing an unused flag warning.
285  string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
286  string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
287endif()
288
289if (LIBCXXABI_USE_COMPILER_RT)
290  list(APPEND LIBCXXABI_LINK_FLAGS "-rtlib=compiler-rt")
291endif()
292
293# Let the library headers know they are currently being used to build the
294# library.
295add_definitions(-D_LIBCXXABI_BUILDING_LIBRARY)
296
297# Disable DLL annotations on Windows for static builds.
298if (WIN32 AND LIBCXXABI_ENABLE_STATIC AND NOT LIBCXXABI_ENABLE_SHARED)
299  add_definitions(-D_LIBCXXABI_DISABLE_DLL_IMPORT_EXPORT)
300endif()
301
302append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror=return-type)
303
304# Get warning flags
305append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_W_FLAG -W)
306append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WALL_FLAG -Wall)
307append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WCHAR_SUBSCRIPTS_FLAG -Wchar-subscripts)
308append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WCONVERSION_FLAG -Wconversion)
309append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WMISMATCHED_TAGS_FLAG -Wmismatched-tags)
310append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WMISSING_BRACES_FLAG -Wmissing-braces)
311append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNEWLINE_EOF_FLAG -Wnewline-eof)
312append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function)
313append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSHADOW_FLAG -Wshadow)
314append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSHORTEN_64_TO_32_FLAG -Wshorten-64-to-32)
315append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSIGN_COMPARE_FLAG -Wsign-compare)
316append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSIGN_CONVERSION_FLAG -Wsign-conversion)
317append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSTRICT_ALIASING_FLAG -Wstrict-aliasing=2)
318append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WSTRICT_OVERFLOW_FLAG -Wstrict-overflow=4)
319append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNUSED_PARAMETER_FLAG -Wunused-parameter)
320append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNUSED_VARIABLE_FLAG -Wunused-variable)
321append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings)
322append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WUNDEF_FLAG -Wundef)
323
324if (LIBCXXABI_ENABLE_WERROR)
325  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror)
326  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WX_FLAG -WX)
327else()
328  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_WNO_ERROR_FLAG -Wno-error)
329  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_NO_WX_FLAG -WX-)
330endif()
331if (LIBCXXABI_ENABLE_PEDANTIC)
332  append_if(LIBCXXABI_COMPILE_FLAGS LIBCXXABI_HAS_PEDANTIC_FLAG -pedantic)
333endif()
334
335# Get feature flags.
336append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_FSTRICT_ALIASING_FLAG -fstrict-aliasing)
337
338# Exceptions
339if (LIBCXXABI_ENABLE_EXCEPTIONS)
340  # Catches C++ exceptions only and tells the compiler to assume that extern C
341  # functions never throw a C++ exception.
342  append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc)
343  append_if(LIBCXXABI_C_FLAGS LIBCXXABI_HAS_FUNWIND_TABLES -funwind-tables)
344else()
345  add_definitions(-D_LIBCXXABI_NO_EXCEPTIONS)
346  append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_NO_EXCEPTIONS_FLAG -fno-exceptions)
347  append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_NO_EHS_FLAG -EHs-)
348  append_if(LIBCXXABI_CXX_FLAGS LIBCXXABI_HAS_NO_EHA_FLAG -EHa-)
349endif()
350
351# Assert
352string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
353if (LIBCXXABI_ENABLE_ASSERTIONS)
354  # MSVC doesn't like _DEBUG on release builds. See PR 4379.
355  if (NOT MSVC)
356    list(APPEND LIBCXXABI_COMPILE_FLAGS -D_DEBUG)
357  endif()
358  # On Release builds cmake automatically defines NDEBUG, so we
359  # explicitly undefine it:
360  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
361    list(APPEND LIBCXXABI_COMPILE_FLAGS -UNDEBUG)
362  endif()
363else()
364  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
365    list(APPEND LIBCXXABI_COMPILE_FLAGS -DNDEBUG)
366  endif()
367endif()
368# Static library
369if (NOT LIBCXXABI_ENABLE_SHARED)
370  list(APPEND LIBCXXABI_COMPILE_FLAGS -D_LIBCPP_BUILD_STATIC)
371endif()
372
373# Threading
374if (NOT LIBCXXABI_ENABLE_THREADS)
375  if (LIBCXXABI_HAS_PTHREAD_API)
376    message(FATAL_ERROR "LIBCXXABI_HAS_PTHREAD_API can only"
377                        " be set to ON when LIBCXXABI_ENABLE_THREADS"
378                        " is also set to ON.")
379  endif()
380  if (LIBCXXABI_HAS_EXTERNAL_THREAD_API)
381    message(FATAL_ERROR "LIBCXXABI_HAS_EXTERNAL_THREAD_API can only"
382                        " be set to ON when LIBCXXABI_ENABLE_THREADS"
383                        " is also set to ON.")
384  endif()
385  if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
386    message(FATAL_ERROR "LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY can only"
387                        " be set to ON when LIBCXXABI_ENABLE_THREADS"
388                        " is also set to ON.")
389  endif()
390  add_definitions(-D_LIBCXXABI_HAS_NO_THREADS)
391endif()
392
393if (LIBCXXABI_HAS_EXTERNAL_THREAD_API)
394  if (LIBCXXABI_HAS_PTHREAD_API)
395    message(FATAL_ERROR "The options LIBCXXABI_HAS_EXTERNAL_THREAD_API"
396                        " and LIBCXXABI_HAS_PTHREAD_API cannot be both"
397                        " set to ON at the same time.")
398  endif()
399  if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
400    message(FATAL_ERROR "The options LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY"
401                        " and LIBCXXABI_HAS_EXTERNAL_THREAD_API cannot be both"
402                        " set to ON at the same time.")
403  endif()
404endif()
405
406if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY AND LIBCXXABI_ENABLE_SHARED)
407  # Need to allow unresolved symbols if this is to work with shared library builds
408  if (APPLE)
409    add_link_flags("-undefined dynamic_lookup")
410  else()
411    # Relax this restriction from HandleLLVMOptions
412    string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
413  endif()
414endif()
415
416if (LIBCXXABI_HAS_PTHREAD_API)
417  add_definitions(-D_LIBCPP_HAS_THREAD_API_PTHREAD)
418endif()
419
420if (LIBCXXABI_HAS_EXTERNAL_THREAD_API)
421  add_definitions(-D_LIBCPP_HAS_THREAD_API_EXTERNAL)
422endif()
423
424if (LIBCXXABI_BUILD_EXTERNAL_THREAD_LIBRARY)
425  add_definitions(-D_LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
426endif()
427
428if (MSVC)
429  add_definitions(-D_CRT_SECURE_NO_WARNINGS)
430endif()
431
432# Define LIBCXXABI_USE_LLVM_UNWINDER for conditional compilation.
433if (LIBCXXABI_USE_LLVM_UNWINDER)
434  add_definitions(-DLIBCXXABI_USE_LLVM_UNWINDER=1)
435endif()
436
437if (LIBCXXABI_SILENT_TERMINATE)
438  add_definitions(-DLIBCXXABI_SILENT_TERMINATE=1)
439endif()
440
441string(REPLACE ";" " " LIBCXXABI_CXX_FLAGS "${LIBCXXABI_CXX_FLAGS}")
442set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FLAGS}")
443set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${LIBCXXABI_C_FLAGS}")
444
445#===============================================================================
446# Setup Source Code
447#===============================================================================
448
449set(LIBCXXABI_LIBUNWIND_INCLUDES "${LIBCXXABI_LIBUNWIND_INCLUDES}" CACHE PATH
450    "Specify path to libunwind includes." FORCE)
451set(LIBCXXABI_LIBUNWIND_PATH "${LIBCXXABI_LIBUNWIND_PATH}" CACHE PATH
452    "Specify path to libunwind source." FORCE)
453
454include_directories(include)
455if (LIBCXXABI_USE_LLVM_UNWINDER OR LLVM_NATIVE_ARCH MATCHES ARM)
456  find_path(
457    LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL
458    libunwind.h
459    PATHS ${LIBCXXABI_LIBUNWIND_INCLUDES}
460          ${LIBCXXABI_LIBUNWIND_PATH}/include
461          ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBUNWIND_INCLUDES}
462          ${LLVM_MAIN_SRC_DIR}/projects/libunwind/include
463          ${LLVM_MAIN_SRC_DIR}/runtimes/libunwind/include
464    NO_DEFAULT_PATH
465  )
466
467  find_path(
468    LIBCXXABI_LIBUNWIND_SOURCES
469    libunwind_ext.h
470    PATHS ${LIBCXXABI_LIBUNWIND_PATH}/src/
471          ${LIBCXXABI_LIBUNWIND_INCLUDES}/../src/
472          ${LLVM_MAIN_SRC_DIR}/projects/libunwind/src/
473          ${LLVM_MAIN_SRC_DIR}/runtimes/libunwind/src/
474    NO_DEFAULT_PATH
475  )
476
477  if (LIBCXXABI_LIBUNWIND_SOURCES STREQUAL "LIBCXXABI_LIBUNWIND_SOURCES-NOTFOUND")
478    message(WARNING "LIBCXXABI_LIBUNWIND_SOURCES was not specified and couldn't be infered.")
479    set(LIBCXXABI_LIBUNWIND_SOURCES "")
480  endif()
481
482  if (NOT LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL STREQUAL "LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL-NOTFOUND")
483    include_directories("${LIBCXXABI_LIBUNWIND_INCLUDES_INTERNAL}")
484  endif()
485  if (NOT LIBCXXABI_LIBUNWIND_SOURCES STREQUAL "")
486    include_directories("${LIBCXXABI_LIBUNWIND_SOURCES}")
487  endif()
488endif()
489
490# Add source code. This also contains all of the logic for deciding linker flags
491# soname, etc...
492add_subdirectory(src)
493
494if (NOT LIBCXXABI_INCLUDE_TESTS OR (LIBCXXABI_STANDALONE_BUILD AND NOT LIBCXXABI_ENABLE_SHARED))
495  # We can't reasonably test the system C++ library with a static libc++abi.
496  # We either need to be able to replace libc++abi at run time (with a shared
497  # libc++abi), or we need to be able to replace the C++ runtime (with a non-
498  # standalone build).
499  message(WARNING "The libc++abi tests aren't valid when libc++abi is built "
500                  "standalone (i.e. outside of llvm/projects/libcxxabi ) and "
501                  "is built without a shared library.  Either build a shared "
502                  "library, build libc++abi at the same time as you build "
503                  "libc++, or do without testing.  No check target will be "
504                  "available!")
505else()
506  add_subdirectory(test)
507  add_subdirectory(fuzz)
508endif()
509