Dan Albert | e11c1a7 | 2014-07-03 19:35:48 +0000 | [diff] [blame] | 1 | #=============================================================================== |
| 2 | # Setup Project |
| 3 | #=============================================================================== |
| 4 | |
| 5 | cmake_minimum_required(VERSION 2.8.8) |
| 6 | |
| 7 | if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) |
| 8 | project(libcxxabi) |
| 9 | |
| 10 | # Rely on llvm-config. |
| 11 | set(CONFIG_OUTPUT) |
| 12 | find_program(LLVM_CONFIG "llvm-config") |
| 13 | if(LLVM_CONFIG) |
| 14 | message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}") |
| 15 | set(CONFIG_COMMAND ${LLVM_CONFIG} |
| 16 | "--bindir" |
| 17 | "--includedir" |
| 18 | "--libdir" |
| 19 | "--prefix" |
| 20 | "--src-root") |
| 21 | execute_process( |
| 22 | COMMAND ${CONFIG_COMMAND} |
| 23 | RESULT_VARIABLE HAD_ERROR |
| 24 | OUTPUT_VARIABLE CONFIG_OUTPUT |
| 25 | ) |
| 26 | if(NOT HAD_ERROR) |
| 27 | string(REGEX REPLACE |
| 28 | "[ \t]*[\r\n]+[ \t]*" ";" |
| 29 | CONFIG_OUTPUT ${CONFIG_OUTPUT}) |
| 30 | else() |
| 31 | string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}") |
| 32 | message(STATUS "${CONFIG_COMMAND_STR}") |
| 33 | message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}") |
| 34 | endif() |
| 35 | else() |
| 36 | message(FATAL_ERROR "llvm-config not found -- ${LLVM_CONFIG}") |
| 37 | endif() |
| 38 | |
| 39 | list(GET CONFIG_OUTPUT 0 TOOLS_BINARY_DIR) |
| 40 | list(GET CONFIG_OUTPUT 1 INCLUDE_DIR) |
| 41 | list(GET CONFIG_OUTPUT 2 LIBRARY_DIR) |
| 42 | list(GET CONFIG_OUTPUT 3 LLVM_OBJ_ROOT) |
| 43 | list(GET CONFIG_OUTPUT 4 MAIN_SRC_DIR) |
| 44 | |
| 45 | set(LLVM_TOOLS_BINARY_DIR ${TOOLS_BINARY_DIR} CACHE PATH "Path to llvm/bin") |
| 46 | set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include") |
| 47 | set(LLVM_LIBRARY_DIR ${LIBRARY_DIR} CACHE PATH "Path to llvm/lib") |
| 48 | set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree") |
| 49 | set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree") |
| 50 | |
| 51 | set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/share/llvm/cmake") |
| 52 | set(LLVMCONFIG_FILE "${LLVM_CMAKE_PATH}/LLVMConfig.cmake") |
| 53 | if(EXISTS ${LLVMCONFIG_FILE}) |
| 54 | list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") |
| 55 | include(${LLVMCONFIG_FILE}) |
Dan Albert | fd961bd | 2014-07-10 02:20:11 +0000 | [diff] [blame^] | 56 | include("${LLVM_CMAKE_PATH}/AddLLVM.cmake") |
Dan Albert | e11c1a7 | 2014-07-03 19:35:48 +0000 | [diff] [blame] | 57 | else() |
| 58 | message(FATAL_ERROR "Not found: ${LLVMCONFIG_FILE}") |
| 59 | endif() |
| 60 | |
| 61 | set(PACKAGE_NAME libcxxabi) |
| 62 | set(PACKAGE_VERSION "${LLVM_PACKAGE_VERSION}") |
| 63 | set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") |
| 64 | set(PACKAGE_BUGREPORT "llvmbugs@cs.uiuc.edu") |
| 65 | |
| 66 | if(EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) |
| 67 | set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) |
| 68 | else() |
| 69 | # Seek installed Lit. |
| 70 | find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit |
| 71 | DOC "Path to lit.py") |
| 72 | endif() |
| 73 | |
| 74 | if(LLVM_LIT) |
| 75 | # Define the default arguments to use with 'lit', and an option for the user |
| 76 | # to override. |
| 77 | set(LIT_ARGS_DEFAULT "-sv") |
| 78 | if (MSVC OR XCODE) |
| 79 | set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") |
| 80 | endif() |
| 81 | set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") |
| 82 | |
| 83 | # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools. |
| 84 | if( WIN32 AND NOT CYGWIN ) |
| 85 | set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools") |
| 86 | endif() |
| 87 | else() |
| 88 | set(LLVM_INCLUDE_TESTS OFF) |
| 89 | endif() |
| 90 | |
| 91 | set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 92 | set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) |
| 93 | |
| 94 | set(LIBCXXABI_BUILT_STANDALONE 1) |
Dan Albert | fd961bd | 2014-07-10 02:20:11 +0000 | [diff] [blame^] | 95 | else() |
| 96 | set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py") |
Dan Albert | e11c1a7 | 2014-07-03 19:35:48 +0000 | [diff] [blame] | 97 | endif() |
| 98 | |
| 99 | #=============================================================================== |
| 100 | # Setup CMake Options |
| 101 | #=============================================================================== |
| 102 | |
| 103 | # Define options. |
| 104 | option(LIBCXXABI_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON) |
| 105 | option(LIBCXXABI_ENABLE_PEDANTIC "Compile with pedantic enabled." ON) |
| 106 | option(LIBCXXABI_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) |
Dan Albert | fd961bd | 2014-07-10 02:20:11 +0000 | [diff] [blame^] | 107 | |
| 108 | # Default to building a shared library so that the default options still test |
| 109 | # the libc++abi that is being built. There are two problems with testing a |
| 110 | # static libc++abi. In the case of a standalone build, the tests will link the |
| 111 | # system's libc++, which might not have been built against our libc++abi. In the |
| 112 | # case of an in tree build, libc++ will prefer a dynamic libc++abi from the |
| 113 | # system over a static libc++abi from the output directory. |
| 114 | option(LIBCXXABI_ENABLE_SHARED "Build libc++abi as a shared library." ON) |
| 115 | |
| 116 | find_path( |
| 117 | LIBCXXABI_LIBCXX_INCLUDES |
| 118 | vector |
| 119 | PATHS ${LIBCXXABI_LIBCXX_INCLUDES} |
| 120 | ${CMAKE_BINARY_DIR}/${LIBCXXABI_LIBCXX_INCLUDES} |
| 121 | ${LLVM_MAIN_SRC_DIR}/projects/libcxx/include |
| 122 | ${LLVM_INCLUDE_DIR}/c++/v1 |
| 123 | ) |
| 124 | |
| 125 | set(LIBCXXABI_LIBCXX_INCLUDES "${LIBCXXABI_LIBCXX_INCLUDES}" CACHE STRING |
| 126 | "Specify path to libc++ includes." FORCE) |
Dan Albert | e11c1a7 | 2014-07-03 19:35:48 +0000 | [diff] [blame] | 127 | |
| 128 | #=============================================================================== |
| 129 | # Configure System |
| 130 | #=============================================================================== |
| 131 | |
| 132 | # Add path for custom modules |
| 133 | set(CMAKE_MODULE_PATH |
| 134 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake" |
| 135 | "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" |
| 136 | ${CMAKE_MODULE_PATH} |
| 137 | ) |
| 138 | |
| 139 | # Configure compiler. |
| 140 | include(config-ix) |
| 141 | |
| 142 | #=============================================================================== |
| 143 | # Setup Compiler Flags |
| 144 | #=============================================================================== |
| 145 | |
| 146 | # Get required flags. |
| 147 | macro(append_if list condition var) |
| 148 | if (${condition}) |
| 149 | list(APPEND ${list} ${var}) |
| 150 | endif() |
| 151 | endmacro() |
| 152 | |
| 153 | if (LIBCXXABI_HAS_NOSTDINCXX_FLAG) |
| 154 | list(APPEND LIBCXXABI_CXX_REQUIRED_FLAGS -nostdinc++) |
| 155 | endif() |
| 156 | if (NOT LIBCXXABI_HAS_STDCXX11_FLAG) |
| 157 | message(FATAL_ERROR "Building libcxxabi requires a C++11 capable compiler.") |
| 158 | endif() |
| 159 | |
| 160 | list(APPEND LIBCXXABI_CXX_REQUIRED_FLAGS -std=c++11) |
| 161 | |
| 162 | # Get warning flags |
| 163 | if (NOT MSVC) |
| 164 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WALL_FLAG -Wall) |
| 165 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function) |
| 166 | list(APPEND LIBCXXABI_CXX_REQUIRED_FLAGS -Werror=return-type) |
| 167 | endif() |
| 168 | |
| 169 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_W_FLAG -W) |
| 170 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_UNUSED_PARAMETER_FLAG -Wno-unused-parameter) |
| 171 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings) |
| 172 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_LONG_LONG_FLAG -Wno-long-long) |
| 173 | if (LIBCXXABI_ENABLE_WERROR) |
| 174 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WERROR_FLAG -Werror) |
| 175 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WX_FLAG -WX) |
| 176 | else() |
| 177 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_WNO_ERROR_FLAG -Wno-error) |
| 178 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_NO_WX_FLAG -WX-) |
| 179 | endif() |
| 180 | if (LIBCXXABI_ENABLE_PEDANTIC) |
| 181 | append_if(LIBCXXABI_CXX_WARNING_FLAGS LIBCXXABI_HAS_PEDANTIC_FLAG -pedantic) |
| 182 | endif() |
| 183 | |
| 184 | # Get feature flags. |
| 185 | # Exceptions |
| 186 | # Catches C++ exceptions only and tells the compiler to assume that extern C |
| 187 | # functions never throw a C++ exception. |
| 188 | append_if(LIBCXXABI_CXX_FEATURE_FLAGS LIBCXXABI_HAS_EHSC_FLAG -EHsc) |
| 189 | |
| 190 | # Assert |
| 191 | string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) |
| 192 | if (LIBCXXABI_ENABLE_ASSERTIONS) |
| 193 | # MSVC doesn't like _DEBUG on release builds. See PR 4379. |
| 194 | if (NOT MSVC) |
| 195 | list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -D_DEBUG) |
| 196 | endif() |
| 197 | # On Release builds cmake automatically defines NDEBUG, so we |
| 198 | # explicitly undefine it: |
| 199 | if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") |
| 200 | list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -UNDEBUG) |
| 201 | endif() |
| 202 | else() |
| 203 | if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") |
| 204 | list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -DNDEBUG) |
| 205 | endif() |
| 206 | endif() |
| 207 | # Static library |
| 208 | if (NOT LIBCXXABI_ENABLE_SHARED) |
| 209 | list(APPEND LIBCXXABI_CXX_FEATURE_FLAGS -D_LIBCPP_BUILD_STATIC) |
| 210 | endif() |
| 211 | |
| 212 | # This is the _ONLY_ place where add_definitions is called. |
| 213 | if (MSVC) |
| 214 | add_definitions(-D_CRT_SECURE_NO_WARNINGS) |
| 215 | endif() |
| 216 | |
| 217 | string(REPLACE ";" " " LIBCXXABI_CXX_REQUIRED_FLAGS "${LIBCXXABI_CXX_REQUIRED_FLAGS}") |
| 218 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_REQUIRED_FLAGS}") |
| 219 | |
| 220 | string(REPLACE ";" " " LIBCXXABI_CXX_WARNING_FLAGS "${LIBCXXABI_CXX_WARNING_FLAGS}") |
| 221 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_WARNING_FLAGS}") |
| 222 | |
| 223 | string(REPLACE ";" " " LIBCXXABI_CXX_FEATURE_FLAGS "${LIBCXXABI_CXX_FEATURE_FLAGS}") |
| 224 | set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${LIBCXXABI_CXX_FEATURE_FLAGS}") |
| 225 | |
| 226 | #=============================================================================== |
| 227 | # Setup Source Code |
| 228 | #=============================================================================== |
| 229 | |
| 230 | include_directories(include) |
| 231 | |
| 232 | # Add source code. This also contains all of the logic for deciding linker flags |
| 233 | # soname, etc... |
| 234 | add_subdirectory(src) |
Dan Albert | fd961bd | 2014-07-10 02:20:11 +0000 | [diff] [blame^] | 235 | |
| 236 | if(NOT LIBCXXABI_ENABLE_SHARED) |
| 237 | # TODO: Fix the libc++ cmake files so that libc++abi can be statically linked. |
| 238 | # As it is now, libc++ will prefer linking against a dynamic libc++abi in the |
| 239 | # system library paths over a static libc++abi in the out directory. This |
| 240 | # would test the system library rather than the one we just built, which isn't |
| 241 | # very helpful. |
| 242 | message(WARNING "The libc++abi tests are currently only valid when " |
| 243 | "LIBCXXABI_ENABLE_SHARED is on, no check target will be " |
| 244 | "available!") |
| 245 | else() |
| 246 | add_subdirectory(test) |
| 247 | endif() |