Revert "[clang-repl] Allow loading of plugins in clang-repl."
[llvm-project.git] / libcxx / CMakeLists.txt
blob6ab44e5785108d246951b1b9ca1044740a96b663
1 # See https://libcxx.llvm.org/docs/BuildingLibcxx.html for instructions on how
2 # to build libcxx with CMake.
4 if (NOT IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libcxxabi")
5   message(FATAL_ERROR "libc++ now requires being built in a monorepo layout with libcxxabi available")
6 endif()
8 #===============================================================================
9 # Setup Project
10 #===============================================================================
11 cmake_minimum_required(VERSION 3.13.4)
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   )
20 set(CMAKE_FOLDER "libc++")
22 set(LIBCXX_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
23 set(LIBCXX_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
24 set(LIBCXX_BINARY_INCLUDE_DIR "${LIBCXX_BINARY_DIR}/include/c++build")
26 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBCXX_STANDALONE_BUILD)
27   project(libcxx CXX C)
29   set(PACKAGE_NAME libcxx)
30   set(PACKAGE_VERSION 14.0.0git)
31   set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
32   set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
34   # In a standalone build, we don't have llvm to automatically generate the
35   # llvm-lit script for us.  So we need to provide an explicit directory that
36   # the configurator should write the script into.
37   set(LIBCXX_STANDALONE_BUILD 1)
38   set(LLVM_LIT_OUTPUT_DIR "${LIBCXX_BINARY_DIR}/bin")
40   # Find the LLVM sources and simulate LLVM CMake options.
41   include(HandleOutOfTreeLLVM)
42 endif()
44 if (LIBCXX_STANDALONE_BUILD)
45   find_package(Python3 COMPONENTS Interpreter)
46   if(NOT Python3_Interpreter_FOUND)
47     message(WARNING "Python3 not found, using python2 as a fallback")
48     find_package(Python2 COMPONENTS Interpreter REQUIRED)
49     if(Python2_VERSION VERSION_LESS 2.7)
50       message(SEND_ERROR "Python 2.7 or newer is required")
51     endif()
53     # Treat python2 as python3
54     add_executable(Python3::Interpreter IMPORTED)
55     set_target_properties(Python3::Interpreter PROPERTIES
56       IMPORTED_LOCATION ${Python2_EXECUTABLE})
57     set(Python3_EXECUTABLE ${Python2_EXECUTABLE})
58   endif()
59 endif()
61 # Require out of source build.
62 include(MacroEnsureOutOfSourceBuild)
63 MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
64  "${PROJECT_NAME} requires an out of source build. Please create a separate
65  build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
66  )
67 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" AND "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
68   message(STATUS "Configuring for clang-cl")
69   set(LIBCXX_TARGETING_CLANG_CL ON)
70 endif()
72 if (MSVC)
73   set(LIBCXX_TARGETING_MSVC ON)
74   message(STATUS "Configuring for MSVC")
75 else()
76   set(LIBCXX_TARGETING_MSVC OFF)
77 endif()
79 #===============================================================================
80 # Setup CMake Options
81 #===============================================================================
82 include(CMakeDependentOption)
83 include(HandleCompilerRT)
85 # Basic options ---------------------------------------------------------------
86 option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." OFF)
87 option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
88 option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON)
89 option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON)
90 set(ENABLE_FILESYSTEM_DEFAULT ON)
91 if (WIN32 AND NOT MINGW)
92   # Filesystem is buildable for windows, but it requires __int128 helper
93   # functions, that currently are provided by libgcc or compiler_rt builtins.
94   # These are available in MinGW environments, but not currently in MSVC
95   # environments.
96   set(ENABLE_FILESYSTEM_DEFAULT OFF)
97 endif()
98 option(LIBCXX_ENABLE_FILESYSTEM "Build filesystem as part of the main libc++ library"
99     ${ENABLE_FILESYSTEM_DEFAULT})
100 option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS})
101 option(LIBCXX_ENABLE_PARALLEL_ALGORITHMS "Enable the parallel algorithms library. This requires the PSTL to be available." OFF)
102 option(LIBCXX_ENABLE_DEBUG_MODE_SUPPORT
103   "Whether to include support for libc++'s debugging mode in the library.
104    By default, this is turned on. If you turn it off and try to enable the
105    debug mode when compiling a program against libc++, it will fail to link
106    since the required support isn't provided in the library." ON)
107 option(LIBCXX_ENABLE_RANDOM_DEVICE
108   "Whether to include support for std::random_device in the library. Disabling
109    this can be useful when building the library for platforms that don't have
110    a source of randomness, such as some embedded platforms. When this is not
111    supported, most of <random> will still be available, but std::random_device
112    will not." ON)
113 option(LIBCXX_ENABLE_LOCALIZATION
114   "Whether to include support for localization in the library. Disabling
115    localization can be useful when porting to platforms that don't support
116    the C locale API (e.g. embedded). When localization is not supported,
117    several parts of the library will be disabled: <iostream>, <regex>, <locale>
118    will be completely unusable, and other parts may be only partly available." ON)
119 option(LIBCXX_ENABLE_UNICODE
120   "Whether to include support for Unicode in the library. Disabling Unicode can
121    be useful when porting to platforms that don't support UTF-8 encoding (e.g.
122    embedded)." ON)
123 option(LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS
124   "Whether to turn on vendor availability annotations on declarations that depend
125    on definitions in a shared library. By default, we assume that we're not building
126    libc++ for any specific vendor, and we disable those annotations. Vendors wishing
127    to provide compile-time errors when using features unavailable on some version of
128    the shared library they shipped should turn this on and see `include/__availability`
129    for more details." OFF)
130 option(LIBCXX_ENABLE_INCOMPLETE_FEATURES
131     "Whether to enable support for incomplete library features. Incomplete features
132     are new library features under development. These features don't guarantee
133     ABI stability nor the quality of completed library features. Vendors
134     shipping the library may want to disable this option." ON)
135 set(LIBCXX_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/legacy.cfg.in" CACHE STRING
136     "The Lit testing configuration to use when running the tests.")
137 set(LIBCXX_TEST_PARAMS "" CACHE STRING
138     "A list of parameters to run the Lit test suite with.")
140 # Benchmark options -----------------------------------------------------------
141 option(LIBCXX_INCLUDE_BENCHMARKS "Build the libc++ benchmarks and their dependencies" ON)
143 set(LIBCXX_BENCHMARK_TEST_ARGS_DEFAULT --benchmark_min_time=0.01)
144 set(LIBCXX_BENCHMARK_TEST_ARGS "${LIBCXX_BENCHMARK_TEST_ARGS_DEFAULT}" CACHE STRING
145     "Arguments to pass when running the benchmarks using check-cxx-benchmarks")
147 set(LIBCXX_BENCHMARK_NATIVE_STDLIB "" CACHE STRING
148         "Build the benchmarks against the specified native STL.
149          The value must be one of libc++/libstdc++")
150 set(LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN "" CACHE STRING
151     "Use alternate GCC toolchain when building the native benchmarks")
153 if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
154   if (NOT (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++"
155         OR LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++"))
156     message(FATAL_ERROR "Invalid value for LIBCXX_BENCHMARK_NATIVE_STDLIB: "
157             "'${LIBCXX_BENCHMARK_NATIVE_STDLIB}'")
158   endif()
159 endif()
161 option(LIBCXX_INCLUDE_DOCS "Build the libc++ documentation." ${LLVM_INCLUDE_DOCS})
162 set(LIBCXX_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
163     "Define suffix of library directory name (32/64)")
164 option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
165 option(LIBCXX_INSTALL_LIBRARY "Install the libc++ library." ON)
166 cmake_dependent_option(LIBCXX_INSTALL_STATIC_LIBRARY
167   "Install the static libc++ library." ON
168   "LIBCXX_ENABLE_STATIC;LIBCXX_INSTALL_LIBRARY" OFF)
169 cmake_dependent_option(LIBCXX_INSTALL_SHARED_LIBRARY
170   "Install the shared libc++ library." ON
171   "LIBCXX_ENABLE_SHARED;LIBCXX_INSTALL_LIBRARY" OFF)
172 cmake_dependent_option(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY
173         "Install libc++experimental.a" ON
174         "LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY;LIBCXX_INSTALL_LIBRARY" OFF)
176 set(LIBCXX_ABI_VERSION "1" CACHE STRING "ABI version of libc++. Can be either 1 or 2, where 2 is currently not stable. Defaults to 1.")
177 set(LIBCXX_ABI_NAMESPACE "" CACHE STRING "The inline ABI namespace used by libc++. It defaults to __n where `n` is the current ABI version.")
178 option(LIBCXX_ABI_UNSTABLE "Unstable ABI of libc++." OFF)
179 option(LIBCXX_ABI_FORCE_ITANIUM "Ignore auto-detection and force use of the Itanium ABI.")
180 option(LIBCXX_ABI_FORCE_MICROSOFT "Ignore auto-detection and force use of the Microsoft ABI.")
182 set(LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION "default" CACHE STRING
183   "Override the implementation to use for comparing typeinfos. By default, this
184    is detected automatically by the library, but this option allows overriding
185    which implementation is used unconditionally.
187    See the documentation in <libcxx/include/typeinfo> for details on what each
188    value means.")
189 set(TYPEINFO_COMPARISON_VALUES "default;1;2;3")
190 if (NOT ("${LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION}" IN_LIST TYPEINFO_COMPARISON_VALUES))
191   message(FATAL_ERROR "Value '${LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION}' is not a valid value for
192                        LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION")
193 endif()
195 option(LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT "Enable per TU ABI insulation by default. To be used by vendors." OFF)
196 set(LIBCXX_ABI_DEFINES "" CACHE STRING "A semicolon separated list of ABI macros to define in the site config header.")
197 option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
198 set(LIBCXX_LIBCPPABI_VERSION "2" CACHE STRING "Version of libc++abi's ABI to re-export from libc++ when re-exporting is enabled.
199                                                Note that this is not related to the version of libc++'s ABI itself!")
201 # ABI Library options ---------------------------------------------------------
202 set(LIBCXX_CXX_ABI "default" CACHE STRING "Specify C++ ABI library to use.")
203 set(CXXABIS none default libcxxabi libcxxrt libstdc++ libsupc++ vcruntime)
204 set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
206 # Setup the default options if LIBCXX_CXX_ABI is not specified.
207 if (LIBCXX_CXX_ABI STREQUAL "default")
208   if (LIBCXX_TARGETING_MSVC)
209     # FIXME: Figure out how to configure the ABI library on Windows.
210     set(LIBCXX_CXX_ABI_LIBNAME "vcruntime")
211   elseif (${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD")
212     set(LIBCXX_CXX_ABI_LIBNAME "libcxxrt")
213   elseif (NOT LIBCXX_STANDALONE_BUILD OR HAVE_LIBCXXABI)
214     set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
215   else()
216     set(LIBCXX_CXX_ABI_LIBNAME "default")
217   endif()
218 else()
219   set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}")
220 endif()
222 option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY
223   "Use a static copy of the ABI library when linking libc++.
224    This option cannot be used with LIBCXX_ENABLE_ABI_LINKER_SCRIPT." OFF)
226 cmake_dependent_option(LIBCXX_STATICALLY_LINK_ABI_IN_STATIC_LIBRARY
227   "Statically link the ABI library to static library" ON
228   "LIBCXX_ENABLE_STATIC_ABI_LIBRARY;LIBCXX_ENABLE_STATIC" OFF)
230 cmake_dependent_option(LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY
231   "Statically link the ABI library to shared library" ON
232   "LIBCXX_ENABLE_STATIC_ABI_LIBRARY;LIBCXX_ENABLE_SHARED" OFF)
234 # Generate and install a linker script inplace of libc++.so. The linker script
235 # will link libc++ to the correct ABI library. This option is on by default
236 # on UNIX platforms other than Apple unless 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY'
237 # is on. This option is also disabled when the ABI library is not specified
238 # or is specified to be "none".
239 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF)
240 if (LLVM_HAVE_LINK_VERSION_SCRIPT AND NOT LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY
241       AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "none"
242       AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "default"
243       AND Python3_EXECUTABLE
244       AND LIBCXX_ENABLE_SHARED)
245     set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON)
246 endif()
248 option(LIBCXX_ENABLE_ABI_LINKER_SCRIPT
249       "Use and install a linker script for the given ABI library"
250       ${ENABLE_LINKER_SCRIPT_DEFAULT_VALUE})
252 option(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS
253   "Build libc++ with definitions for operator new/delete. These are normally
254    defined in libc++abi, but this option can be used to define them in libc++
255    instead. If you define them in libc++, make sure they are NOT defined in
256    libc++abi. Doing otherwise is an ODR violation." OFF)
257 # Build libc++abi with libunwind. We need this option to determine whether to
258 # link with libunwind or libgcc_s while running the test cases.
259 option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF)
261 # Target options --------------------------------------------------------------
262 option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++." ${LLVM_BUILD_32_BITS})
263 set(LIBCXX_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}" CACHE STRING "Use alternate target triple.")
264 set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.")
265 set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.")
267 # Feature options -------------------------------------------------------------
268 option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON)
269 option(LIBCXX_ENABLE_RTTI "Use run time type information." ON)
270 option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON)
271 option(LIBCXX_ENABLE_MONOTONIC_CLOCK
272   "Build libc++ with support for a monotonic clock.
273    This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON)
274 option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF)
275 option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
276 option(LIBCXX_HAS_WIN32_THREAD_API "Ignore auto-detection and force use of win32 thread API" OFF)
277 option(LIBCXX_HAS_EXTERNAL_THREAD_API
278   "Build libc++ with an externalized threading API.
279    This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON." OFF)
280 option(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY
281     "Build libc++ with an externalized threading library.
282      This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON" OFF)
284 # Misc options ----------------------------------------------------------------
285 # FIXME: Turn -pedantic back ON. It is currently off because it warns
286 # about #include_next which is used everywhere.
287 option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF)
288 option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
289 option(LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS "Disable #warnings about conflicting macros." OFF)
291 option(LIBCXX_GENERATE_COVERAGE "Enable generating code coverage." OFF)
292 set(LIBCXX_COVERAGE_LIBRARY "" CACHE STRING
293     "The Profile-rt library used to build with code coverage")
295 set(LIBCXX_CONFIGURE_IDE_DEFAULT OFF)
296 if (XCODE OR MSVC_IDE)
297   set(LIBCXX_CONFIGURE_IDE_DEFAULT ON)
298 endif()
299 option(LIBCXX_CONFIGURE_IDE "Configure libcxx for use within an IDE"
300       ${LIBCXX_CONFIGURE_IDE_DEFAULT})
302 option(LIBCXX_HERMETIC_STATIC_LIBRARY
303   "Do not export any symbols from the static library." OFF)
305 #===============================================================================
306 # Check option configurations
307 #===============================================================================
309 # Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON only when
310 # LIBCXX_ENABLE_THREADS is on.
311 if(LIBCXX_ENABLE_THREADS AND NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
312   message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF"
313                       " when LIBCXX_ENABLE_THREADS is also set to OFF.")
314 endif()
316 if(NOT LIBCXX_ENABLE_THREADS)
317   if(LIBCXX_HAS_PTHREAD_API)
318     message(FATAL_ERROR "LIBCXX_HAS_PTHREAD_API can only be set to ON"
319                         " when LIBCXX_ENABLE_THREADS is also set to ON.")
320   endif()
321   if(LIBCXX_HAS_EXTERNAL_THREAD_API)
322     message(FATAL_ERROR "LIBCXX_HAS_EXTERNAL_THREAD_API can only be set to ON"
323                         " when LIBCXX_ENABLE_THREADS is also set to ON.")
324   endif()
325   if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
326     message(FATAL_ERROR "LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY can only be set "
327                         "to ON when LIBCXX_ENABLE_THREADS is also set to ON.")
328   endif()
329   if (LIBCXX_HAS_WIN32_THREAD_API)
330     message(FATAL_ERROR "LIBCXX_HAS_WIN32_THREAD_API can only be set to ON"
331                         " when LIBCXX_ENABLE_THREADS is also set to ON.")
332   endif()
334 endif()
336 if (LIBCXX_HAS_EXTERNAL_THREAD_API)
337   if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
338     message(FATAL_ERROR "The options LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY and "
339                         "LIBCXX_HAS_EXTERNAL_THREAD_API cannot both be ON at "
340                         "the same time")
341   endif()
342   if (LIBCXX_HAS_PTHREAD_API)
343     message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
344                         "and LIBCXX_HAS_PTHREAD_API cannot be both"
345                         "set to ON at the same time.")
346   endif()
347   if (LIBCXX_HAS_WIN32_THREAD_API)
348     message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
349                         "and LIBCXX_HAS_WIN32_THREAD_API cannot be both"
350                         "set to ON at the same time.")
351   endif()
352 endif()
354 if (LIBCXX_HAS_PTHREAD_API)
355   if (LIBCXX_HAS_WIN32_THREAD_API)
356     message(FATAL_ERROR "The options LIBCXX_HAS_PTHREAD_API"
357                         "and LIBCXX_HAS_WIN32_THREAD_API cannot be both"
358                         "set to ON at the same time.")
359   endif()
360 endif()
362 # Ensure LLVM_USE_SANITIZER is not specified when LIBCXX_GENERATE_COVERAGE
363 # is ON.
364 if (LLVM_USE_SANITIZER AND LIBCXX_GENERATE_COVERAGE)
365   message(FATAL_ERROR "LLVM_USE_SANITIZER cannot be used with LIBCXX_GENERATE_COVERAGE")
366 endif()
368 # Set LIBCXX_BUILD_32_BITS to (LIBCXX_BUILD_32_BITS OR LLVM_BUILD_32_BITS)
369 # and check that we can build with 32 bits if requested.
370 if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
371   if (LIBCXX_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
372     message(STATUS "Building 32 bits executables and libraries.")
373   endif()
374 elseif(LIBCXX_BUILD_32_BITS)
375   message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.")
376 endif()
378 # Warn users that LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option.
379 if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
380   message(WARNING "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option")
381   if (LIBCXX_ENABLE_STATIC AND NOT Python3_EXECUTABLE)
382     message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY requires python but it was not found.")
383   endif()
384 endif()
386 if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
387     if (APPLE)
388       message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets")
389     endif()
390     if (NOT LIBCXX_ENABLE_SHARED)
391       message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.")
392     endif()
393 endif()
395 if (LIBCXX_STATICALLY_LINK_ABI_IN_SHARED_LIBRARY AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
396     message(FATAL_ERROR "Conflicting options given.
397         LIBCXX_ENABLE_STATIC_ABI_LIBRARY cannot be specified with
398         LIBCXX_ENABLE_ABI_LINKER_SCRIPT")
399 endif()
401 if (LIBCXX_ABI_FORCE_ITANIUM AND LIBCXX_ABI_FORCE_MICROSOFT)
402   message(FATAL_ERROR "Only one of LIBCXX_ABI_FORCE_ITANIUM and LIBCXX_ABI_FORCE_MICROSOFT can be specified.")
403 endif ()
405 #===============================================================================
406 # Configure System
407 #===============================================================================
409 # TODO: Projects that depend on libc++ should use LIBCXX_GENERATED_INCLUDE_DIR
410 # instead of hard-coding include/c++/v1.
411 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
412   set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
413   set(LIBCXX_GENERATED_INCLUDE_DIR "${LLVM_BINARY_DIR}/include/c++/v1")
414   set(LIBCXX_GENERATED_INCLUDE_TARGET_DIR "${LLVM_BINARY_DIR}/include/${LLVM_DEFAULT_TARGET_TRIPLE}/c++/v1")
415   set(LIBCXX_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
416       "Path where built libc++ libraries should be installed.")
417   set(LIBCXX_INSTALL_RUNTIME_DIR bin CACHE PATH
418       "Path where built libc++ runtime libraries should be installed.")
419   set(LIBCXX_INSTALL_INCLUDE_DIR "include/c++/v1" CACHE PATH
420       "Path where target-agnostic libc++ headers should be installed.")
421   set(LIBCXX_INSTALL_INCLUDE_TARGET_DIR "include/${LLVM_DEFAULT_TARGET_TRIPLE}/c++/v1" CACHE PATH
422       "Path where target-specific libc++ headers should be installed.")
423   if(LIBCXX_LIBDIR_SUBDIR)
424     string(APPEND LIBCXX_LIBRARY_DIR /${LIBCXX_LIBDIR_SUBDIR})
425     string(APPEND LIBCXX_INSTALL_LIBRARY_DIR /${LIBCXX_LIBDIR_SUBDIR})
426   endif()
427 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
428   set(LIBCXX_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
429   set(LIBCXX_GENERATED_INCLUDE_DIR "${LLVM_BINARY_DIR}/include/c++/v1")
430   set(LIBCXX_GENERATED_INCLUDE_TARGET_DIR "${LIBCXX_GENERATED_INCLUDE_DIR}")
431   set(LIBCXX_INSTALL_LIBRARY_DIR lib${LIBCXX_LIBDIR_SUFFIX} CACHE PATH
432       "Path where built libc++ libraries should be installed.")
433   set(LIBCXX_INSTALL_RUNTIME_DIR bin CACHE PATH
434       "Path where built libc++ runtime libraries should be installed.")
435   set(LIBCXX_INSTALL_INCLUDE_DIR "include/c++/v1" CACHE PATH
436       "Path where target-agnostic libc++ headers should be installed.")
437   set(LIBCXX_INSTALL_INCLUDE_TARGET_DIR "${LIBCXX_INSTALL_INCLUDE_DIR}" CACHE PATH
438       "Path where target-specific libc++ headers should be installed.")
439 else()
440   set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
441   set(LIBCXX_GENERATED_INCLUDE_DIR "${CMAKE_BINARY_DIR}/include/c++/v1")
442   set(LIBCXX_GENERATED_INCLUDE_TARGET_DIR "${LIBCXX_GENERATED_INCLUDE_DIR}")
443   set(LIBCXX_INSTALL_LIBRARY_DIR lib${LIBCXX_LIBDIR_SUFFIX} CACHE PATH
444       "Path where built libc++ libraries should be installed.")
445   set(LIBCXX_INSTALL_RUNTIME_DIR bin CACHE PATH
446       "Path where built libc++ runtime libraries should be installed.")
447   set(LIBCXX_INSTALL_INCLUDE_DIR "include/c++/v1" CACHE PATH
448       "Path where target-agnostic libc++ headers should be installed.")
449   set(LIBCXX_INSTALL_INCLUDE_TARGET_DIR "${LIBCXX_INSTALL_INCLUDE_DIR}" CACHE PATH
450       "Path where target-specific libc++ headers should be installed.")
451 endif()
453 file(MAKE_DIRECTORY "${LIBCXX_BINARY_INCLUDE_DIR}")
455 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
456 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
457 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
459 # Declare libc++ configuration variables.
460 # They are intended for use as follows:
461 # LIBCXX_CXX_FLAGS: General flags for both the compiler and linker.
462 # LIBCXX_COMPILE_FLAGS: Compile only flags.
463 # LIBCXX_LINK_FLAGS: Linker only flags.
464 # LIBCXX_LIBRARIES: libraries libc++ is linked to.
465 set(LIBCXX_COMPILE_FLAGS "")
466 set(LIBCXX_LINK_FLAGS "")
467 set(LIBCXX_LIBRARIES "")
469 # Include macros for adding and removing libc++ flags.
470 include(HandleLibcxxFlags)
472 # Target flags ================================================================
473 # These flags get added to CMAKE_CXX_FLAGS and CMAKE_C_FLAGS so that
474 # 'config-ix' use them during feature checks. It also adds them to both
475 # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'
476 add_target_flags_if(LIBCXX_BUILD_32_BITS "-m32")
478 if(LIBCXX_TARGET_TRIPLE)
479   add_target_flags_if_supported("--target=${LIBCXX_TARGET_TRIPLE}")
480 elseif(CMAKE_CXX_COMPILER_TARGET)
481   set(LIBCXX_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
482 endif()
483 if(LIBCXX_SYSROOT)
484   add_target_flags_if_supported("--sysroot=${LIBCXX_SYSROOT}")
485 elseif(CMAKE_SYSROOT)
486   set(LIBCXX_SYSROOT "${CMAKE_SYSROOT}")
487 endif()
488 if(LIBCXX_GCC_TOOLCHAIN)
489   add_target_flags_if_supported("--gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}")
490 elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
491   set(LIBCXX_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
492 endif()
494 # Configure compiler.
495 include(config-ix)
497 # Configure coverage options.
498 if (LIBCXX_GENERATE_COVERAGE)
499   include(CodeCoverage)
500   set(CMAKE_BUILD_TYPE "COVERAGE" CACHE STRING "" FORCE)
501 endif()
503 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
504 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
505   set(LIBCXX_DEBUG_BUILD ON)
506 else()
507   set(LIBCXX_DEBUG_BUILD OFF)
508 endif()
510 #===============================================================================
511 # Setup Compiler Flags
512 #===============================================================================
514 include(HandleLibCXXABI) # Setup the ABI library flags
516 if (NOT LIBCXX_STANDALONE_BUILD)
517   # Remove flags that may have snuck in.
518   remove_flags(-DNDEBUG -UNDEBUG -D_DEBUG
519                -lc++abi)
520 endif()
521 remove_flags(--stdlib=libc++ -stdlib=libc++ --stdlib=libstdc++ -stdlib=libstdc++)
523 # FIXME: Remove all debug flags and flags that change which Windows
524 # default libraries are linked. Currently we only support linking the
525 # non-debug DLLs
526 remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md")
528 # FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
529 # Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
530 # so they don't get transformed into -Wno and -errors respectively.
531 remove_flags(-Wno-pedantic -pedantic-errors -pedantic)
533 # Required flags ==============================================================
534 function(cxx_add_basic_build_flags target)
536   # Require C++20 for all targets. C++17 is needed to use aligned allocation
537   # in the dylib. C++20 is needed to use char8_t.
538   set_target_properties(${target} PROPERTIES
539     CXX_STANDARD 20
540     CXX_STANDARD_REQUIRED YES
541     CXX_EXTENSIONS NO)
543   # When building the dylib, don't warn for unavailable aligned allocation
544   # functions based on the deployment target -- they are always available
545   # because they are provided by the dylib itself with the excepton of z/OS.
546   if (ZOS)
547     target_add_compile_flags_if_supported(${target} PRIVATE -fno-aligned-allocation)
548   else()
549     target_add_compile_flags_if_supported(${target} PRIVATE -faligned-allocation)
550   endif()
552   # On all systems the system c++ standard library headers need to be excluded.
553   # MSVC only has -X, which disables all default includes; including the crt.
554   # Thus, we do nothing and hope we don't accidentally include any of the C++
555   # headers
556   target_add_compile_flags_if_supported(${target} PUBLIC -nostdinc++)
558   # Hide all inline function definitions which have not explicitly been marked
559   # visible. This prevents new definitions for inline functions from appearing in
560   # the dylib when get ODR used by another function.
561   target_add_compile_flags_if_supported(${target} PRIVATE -fvisibility-inlines-hidden)
563   # Our visibility annotations are not quite right for non-Clang compilers,
564   # so we end up not exporting all the symbols we should. In the future, we
565   # can improve the situation by providing an explicit list of exported
566   # symbols on all compilers.
567   if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
568     target_add_compile_flags_if_supported(${target} PRIVATE -fvisibility=hidden)
569   endif()
571   if (LIBCXX_CONFIGURE_IDE)
572     # This simply allows IDE to process <experimental/coroutine>
573     target_add_compile_flags_if_supported(${target} PRIVATE -fcoroutines-ts)
574   endif()
576   # Let the library headers know they are currently being used to build the
577   # library.
578   target_compile_definitions(${target} PRIVATE -D_LIBCPP_BUILDING_LIBRARY)
580   if (NOT LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS)
581     target_compile_definitions(${target} PRIVATE -D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS)
582   endif()
584   if (LIBCXX_HAS_COMMENT_LIB_PRAGMA)
585     if (LIBCXX_HAS_PTHREAD_LIB)
586       target_compile_definitions(${target} PRIVATE -D_LIBCPP_LINK_PTHREAD_LIB)
587     endif()
588     if (LIBCXX_HAS_RT_LIB)
589       target_compile_definitions(${target} PRIVATE -D_LIBCPP_LINK_RT_LIB)
590     endif()
591   endif()
592 endfunction()
594 # Warning flags ===============================================================
595 function(cxx_add_warning_flags target)
596   target_compile_definitions(${target} PUBLIC -D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
597   if (MSVC)
598     # -W4 is the cl.exe/clang-cl equivalent of -Wall. (In cl.exe and clang-cl,
599     # -Wall is equivalent to -Weverything in GCC style compiler drivers.)
600     target_add_compile_flags_if_supported(${target} PRIVATE -W4)
601   else()
602     target_add_compile_flags_if_supported(${target} PRIVATE -Wall)
603   endif()
604   target_add_compile_flags_if_supported(${target} PRIVATE -Wextra -W -Wwrite-strings
605                                                           -Wno-unused-parameter -Wno-long-long
606                                                           -Werror=return-type -Wextra-semi -Wundef)
607   if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
608     target_add_compile_flags_if_supported(${target} PRIVATE
609       -Wno-user-defined-literals
610       -Wno-covered-switch-default
611       -Wno-suggest-override
612     )
613     if (LIBCXX_TARGETING_CLANG_CL)
614       target_add_compile_flags_if_supported(${target} PRIVATE
615         -Wno-c++98-compat
616         -Wno-c++98-compat-pedantic
617         -Wno-c++11-compat
618         -Wno-undef
619         -Wno-reserved-id-macro
620         -Wno-gnu-include-next
621         -Wno-gcc-compat # For ignoring "'diagnose_if' is a clang extension" warnings
622         -Wno-zero-as-null-pointer-constant # FIXME: Remove this and fix all occurrences.
623         -Wno-deprecated-dynamic-exception-spec # For auto_ptr
624         -Wno-sign-conversion
625         -Wno-old-style-cast
626         -Wno-deprecated # FIXME: Remove this and fix all occurrences.
627         -Wno-shift-sign-overflow # FIXME: Why do we need this with clang-cl but not clang?
628         -Wno-double-promotion # FIXME: remove me
629       )
630     endif()
631   elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
632     target_add_compile_flags_if_supported(${target} PRIVATE
633       -Wno-literal-suffix
634       -Wno-c++14-compat
635       -Wno-noexcept-type
636       -Wno-suggest-override)
637   endif()
638   if (LIBCXX_ENABLE_WERROR)
639     target_add_compile_flags_if_supported(${target} PRIVATE -Werror)
640     target_add_compile_flags_if_supported(${target} PRIVATE -WX)
641   else()
642     # TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is
643     # added elsewhere.
644     target_add_compile_flags_if_supported(${target} PRIVATE -Wno-error)
645   endif()
646   if (LIBCXX_ENABLE_PEDANTIC)
647     target_add_compile_flags_if_supported(${target} PRIVATE -pedantic)
648   endif()
649   if (LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS)
650     target_compile_definitions(${target} PRIVATE -D_LIBCPP_DISABLE_MACRO_CONFLICT_WARNINGS)
651   endif()
652 endfunction()
654 # Exception flags =============================================================
655 function(cxx_add_exception_flags target)
656   if (LIBCXX_ENABLE_EXCEPTIONS)
657     # Catches C++ exceptions only and tells the compiler to assume that extern C
658     # functions never throw a C++ exception.
659     target_add_compile_flags_if_supported(${target} PUBLIC -EHsc)
660   else()
661     target_add_compile_flags_if_supported(${target} PUBLIC -EHs- -EHa-)
662     target_add_compile_flags_if_supported(${target} PUBLIC -fno-exceptions)
663   endif()
664 endfunction()
666 # RTTI flags ==================================================================
667 function(cxx_add_rtti_flags target)
668   if (NOT LIBCXX_ENABLE_RTTI)
669     target_add_compile_flags_if_supported(${target} PUBLIC -GR-)
670     target_add_compile_flags_if_supported(${target} PUBLIC -fno-rtti)
671   endif()
672 endfunction()
674 # Threading flags =============================================================
675 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY AND LIBCXX_ENABLE_SHARED)
676   # Need to allow unresolved symbols if this is to work with shared library builds
677   if (APPLE)
678     add_link_flags("-undefined dynamic_lookup")
679   else()
680     # Relax this restriction from HandleLLVMOptions
681     string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
682   endif()
683 endif()
685 # Assertion flags =============================================================
686 define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG)
687 define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG)
688 define_if(LIBCXX_ENABLE_ASSERTIONS -D_LIBCPP_DEBUG=0)
689 define_if(LIBCXX_DEBUG_BUILD -D_DEBUG)
690 if (LIBCXX_ENABLE_ASSERTIONS AND NOT LIBCXX_DEBUG_BUILD)
691   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
692   define_if_not(LIBCXX_TARGETING_MSVC -D_DEBUG)
693 endif()
695 # Modules flags ===============================================================
696 # FIXME The libc++ sources are fundamentally non-modular. They need special
697 # versions of the headers in order to provide C++03 and legacy ABI definitions.
698 # NOTE: The public headers can be used with modules in all other contexts.
699 function(cxx_add_module_flags target)
700   if (LLVM_ENABLE_MODULES)
701     # Ignore that the rest of the modules flags are now unused.
702     target_add_compile_flags_if_supported(${target} PUBLIC -Wno-unused-command-line-argument)
703     target_compile_options(${target} PUBLIC -fno-modules)
704   endif()
705 endfunction()
707 # Sanitizer flags =============================================================
709 function(get_sanitizer_flags OUT_VAR  USE_SANITIZER)
710   set(SANITIZER_FLAGS)
711   set(USE_SANITIZER "${USE_SANITIZER}")
712   # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
713   # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
714   if (USE_SANITIZER AND NOT MSVC)
715     append_flags_if_supported(SANITIZER_FLAGS "-fno-omit-frame-pointer")
716     append_flags_if_supported(SANITIZER_FLAGS "-gline-tables-only")
718     if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
719             NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
720       append_flags_if_supported(SANITIZER_FLAGS "-gline-tables-only")
721     endif()
722     if (USE_SANITIZER STREQUAL "Address")
723       append_flags(SANITIZER_FLAGS "-fsanitize=address")
724     elseif (USE_SANITIZER MATCHES "Memory(WithOrigins)?")
725       append_flags(SANITIZER_FLAGS -fsanitize=memory)
726       if (USE_SANITIZER STREQUAL "MemoryWithOrigins")
727         append_flags(SANITIZER_FLAGS "-fsanitize-memory-track-origins")
728       endif()
729     elseif (USE_SANITIZER STREQUAL "Undefined")
730       append_flags(SANITIZER_FLAGS "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all")
731     elseif (USE_SANITIZER STREQUAL "Address;Undefined" OR
732             USE_SANITIZER STREQUAL "Undefined;Address")
733       append_flags(SANITIZER_FLAGS "-fsanitize=address,undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all")
734     elseif (USE_SANITIZER STREQUAL "Thread")
735       append_flags(SANITIZER_FLAGS -fsanitize=thread)
736     elseif (USE_SANITIZER STREQUAL "DataFlow")
737       append_flags(SANITIZER_FLAGS -fsanitize=dataflow)
738     else()
739       message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${USE_SANITIZER}")
740     endif()
741   elseif(USE_SANITIZER AND MSVC)
742     message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
743   endif()
744   set(${OUT_VAR} "${SANITIZER_FLAGS}" PARENT_SCOPE)
745 endfunction()
747 # Configure for sanitizers. If LIBCXX_STANDALONE_BUILD then we have to do
748 # the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
749 if (LIBCXX_STANDALONE_BUILD)
750   set(LLVM_USE_SANITIZER "" CACHE STRING
751       "Define the sanitizer used to build the library and tests")
752 endif()
753 get_sanitizer_flags(SANITIZER_FLAGS "${LLVM_USE_SANITIZER}")
754 if (LIBCXX_STANDALONE_BUILD AND SANITIZER_FLAGS)
755   add_flags(${SANITIZER_FLAGS})
756 endif()
758 # Link system libraries =======================================================
759 function(cxx_link_system_libraries target)
761 # In order to remove just libc++ from the link step
762 # we need to use -nostdlib++ whenever it is supported.
763 # Unfortunately this cannot be used universally because for example g++ supports
764 # only -nodefaultlibs in which case all libraries will be removed and
765 # all libraries but c++ have to be added in manually.
766   if (LIBCXX_SUPPORTS_NOSTDLIBXX_FLAG)
767     target_add_link_flags_if_supported(${target} PRIVATE "-nostdlib++")
768   else()
769     target_add_link_flags_if_supported(${target} PRIVATE "-nodefaultlibs")
770     target_add_compile_flags_if_supported(${target} PRIVATE "/Zl")
771     target_add_link_flags_if_supported(${target} PRIVATE "/nodefaultlib")
772   endif()
774   if (LIBCXX_HAS_SYSTEM_LIB)
775     target_link_libraries(${target} PRIVATE System)
776   endif()
778   if (LIBCXX_HAS_PTHREAD_LIB)
779     target_link_libraries(${target} PRIVATE pthread)
780   endif()
782   if (LIBCXX_HAS_C_LIB)
783     target_link_libraries(${target} PRIVATE c)
784   endif()
786   if (LIBCXX_HAS_M_LIB)
787     target_link_libraries(${target} PRIVATE m)
788   endif()
790   if (LIBCXX_HAS_RT_LIB)
791     target_link_libraries(${target} PRIVATE rt)
792   endif()
794   if (LIBCXX_USE_COMPILER_RT)
795     find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
796     if (LIBCXX_BUILTINS_LIBRARY)
797       target_link_libraries(${target} PRIVATE "${LIBCXX_BUILTINS_LIBRARY}")
798     endif()
799   elseif (LIBCXX_HAS_GCC_LIB)
800     target_link_libraries(${target} PRIVATE gcc)
801   elseif (LIBCXX_HAS_GCC_S_LIB)
802     target_link_libraries(${target} PRIVATE gcc_s)
803   endif()
805   if (LIBCXX_HAS_ATOMIC_LIB)
806     target_link_libraries(${target} PRIVATE atomic)
807   endif()
809   if (MINGW)
810     target_link_libraries(${target} PRIVATE "${MINGW_LIBRARIES}")
811   endif()
813   if (LIBCXX_TARGETING_MSVC)
814     if (LIBCXX_DEBUG_BUILD)
815       set(LIB_SUFFIX "d")
816     else()
817       set(LIB_SUFFIX "")
818     endif()
820     target_link_libraries(${target} PRIVATE ucrt${LIB_SUFFIX}) # Universal C runtime
821     target_link_libraries(${target} PRIVATE vcruntime${LIB_SUFFIX}) # C++ runtime
822     target_link_libraries(${target} PRIVATE msvcrt${LIB_SUFFIX}) # C runtime startup files
823     target_link_libraries(${target} PRIVATE msvcprt${LIB_SUFFIX}) # C++ standard library. Required for exception_ptr internals.
824     # Required for standards-complaint wide character formatting functions
825     # (e.g. `printfw`/`scanfw`)
826     target_link_libraries(${target} PRIVATE iso_stdio_wide_specifiers)
827   endif()
829   if (ANDROID AND ANDROID_PLATFORM_LEVEL LESS 21)
830     target_link_libraries(${target} PUBLIC android_support)
831   endif()
832 endfunction()
834 # Windows-related flags =======================================================
835 function(cxx_add_windows_flags target)
836   if(WIN32 AND NOT MINGW)
837     target_compile_definitions(${target} PRIVATE
838                                  # Ignore the -MSC_VER mismatch, as we may build
839                                  # with a different compatibility version.
840                                  _ALLOW_MSC_VER_MISMATCH
841                                  # Don't check the msvcprt iterator debug levels
842                                  # as we will define the iterator types; libc++
843                                  # uses a different macro to identify the debug
844                                  # level.
845                                  _ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH
846                                  # We are building the c++ runtime, don't pull in
847                                  # msvcprt.
848                                  _CRTBLD
849                                  # Don't warn on the use of "deprecated"
850                                  # "insecure" functions which are standards
851                                  # specified.
852                                  _CRT_SECURE_NO_WARNINGS
853                                  # Use the ISO conforming behaviour for conversion
854                                  # in printf, scanf.
855                                  _CRT_STDIO_ISO_WIDE_SPECIFIERS)
856   endif()
857 endfunction()
859 # Configuration file flags =====================================================
860 if (NOT LIBCXX_ABI_VERSION EQUAL 1)
861   config_define(${LIBCXX_ABI_VERSION} _LIBCPP_ABI_VERSION)
862 endif()
863 if (NOT LIBCXX_ABI_NAMESPACE STREQUAL "")
864   if (NOT LIBCXX_ABI_NAMESPACE MATCHES "__.*")
865     message(FATAL_ERROR "LIBCXX_ABI_NAMESPACE must be a reserved identifier.")
866   endif()
867   if (LIBCXX_ABI_NAMESPACE MATCHES "__[0-9]+$")
868     message(FATAL_ERROR "LIBCXX_ABI_NAMESPACE '${LIBCXX_ABI_NAMESPACE}' is reserved for use by libc++.")
869   endif()
870   config_define(${LIBCXX_ABI_NAMESPACE} _LIBCPP_ABI_NAMESPACE)
871 endif()
872 config_define_if(LIBCXX_ABI_UNSTABLE _LIBCPP_ABI_UNSTABLE)
873 config_define_if(LIBCXX_ABI_FORCE_ITANIUM _LIBCPP_ABI_FORCE_ITANIUM)
874 config_define_if(LIBCXX_ABI_FORCE_MICROSOFT _LIBCPP_ABI_FORCE_MICROSOFT)
875 config_define_if(LIBCXX_HIDE_FROM_ABI_PER_TU_BY_DEFAULT _LIBCPP_HIDE_FROM_ABI_PER_TU_BY_DEFAULT)
876 config_define_if_not(LIBCXX_ENABLE_THREADS _LIBCPP_HAS_NO_THREADS)
877 config_define_if_not(LIBCXX_ENABLE_MONOTONIC_CLOCK _LIBCPP_HAS_NO_MONOTONIC_CLOCK)
878 if (NOT LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION STREQUAL "default")
879   config_define("${LIBCXX_TYPEINFO_COMPARISON_IMPLEMENTATION}" _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION)
880 endif()
881 config_define_if(LIBCXX_HAS_PTHREAD_API _LIBCPP_HAS_THREAD_API_PTHREAD)
882 config_define_if(LIBCXX_HAS_EXTERNAL_THREAD_API _LIBCPP_HAS_THREAD_API_EXTERNAL)
883 config_define_if(LIBCXX_HAS_WIN32_THREAD_API _LIBCPP_HAS_THREAD_API_WIN32)
884 config_define_if(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
885 config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC)
886 config_define_if(LIBCXX_NO_VCRUNTIME _LIBCPP_NO_VCRUNTIME)
887 config_define_if(LIBCXX_ENABLE_PARALLEL_ALGORITHMS _LIBCPP_HAS_PARALLEL_ALGORITHMS)
888 config_define_if_not(LIBCXX_ENABLE_FILESYSTEM _LIBCPP_HAS_NO_FILESYSTEM_LIBRARY)
889 config_define_if_not(LIBCXX_ENABLE_RANDOM_DEVICE _LIBCPP_HAS_NO_RANDOM_DEVICE)
890 config_define_if_not(LIBCXX_ENABLE_LOCALIZATION _LIBCPP_HAS_NO_LOCALIZATION)
891 config_define_if_not(LIBCXX_ENABLE_UNICODE _LIBCPP_HAS_NO_UNICODE)
892 config_define_if_not(LIBCXX_ENABLE_VENDOR_AVAILABILITY_ANNOTATIONS _LIBCPP_HAS_NO_VENDOR_AVAILABILITY_ANNOTATIONS)
893 # Incomplete features get their own specific disabling flags. This makes it
894 # easier to grep for target specific flags once the feature is complete.
895 config_define_if_not(LIBCXX_ENABLE_INCOMPLETE_FEATURES _LIBCPP_HAS_NO_INCOMPLETE_FORMAT)
896 config_define_if_not(LIBCXX_ENABLE_INCOMPLETE_FEATURES _LIBCPP_HAS_NO_INCOMPLETE_RANGES)
898 if (LIBCXX_ABI_DEFINES)
899   set(abi_defines)
900   foreach (abi_define ${LIBCXX_ABI_DEFINES})
901     if (NOT abi_define MATCHES "^_LIBCPP_ABI_")
902       message(SEND_ERROR "Invalid ABI macro ${abi_define} in LIBCXX_ABI_DEFINES")
903     endif()
904     list(APPEND abi_defines "#define ${abi_define}")
905   endforeach()
906   string(REPLACE ";" "\n" abi_defines "${abi_defines}")
907   config_define(${abi_defines} _LIBCPP_ABI_DEFINES)
908 endif()
910 # By default libc++ on Windows expects to use a shared library, which requires
911 # the headers to use DLL import/export semantics. However when building a
912 # static library only we modify the headers to disable DLL import/export.
913 if (DEFINED WIN32 AND LIBCXX_ENABLE_STATIC AND NOT LIBCXX_ENABLE_SHARED)
914   message(STATUS "Generating custom __config for non-DLL Windows build")
915   config_define(ON _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
916 endif()
918 if (WIN32 AND LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
919   # If linking libcxxabi statically into libcxx, skip the dllimport attributes
920   # on symbols we refer to from libcxxabi.
921   add_definitions(-D_LIBCXXABI_DISABLE_VISIBILITY_ANNOTATIONS)
922 endif()
924 # Setup all common build flags =================================================
925 function(cxx_add_common_build_flags target)
926   cxx_add_basic_build_flags(${target})
927   cxx_add_warning_flags(${target})
928   cxx_add_windows_flags(${target})
929   cxx_add_exception_flags(${target})
930   cxx_add_rtti_flags(${target})
931   cxx_add_module_flags(${target})
932   cxx_link_system_libraries(${target})
933 endfunction()
935 #===============================================================================
936 # Setup Source Code And Tests
937 #===============================================================================
938 add_subdirectory(include)
939 add_subdirectory(src)
940 add_subdirectory(utils)
942 set(LIBCXX_TEST_DEPS "")
944 if (LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
945   list(APPEND LIBCXX_TEST_DEPS cxx_experimental)
946 endif()
948 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
949   list(APPEND LIBCXX_TEST_DEPS cxx_external_threads)
950 endif()
952 if (LIBCXX_INCLUDE_BENCHMARKS)
953   add_subdirectory(benchmarks)
954 endif()
956 if (LIBCXX_INCLUDE_TESTS)
957   add_subdirectory(test)
958   add_subdirectory(lib/abi)
959   if (LIBCXX_STANDALONE_BUILD)
960     include(AddLLVM) # for get_llvm_lit_path
961     # Make sure the llvm-lit script is generated into the bin directory, and
962     # do it after adding all tests, since the generated script will only work
963     # correctly discovered tests against test locations from the source tree
964     # that have already been discovered.
965     add_subdirectory(${LLVM_MAIN_SRC_DIR}/utils/llvm-lit
966                      ${CMAKE_CURRENT_BINARY_DIR}/llvm-lit)
967   endif()
968 endif()
970 if (LIBCXX_INCLUDE_DOCS)
971   add_subdirectory(docs)
972 endif()