1 # See www/CMake.html for instructions on how to build libcxx with CMake. 2 3 #=============================================================================== 4 # Setup Project 5 #=============================================================================== 6 cmake_minimum_required(VERSION 2.8) 7 8 if(POLICY CMP0042) 9 cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default 10 endif() 11 if(POLICY CMP0022) 12 cmake_policy(SET CMP0022 NEW) # Required when interacting with LLVM and Clang 13 endif() 14 15 # Add path for custom modules 16 set(CMAKE_MODULE_PATH 17 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" 19 ${CMAKE_MODULE_PATH} 20 ) 21 22 # Find the LLVM sources and simulate LLVM CMake options. 23 include(HandleOutOfTreeLLVM) 24 25 if (LIBCXX_BUILT_STANDALONE) 26 project(libcxx CXX C) 27 28 set(PACKAGE_NAME libcxx) 29 set(PACKAGE_VERSION trunk-svn) 30 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 31 set(PACKAGE_BUGREPORT "llvm-bugs (a] lists.llvm.org") 32 endif () 33 34 if (LIBCXX_BUILT_STANDALONE AND NOT LLVM_FOUND) 35 message(WARNING "UNSUPPORTED LIBCXX CONFIGURATION DETECTED: " 36 "llvm-config not found and LLVM_PATH not defined.\n" 37 "Reconfigure with -DLLVM_CONFIG=path/to/llvm-config " 38 "or -DLLVM_PATH=path/to/llvm-source-root.") 39 endif() 40 41 # Require out of source build. 42 include(MacroEnsureOutOfSourceBuild) 43 MACRO_ENSURE_OUT_OF_SOURCE_BUILD( 44 "${PROJECT_NAME} requires an out of source build. Please create a separate 45 build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there." 46 ) 47 48 #=============================================================================== 49 # Setup CMake Options 50 #=============================================================================== 51 52 # Basic options --------------------------------------------------------------- 53 option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON) 54 option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON) 55 option(LIBCXX_ENABLE_EXPERIMENTAL_LIBRARY "Build libc++experimental.a" ON) 56 option(LIBCXX_INCLUDE_TESTS "Build the libc++ tests." ${LLVM_INCLUDE_TESTS}) 57 option(LIBCXX_INCLUDE_DOCS "Build the libc++ documentation." ${LLVM_INCLUDE_DOCS}) 58 set(LIBCXX_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING 59 "Define suffix of library directory name (32/64)") 60 option(LIBCXX_INSTALL_HEADERS "Install the libc++ headers." ON) 61 option(LIBCXX_INSTALL_LIBRARY "Install the libc++ library." ON) 62 option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON) 63 option(LIBCXX_INSTALL_EXPERIMENTAL_LIBRARY "Install libc++experimental.a" OFF) 64 set(LIBCXX_ABI_VERSION 1 CACHE STRING "ABI version of libc++.") 65 option(LIBCXX_ABI_UNSTABLE "Unstable ABI of libc++." OFF) 66 67 # ABI Library options --------------------------------------------------------- 68 set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING 69 "Specify C++ ABI library to use." FORCE) 70 set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++) 71 set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS}) 72 73 # Setup the default options if LIBCXX_CXX_ABI is not specified. 74 if (NOT LIBCXX_CXX_ABI) 75 if (NOT DEFINED LIBCXX_BUILT_STANDALONE AND 76 IS_DIRECTORY "${CMAKE_SOURCE_DIR}/projects/libcxxabi") 77 set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi") 78 set(LIBCXX_CXX_ABI_INCLUDE_PATHS "${CMAKE_SOURCE_DIR}/projects/libcxxabi/include") 79 set(LIBCXX_CXX_ABI_INTREE 1) 80 else () 81 set(LIBCXX_CXX_ABI_LIBNAME "none") 82 endif () 83 else () 84 set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}") 85 endif () 86 87 # Use a static copy of the ABI library when linking libc++. This option 88 # cannot be used with LIBCXX_ENABLE_ABI_LINKER_SCRIPT. 89 option(LIBCXX_ENABLE_STATIC_ABI_LIBRARY "Statically link the ABI library" OFF) 90 91 # Generate and install a linker script inplace of libc++.so. The linker script 92 # will link libc++ to the correct ABI library. This option is on by default 93 # On UNIX platforms other than Apple unless 'LIBCXX_ENABLE_STATIC_ABI_LIBRARY' 94 # is on. This option is also disabled when the ABI library is not specified 95 # or is specified to be "none". 96 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE OFF) 97 if (LLVM_HAVE_LINK_VERSION_SCRIPT AND NOT LIBCXX_ENABLE_STATIC_ABI_LIBRARY 98 AND NOT LIBCXX_CXX_ABI_LIBNAME STREQUAL "none" 99 AND PYTHONINTERP_FOUND 100 AND LIBCXX_ENABLE_SHARED) 101 set(ENABLE_LINKER_SCRIPT_DEFAULT_VALUE ON) 102 endif() 103 104 option(LIBCXX_ENABLE_ABI_LINKER_SCRIPT 105 "Use and install a linker script for the given ABI library" 106 ${ENABLE_LINKER_SCRIPT_DEFAULT_VALUE}) 107 108 # Build libc++abi with libunwind. We need this option to determine whether to 109 # link with libunwind or libgcc_s while running the test cases. 110 option(LIBCXXABI_USE_LLVM_UNWINDER "Build and use the LLVM unwinder." OFF) 111 112 # Target options -------------------------------------------------------------- 113 option(LIBCXX_BUILD_32_BITS "Build 32 bit libc++." ${LLVM_BUILD_32_BITS}) 114 set(LIBCXX_SYSROOT "" CACHE STRING "Use alternate sysroot.") 115 set(LIBCXX_GCC_TOOLCHAIN "" CACHE STRING "Use alternate GCC toolchain.") 116 117 # Feature options ------------------------------------------------------------- 118 option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON) 119 option(LIBCXX_ENABLE_RTTI "Use run time type information." ON) 120 option(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE "Build libc++ with support for the global filesystem namespace." ON) 121 option(LIBCXX_ENABLE_STDIN "Build libc++ with support for stdin/std::cin." ON) 122 option(LIBCXX_ENABLE_STDOUT "Build libc++ with support for stdout/std::cout." ON) 123 option(LIBCXX_ENABLE_THREADS "Build libc++ with support for threads." ON) 124 option(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS "Build libc++ with support for thread-unsafe C functions" ON) 125 option(LIBCXX_ENABLE_MONOTONIC_CLOCK 126 "Build libc++ with support for a monotonic clock. 127 This option may only be set to OFF when LIBCXX_ENABLE_THREADS=OFF." ON) 128 option(LIBCXX_HAS_MUSL_LIBC "Build libc++ with support for the Musl C library" OFF) 129 130 # Misc options ---------------------------------------------------------------- 131 # FIXME: Turn -pedantic back ON. It is currently off because it warns 132 # about #include_next which is used everywhere. 133 option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." OFF) 134 option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) 135 136 option(LIBCXX_GENERATE_COVERAGE "Enable generating code coverage." OFF) 137 set(LIBCXX_COVERAGE_LIBRARY "" CACHE STRING 138 "The Profile-rt library used to build with code coverage") 139 140 # Don't allow a user to accidentally overwrite the system libc++ installation on Darwin. 141 # If the user specifies -DCMAKE_INSTALL_PREFIX=/usr the install rules for libc++ 142 # will not be generated and a warning will be issued. 143 option(LIBCXX_OVERRIDE_DARWIN_INSTALL "Enable overwriting darwins libc++ installation." OFF) 144 mark_as_advanced(LIBCXX_OVERRIDE_DARWIN_INSTALL) # Don't show this option by default. 145 146 if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin" AND NOT LIBCXX_OVERRIDE_DARWIN_INSTALL) 147 if ("${CMAKE_INSTALL_PREFIX}" STREQUAL "/usr") 148 message(WARNING "Disabling libc++ install rules because installation would " 149 "overwrite the systems installation. Configure with " 150 "-DLIBCXX_OVERRIDE_DARWIN_INSTALL=ON to suppress this behaviour.") 151 mark_as_advanced(CLEAR LIBCXX_OVERRIDE_DARWIN_INSTALL) # Show the override option. 152 set(LIBCXX_INSTALL_HEADERS OFF) 153 set(LIBCXX_INSTALL_LIBRARY OFF) 154 endif() 155 endif() 156 157 set(LIBCXX_CONFIGURE_IDE_DEFAULT OFF) 158 if (XCODE OR MSVC_IDE) 159 set(LIBCXX_CONFIGURE_IDE_DEFAULT ON) 160 endif() 161 option(LIBCXX_CONFIGURE_IDE "Configure libcxx for use within an IDE" 162 ${LIBCXX_CONFIGURE_IDE_DEFAULT}) 163 164 #=============================================================================== 165 # Check option configurations 166 #=============================================================================== 167 168 # Ensure LIBCXX_ENABLE_MONOTONIC_CLOCK is set to ON only when 169 # LIBCXX_ENABLE_THREADS is on. 170 if(LIBCXX_ENABLE_THREADS AND NOT LIBCXX_ENABLE_MONOTONIC_CLOCK) 171 message(FATAL_ERROR "LIBCXX_ENABLE_MONOTONIC_CLOCK can only be set to OFF" 172 " when LIBCXX_ENABLE_THREADS is also set to OFF.") 173 endif() 174 175 # Ensure LLVM_USE_SANITIZER is not specified when LIBCXX_GENERATE_COVERAGE 176 # is ON. 177 if (LLVM_USE_SANITIZER AND LIBCXX_GENERATE_COVERAGE) 178 message(FATAL_ERROR "LLVM_USE_SANITIZER cannot be used with LIBCXX_GENERATE_COVERAGE") 179 endif() 180 181 # Set LIBCXX_BUILD_32_BITS to (LIBCXX_BUILD_32_BITS OR LLVM_BUILD_32_BITS) 182 # and check that we can build with 32 bits if requested. 183 if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32) 184 if (LIBCXX_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM 185 message(STATUS "Building 32 bits executables and libraries.") 186 endif() 187 elseif(LIBCXX_BUILD_32_BITS) 188 message(FATAL_ERROR "LIBCXX_BUILD_32_BITS=ON is not supported on this platform.") 189 endif() 190 191 # Check that this option is not enabled on Apple and emit a usage warning. 192 if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY) 193 if (APPLE) 194 message(FATAL_ERROR "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is not supported on OS X") 195 else() 196 message(WARNING "LIBCXX_ENABLE_STATIC_ABI_LIBRARY is an experimental option") 197 endif() 198 endif() 199 200 if (LIBCXX_ENABLE_ABI_LINKER_SCRIPT) 201 if (APPLE) 202 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT cannot be used on APPLE targets") 203 endif() 204 if (NOT PYTHONINTERP_FOUND) 205 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT requires python but it was not found.") 206 endif() 207 if (NOT LIBCXX_ENABLE_SHARED) 208 message(FATAL_ERROR "LIBCXX_ENABLE_ABI_LINKER_SCRIPT is only available for shared library builds.") 209 endif() 210 endif() 211 212 if (LIBCXX_ENABLE_STATIC_ABI_LIBRARY AND LIBCXX_ENABLE_ABI_LINKER_SCRIPT) 213 message(FATAL_ERROR "Conflicting options given. 214 LIBCXX_ENABLE_STATIC_ABI_LIBRARY cannot be specified with 215 LIBCXX_ENABLE_ABI_LINKER_SCRIPT") 216 endif() 217 218 if (LIBCXX_HAS_MUSL_LIBC AND NOT LIBCXX_INSTALL_SUPPORT_HEADERS) 219 message(FATAL_ERROR "LIBCXX_INSTALL_SUPPORT_HEADERS can not be turned off" 220 "when building for Musl with LIBCXX_HAS_MUSL_LIBC.") 221 endif() 222 223 #=============================================================================== 224 # Configure System 225 #=============================================================================== 226 227 set(LIBCXX_COMPILER ${CMAKE_CXX_COMPILER}) 228 set(LIBCXX_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 229 set(LIBCXX_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 230 set(LIBCXX_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBCXX_LIBDIR_SUFFIX}) 231 232 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR}) 233 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBCXX_LIBRARY_DIR}) 234 235 # Declare libc++ configuration variables. 236 # They are intended for use as follows: 237 # LIBCXX_CXX_FLAGS: General flags for both the compiler and linker. 238 # LIBCXX_COMPILE_FLAGS: Compile only flags. 239 # LIBCXX_LINK_FLAGS: Linker only flags. 240 set(LIBCXX_COMPILE_FLAGS "") 241 set(LIBCXX_LINK_FLAGS "") 242 set(LIBCXX_LIBRARIES "") 243 244 # Configure compiler. 245 include(config-ix) 246 247 # Configure coverage options. 248 if (LIBCXX_GENERATE_COVERAGE) 249 include(CodeCoverage) 250 set(CMAKE_BUILD_TYPE "COVERAGE" CACHE STRING "" FORCE) 251 endif() 252 253 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 254 255 #=============================================================================== 256 # Setup Compiler Flags 257 #=============================================================================== 258 259 include(HandleLibCXXABI) # Setup the ABI library flags 260 261 # Include macros for adding and removing libc++ flags. 262 include(HandleLibcxxFlags) 263 264 # Remove flags that may have snuck in. 265 remove_flags(-DNDEBUG -UNDEBUG -D_DEBUG 266 -stdlib=libc++ -stdlib=libstdc++ -lc++abi -m32) 267 268 # FIXME(EricWF): See the FIXME on LIBCXX_ENABLE_PEDANTIC. 269 # Remove the -pedantic flag and -Wno-pedantic and -pedantic-errors 270 # so they don't get transformed into -Wno and -errors respectivly. 271 remove_flags(-Wno-pedantic -pedantic-errors -pedantic) 272 273 # Required flags ============================================================== 274 add_compile_flags_if_supported(-std=c++11) 275 if (NOT MSVC AND NOT LIBCXX_SUPPORTS_STD_EQ_CXX11_FLAG) 276 message(FATAL_ERROR "C++11 is required but the compiler does not support -std=c++11") 277 endif() 278 279 # On all systems the system c++ standard library headers need to be excluded. 280 # MSVC only has -X, which disables all default includes; including the crt. 281 # Thus, we do nothing and hope we don't accidentally include any of the C++ 282 # headers 283 add_compile_flags_if_supported(-nostdinc++) 284 285 # Target flags ================================================================ 286 add_flags_if(LIBCXX_BUILD_32_BITS -m32) 287 add_flags_if(LIBCXX_TARGET_TRIPLE "-target ${LIBCXX_TARGET_TRIPLE}") 288 add_flags_if(LIBCXX_SYSROOT "--sysroot ${LIBCXX_SYSROOT}") 289 add_flags_if(LIBCXX_GCC_TOOLCHAIN "-gcc-toolchain ${LIBCXX_GCC_TOOLCHAIN}") 290 291 # Warning flags =============================================================== 292 add_definitions(-D_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 293 add_compile_flags_if_supported( 294 -Wall -W -Wwrite-strings 295 -Wno-unused-parameter -Wno-long-long 296 -Werror=return-type) 297 if (LIBCXX_ENABLE_WERROR) 298 add_compile_flags_if_supported(-Werror) 299 add_compile_flags_if_supported(-WX) 300 else() 301 # TODO(EricWF) Remove this. We shouldn't be suppressing errors when -Werror is 302 # added elsewhere. 303 add_compile_flags_if_supported(-Wno-error) 304 endif() 305 if (LIBCXX_ENABLE_PEDANTIC) 306 add_compile_flags_if_supported(-pedantic) 307 endif() 308 309 # Exception flags ============================================================= 310 if (LIBCXX_ENABLE_EXCEPTIONS) 311 # Catches C++ exceptions only and tells the compiler to assume that extern C 312 # functions never throw a C++ exception. 313 add_compile_flags_if_supported(-EHsc) 314 else() 315 add_definitions(-D_LIBCPP_NO_EXCEPTIONS) 316 add_compile_flags_if_supported(-EHs- -EHa-) 317 add_compile_flags_if_supported(-fno-exceptions) 318 endif() 319 320 # RTTI flags ================================================================== 321 if (NOT LIBCXX_ENABLE_RTTI) 322 add_definitions(-D_LIBCPP_NO_RTTI) 323 add_compile_flags_if_supported(-GR-) 324 add_compile_flags_if_supported(-fno-rtti) 325 endif() 326 327 # Assertion flags ============================================================= 328 define_if(LIBCXX_ENABLE_ASSERTIONS -UNDEBUG) 329 define_if_not(LIBCXX_ENABLE_ASSERTIONS -DNDEBUG) 330 if (LIBCXX_ENABLE_ASSERTIONS) 331 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 332 define_if_not(MSVC -D_DEBUG) 333 endif() 334 335 # Feature flags =============================================================== 336 define_if(MSVC -D_CRT_SECURE_NO_WARNINGS) 337 338 # Sanitizer flags ============================================================= 339 340 # Configure for sanitizers. If LIBCXX_BUILT_STANDALONE then we have to do 341 # the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it. 342 if (LIBCXX_BUILT_STANDALONE) 343 set(LLVM_USE_SANITIZER "" CACHE STRING 344 "Define the sanitizer used to build the library and tests") 345 # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC. 346 # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do. 347 if (LLVM_USE_SANITIZER AND NOT MSVC) 348 add_flags_if_supported("-fno-omit-frame-pointer") 349 add_flags_if_supported("-gline-tables-only") 350 351 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" AND 352 NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELWITHDEBINFO") 353 add_flags_if_supported("-gline-tables-only") 354 endif() 355 if (LLVM_USE_SANITIZER STREQUAL "Address") 356 add_flags("-fsanitize=address") 357 elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") 358 add_flags(-fsanitize=memory) 359 if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") 360 add_flags("-fsanitize-memory-track-origins") 361 endif() 362 elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") 363 add_flags("-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover=all") 364 elseif (LLVM_USE_SANITIZER STREQUAL "Thread") 365 add_flags(-fsanitize=thread) 366 else() 367 message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") 368 endif() 369 elseif(LLVM_USE_SANITIZER AND MSVC) 370 message(WARNING "LLVM_USE_SANITIZER is not supported on this platform.") 371 endif() 372 endif() 373 374 # Configuration file flags ===================================================== 375 if (NOT LIBCXX_ABI_VERSION EQUAL "1") 376 config_define(${LIBCXX_ABI_VERSION} _LIBCPP_ABI_VERSION) 377 endif() 378 config_define_if(LIBCXX_ABI_UNSTABLE _LIBCPP_ABI_UNSTABLE) 379 380 config_define_if_not(LIBCXX_ENABLE_GLOBAL_FILESYSTEM_NAMESPACE _LIBCPP_HAS_NO_GLOBAL_FILESYSTEM_NAMESPACE) 381 config_define_if_not(LIBCXX_ENABLE_STDIN _LIBCPP_HAS_NO_STDIN) 382 config_define_if_not(LIBCXX_ENABLE_STDOUT _LIBCPP_HAS_NO_STDOUT) 383 config_define_if_not(LIBCXX_ENABLE_THREADS _LIBCPP_HAS_NO_THREADS) 384 config_define_if_not(LIBCXX_ENABLE_MONOTONIC_CLOCK _LIBCPP_HAS_NO_MONOTONIC_CLOCK) 385 config_define_if_not(LIBCXX_ENABLE_THREAD_UNSAFE_C_FUNCTIONS _LIBCPP_HAS_NO_THREAD_UNSAFE_C_FUNCTIONS) 386 387 config_define_if(LIBCXX_HAS_MUSL_LIBC _LIBCPP_HAS_MUSL_LIBC) 388 389 if (LIBCXX_NEEDS_SITE_CONFIG) 390 configure_file( 391 include/__config_site.in 392 ${LIBCXX_BINARY_DIR}/__config_site 393 @ONLY) 394 # Provide the config definitions by included the generated __config_site 395 # file at compile time. 396 add_compile_flags("-include ${LIBCXX_BINARY_DIR}/__config_site") 397 endif() 398 399 #=============================================================================== 400 # Setup Source Code And Tests 401 #=============================================================================== 402 include_directories(include) 403 add_subdirectory(include) 404 add_subdirectory(lib) 405 406 if (LIBCXX_INCLUDE_TESTS) 407 add_subdirectory(test) 408 endif() 409 if (LIBCXX_INCLUDE_DOCS) 410 add_subdirectory(docs) 411 endif() 412