1 # See www/CMake.html for instructions on how to build libcxx with CMake.
3 #===============================================================================
5 #===============================================================================
6 cmake_minimum_required(VERSION 3.4.3)
9 cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
12 cmake_policy(SET CMP0022 NEW) # Required when interacting with LLVM and Clang
15 # Add path for custom modules
17 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
23 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
26 set(PACKAGE_NAME libcxx)
27 set(PACKAGE_VERSION 4.0.0)
28 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
29 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
31 # Find the LLVM sources and simulate LLVM CMake options.
32 include(HandleOutOfTreeLLVM)
35 # Require out of source build.
36 include(MacroEnsureOutOfSourceBuild)
37 MACRO_ENSURE_OUT_OF_SOURCE_BUILD(
38 "${PROJECT_NAME} requires an out of source build. Please create a separate
39 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there."
42 #===============================================================================
44 #===============================================================================
45 include(CMakeDependentOption)
47 # Basic options ---------------------------------------------------------------
48 option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
49 option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON)
50 option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON)
51 option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON)
52 option(LIBCXX_ENABLE_FILESYSTEM
53 "Build filesystem as part of libc++experimental.a" ${LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY})
54 option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS})
56 # Benchmark options -----------------------------------------------------------
57 option(LIBCXX_INCLUDE_BENCHMARKS "Build the libc++ benchmarks and their dependancies" ON)
58 set(LIBCXX_BENCHMARK_NATIVE_STDLIB "" CACHE STRING
59 "Build the benchmarks against the specified native STL.
60 The value must be one of libc++/libstdc++")
61 set(LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN "" CACHE STRING
62 "Use alternate GCC toolchain when building the native benchmarks")
64 if (LIBCXX_BENCHMARK_NATIVE_STDLIB)
65 if (NOT (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++"
66 OR LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++"))
67 message(FATAL_ERROR "Invalid value for LIBCXX_BENCHMARK_NATIVE_STDLIB: "
68 "'${LIBCXX_BENCHMARK_NATIVE_STDLIB}'")
72 option(LIBCXX_INCLUDE_DOCS "Build the libc++ documentation." ${LLVM_INCLUDE_DOCS})
73 set(LIBCXX_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
74 "Define suffix of library directory name (32/64)")
75 option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON)
76 option(LIBCXX_INSTALL_LIBRARY "Install the libc++ library." ON)
77 option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON)
78 cmake_dependent_option(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY
79 "Install libc++experimental.a" ON
80 "LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY;LIBCXX_INSTALL_LIBRARY" OFF)
81 set(LIBCXX_ABI_VERSION 1 CACHE STRING "ABI version of libc++.")
82 option(LIBCXX_ABI_UNSTABLE "Unstable ABI of libc++." OFF)
83 option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
85 if (NOT LIBCXX_ENABLE_SHARED AND NOT LIBCXX_ENABLE_STATIC)
86 message(FATAL_ERROR "libc++ must be built as either a shared or static library.")
89 # ABI Library options ---------------------------------------------------------
90 set(LIBCXX_CXX_ABI "default" CACHE STRING
91 "Specify C++ ABI library to use.")
92 set(CXXABIS none default libcxxabi libcxxrt libstdc++ libsupc++)
93 set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS})
95 # FIXME: This is a temporary hack to force LLVM buildbots to store
96 # the fixed cache entry instead of the previous cache entry. This is needed
97 # because some LLVM buildbots don't clear their cache. It will be removed
98 # once all LLVM bots have been run.
99 if (LIBCXX_CXX_ABI STREQUAL "")
100 set(LIBCXX_CXX_ABI "default" CACHE STRING
101 "Specify the C++ ABI library to use." FORCE)
104 # Setup the default options if LIBCXX_CXX_ABI is not specified.
105 if (LIBCXX_CXX_ABI STREQUAL "default")
107 LIBCXX_LIBCXXABI_INCLUDES_INTERNAL
109 PATHS ${LLVM_MAIN_SRC_DIR}/projects/libcxxabi/include
110 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxxabi/include
113 if (NOT DEFINED LIBCXX_STANDALONE_BUILD AND
114 IS_DIRECTORY "${LIBCXX_LIBCXXABI_INCLUDES_INTERNAL}")
115 set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi")
116 set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_LIBCXXABI_INCLUDES_INTERNAL}")
117 set(LIBCXX_CXX_ABI_INTREE 1)
119 if (WIN32 AND NOT MINGW)
120 # FIXME: Figure out how to configure the ABI library on Windows.
121 set(LIBCXX_CXX_ABI_LIBNAME "none")
123 set(LIBCXX_CXX_ABI_LIBNAME "default")
127 set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}")
130 # Use a static copy of the ABI library when linking libc++. This option
131 # cannot be used with LIBCXX_ENABLE_ABI_LINKER_SCRIPT.
132 option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY "Statically link the ABI library" OFF)
134 # Generate and install a linker script inplace of libc++.so. The linker script
135 # will link libc++ to the correct ABI library. This option is on by default
136 # On UNIX platforms other than Apple unless 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY'
137 # is on. This option is also disabled when the ABI library is not specified
138 # or is specified to be "none".
139 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF)
140 if (LLVM_HAVE_LINK_VERSION_SCRIPT AND NOT LIBCXX_ENABLE_STATIC_ABI_LIBRARY
141 AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "none"
142 AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "default"
143 AND PYTHONINTERP_FOUND
144 AND LIBCXX_ENABLE_SHARED)
145 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON)
148 option(LIBCXX_ENABLE_ABI_LINKER_SCRIPT
149 "Use and install a linker script for the given ABI library"
150 ${ENABLE_LINKER_SCRIPT_DEFAULT_VALUE})
152 # Build libc++abi with libunwind. We need this option to determine whether to
153 # link with libunwind or libgcc_s while running the test cases.
154 option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF)
156 # Target options --------------------------------------------------------------
157 option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++." ${LLVM_BUILD_32_BITS})
158 set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.")
159 set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.")
161 # Feature options -------------------------------------------------------------
162 option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON)
163 option(LIBCXX_ENABLE_RTTI "Use run time type information." ON)
164 option(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE "Build libc++ with support for the global filesystem namespace." ON)
165 option(LIBCXX_ENABLE_STDIN "Build libc++ with support for stdin/std::cin." ON)
166 option(LIBCXX_ENABLE_STDOUT "Build libc++ with support for stdout/std::cout." ON)
167 option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON)
168 option(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS "Build libc++ with support for thread-unsafe C functions" ON)
169 option(LIBCXX_ENABLE_MONOTONIC_CLOCK
170 "Build libc++ with support for a monotonic clock.
171 This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON)
172 option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF)
173 option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF)
174 option(LIBCXX_HAS_EXTERNAL_THREAD_API
175 "Build libc++ with an externalized threading API.
176 This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON." OFF)
177 option(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY
178 "Build libc++ with an externalized threading library.
179 This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON" OFF)
181 # Misc options ----------------------------------------------------------------
182 # FIXME: Turn -pedantic back ON. It is currently off because it warns
183 # about #include_next which is used everywhere.
184 option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF)
185 option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
186 option(LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS "Disable #warnings about conflicting macros." OFF)
188 option(LIBCXX_GENERATE_COVERAGE "Enable generating code coverage." OFF)
189 set(LIBCXX_COVERAGE_LIBRARY "" CACHE STRING
190 "The Profile-rt library used to build with code coverage")
192 # Don't allow a user to accidentally overwrite the system libc++ installation on Darwin.
193 # If the user specifies -DCMAKE_INSTALL_PREFIX=/usr the install rules for libc++
194 # will not be generated and a warning will be issued.
195 option(LIBCXX_OVERRIDE_DARWIN_INSTALL "Enable overwriting darwins libc++ installation." OFF)
196 mark_as_advanced(LIBCXX_OVERRIDE_DARWIN_INSTALL) # Don't show this option by default.
198 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT LIBCXX_OVERRIDE_DARWIN_INSTALL)
199 if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr")
200 message(WARNING "Disabling libc++ install rules because installation would "
201 "overwrite the systems installation. Configure with "
202 "-DLIBCXX_OVERRIDE_DARWIN_INSTALL=ON to suppress this behaviour.")
203 mark_as_advanced(CLEAR LIBCXX_OVERRIDE_DARWIN_INSTALL) # Show the override option.
204 set(LIBCXX_INSTALL_HEADERS OFF)
205 set(LIBCXX_INSTALL_LIBRARY OFF)
209 set(LIBCXX_CONFIGURE_IDE_DEFAULT OFF)
210 if (XCODE OR MSVC_IDE)
211 set(LIBCXX_CONFIGURE_IDE_DEFAULT ON)
213 option(LIBCXX_CONFIGURE_IDE "Configure libcxx for use within an IDE"
214 ${LIBCXX_CONFIGURE_IDE_DEFAULT})
216 #===============================================================================
217 # Check option configurations
218 #===============================================================================
220 if (LIBCXX_ENABLE_FILESYSTEM AND NOT LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY)
222 "LIBCXX_ENABLE_FILESYSTEM cannot be turned on when LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF")
225 # Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON only when
226 # LIBCXX_ENABLE_THREADS is on.
227 if(LIBCXX_ENABLE_THREADS AND NOT LIBCXX_ENABLE_MONOTONIC_CLOCK)
228 message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF"
229 " when LIBCXX_ENABLE_THREADS is also set to OFF.")
232 if(NOT LIBCXX_ENABLE_THREADS)
233 if(LIBCXX_HAS_PTHREAD_API)
234 message(FATAL_ERROR "LIBCXX_HAS_PTHREAD_API can only be set to ON"
235 " when LIBCXX_ENABLE_THREADS is also set to ON.")
237 if(LIBCXX_HAS_EXTERNAL_THREAD_API)
238 message(FATAL_ERROR "LIBCXX_HAS_EXTERNAL_THREAD_API can only be set to ON"
239 " when LIBCXX_ENABLE_THREADS is also set to ON.")
241 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
242 message(FATAL_ERROR "LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY can only be set "
243 "to ON when LIBCXX_ENABLE_THREADS is also set to ON.")
248 if (LIBCXX_HAS_EXTERNAL_THREAD_API)
249 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY)
250 message(FATAL_ERROR "The options LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY and "
251 "LIBCXX_HAS_EXTERNAL_THREAD_API cannot both be ON at "
254 if (LIBCXX_HAS_PTHREAD_API)
255 message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API"
256 "and LIBCXX_HAS_PTHREAD_API cannot be both"
257 "set to ON at the same time.")
261 # Ensure LLVM_USE_SANITIZER is not specified when LIBCXX_GENERATE_COVERAGE
263 if (LLVM_USE_SANITIZER AND LIBCXX_GENERATE_COVERAGE)
264 message(FATAL_ERROR "LLVM_USE_SANITIZER cannot be used with LIBCXX_GENERATE_COVERAGE")
267 # Set LIBCXX_BUILD_32_BITS to (LIBCXX_BUILD_32_BITS OR LLVM_BUILD_32_BITS)
268 # and check that we can build with 32 bits if requested.
269 if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
270 if (LIBCXX_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
271 message(STATUS "Building 32 bits executables and libraries.")
273 elseif(LIBCXX_BUILD_32_BITS)
274 message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.")
277 # Check that this option is not enabled on Apple and emit a usage warning.
278 if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY)
280 message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is not supported on OS X")
282 message(WARNING "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option")
284 if (LIBCXX_ENABLE_STATIC AND NOT PYTHONINTERP_FOUND)
285 message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY requires python but it was not found.")
289 if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
291 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets")
293 if (NOT PYTHONINTERP_FOUND)
294 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT requires python but it was not found.")
296 if (NOT LIBCXX_ENABLE_SHARED)
297 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.")
301 if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT)
302 message(FATAL_ERROR "Conflicting options given.
303 LIBCXX_ENABLE_STATIC_ABI_LIBRARY cannot be specified with
304 LIBCXX_ENABLE_ABI_LINKER_SCRIPT")
307 if (LIBCXX_HAS_MUSL_LIBC AND NOT LIBCXX_INSTALL_SUPPORT_HEADERS)
308 message(FATAL_ERROR "LIBCXX_INSTALL_SUPPORT_HEADERS can not be turned off"
309 "when building for Musl with LIBCXX_HAS_MUSL_LIBC.")
312 #===============================================================================
314 #===============================================================================
316 set(LIBCXX_COMPILER ${CMAKE_CXX_COMPILER})
317 set(LIBCXX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
318 set(LIBCXX_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
319 set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX})
321 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
322 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR})
324 # Declare libc++ configuration variables.
325 # They are intended for use as follows:
326 # LIBCXX_CXX_FLAGS: General flags for both the compiler and linker.
327 # LIBCXX_COMPILE_FLAGS: Compile only flags.
328 # LIBCXX_LINK_FLAGS: Linker only flags.
329 # LIBCXX_LIBRARIES: libraries libc++ is linked to.
330 # LIBCXX_INTERFACE_LIBRARIES: Libraries that must be linked when using libc++
331 # These libraries are exposed in the linker script.
332 set(LIBCXX_COMPILE_FLAGS "")
333 set(LIBCXX_LINK_FLAGS "")
334 set(LIBCXX_LIBRARIES "")
335 set(LIBCXX_INTERFACE_LIBRARIES "")
337 # Include macros for adding and removing libc++ flags.
338 include(HandleLibcxxFlags)
340 # Target flags ================================================================
341 # These flags get added to CMAKE_CXX_FLAGS and CMAKE_C_FLAGS so that
342 # 'config-ix' use them during feature checks. It also adds them to both
343 # 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS'
344 add_target_flags_if(LIBCXX_BUILD_32_BITS "-m32")
345 add_target_flags_if(LIBCXX_TARGET_TRIPLE "-target ${LIBCXX_TARGET_TRIPLE}")
346 add_target_flags_if(LIBCXX_SYSROOT "--sysroot=${LIBCXX_SYSROOT}")
347 add_target_flags_if(LIBCXX_GCC_TOOLCHAIN "-gcc-toolchain ${LIBCXX_GCC_TOOLCHAIN}")
348 if (LIBCXX_TARGET_TRIPLE)
349 set(TARGET_TRIPLE "${LIBCXX_TARGET_TRIPLE}")
352 # Configure compiler.
355 if (LIBCXX_USE_COMPILER_RT)
356 list(APPEND LIBCXX_LINK_FLAGS "-rtlib=compiler-rt")
359 # Configure coverage options.
360 if (LIBCXX_GENERATE_COVERAGE)
361 include(CodeCoverage)
362 set(CMAKE_BUILD_TYPE "COVERAGE" CACHE STRING "" FORCE)
365 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
367 #===============================================================================
368 # Setup Compiler Flags
369 #===============================================================================
371 include(HandleLibCXXABI) # Setup the ABI library flags
373 if (NOT LIBCXX_STANDALONE_BUILD)
374 # Remove flags that may have snuck in.
375 remove_flags(-DNDEBUG -UNDEBUG -D_DEBUG
378 remove_flags(-stdlib=libc++ -stdlib=libstdc++)
380 # FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC.
381 # Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors
382 # so they don't get transformed into -Wno and -errors respectivly.
383 remove_flags(-Wno-pedantic -pedantic-errors -pedantic)
385 # Required flags ==============================================================
386 set(LIBCXX_STANDARD_VER c++11 CACHE INTERNAL "internal option to change build dialect")
387 if (LIBCXX_HAS_MUSL_LIBC)
388 # musl's pthread implementations uses volatile types in their structs which is
389 # not a constexpr in C++11 but is in C++14, so we use C++14 with musl.
390 set(LIBCXX_STANDARD_VER c++14 CACHE INTERNAL "internal option to change build dialect")
392 add_compile_flags_if_supported(-std=${LIBCXX_STANDARD_VER})
393 mangle_name("LIBCXX_SUPPORTS_STD_EQ_${LIBCXX_STANDARD_VER}_FLAG" SUPPORTS_DIALECT_NAME)
394 if(NOT ${SUPPORTS_DIALECT_NAME})
395 if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" AND NOT "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
396 message(FATAL_ERROR "C++11 or greater is required but the compiler does not support ${LIBCXX_STANDARD_VER}")
400 # On all systems the system c++ standard library headers need to be excluded.
401 # MSVC only has -X, which disables all default includes; including the crt.
402 # Thus, we do nothing and hope we don't accidentally include any of the C++
404 add_compile_flags_if_supported(-nostdinc++)
406 # Hide all inline function definitions which have not explicitly been marked
407 # visible. This prevents new definitions for inline functions from appearing in
408 # the dylib when get ODR used by another function.
409 add_compile_flags_if_supported(-fvisibility-inlines-hidden)
411 # Let the library headers know they are currently being used to build the
413 add_definitions(-D_LIBCPP_BUILDING_LIBRARY)
415 # Warning flags ===============================================================
416 add_definitions(-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
417 add_compile_flags_if_supported(
418 -Wall -Wextra -W -Wwrite-strings
419 -Wno-unused-parameter -Wno-long-long
421 if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")
422 add_compile_flags_if_supported(
423 -Wno-user-defined-literals
424 -Wno-covered-switch-default)
425 elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU")
426 add_compile_flags_if_supported(
430 if (LIBCXX_ENABLE_WERROR)
431 add_compile_flags_if_supported(-Werror)
432 add_compile_flags_if_supported(-WX)
434 # TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is
436 add_compile_flags_if_supported(-Wno-error)
438 if (LIBCXX_ENABLE_PEDANTIC)
439 add_compile_flags_if_supported(-pedantic)
441 if (LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS)
442 add_definitions(-D_LIBCPP_DISABLE_MACRO_CONFLICT_WARNINGS)
445 # Exception flags =============================================================
446 if (LIBCXX_ENABLE_EXCEPTIONS)
447 # Catches C++ exceptions only and tells the compiler to assume that extern C
448 # functions never throw a C++ exception.
449 add_compile_flags_if_supported(-EHsc)
451 add_definitions(-D_LIBCPP_NO_EXCEPTIONS)
452 add_compile_flags_if_supported(-EHs- -EHa-)
453 add_compile_flags_if_supported(-fno-exceptions)
456 # RTTI flags ==================================================================
457 if (NOT LIBCXX_ENABLE_RTTI)
458 add_definitions(-D_LIBCPP_NO_RTTI)
459 add_compile_flags_if_supported(-GR-)
460 add_compile_flags_if_supported(-fno-rtti)
463 # Threading flags =============================================================
464 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY AND LIBCXX_ENABLE_SHARED)
465 # Need to allow unresolved symbols if this is to work with shared library builds
467 add_link_flags("-undefined dynamic_lookup")
469 # Relax this restriction from HandleLLVMOptions
470 string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}")
474 # Assertion flags =============================================================
475 define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG)
476 define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG)
477 if (LIBCXX_ENABLE_ASSERTIONS)
478 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
479 define_if_not(MSVC -D_DEBUG)
482 # Modules flags ===============================================================
483 # FIXME The libc++ sources are fundamentally non-modular. They need special
484 # versions of the headers in order to provide C++03 and legacy ABI definitions.
485 # NOTE: The public headers can be used with modules in all other contexts.
486 if (LLVM_ENABLE_MODULES)
487 # Ignore that the rest of the modules flags are now unused.
488 add_compile_flags_if_supported(-Wno-unused-command-line-argument)
489 add_compile_flags(-fno-modules)
492 # Sanitizer flags =============================================================
494 # Configure for sanitizers. If LIBCXX_STANDALONE_BUILD then we have to do
495 # the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it.
496 if (LIBCXX_STANDALONE_BUILD)
497 set(LLVM_USE_SANITIZER "" CACHE STRING
498 "Define the sanitizer used to build the library and tests")
499 # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC.
500 # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do.
501 if (LLVM_USE_SANITIZER AND NOT MSVC)
502 add_flags_if_supported("-fno-omit-frame-pointer")
503 add_flags_if_supported("-gline-tables-only")
505 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND
506 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO")
507 add_flags_if_supported("-gline-tables-only")
509 if (LLVM_USE_SANITIZER STREQUAL "Address")
510 add_flags("-fsanitize=address")
511 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?")
512 add_flags(-fsanitize=memory)
513 if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins")
514 add_flags("-fsanitize-memory-track-origins")
516 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined")
517 add_flags("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all")
518 elseif (LLVM_USE_SANITIZER STREQUAL "Thread")
519 add_flags(-fsanitize=thread)
521 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}")
523 elseif(LLVM_USE_SANITIZER AND MSVC)
524 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.")
528 # Configuration file flags =====================================================
529 if (NOT LIBCXX_ABI_VERSION EQUAL "1")
530 config_define(${LIBCXX_ABI_VERSION} _LIBCPP_ABI_VERSION)
532 config_define_if(LIBCXX_ABI_UNSTABLE _LIBCPP_ABI_UNSTABLE)
534 config_define_if_not(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE)
535 config_define_if_not(LIBCXX_ENABLE_STDIN _LIBCPP_HAS_NO_STDIN)
536 config_define_if_not(LIBCXX_ENABLE_STDOUT _LIBCPP_HAS_NO_STDOUT)
537 config_define_if_not(LIBCXX_ENABLE_THREADS _LIBCPP_HAS_NO_THREADS)
538 config_define_if_not(LIBCXX_ENABLE_MONOTONIC_CLOCK _LIBCPP_HAS_NO_MONOTONIC_CLOCK)
539 config_define_if_not(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS)
541 config_define_if(LIBCXX_HAS_PTHREAD_API _LIBCPP_HAS_THREAD_API_PTHREAD)
542 config_define_if(LIBCXX_HAS_EXTERNAL_THREAD_API _LIBCPP_HAS_THREAD_API_EXTERNAL)
543 config_define_if(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL)
544 config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC)
546 # By default libc++ on Windows expects to use a shared library, which requires
547 # the headers to use DLL import/export semantics. However when building a
548 # static library only we modify the headers to disable DLL import/export.
549 if (DEFINED WIN32 AND LIBCXX_ENABLE_STATIC AND NOT LIBCXX_ENABLE_SHARED)
550 message(STATUS "Generating custom __config for non-DLL Windows build")
551 config_define(ON _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)
554 if (LIBCXX_NEEDS_SITE_CONFIG)
555 configure_file("include/__config_site.in"
556 "${LIBCXX_BINARY_DIR}/__config_site"
559 # Provide the config definitions by included the generated __config_site
560 # file at compile time.
561 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC")
562 add_compile_flags("/FI\"${LIBCXX_BINARY_DIR}/__config_site\"")
564 add_compile_flags("-include ${LIBCXX_BINARY_DIR}/__config_site")
568 #===============================================================================
569 # Setup Source Code And Tests
570 #===============================================================================
571 include_directories(include)
572 add_subdirectory(include)
573 add_subdirectory(lib)
576 if (LIBCXX_INCLUDE_BENCHMARKS)
577 add_subdirectory(benchmarks)
579 if (LIBCXX_INCLUDE_TESTS)
580 add_subdirectory(test)
581 add_subdirectory(lib/abi)
583 if (LIBCXX_INCLUDE_DOCS)
584 add_subdirectory(docs)