1# See www/CMake.html for instructions on how to build libcxx with CMake. 2 3#=============================================================================== 4# Setup Project 5#=============================================================================== 6cmake_minimum_required(VERSION 3.4.3) 7 8if(POLICY CMP0042) 9 cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default 10endif() 11if(POLICY CMP0022) 12 cmake_policy(SET CMP0022 NEW) # Required when interacting with LLVM and Clang 13endif() 14 15# Add path for custom modules 16set(CMAKE_MODULE_PATH 17 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" 19 ${CMAKE_MODULE_PATH} 20 ) 21 22if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) 23 project(libcxx CXX C) 24 25 set(PACKAGE_NAME libcxx) 26 set(PACKAGE_VERSION 4.0.0svn) 27 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 28 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org") 29 30 # Find the LLVM sources and simulate LLVM CMake options. 31 include(HandleOutOfTreeLLVM) 32endif() 33 34if (LIBCXX_STANDALONE_BUILD) 35 include(FindPythonInterp) 36 if( NOT PYTHONINTERP_FOUND ) 37 message(WARNING "Failed to find python interpreter. " 38 "The libc++ test suite will be disabled.") 39 set(LLVM_INCLUDE_TESTS OFF) 40 endif() 41endif() 42 43# Require out of source build. 44include(MacroEnsureOutOfSourceBuild) 45MACRO_ENSURE_OUT_OF_SOURCE_BUILD( 46 "${PROJECT_NAME} requires an out of source build. Please create a separate 47 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there." 48 ) 49 50if (MSVC) 51 set(LIBCXX_TARGETING_MSVC ON) 52else() 53 set(LIBCXX_TARGETING_MSVC OFF) 54endif() 55 56#=============================================================================== 57# Setup CMake Options 58#=============================================================================== 59include(CMakeDependentOption) 60include(HandleCompilerRT) 61 62# Basic options --------------------------------------------------------------- 63option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." OFF) 64option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON) 65option(LIBCXX_ENABLE_STATIC "Build libc++ as a static library." ON) 66option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON) 67option(LIBCXX_ENABLE_FILESYSTEM 68 "Build filesystem as part of libc++experimental.a" ${LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY}) 69option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS}) 70 71# Benchmark options ----------------------------------------------------------- 72option(LIBCXX_INCLUDE_BENCHMARKS "Build the libc++ benchmarks and their dependancies" ON) 73set(LIBCXX_BENCHMARK_NATIVE_STDLIB "" CACHE STRING 74 "Build the benchmarks against the specified native STL. 75 The value must be one of libc++/libstdc++") 76set(LIBCXX_BENCHMARK_NATIVE_GCC_TOOLCHAIN "" CACHE STRING 77 "Use alternate GCC toolchain when building the native benchmarks") 78 79if (LIBCXX_BENCHMARK_NATIVE_STDLIB) 80 if (NOT (LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libc++" 81 OR LIBCXX_BENCHMARK_NATIVE_STDLIB STREQUAL "libstdc++")) 82 message(FATAL_ERROR "Invalid value for LIBCXX_BENCHMARK_NATIVE_STDLIB: " 83 "'${LIBCXX_BENCHMARK_NATIVE_STDLIB}'") 84 endif() 85endif() 86 87option(LIBCXX_INCLUDE_DOCS "Build the libc++ documentation." ${LLVM_INCLUDE_DOCS}) 88set(LIBCXX_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING 89 "Define suffix of library directory name (32/64)") 90option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON) 91option(LIBCXX_INSTALL_LIBRARY "Install the libc++ library." ON) 92option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON) 93cmake_dependent_option(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY 94 "Install libc++experimental.a" ON 95 "LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY;LIBCXX_INSTALL_LIBRARY" OFF) 96set(LIBCXX_ABI_VERSION 1 CACHE STRING "ABI version of libc++.") 97option(LIBCXX_ABI_UNSTABLE "Unstable ABI of libc++." OFF) 98option(LIBCXX_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF) 99 100if (NOT LIBCXX_ENABLE_SHARED AND NOT LIBCXX_ENABLE_STATIC) 101 message(FATAL_ERROR "libc++ must be built as either a shared or static library.") 102endif() 103 104# ABI Library options --------------------------------------------------------- 105set(LIBCXX_CXX_ABI "default" CACHE STRING 106 "Specify C++ ABI library to use.") 107set(CXXABIS none default libcxxabi libcxxrt libstdc++ libsupc++ vcruntime) 108set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS}) 109 110# Setup the default options if LIBCXX_CXX_ABI is not specified. 111if (LIBCXX_CXX_ABI STREQUAL "default") 112 find_path( 113 LIBCXX_LIBCXXABI_INCLUDES_INTERNAL 114 cxxabi.h 115 PATHS ${LLVM_MAIN_SRC_DIR}/projects/libcxxabi/include 116 ${LLVM_MAIN_SRC_DIR}/runtimes/libcxxabi/include 117 NO_DEFAULT_PATH 118 ) 119 if ((NOT LIBCXX_STANDALONE_BUILD OR HAVE_LIBCXXABI) AND 120 IS_DIRECTORY "${LIBCXX_LIBCXXABI_INCLUDES_INTERNAL}") 121 set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi") 122 set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${LIBCXX_LIBCXXABI_INCLUDES_INTERNAL}") 123 set(LIBCXX_CXX_ABI_INTREE 1) 124 else() 125 if (LIBCXX_TARGETING_MSVC) 126 # FIXME: Figure out how to configure the ABI library on Windows. 127 set(LIBCXX_CXX_ABI_LIBNAME "vcruntime") 128 elseif(APPLE) 129 set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi") 130 set(LIBCXX_CXX_ABI_SYSTEM 1) 131 else() 132 set(LIBCXX_CXX_ABI_LIBNAME "default") 133 endif() 134 endif() 135else() 136 set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}") 137endif() 138 139# Use a static copy of the ABI library when linking libc++. This option 140# cannot be used with LIBCXX_ENABLE_ABI_LINKER_SCRIPT. 141option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY "Statically link the ABI library" OFF) 142 143# Generate and install a linker script inplace of libc++.so. The linker script 144# will link libc++ to the correct ABI library. This option is on by default 145# On UNIX platforms other than Apple unless 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' 146# is on. This option is also disabled when the ABI library is not specified 147# or is specified to be "none". 148set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF) 149if (LLVM_HAVE_LINK_VERSION_SCRIPT AND NOT LIBCXX_ENABLE_STATIC_ABI_LIBRARY 150 AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "none" 151 AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "default" 152 AND PYTHONINTERP_FOUND 153 AND LIBCXX_ENABLE_SHARED) 154 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON) 155endif() 156 157option(LIBCXX_ENABLE_ABI_LINKER_SCRIPT 158 "Use and install a linker script for the given ABI library" 159 ${ENABLE_LINKER_SCRIPT_DEFAULT_VALUE}) 160 161set(ENABLE_NEW_DELETE_DEFAULT ON) 162if (LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS) 163# FIXME: This option should default to off. Unfortunatly GCC 4.9 fails to link 164# programs to due undefined references to new/delete in libc++abi so to work 165# around this libc++abi currently defaults LIBCXXABI_ENABLE_NEW_DELETE_DEFINITIONS 166# to ON. Once the GCC bug has been worked around this option should be changed 167# back to OFF. 168 set(ENABLE_NEW_DELETE_DEFAULT ON) 169endif() 170 171option(LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS 172 "Build libc++ with definitions for operator new/delete. This option can 173 be used to disable the definitions when libc++abi is expected to provide 174 them" ${ENABLE_NEW_DELETE_DEFAULT}) 175 176# Build libc++abi with libunwind. We need this option to determine whether to 177# link with libunwind or libgcc_s while running the test cases. 178option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF) 179option(LIBCXXABI_ENABLE_STATIC_UNWINDER "Statically link the LLVM unwinder." OFF) 180 181# Target options -------------------------------------------------------------- 182option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++." ${LLVM_BUILD_32_BITS}) 183set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.") 184set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.") 185 186# Feature options ------------------------------------------------------------- 187option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON) 188option(LIBCXX_ENABLE_RTTI "Use run time type information." ON) 189option(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE "Build libc++ with support for the global filesystem namespace." ON) 190option(LIBCXX_ENABLE_STDIN "Build libc++ with support for stdin/std::cin." ON) 191option(LIBCXX_ENABLE_STDOUT "Build libc++ with support for stdout/std::cout." ON) 192option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON) 193option(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS "Build libc++ with support for thread-unsafe C functions" ON) 194option(LIBCXX_ENABLE_MONOTONIC_CLOCK 195 "Build libc++ with support for a monotonic clock. 196 This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON) 197option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF) 198option(LIBCXX_HAS_PTHREAD_API "Ignore auto-detection and force use of pthread API" OFF) 199option(LIBCXX_HAS_EXTERNAL_THREAD_API 200 "Build libc++ with an externalized threading API. 201 This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON." OFF) 202option(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY 203 "Build libc++ with an externalized threading library. 204 This option may only be set to ON when LIBCXX_ENABLE_THREADS=ON" OFF) 205 206# Misc options ---------------------------------------------------------------- 207# FIXME: Turn -pedantic back ON. It is currently off because it warns 208# about #include_next which is used everywhere. 209option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF) 210option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) 211option(LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS "Disable #warnings about conflicting macros." OFF) 212 213option(LIBCXX_GENERATE_COVERAGE "Enable generating code coverage." OFF) 214set(LIBCXX_COVERAGE_LIBRARY "" CACHE STRING 215 "The Profile-rt library used to build with code coverage") 216 217# Don't allow a user to accidentally overwrite the system libc++ installation on Darwin. 218# If the user specifies -DCMAKE_INSTALL_PREFIX=/usr the install rules for libc++ 219# will not be generated and a warning will be issued. 220option(LIBCXX_OVERRIDE_DARWIN_INSTALL "Enable overwriting darwins libc++ installation." OFF) 221mark_as_advanced(LIBCXX_OVERRIDE_DARWIN_INSTALL) # Don't show this option by default. 222 223if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT LIBCXX_OVERRIDE_DARWIN_INSTALL) 224 if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr") 225 message(WARNING "Disabling libc++ install rules because installation would " 226 "overwrite the systems installation. Configure with " 227 "-DLIBCXX_OVERRIDE_DARWIN_INSTALL=ON to suppress this behaviour.") 228 mark_as_advanced(CLEAR LIBCXX_OVERRIDE_DARWIN_INSTALL) # Show the override option. 229 set(LIBCXX_INSTALL_HEADERS OFF) 230 set(LIBCXX_INSTALL_LIBRARY OFF) 231 endif() 232endif() 233 234set(LIBCXX_CONFIGURE_IDE_DEFAULT OFF) 235if (XCODE OR MSVC_IDE) 236 set(LIBCXX_CONFIGURE_IDE_DEFAULT ON) 237endif() 238option(LIBCXX_CONFIGURE_IDE "Configure libcxx for use within an IDE" 239 ${LIBCXX_CONFIGURE_IDE_DEFAULT}) 240 241#=============================================================================== 242# Check option configurations 243#=============================================================================== 244 245if (LIBCXX_ENABLE_FILESYSTEM AND NOT LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY) 246 message(FATAL_ERROR 247 "LIBCXX_ENABLE_FILESYSTEM cannot be turned on when LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY=OFF") 248endif() 249 250# Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON only when 251# LIBCXX_ENABLE_THREADS is on. 252if(LIBCXX_ENABLE_THREADS AND NOT LIBCXX_ENABLE_MONOTONIC_CLOCK) 253 message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF" 254 " when LIBCXX_ENABLE_THREADS is also set to OFF.") 255endif() 256 257if(NOT LIBCXX_ENABLE_THREADS) 258 if(LIBCXX_HAS_PTHREAD_API) 259 message(FATAL_ERROR "LIBCXX_HAS_PTHREAD_API can only be set to ON" 260 " when LIBCXX_ENABLE_THREADS is also set to ON.") 261 endif() 262 if(LIBCXX_HAS_EXTERNAL_THREAD_API) 263 message(FATAL_ERROR "LIBCXX_HAS_EXTERNAL_THREAD_API can only be set to ON" 264 " when LIBCXX_ENABLE_THREADS is also set to ON.") 265 endif() 266 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY) 267 message(FATAL_ERROR "LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY can only be set " 268 "to ON when LIBCXX_ENABLE_THREADS is also set to ON.") 269 endif() 270 271endif() 272 273if (LIBCXX_HAS_EXTERNAL_THREAD_API) 274 if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY) 275 message(FATAL_ERROR "The options LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY and " 276 "LIBCXX_HAS_EXTERNAL_THREAD_API cannot both be ON at " 277 "the same time") 278 endif() 279 if (LIBCXX_HAS_PTHREAD_API) 280 message(FATAL_ERROR "The options LIBCXX_HAS_EXTERNAL_THREAD_API" 281 "and LIBCXX_HAS_PTHREAD_API cannot be both" 282 "set to ON at the same time.") 283 endif() 284endif() 285 286# Ensure LLVM_USE_SANITIZER is not specified when LIBCXX_GENERATE_COVERAGE 287# is ON. 288if (LLVM_USE_SANITIZER AND LIBCXX_GENERATE_COVERAGE) 289 message(FATAL_ERROR "LLVM_USE_SANITIZER cannot be used with LIBCXX_GENERATE_COVERAGE") 290endif() 291 292# Set LIBCXX_BUILD_32_BITS to (LIBCXX_BUILD_32_BITS OR LLVM_BUILD_32_BITS) 293# and check that we can build with 32 bits if requested. 294if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32) 295 if (LIBCXX_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM 296 message(STATUS "Building 32 bits executables and libraries.") 297 endif() 298elseif(LIBCXX_BUILD_32_BITS) 299 message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.") 300endif() 301 302# Check that this option is not enabled on Apple and emit a usage warning. 303if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY) 304 if (APPLE) 305 message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is not supported on OS X") 306 else() 307 message(WARNING "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option") 308 endif() 309 if (LIBCXX_ENABLE_STATIC AND NOT PYTHONINTERP_FOUND) 310 message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY requires python but it was not found.") 311 endif() 312endif() 313 314if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT) 315 if (APPLE) 316 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets") 317 endif() 318 if (NOT PYTHONINTERP_FOUND) 319 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT requires python but it was not found.") 320 endif() 321 if (NOT LIBCXX_ENABLE_SHARED) 322 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.") 323 endif() 324endif() 325 326if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT) 327 message(FATAL_ERROR "Conflicting options given. 328 LIBCXX_ENABLE_STATIC_ABI_LIBRARY cannot be specified with 329 LIBCXX_ENABLE_ABI_LINKER_SCRIPT") 330endif() 331 332if (LIBCXX_HAS_MUSL_LIBC AND NOT LIBCXX_INSTALL_SUPPORT_HEADERS) 333 message(FATAL_ERROR "LIBCXX_INSTALL_SUPPORT_HEADERS can not be turned off" 334 "when building for Musl with LIBCXX_HAS_MUSL_LIBC.") 335endif() 336 337#=============================================================================== 338# Configure System 339#=============================================================================== 340 341set(LIBCXX_COMPILER ${CMAKE_CXX_COMPILER}) 342set(LIBCXX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 343set(LIBCXX_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 344set(LIBCXX_BINARY_INCLUDE_DIR "${LIBCXX_BINARY_DIR}/include/c++build") 345set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX}) 346file(MAKE_DIRECTORY "${LIBCXX_BINARY_INCLUDE_DIR}") 347 348set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR}) 349set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR}) 350 351# Declare libc++ configuration variables. 352# They are intended for use as follows: 353# LIBCXX_CXX_FLAGS: General flags for both the compiler and linker. 354# LIBCXX_COMPILE_FLAGS: Compile only flags. 355# LIBCXX_LINK_FLAGS: Linker only flags. 356# LIBCXX_LIBRARIES: libraries libc++ is linked to. 357# LIBCXX_INTERFACE_LIBRARIES: Libraries that must be linked when using libc++ 358# These libraries are exposed in the linker script. 359set(LIBCXX_COMPILE_FLAGS "") 360set(LIBCXX_LINK_FLAGS "") 361set(LIBCXX_LIBRARIES "") 362set(LIBCXX_INTERFACE_LIBRARIES "") 363 364# Include macros for adding and removing libc++ flags. 365include(HandleLibcxxFlags) 366 367# Target flags ================================================================ 368# These flags get added to CMAKE_CXX_FLAGS and CMAKE_C_FLAGS so that 369# 'config-ix' use them during feature checks. It also adds them to both 370# 'LIBCXX_COMPILE_FLAGS' and 'LIBCXX_LINK_FLAGS' 371add_target_flags_if(LIBCXX_BUILD_32_BITS "-m32") 372add_target_flags_if(LIBCXX_TARGET_TRIPLE "-target ${LIBCXX_TARGET_TRIPLE}") 373add_target_flags_if(LIBCXX_SYSROOT "--sysroot=${LIBCXX_SYSROOT}") 374add_target_flags_if(LIBCXX_GCC_TOOLCHAIN "-gcc-toolchain ${LIBCXX_GCC_TOOLCHAIN}") 375if (LIBCXX_TARGET_TRIPLE) 376 set(TARGET_TRIPLE "${LIBCXX_TARGET_TRIPLE}") 377endif() 378 379# Configure compiler. 380include(config-ix) 381 382if (LIBCXX_USE_COMPILER_RT) 383 list(APPEND LIBCXX_LINK_FLAGS "-rtlib=compiler-rt") 384endif() 385 386# Configure coverage options. 387if (LIBCXX_GENERATE_COVERAGE) 388 include(CodeCoverage) 389 set(CMAKE_BUILD_TYPE "COVERAGE" CACHE STRING "" FORCE) 390endif() 391 392string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 393if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG") 394 set(LIBCXX_DEBUG_BUILD ON) 395else() 396 set(LIBCXX_DEBUG_BUILD OFF) 397endif() 398 399#=============================================================================== 400# Setup Compiler Flags 401#=============================================================================== 402 403include(HandleLibCXXABI) # Setup the ABI library flags 404 405if (NOT LIBCXX_STANDALONE_BUILD) 406 # Remove flags that may have snuck in. 407 remove_flags(-DNDEBUG -UNDEBUG -D_DEBUG 408 -lc++abi) 409endif() 410remove_flags(-stdlib=libc++ -stdlib=libstdc++) 411 412# FIXME: Remove all debug flags and flags that change which Windows 413# default libraries are linked. Currently we only support linking the 414# non-debug DLLs 415remove_flags("/D_DEBUG" "/MTd" "/MDd" "/MT" "/Md") 416 417# FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEqDANTIC. 418# Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors 419# so they don't get transformed into -Wno and -errors respectivly. 420remove_flags(-Wno-pedantic -pedantic-errors -pedantic) 421 422# Required flags ============================================================== 423set(LIBCXX_STANDARD_VER c++11 CACHE INTERNAL "internal option to change build dialect") 424if (LIBCXX_HAS_MUSL_LIBC) 425 # musl's pthread implementations uses volatile types in their structs which is 426 # not a constexpr in C++11 but is in C++14, so we use C++14 with musl. 427 set(LIBCXX_STANDARD_VER c++14 CACHE INTERNAL "internal option to change build dialect") 428endif() 429add_compile_flags_if_supported(-std=${LIBCXX_STANDARD_VER}) 430mangle_name("LIBCXX_SUPPORTS_STD_EQ_${LIBCXX_STANDARD_VER}_FLAG" SUPPORTS_DIALECT_NAME) 431if(NOT ${SUPPORTS_DIALECT_NAME}) 432 if(NOT "${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" AND NOT "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC") 433 message(FATAL_ERROR "C++11 or greater is required but the compiler does not support ${LIBCXX_STANDARD_VER}") 434 endif() 435endif() 436 437# On all systems the system c++ standard library headers need to be excluded. 438# MSVC only has -X, which disables all default includes; including the crt. 439# Thus, we do nothing and hope we don't accidentally include any of the C++ 440# headers 441add_compile_flags_if_supported(-nostdinc++) 442 443# Hide all inline function definitions which have not explicitly been marked 444# visible. This prevents new definitions for inline functions from appearing in 445# the dylib when get ODR used by another function. 446add_compile_flags_if_supported(-fvisibility-inlines-hidden) 447 448# Let the library headers know they are currently being used to build the 449# library. 450add_definitions(-D_LIBCPP_BUILDING_LIBRARY) 451 452if (NOT LIBCXX_ENABLE_NEW_DELETE_DEFINITIONS) 453 add_definitions(-D_LIBCPP_DISABLE_NEW_DELETE_DEFINITIONS) 454endif() 455 456# Warning flags =============================================================== 457add_definitions(-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 458add_compile_flags_if_supported( 459 -Wall -Wextra -W -Wwrite-strings 460 -Wno-unused-parameter -Wno-long-long 461 -Werror=return-type) 462if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang") 463 add_compile_flags_if_supported( 464 -Wno-user-defined-literals 465 -Wno-covered-switch-default) 466elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") 467 add_compile_flags_if_supported( 468 -Wno-literal-suffix 469 -Wno-c++14-compat) 470endif() 471if (LIBCXX_ENABLE_WERROR) 472 add_compile_flags_if_supported(-Werror) 473 add_compile_flags_if_supported(-WX) 474else() 475 # TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is 476 # added elsewhere. 477 add_compile_flags_if_supported(-Wno-error) 478endif() 479if (LIBCXX_ENABLE_PEDANTIC) 480 add_compile_flags_if_supported(-pedantic) 481endif() 482if (LIBCXX_DISABLE_MACRO_CONFLICT_WARNINGS) 483 add_definitions(-D_LIBCPP_DISABLE_MACRO_CONFLICT_WARNINGS) 484endif() 485 486# Exception flags ============================================================= 487if (LIBCXX_ENABLE_EXCEPTIONS) 488 # Catches C++ exceptions only and tells the compiler to assume that extern C 489 # functions never throw a C++ exception. 490 add_compile_flags_if_supported(-EHsc) 491else() 492 add_definitions(-D_LIBCPP_NO_EXCEPTIONS) 493 add_compile_flags_if_supported(-EHs- -EHa-) 494 add_compile_flags_if_supported(-fno-exceptions) 495endif() 496 497# RTTI flags ================================================================== 498if (NOT LIBCXX_ENABLE_RTTI) 499 add_definitions(-D_LIBCPP_NO_RTTI) 500 add_compile_flags_if_supported(-GR-) 501 add_compile_flags_if_supported(-fno-rtti) 502endif() 503 504# Threading flags ============================================================= 505if (LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY AND LIBCXX_ENABLE_SHARED) 506 # Need to allow unresolved symbols if this is to work with shared library builds 507 if (APPLE) 508 add_link_flags("-undefined dynamic_lookup") 509 else() 510 # Relax this restriction from HandleLLVMOptions 511 string(REPLACE "-Wl,-z,defs" "" CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}") 512 endif() 513endif() 514 515# Assertion flags ============================================================= 516define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG) 517define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG) 518define_if(LIBCXX_ENABLE_ASSERTIONS -D_LIBCPP_DEBUG=0) 519define_if(LIBCXX_DEBUG_BUILD -D_DEBUG) 520if (LIBCXX_ENABLE_ASSERTIONS AND NOT LIBCXX_DEBUG_BUILD) 521 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 522 define_if_not(LIBCXX_TARGETING_MSVC -D_DEBUG) 523endif() 524 525# Modules flags =============================================================== 526# FIXME The libc++ sources are fundamentally non-modular. They need special 527# versions of the headers in order to provide C++03 and legacy ABI definitions. 528# NOTE: The public headers can be used with modules in all other contexts. 529if (LLVM_ENABLE_MODULES) 530 # Ignore that the rest of the modules flags are now unused. 531 add_compile_flags_if_supported(-Wno-unused-command-line-argument) 532 add_compile_flags(-fno-modules) 533endif() 534 535# Sanitizer flags ============================================================= 536 537# Configure for sanitizers. If LIBCXX_STANDALONE_BUILD then we have to do 538# the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it. 539if (LIBCXX_STANDALONE_BUILD) 540 set(LLVM_USE_SANITIZER "" CACHE STRING 541 "Define the sanitizer used to build the library and tests") 542 # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC. 543 # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do. 544 if (LLVM_USE_SANITIZER AND NOT MSVC) 545 add_flags_if_supported("-fno-omit-frame-pointer") 546 add_flags_if_supported("-gline-tables-only") 547 548 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND 549 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO") 550 add_flags_if_supported("-gline-tables-only") 551 endif() 552 if (LLVM_USE_SANITIZER STREQUAL "Address") 553 add_flags("-fsanitize=address") 554 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") 555 add_flags(-fsanitize=memory) 556 if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") 557 add_flags("-fsanitize-memory-track-origins") 558 endif() 559 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") 560 add_flags("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all") 561 elseif (LLVM_USE_SANITIZER STREQUAL "Thread") 562 add_flags(-fsanitize=thread) 563 else() 564 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") 565 endif() 566 elseif(LLVM_USE_SANITIZER AND MSVC) 567 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.") 568 endif() 569endif() 570 571# Configuration file flags ===================================================== 572if (NOT LIBCXX_ABI_VERSION EQUAL "1") 573 config_define(${LIBCXX_ABI_VERSION} _LIBCPP_ABI_VERSION) 574endif() 575config_define_if(LIBCXX_ABI_UNSTABLE _LIBCPP_ABI_UNSTABLE) 576 577config_define_if_not(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE) 578config_define_if_not(LIBCXX_ENABLE_STDIN _LIBCPP_HAS_NO_STDIN) 579config_define_if_not(LIBCXX_ENABLE_STDOUT _LIBCPP_HAS_NO_STDOUT) 580config_define_if_not(LIBCXX_ENABLE_THREADS _LIBCPP_HAS_NO_THREADS) 581config_define_if_not(LIBCXX_ENABLE_MONOTONIC_CLOCK _LIBCPP_HAS_NO_MONOTONIC_CLOCK) 582config_define_if_not(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS) 583 584config_define_if(LIBCXX_HAS_PTHREAD_API _LIBCPP_HAS_THREAD_API_PTHREAD) 585config_define_if(LIBCXX_HAS_EXTERNAL_THREAD_API _LIBCPP_HAS_THREAD_API_EXTERNAL) 586config_define_if(LIBCXX_BUILD_EXTERNAL_THREAD_LIBRARY _LIBCPP_HAS_THREAD_LIBRARY_EXTERNAL) 587config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC) 588 589# By default libc++ on Windows expects to use a shared library, which requires 590# the headers to use DLL import/export semantics. However when building a 591# static library only we modify the headers to disable DLL import/export. 592if (DEFINED WIN32 AND LIBCXX_ENABLE_STATIC AND NOT LIBCXX_ENABLE_SHARED) 593 message(STATUS "Generating custom __config for non-DLL Windows build") 594 config_define(ON _LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS) 595endif() 596 597if (LIBCXX_NEEDS_SITE_CONFIG) 598 configure_file("include/__config_site.in" 599 "${LIBCXX_BINARY_DIR}/__config_site" 600 @ONLY) 601 602 # Provide the config definitions by included the generated __config_site 603 # file at compile time. 604 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC" OR "${CMAKE_CXX_SIMULATE_ID}" STREQUAL "MSVC") 605 add_compile_flags("/FI\"${LIBCXX_BINARY_DIR}/__config_site\"") 606 else() 607 add_compile_flags("-include ${LIBCXX_BINARY_DIR}/__config_site") 608 endif() 609endif() 610 611#=============================================================================== 612# Setup Source Code And Tests 613#=============================================================================== 614include_directories(include) 615add_subdirectory(include) 616add_subdirectory(lib) 617 618 619if (LIBCXX_INCLUDE_BENCHMARKS) 620 add_subdirectory(benchmarks) 621endif() 622 623# Create the lit.site.cfg file even when LIBCXX_INCLUDE_TESTS is OFF or 624# LLVM_FOUND is OFF. This allows users to run the tests manually using 625# LIT without requiring a full LLVM checkout. 626add_subdirectory(test) 627if (LIBCXX_INCLUDE_TESTS) 628 add_subdirectory(lib/abi) 629endif() 630 631if (LIBCXX_INCLUDE_DOCS) 632 add_subdirectory(docs) 633endif() 634