1# See www/CMake.html for instructions on how to build libcxx with CMake. 2 3#=============================================================================== 4# Setup Project 5#=============================================================================== 6 7project(libcxx CXX C) 8cmake_minimum_required(VERSION 2.8) 9 10set(PACKAGE_NAME libcxx) 11set(PACKAGE_VERSION trunk-svn) 12set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 13set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu") 14 15# Add path for custom modules 16set(CMAKE_MODULE_PATH 17 ${CMAKE_MODULE_PATH} 18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 19 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" 20 ) 21 22# Require out of source build. 23include(MacroEnsureOutOfSourceBuild) 24MACRO_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 29#=============================================================================== 30# Setup CMake Options 31#=============================================================================== 32 33# Define options. 34option(LIBCXX_ENABLE_EXCEPTIONS "Use exceptions." ON) 35option(LIBCXX_ENABLE_RTTI "Use run time type information." ON) 36option(LIBCXX_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON) 37option(LIBCXX_ENABLE_PEDANTIC "Compile with pedantic enabled." ON) 38option(LIBCXX_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) 39option(LIBCXX_ENABLE_CXX0X "Enable -std=c++0x and use of c++0x language features if the compiler supports it." ON) 40option(LIBCXX_ENABLE_SHARED "Build libc++ as a shared library." ON) 41 42set(CXXABIS none libcxxabi libcxxrt libsupc++) 43if (NOT DEFINED LIBCXX_CXX_ABI) 44 set(LIBCXX_CXX_ABI "none") 45endif() 46set(LIBCXX_CXX_ABI "${LIBCXX_CXX_ABI}" CACHE STRING 47 "Specify C++ ABI library to use." FORCE) 48set_property(CACHE LIBCXX_CXX_ABI PROPERTY STRINGS "";${CXXABIS}) 49 50#=============================================================================== 51# Configure System 52#=============================================================================== 53 54# Get triples. 55include(GetTriple) 56get_host_triple(LIBCXX_HOST_TRIPLE 57 LIBCXX_HOST_ARCH 58 LIBCXX_HOST_VENDOR 59 LIBCXX_HOST_OS 60 ) 61set(LIBCXX_HOST_TRIPLE ${LIBCXX_HOST_TRIPLE} CACHE STRING "Host triple.") 62get_target_triple(LIBCXX_TARGET_TRIPLE 63 LIBCXX_TARGET_ARCH 64 LIBCXX_TARGET_VENDOR 65 LIBCXX_TARGET_OS 66 ) 67set(LIBCXX_TARGET_TRIPLE ${LIBCXX_TARGET_TRIPLE} CACHE STRING "Target triple.") 68 69if ("${LIBCXX_CXX_ABI}" STREQUAL "libsupc++") 70 set(LIBCXX_LIBSUPCXX_INCLUDE_PATHS "${LIBCXX_LIBSUPCXX_INCLUDE_PATHS}" 71 CACHE STRINGS 72 "Paths to libsupc++ include directories separate by ';'.") 73 set(LIBCXX_CXX_ABI_LIBRARIES supc++) 74 set(LIBCXX_LIBSUPCXX_FILES 75 cxxabi.h 76 bits/c++config.h 77 bits/os_defines.h 78 bits/cpu_defines.h 79 bits/cxxabi_tweaks.h 80 bits/cxxabi_forced.h 81 ) 82 # Create include directories. 83 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include") 84 file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/include/bits") 85 set(LIBCXX_LIBSUPCXX_FILE_PATHS) 86 foreach(path ${LIBCXX_LIBSUPCXX_FILES}) 87 set(found FALSE) 88 foreach(incpath ${LIBCXX_LIBSUPCXX_INCLUDE_PATHS}) 89 if (EXISTS "${incpath}/${path}") 90 set(found TRUE) 91 get_filename_component(dstdir ${path} PATH) 92 get_filename_component(file ${path} NAME) 93 add_custom_command( 94 OUTPUT "${CMAKE_BINARY_DIR}/include/${dstdir}/${file}" 95 COMMAND ${CMAKE_COMMAND} -E copy_if_different 96 "${incpath}/${path}" 97 "${CMAKE_BINARY_DIR}/include/${dstdir}" 98 MAIN_DEPENDENCY "${incpath}/${path}" 99 ) 100 list(APPEND LIBCXX_CXX_ABI_DEPS 101 "${CMAKE_BINARY_DIR}/include/${dstdir}/${file}") 102 endif() 103 endforeach() 104 if (NOT found) 105 message(FATAL_ERROR "Failed to find ${path}") 106 endif() 107 endforeach() 108 add_custom_target(supcxx_headers DEPENDS ${LIBCXX_CXX_ABI_DEPS}) 109 set(LIBCXX_CXX_ABI_DEPS supcxx_headers) 110 include_directories("${CMAKE_BINARY_DIR}/include") 111 install(DIRECTORY "${CMAKE_BINARY_DIR}/include/" 112 DESTINATION include/c++/v1 113 FILES_MATCHING 114 PATTERN "*" 115 ) 116elseif (NOT "${LIBCXX_CXX_ABI}" STREQUAL "none") 117 message(FATAL_ERROR 118 "Currently only none and libsupc++ are supported for c++ abi.") 119endif () 120 121# Configure compiler. 122include(config-ix) 123 124#=============================================================================== 125# Setup Compiler Flags 126#=============================================================================== 127 128# Get required flags. 129# On all systems the system c++ standard library headers need to be excluded. 130if (MSVC) 131 # MSVC only has -X, which disables all default includes; including the crt. 132 # Thus, we do nothing and hope we don't accidentally include any of the C++ 133 # headers. 134else() 135 if (LIBCXX_HAS_NOSTDINCXX_FLAG) 136 set(LIBCXX_CXX_REQUIRED_FLAGS -nostdinc++) 137 endif() 138 if (LIBCXX_ENABLE_CXX0X AND LIBCXX_HAS_STDCXX0X_FLAG) 139 list(APPEND LIBCXX_CXX_REQUIRED_FLAGS -std=c++0x) 140 endif() 141endif() 142 143macro(append_if list condition var) 144 if (${condition}) 145 list(APPEND ${list} ${var}) 146 endif() 147endmacro() 148 149# Get warning flags 150append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WALL_FLAG -Wall) 151append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_W_FLAG -W) 152append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter) 153append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings) 154append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WNO_LONG_LONG_FLAG -Wno-long-long) 155if (LIBCXX_ENABLE_WERROR) 156 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WERROR_FLAG -Werror) 157 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_WX_FLAG -WX) 158endif() 159if (LIBCXX_ENABLE_PEDANTIC) 160 append_if(LIBCXX_CXX_WARNING_FLAGS LIBCXX_HAS_PEDANTIC_FLAG -pedantic) 161endif() 162 163# Get feature flags. 164# Exceptions 165if (LIBCXX_ENABLE_EXCEPTIONS) 166 # Catches C++ exceptions only and tells the compiler to assume that extern C 167 # functions never throw a C++ exception. 168 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_EHSC_FLAG -EHsc) 169else() 170 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_EXCEPTIONS) 171 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHS_FLAG -EHs-) 172 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_EHA_FLAG -EHa-) 173 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_EXCEPTIONS_FLAG -fno-exceptions) 174endif() 175# RTTI 176if (NOT LIBCXX_ENABLE_RTTI) 177 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_LIBCPP_NO_RTTI) 178 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_NO_GR_FLAG -GR-) 179 append_if(LIBCXX_CXX_FEATURE_FLAGS LIBCXX_HAS_FNO_RTTI_FLAG -fno-rtti) 180endif() 181# Assert 182string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 183if (LIBCXX_ENABLE_ASSERTIONS) 184 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 185 if (NOT MSVC) 186 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -D_DEBUG) 187 endif() 188 # On Release builds cmake automatically defines NDEBUG, so we 189 # explicitly undefine it: 190 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 191 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -UNDEBUG) 192 endif() 193else() 194 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 195 list(APPEND LIBCXX_CXX_FEATURE_FLAGS -DNDEBUG) 196 endif() 197endif() 198 199# This is the _ONLY_ place where add_definitions is called. 200add_definitions( 201 ${LIBCXX_CXX_REQUIRED_FLAGS} 202 ${LIBCXX_CXX_WARNING_FLAGS} 203 ${LIBCXX_CXX_FEATURE_FLAGS} 204 ) 205 206#=============================================================================== 207# Setup Source Code 208#=============================================================================== 209 210include_directories(include) 211 212# Add source code. This also contains all of the logic for deciding linker flags 213# soname, etc... 214add_subdirectory(lib) 215 216#=============================================================================== 217# Setup Tests 218#=============================================================================== 219 220add_subdirectory(test) 221