| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 1 | # See www/CMake.html for instructions on how to build libcxx with CMake. |
| 2 | |
| 3 | #=============================================================================== |
| 4 | # Setup Project |
| 5 | #=============================================================================== |
| 6 | |
| 7 | project(libcxx CXX C) |
| 8 | cmake_minimum_required(VERSION 2.8) |
| 9 | |
| 10 | set(PACKAGE_NAME libcxx) |
| 11 | set(PACKAGE_VERSION trunk-svn) |
| 12 | set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") |
| 13 | set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu") |
| 14 | |
| 15 | # Add path for custom modules |
| 16 | set(CMAKE_MODULE_PATH |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 17 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake" |
| 18 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
| Alexey Samsonov | 179fa78 | 2013-09-30 09:10:01 | [diff] [blame] | 19 | ${CMAKE_MODULE_PATH} |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | # Require out of source build. |
| 23 | include(MacroEnsureOutOfSourceBuild) |
| 24 | MACRO_ENSURE_OUT_OF_SOURCE_BUILD( |
| 25 | "${PROJECT_NAME} requires an out of source build. Please create a separate |
| 26 | build directory and run 'cmake /path/to/${PROJECT_NAME} [options]' there." |
| 27 | ) |
| 28 | |
| Peter Collingbourne | 4c81b00 | 2013-10-03 21:58:25 | [diff] [blame] | 29 | if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
| 30 | set(LIBCXX_BUILT_STANDALONE 1) |
| 31 | endif() |
| 32 | |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 33 | #=============================================================================== |
| 34 | # Setup CMake Options |
| 35 | #=============================================================================== |
| 36 | |
| 37 | # Define options. |
| 38 | option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON) |
| 39 | option(LIBCXX_ENABLE_RTTI "Use run time type information." ON) |
| 40 | option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON) |
| 41 | option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." ON) |
| 42 | option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) |
| Eric Fiselier | dfe5e72 | 2014-08-16 01:35:36 | [diff] [blame] | 43 | option(LIBCXX_ENABLE_CXX1Y "Enable -std=c++1y and use of c++1y language features if the compiler supports it." OFF) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 44 | option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON) |
| Michael Gottesman | c64c980 | 2013-09-02 07:28:05 | [diff] [blame] | 45 | option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON) |
| Eric Fiselier | 25a1516 | 2014-08-18 05:03:46 | [diff] [blame] | 46 | if (LIBCXX_BUILT_STANDALONE) |
| 47 | set(LLVM_USE_SANITIZER "" CACHE STRING |
| 48 | "Define the sanitizer used to build the library and tests") |
| 49 | endif() |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 50 | |
| Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 | [diff] [blame] | 51 | set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++) |
| Viktor Kutuzov | f2e8c04 | 2014-08-08 06:53:30 | [diff] [blame] | 52 | if (NOT LIBCXX_CXX_ABI) |
| 53 | if (NOT DEFINED LIBCXX_BUILT_STANDALONE AND |
| 54 | IS_DIRECTORY "${CMAKE_SOURCE_DIR}/projects/libcxxabi") |
| 55 | set(LIBCXX_CXX_ABI_LIBNAME "libcxxabi") |
| Dan Albert | 0c6d1a8 | 2014-07-26 23:08:33 | [diff] [blame] | 56 | set(LIBCXX_LIBCXXABI_INCLUDE_PATHS "${CMAKE_SOURCE_DIR}/projects/libcxxabi/include") |
| 57 | set(LIBCXX_CXX_ABI_INTREE 1) |
| 58 | else () |
| Viktor Kutuzov | f2e8c04 | 2014-08-08 06:53:30 | [diff] [blame] | 59 | set(LIBCXX_CXX_ABI_LIBNAME "none") |
| Dan Albert | 0c6d1a8 | 2014-07-26 23:08:33 | [diff] [blame] | 60 | endif () |
| Viktor Kutuzov | f2e8c04 | 2014-08-08 06:53:30 | [diff] [blame] | 61 | else () |
| 62 | set(LIBCXX_CXX_ABI_LIBNAME "${LIBCXX_CXX_ABI}") |
| 63 | endif () |
| Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 | [diff] [blame] | 64 | set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING |
| 65 | "Specify C++ ABI library to use." FORCE) |
| Howard Hinnant | 9c07b14 | 2013-08-19 21:42:07 | [diff] [blame] | 66 | set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS}) |
| Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 | [diff] [blame] | 67 | |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 68 | #=============================================================================== |
| 69 | # Configure System |
| 70 | #=============================================================================== |
| 71 | |
| 72 | # Get triples. |
| 73 | include(GetTriple) |
| 74 | get_host_triple(LIBCXX_HOST_TRIPLE |
| 75 | LIBCXX_HOST_ARCH |
| 76 | LIBCXX_HOST_VENDOR |
| 77 | LIBCXX_HOST_OS |
| 78 | ) |
| 79 | set(LIBCXX_HOST_TRIPLE ${LIBCXX_HOST_TRIPLE} CACHE STRING "Host triple.") |
| 80 | get_target_triple(LIBCXX_TARGET_TRIPLE |
| 81 | LIBCXX_TARGET_ARCH |
| 82 | LIBCXX_TARGET_VENDOR |
| 83 | LIBCXX_TARGET_OS |
| 84 | ) |
| 85 | set(LIBCXX_TARGET_TRIPLE ${LIBCXX_TARGET_TRIPLE} CACHE STRING "Target triple.") |
| 86 | |
| Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 | [diff] [blame] | 87 | |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 88 | # Configure compiler. |
| 89 | include(config-ix) |
| Eric Fiselier | 9071bc0 | 2014-10-18 01:15:17 | [diff] [blame] | 90 | # Configure ABI library |
| 91 | include(HandleLibCXXABI) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 92 | |
| 93 | #=============================================================================== |
| 94 | # Setup Compiler Flags |
| 95 | #=============================================================================== |
| 96 | |
| 97 | # Get required flags. |
| 98 | # On all systems the system c++ standard library headers need to be excluded. |
| 99 | if (MSVC) |
| 100 | # MSVC only has -X, which disables all default includes; including the crt. |
| 101 | # Thus, we do nothing and hope we don't accidentally include any of the C++ |
| 102 | # headers. |
| 103 | else() |
| 104 | if (LIBCXX_HAS_NOSTDINCXX_FLAG) |
| Saleem Abdulrasool | eb54781 | 2014-03-12 04:11:31 | [diff] [blame] | 105 | list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -nostdinc++) |
| David Fang | 44ead61 | 2014-06-24 20:32:11 | [diff] [blame] | 106 | string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 107 | endif() |
| Eric Fiselier | dfe5e72 | 2014-08-16 01:35:36 | [diff] [blame] | 108 | # If c++1y has been enabled then attempt to use it. Fail if it is no supported |
| 109 | # by the compiler. Otherwise choose c++11 and ensure the compiler supports it. |
| 110 | if (LIBCXX_ENABLE_CXX1Y) |
| 111 | if (LIBCXX_HAS_STDCXX1Y_FLAG) |
| 112 | set(LIBCXX_STD_VERSION c++1y) |
| 113 | else() |
| 114 | message(FATAL_ERROR "c++1y was enabled but the compiler does not support it.") |
| 115 | endif() |
| 116 | else() |
| 117 | if (LIBCXX_HAS_STDCXX11_FLAG) |
| 118 | set(LIBCXX_STD_VERSION c++11) |
| 119 | else() |
| 120 | message(FATAL_ERROR "c++11 is required by libc++ but is not supported by the compiler") |
| 121 | endif() |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 122 | endif() |
| Eric Fiselier | dfe5e72 | 2014-08-16 01:35:36 | [diff] [blame] | 123 | # LIBCXX_STD_VERSION should always be set at this point. |
| 124 | list(APPEND LIBCXX_CXX_REQUIRED_FLAGS "-std=${LIBCXX_STD_VERSION}") |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 125 | endif() |
| 126 | |
| 127 | macro(append_if list condition var) |
| 128 | if (${condition}) |
| 129 | list(APPEND ${list} ${var}) |
| 130 | endif() |
| 131 | endmacro() |
| 132 | |
| 133 | # Get warning flags |
| Howard Hinnant | adb73b1 | 2013-10-05 00:07:35 | [diff] [blame] | 134 | if (NOT MSVC) |
| 135 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WALL_FLAG -Wall) |
| Saleem Abdulrasool | eb54781 | 2014-03-12 04:11:31 | [diff] [blame] | 136 | list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -Werror=return-type) |
| Howard Hinnant | adb73b1 | 2013-10-05 00:07:35 | [diff] [blame] | 137 | endif() |
| Marshall Clow | 0f7afe7 | 2013-10-21 15:56:35 | [diff] [blame] | 138 | |
| Douglas Gregor | f9b6e7e | 2012-06-07 22:26:00 | [diff] [blame] | 139 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_W_FLAG -W) |
| 140 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter) |
| 141 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings) |
| 142 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_LONG_LONG_FLAG -Wno-long-long) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 143 | if (LIBCXX_ENABLE_WERROR) |
| Douglas Gregor | f9b6e7e | 2012-06-07 22:26:00 | [diff] [blame] | 144 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WERROR_FLAG -Werror) |
| 145 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WX_FLAG -WX) |
| Alexey Samsonov | f7eb573 | 2013-10-02 07:44:19 | [diff] [blame] | 146 | else() |
| 147 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_ERROR_FLAG -Wno-error) |
| 148 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_NO_WX_FLAG -WX-) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 149 | endif() |
| 150 | if (LIBCXX_ENABLE_PEDANTIC) |
| Douglas Gregor | f9b6e7e | 2012-06-07 22:26:00 | [diff] [blame] | 151 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_PEDANTIC_FLAG -pedantic) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 152 | endif() |
| 153 | |
| 154 | # Get feature flags. |
| 155 | # Exceptions |
| 156 | if (LIBCXX_ENABLE_EXCEPTIONS) |
| 157 | # Catches C++ exceptions only and tells the compiler to assume that extern C |
| 158 | # functions never throw a C++ exception. |
| 159 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_EHSC_FLAG -EHsc) |
| 160 | else() |
| 161 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_EXCEPTIONS) |
| 162 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHS_FLAG -EHs-) |
| 163 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHA_FLAG -EHa-) |
| 164 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions) |
| 165 | endif() |
| 166 | # RTTI |
| 167 | if (NOT LIBCXX_ENABLE_RTTI) |
| 168 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_RTTI) |
| 169 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_GR_FLAG -GR-) |
| 170 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_RTTI_FLAG -fno-rtti) |
| 171 | endif() |
| 172 | # Assert |
| Howard Hinnant | e103a3d | 2012-08-05 17:37:39 | [diff] [blame] | 173 | string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) |
| 174 | if (LIBCXX_ENABLE_ASSERTIONS) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 175 | # MSVC doesn't like _DEBUG on release builds. See PR 4379. |
| 176 | if (NOT MSVC) |
| 177 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_DEBUG) |
| 178 | endif() |
| 179 | # On Release builds cmake automatically defines NDEBUG, so we |
| 180 | # explicitly undefine it: |
| 181 | if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") |
| 182 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -UNDEBUG) |
| 183 | endif() |
| 184 | else() |
| 185 | if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") |
| 186 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -DNDEBUG) |
| 187 | endif() |
| 188 | endif() |
| Howard Hinnant | 4a0e74f | 2013-02-25 15:50:36 | [diff] [blame] | 189 | # Static library |
| 190 | if (NOT LIBCXX_ENABLE_SHARED) |
| 191 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_BUILD_STATIC) |
| 192 | endif() |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 193 | |
| 194 | # This is the _ONLY_ place where add_definitions is called. |
| Marshall Clow | 0f7afe7 | 2013-10-21 15:56:35 | [diff] [blame] | 195 | if (MSVC) |
| 196 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) |
| 197 | endif() |
| 198 | |
| Eric Fiselier | 25a1516 | 2014-08-18 05:03:46 | [diff] [blame] | 199 | # Configure for sanitizers. If LIBCXX_BUILT_STANDALONE then we have to do |
| 200 | # the flag translation ourselves. Othewise LLVM's CMakeList.txt will handle it. |
| 201 | if (LIBCXX_BUILT_STANDALONE) |
| 202 | # NOTE: LLVM_USE_SANITIZER checks for a UNIX like system instead of MSVC. |
| 203 | # But we don't have LLVM_ON_UNIX so checking for MSVC is the best we can do. |
| 204 | if (LLVM_USE_SANITIZER AND NOT MSVC) |
| 205 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_OMIT_FRAME_POINTER_FLAG |
| 206 | "-fno-omit-frame-pointer") |
| 207 | if (LLVM_USE_SANITIZER STREQUAL "Address") |
| 208 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS "-fsanitize=address") |
| 209 | elseif (LLVM_USE_SANITIZER MATCHES "Memory(WithOrigins)?") |
| 210 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS "-fsanitize=memory") |
| 211 | if (LLVM_USE_SANITIZER STREQUAL "MemoryWithOrigins") |
| 212 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS "-fsanitize-memory-track-origins") |
| 213 | endif() |
| Eric Fiselier | 66d529f | 2014-10-16 23:21:59 | [diff] [blame] | 214 | elseif (LLVM_USE_SANITIZER STREQUAL "Undefined") |
| 215 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS |
| 216 | "-fsanitize=undefined -fno-sanitize=vptr,function -fno-sanitize-recover") |
| Eric Fiselier | 25a1516 | 2014-08-18 05:03:46 | [diff] [blame] | 217 | else() |
| 218 | message(WARNING "Unsupported value of LLVM_USE_SANITIZER: ${LLVM_USE_SANITIZER}") |
| 219 | endif() |
| 220 | elseif(MSVC) |
| 221 | message(WARNING "LLVM_USE_SANITIZER is not supported with MSVC") |
| 222 | endif() |
| 223 | endif() |
| 224 | |
| Saleem Abdulrasool | 89a52ff | 2014-03-12 04:11:28 | [diff] [blame] | 225 | string(REPLACE ";" " " LIBCXX_CXX_REQUIRED_FLAGS "${LIBCXX_CXX_REQUIRED_FLAGS}") |
| 226 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_REQUIRED_FLAGS}") |
| 227 | |
| 228 | string(REPLACE ";" " " LIBCXX_CXX_WARNING_FLAGS "${LIBCXX_CXX_WARNING_FLAGS}") |
| 229 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_WARNING_FLAGS}") |
| 230 | |
| 231 | string(REPLACE ";" " " LIBCXX_CXX_FEATURE_FLAGS "${LIBCXX_CXX_FEATURE_FLAGS}") |
| 232 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXX_CXX_FEATURE_FLAGS}") |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 233 | |
| 234 | #=============================================================================== |
| 235 | # Setup Source Code |
| 236 | #=============================================================================== |
| 237 | |
| 238 | include_directories(include) |
| Howard Hinnant | c4962b3 | 2013-11-15 17:18:57 | [diff] [blame] | 239 | add_subdirectory(include) |
| Michael J. Spencer | 626916f | 2010-12-10 19:47:54 | [diff] [blame] | 240 | |
| 241 | # Add source code. This also contains all of the logic for deciding linker flags |
| 242 | # soname, etc... |
| 243 | add_subdirectory(lib) |
| 244 | |
| 245 | #=============================================================================== |
| 246 | # Setup Tests |
| 247 | #=============================================================================== |
| 248 | |
| 249 | add_subdirectory(test) |