Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [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 +0000 | [diff] [blame] | 17 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake" |
| 18 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
Alexey Samsonov | 179fa78 | 2013-09-30 09:10:01 +0000 | [diff] [blame] | 19 | ${CMAKE_MODULE_PATH} |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [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 +0000 | [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 +0000 | [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) |
| 43 | option(LIBCXX_ENABLE_CXX0X "Enable -std=c++0x and use of c++0x language features if the compiler supports it." ON) |
| 44 | option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON) |
Michael Gottesman | c64c980 | 2013-09-02 07:28:05 +0000 | [diff] [blame] | 45 | option(LIBCXX_INSTALL_SUPPORT_HEADERS "Install libc++ support headers." ON) |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 46 | |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 47 | set(CXXABIS none libcxxabi libcxxrt libstdc++ libsupc++) |
Michael J. Spencer | db8a030 | 2012-12-31 19:34:21 +0000 | [diff] [blame] | 48 | if (NOT DEFINED LIBCXX_CXX_ABI) |
| 49 | set(LIBCXX_CXX_ABI "none") |
| 50 | endif() |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 51 | set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING |
| 52 | "Specify C++ ABI library to use." FORCE) |
Howard Hinnant | 9c07b14 | 2013-08-19 21:42:07 +0000 | [diff] [blame] | 53 | set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS ;${CXXABIS}) |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 54 | |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 55 | #=============================================================================== |
| 56 | # Configure System |
| 57 | #=============================================================================== |
| 58 | |
| 59 | # Get triples. |
| 60 | include(GetTriple) |
| 61 | get_host_triple(LIBCXX_HOST_TRIPLE |
| 62 | LIBCXX_HOST_ARCH |
| 63 | LIBCXX_HOST_VENDOR |
| 64 | LIBCXX_HOST_OS |
| 65 | ) |
| 66 | set(LIBCXX_HOST_TRIPLE ${LIBCXX_HOST_TRIPLE} CACHE STRING "Host triple.") |
| 67 | get_target_triple(LIBCXX_TARGET_TRIPLE |
| 68 | LIBCXX_TARGET_ARCH |
| 69 | LIBCXX_TARGET_VENDOR |
| 70 | LIBCXX_TARGET_OS |
| 71 | ) |
| 72 | set(LIBCXX_TARGET_TRIPLE ${LIBCXX_TARGET_TRIPLE} CACHE STRING "Target triple.") |
| 73 | |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 74 | #=============================================================================== |
| 75 | # Add an ABI library if appropriate |
| 76 | #=============================================================================== |
| 77 | |
| 78 | # |
| 79 | # _setup_abi: Set up the build to use an ABI library |
| 80 | # |
| 81 | # Parameters: |
| 82 | # abidefines: A list of defines needed to compile libc++ with the ABI library |
| 83 | # abilibs : A list of libraries to link against |
| 84 | # abifiles : A list of files (which may be relative paths) to copy into the |
| 85 | # libc++ build tree for the build. These files will also be |
| 86 | # installed alongside the libc++ headers. |
| 87 | # abidirs : A list of relative paths to create under an include directory |
| 88 | # in the libc++ build directory. |
| 89 | # |
| 90 | macro(setup_abi_lib abipathvar abidefines abilibs abifiles abidirs) |
| 91 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS ${abidefines}) |
| 92 | set(${abipathvar} "${${abipathvar}}" |
| 93 | CACHE STRINGS |
| 94 | "Paths to ABI include directories separate by ';'." |
| 95 | ) |
| 96 | set(LIBCXX_CXX_ABI_LIBRARIES ${abilibs}) |
| 97 | set(LIBCXX_ABILIB_FILES ${abifiles}) |
Michael J. Spencer | db8a030 | 2012-12-31 19:34:21 +0000 | [diff] [blame] | 98 | file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include") |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 99 | foreach(_d ${abidirs}) |
| 100 | file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include/${_d}") |
| 101 | endforeach() |
| 102 | set(LIBCXX_ABILIB_FILE_PATHS) |
| 103 | foreach(fpath ${LIBCXX_ABILIB_FILES}) |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 104 | set(found FALSE) |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 105 | foreach(incpath ${${abipathvar}}) |
| 106 | if (EXISTS "${incpath}/${fpath}") |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 107 | set(found TRUE) |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 108 | get_filename_component(dstdir ${fpath} PATH) |
| 109 | get_filename_component(ifile ${fpath} NAME) |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 110 | add_custom_command( |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 111 | OUTPUT "${CMAKE_BINARY_DIR}/include/${dstdir}/${ifile}" |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 112 | COMMAND ${CMAKE_COMMAND} -E copy_if_different |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 113 | "${incpath}/${fpath}" |
Howard Hinnant | b777d6a | 2013-04-02 21:33:01 +0000 | [diff] [blame] | 114 | "${CMAKE_BINARY_DIR}/include/${dstdir}" |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 115 | MAIN_DEPENDENCY "${incpath}/${fpath}" |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 116 | ) |
| 117 | list(APPEND LIBCXX_CXX_ABI_DEPS |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 118 | "${CMAKE_BINARY_DIR}/include/${dstdir}/${ifile}" |
| 119 | ) |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 120 | endif() |
| 121 | endforeach() |
| 122 | if (NOT found) |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 123 | message(FATAL_ERROR "Failed to find ${fpath}") |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 124 | endif() |
| 125 | endforeach() |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 126 | add_custom_target(abilib_headers DEPENDS ${LIBCXX_CXX_ABI_DEPS}) |
| 127 | set(LIBCXX_CXX_ABI_DEPS abilib_headers) |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 128 | include_directories("${CMAKE_BINARY_DIR}/include") |
| 129 | install(DIRECTORY "${CMAKE_BINARY_DIR}/include/" |
| 130 | DESTINATION include/c++/v1 |
| 131 | FILES_MATCHING |
| 132 | PATTERN "*" |
| 133 | ) |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 134 | endmacro() |
| 135 | |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 136 | if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++" OR |
| 137 | "${LIBCXX_CXX_ABI}" STREQUAL "libsupc++") |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 138 | set(_LIBSUPCXX_INCLUDE_FILES |
| 139 | cxxabi.h bits/c++config.h bits/os_defines.h bits/cpu_defines.h |
| 140 | bits/cxxabi_tweaks.h bits/cxxabi_forced.h |
| 141 | ) |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 142 | if ("${LIBCXX_CXX_ABI}" STREQUAL "libstdc++") |
| 143 | set(_LIBSUPCXX_DEFINES "-DLIBSTDCXX") |
| 144 | set(_LIBSUPCXX_LIBNAME stdc++) |
| 145 | else() |
| 146 | set(_LIBSUPCXX_DEFINES "") |
| 147 | set(_LIBSUPCXX_LIBNAME supc++) |
| 148 | endif() |
| 149 | setup_abi_lib("LIBCXX_LIBSUPCXX_INCLUDE_PATHS" |
| 150 | "-D__GLIBCXX__ ${_LIBSUPCXX_DEFINES}" |
| 151 | "${_LIBSUPCXX_LIBNAME}" "${_LIBSUPCXX_INCLUDE_FILES}" "bits" |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 152 | ) |
Howard Hinnant | 46c49d1 | 2013-02-07 15:27:39 +0000 | [diff] [blame] | 153 | elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxabi") |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 154 | setup_abi_lib("LIBCXX_LIBCXXABI_INCLUDE_PATHS" "" |
Peter Collingbourne | 66a8fce | 2013-10-03 22:58:36 +0000 | [diff] [blame] | 155 | "c++abi" "cxxabi.h" "" |
Howard Hinnant | 46c49d1 | 2013-02-07 15:27:39 +0000 | [diff] [blame] | 156 | ) |
| 157 | elseif ("${LIBCXX_CXX_ABI}" STREQUAL "libcxxrt") |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 158 | setup_abi_lib("LIBCXX_LIBCXXRT_INCLUDE_PATHS" "-DLIBCXXRT" |
| 159 | "cxxrt" "cxxabi.h;unwind.h;unwind-arm.h;unwind-itanium.h" "" |
Howard Hinnant | 46c49d1 | 2013-02-07 15:27:39 +0000 | [diff] [blame] | 160 | ) |
Michael J. Spencer | db8a030 | 2012-12-31 19:34:21 +0000 | [diff] [blame] | 161 | elseif (NOT "${LIBCXX_CXX_ABI}" STREQUAL "none") |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 162 | message(FATAL_ERROR |
Peter Collingbourne | d0d308f | 2013-10-06 22:13:19 +0000 | [diff] [blame] | 163 | "Currently libstdc++, libsupc++, libcxxabi, libcxxrt and none are " |
Howard Hinnant | b85dea3 | 2013-02-08 19:04:53 +0000 | [diff] [blame] | 164 | "supported for c++ abi." |
| 165 | ) |
Michael J. Spencer | a358fbe | 2012-11-30 21:02:29 +0000 | [diff] [blame] | 166 | endif () |
| 167 | |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 168 | # Configure compiler. |
| 169 | include(config-ix) |
| 170 | |
| 171 | #=============================================================================== |
| 172 | # Setup Compiler Flags |
| 173 | #=============================================================================== |
| 174 | |
| 175 | # Get required flags. |
| 176 | # On all systems the system c++ standard library headers need to be excluded. |
| 177 | if (MSVC) |
| 178 | # MSVC only has -X, which disables all default includes; including the crt. |
| 179 | # Thus, we do nothing and hope we don't accidentally include any of the C++ |
| 180 | # headers. |
| 181 | else() |
| 182 | if (LIBCXX_HAS_NOSTDINCXX_FLAG) |
| 183 | set(LIBCXX_CXX_REQUIRED_FLAGS -nostdinc++) |
| 184 | endif() |
| 185 | if (LIBCXX_ENABLE_CXX0X AND LIBCXX_HAS_STDCXX0X_FLAG) |
| 186 | list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -std=c++0x) |
| 187 | endif() |
| 188 | endif() |
| 189 | |
| 190 | macro(append_if list condition var) |
| 191 | if (${condition}) |
| 192 | list(APPEND ${list} ${var}) |
| 193 | endif() |
| 194 | endmacro() |
| 195 | |
| 196 | # Get warning flags |
Howard Hinnant | adb73b1 | 2013-10-05 00:07:35 +0000 | [diff] [blame] | 197 | if (NOT MSVC) |
| 198 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WALL_FLAG -Wall) |
Marshall Clow | 0f7afe7 | 2013-10-21 15:56:35 +0000 | [diff] [blame] | 199 | set(LIBCXX_CXX_REQUIRED_FLAGS, -Werror=return-type) |
Howard Hinnant | adb73b1 | 2013-10-05 00:07:35 +0000 | [diff] [blame] | 200 | endif() |
Marshall Clow | 0f7afe7 | 2013-10-21 15:56:35 +0000 | [diff] [blame] | 201 | |
Douglas Gregor | f9b6e7e | 2012-06-07 22:26:00 +0000 | [diff] [blame] | 202 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_W_FLAG -W) |
| 203 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter) |
| 204 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings) |
| 205 | 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 +0000 | [diff] [blame] | 206 | if (LIBCXX_ENABLE_WERROR) |
Douglas Gregor | f9b6e7e | 2012-06-07 22:26:00 +0000 | [diff] [blame] | 207 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WERROR_FLAG -Werror) |
| 208 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WX_FLAG -WX) |
Alexey Samsonov | f7eb573 | 2013-10-02 07:44:19 +0000 | [diff] [blame] | 209 | else() |
| 210 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_ERROR_FLAG -Wno-error) |
| 211 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_NO_WX_FLAG -WX-) |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 212 | endif() |
| 213 | if (LIBCXX_ENABLE_PEDANTIC) |
Douglas Gregor | f9b6e7e | 2012-06-07 22:26:00 +0000 | [diff] [blame] | 214 | append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_PEDANTIC_FLAG -pedantic) |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 215 | endif() |
| 216 | |
| 217 | # Get feature flags. |
| 218 | # Exceptions |
| 219 | if (LIBCXX_ENABLE_EXCEPTIONS) |
| 220 | # Catches C++ exceptions only and tells the compiler to assume that extern C |
| 221 | # functions never throw a C++ exception. |
| 222 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_EHSC_FLAG -EHsc) |
| 223 | else() |
| 224 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_EXCEPTIONS) |
| 225 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHS_FLAG -EHs-) |
| 226 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHA_FLAG -EHa-) |
| 227 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions) |
| 228 | endif() |
| 229 | # RTTI |
| 230 | if (NOT LIBCXX_ENABLE_RTTI) |
| 231 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_RTTI) |
| 232 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_GR_FLAG -GR-) |
| 233 | append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_RTTI_FLAG -fno-rtti) |
| 234 | endif() |
| 235 | # Assert |
Howard Hinnant | e103a3d | 2012-08-05 17:37:39 +0000 | [diff] [blame] | 236 | string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) |
| 237 | if (LIBCXX_ENABLE_ASSERTIONS) |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 238 | # MSVC doesn't like _DEBUG on release builds. See PR 4379. |
| 239 | if (NOT MSVC) |
| 240 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_DEBUG) |
| 241 | endif() |
| 242 | # On Release builds cmake automatically defines NDEBUG, so we |
| 243 | # explicitly undefine it: |
| 244 | if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") |
| 245 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -UNDEBUG) |
| 246 | endif() |
| 247 | else() |
| 248 | if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") |
| 249 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -DNDEBUG) |
| 250 | endif() |
| 251 | endif() |
Howard Hinnant | 4a0e74f | 2013-02-25 15:50:36 +0000 | [diff] [blame] | 252 | # Static library |
| 253 | if (NOT LIBCXX_ENABLE_SHARED) |
| 254 | list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_BUILD_STATIC) |
| 255 | endif() |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 256 | |
| 257 | # This is the _ONLY_ place where add_definitions is called. |
Marshall Clow | 0f7afe7 | 2013-10-21 15:56:35 +0000 | [diff] [blame] | 258 | if (MSVC) |
| 259 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) |
| 260 | endif() |
| 261 | |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 262 | add_definitions( |
| 263 | ${LIBCXX_CXX_REQUIRED_FLAGS} |
| 264 | ${LIBCXX_CXX_WARNING_FLAGS} |
| 265 | ${LIBCXX_CXX_FEATURE_FLAGS} |
| 266 | ) |
| 267 | |
| 268 | #=============================================================================== |
| 269 | # Setup Source Code |
| 270 | #=============================================================================== |
| 271 | |
| 272 | include_directories(include) |
Howard Hinnant | c4962b3 | 2013-11-15 17:18:57 +0000 | [diff] [blame] | 273 | add_subdirectory(include) |
Michael J. Spencer | 626916f | 2010-12-10 19:47:54 +0000 | [diff] [blame] | 274 | |
| 275 | # Add source code. This also contains all of the logic for deciding linker flags |
| 276 | # soname, etc... |
| 277 | add_subdirectory(lib) |
| 278 | |
| 279 | #=============================================================================== |
| 280 | # Setup Tests |
| 281 | #=============================================================================== |
| 282 | |
| 283 | add_subdirectory(test) |