1#=============================================================================== 2# Setup Project 3#=============================================================================== 4 5cmake_minimum_required(VERSION 2.8.8) 6 7if (POLICY CMP0042) 8 cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default 9endif() 10 11if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) 12 project(libunwind) 13 14 # Rely on llvm-config. 15 set(CONFIG_OUTPUT) 16 find_program(LLVM_CONFIG "llvm-config") 17 if (DEFINED LLVM_PATH) 18 set(LLVM_INCLUDE_DIR ${LLVM_INCLUDE_DIR} CACHE PATH "Path to llvm/include") 19 set(LLVM_PATH ${LLVM_PATH} CACHE PATH "Path to LLVM source tree") 20 set(LLVM_MAIN_SRC_DIR ${LLVM_PATH}) 21 set(LLVM_CMAKE_PATH "${LLVM_PATH}/cmake/modules") 22 elseif (LLVM_CONFIG) 23 message(STATUS "Found LLVM_CONFIG as ${LLVM_CONFIG}") 24 set(CONFIG_COMMAND ${LLVM_CONFIG} "--includedir" "--prefix" "--src-root") 25 execute_process(COMMAND ${CONFIG_COMMAND} 26 RESULT_VARIABLE HAD_ERROR 27 OUTPUT_VARIABLE CONFIG_OUTPUT) 28 if (NOT HAD_ERROR) 29 string(REGEX REPLACE "[ \t]*[\r\n]+[ \t]*" ";" 30 CONFIG_OUTPUT ${CONFIG_OUTPUT}) 31 else () 32 string(REPLACE ";" " " CONFIG_COMMAND_STR "${CONFIG_COMMAND}") 33 message(STATUS "${CONFIG_COMMAND_STR}") 34 message(FATAL_ERROR "llvm-config failed with status ${HAD_ERROR}") 35 endif () 36 37 list(GET CONFIG_OUTPUT 0 INCLUDE_DIR) 38 list(GET CONFIG_OUTPUT 1 LLVM_OBJ_ROOT) 39 list(GET CONFIG_OUTPUT 2 MAIN_SRC_DIR) 40 41 set(LLVM_INCLUDE_DIR ${INCLUDE_DIR} CACHE PATH "Path to llvm/include") 42 set(LLVM_BINARY_DIR ${LLVM_OBJ_ROOT} CACHE PATH "Path to LLVM build tree") 43 set(LLVM_MAIN_SRC_DIR ${MAIN_SRC_DIR} CACHE PATH "Path to LLVM source tree") 44 set(LLVM_CMAKE_PATH "${LLVM_BINARY_DIR}/share/llvm/cmake") 45 set(LLVM_LIT_PATH "${LLVM_PATH}/utils/lit/lit.py") 46 else () 47 message(FATAL_ERROR "llvm-config not found and LLVM_MAIN_SRC_DIR not defined. " 48 "Reconfigure with -DLLVM_CONFIG=path/to/llvm-config " 49 "or -DLLVM_PATH=path/to/llvm-source-root.") 50 endif () 51 52 if (EXISTS ${LLVM_CMAKE_PATH}) 53 list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_PATH}") 54 include("${LLVM_CMAKE_PATH}/AddLLVM.cmake") 55 include("${LLVM_CMAKE_PATH}/HandleLLVMOptions.cmake") 56 else () 57 message(FATAL_ERROR "Not found: ${LLVM_CMAKE_PATH}") 58 endif () 59 60 set(PACKAGE_NAME libunwind) 61 set(PACKAGE_VERSION 3.8.0svn) 62 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 63 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org") 64 65 if (EXISTS ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) 66 set(LLVM_LIT ${LLVM_MAIN_SRC_DIR}/utils/lit/lit.py) 67 else () 68 # Seek installed Lit. 69 find_program(LLVM_LIT "lit.py" ${LLVM_MAIN_SRC_DIR}/utils/lit 70 DOC "Path to lit.py") 71 endif () 72 73 if (LLVM_LIT) 74 # Define the default arguments to use with 'lit', and an option for the user 75 # to override. 76 set(LIT_ARGS_DEFAULT "-sv") 77 if (MSVC OR XCODE) 78 set(LIT_ARGS_DEFAULT "${LIT_ARGS_DEFAULT} --no-progress-bar") 79 endif () 80 set(LLVM_LIT_ARGS "${LIT_ARGS_DEFAULT}" CACHE STRING "Default options for lit") 81 82 # On Win32 hosts, provide an option to specify the path to the GnuWin32 tools. 83 if (WIN32 AND NOT CYGWIN) 84 set(LLVM_LIT_TOOLS_DIR "" CACHE PATH "Path to GnuWin32 tools") 85 endif () 86 else () 87 set(LLVM_INCLUDE_TESTS OFF) 88 endif () 89 90 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}) 91 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}) 92else() 93 set(LLVM_MAIN_SRC_DIR "${CMAKE_SOURCE_DIR}" CACHE PATH "Path to LLVM source tree") 94 set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py") 95endif() 96 97#=============================================================================== 98# Setup CMake Options 99#=============================================================================== 100 101# Define options. 102option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON) 103option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON) 104option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) 105option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON) 106 107set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE STRING "GCC toolchain for cross compiling.") 108set(LIBUNWIND_SYSROOT "" CACHE STRING "Sysroot for cross compiling.") 109 110#=============================================================================== 111# Configure System 112#=============================================================================== 113 114# Add path for custom modules 115set(CMAKE_MODULE_PATH 116 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 117 ${CMAKE_MODULE_PATH}) 118 119# Configure compiler. 120include(config-ix) 121 122set(LIBUNWIND_COMPILER ${CMAKE_CXX_COMPILER}) 123set(LIBUNWIND_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 124set(LIBUNWIND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 125set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LLVM_LIBDIR_SUFFIX}) 126 127#=============================================================================== 128# Setup Compiler Flags 129#=============================================================================== 130 131# Get required flags. 132macro(append_if list condition var) 133 if (${condition}) 134 list(APPEND ${list} ${var}) 135 endif() 136endmacro() 137 138set(LIBUNWIND_C_FLAGS "") 139set(LIBUNWIND_CXX_FLAGS "") 140set(LIBUNWIND_COMPILE_FLAGS "") 141set(LIBUNWIND_LINK_FLAGS "") 142 143append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WERROR_FLAG -Werror=return-type) 144 145# Get warning flags 146append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_W_FLAG -W) 147append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WALL_FLAG -Wall) 148append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WCHAR_SUBSCRIPTS_FLAG -Wchar-subscripts) 149append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WCONVERSION_FLAG -Wconversion) 150append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WMISMATCHED_TAGS_FLAG -Wmismatched-tags) 151append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WMISSING_BRACES_FLAG -Wmissing-braces) 152append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WNEWLINE_EOF_FLAG -Wnewline-eof) 153append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WNO_UNUSED_FUNCTION_FLAG -Wno-unused-function) 154append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSHADOW_FLAG -Wshadow) 155append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSHORTEN_64_TO_32_FLAG -Wshorten-64-to-32) 156append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSIGN_COMPARE_FLAG -Wsign-compare) 157append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSIGN_CONVERSION_FLAG -Wsign-conversion) 158append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSTRICT_ALIASING_FLAG -Wstrict-aliasing=2) 159append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WSTRICT_OVERFLOW_FLAG -Wstrict-overflow=4) 160append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WUNUSED_PARAMETER_FLAG -Wunused-parameter) 161append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WUNUSED_VARIABLE_FLAG -Wunused-variable) 162append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WWRITE_STRINGS_FLAG -Wwrite-strings) 163append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WUNDEF_FLAG -Wundef) 164 165if (LIBUNWIND_ENABLE_WERROR) 166 append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WERROR_FLAG -Werror) 167 append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WX_FLAG -WX) 168else() 169 append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_WNO_ERROR_FLAG -Wno-error) 170 append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_NO_WX_FLAG -WX-) 171endif() 172 173if (LIBUNWIND_ENABLE_PEDANTIC) 174 append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_HAS_PEDANTIC_FLAG -pedantic) 175endif() 176 177# Get feature flags. 178# Exceptions 179# Catches C++ exceptions only and tells the compiler to assume that extern C 180# functions never throw a C++ exception. 181append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_FSTRICT_ALIASING_FLAG -fstrict-aliasing) 182append_if(LIBUNWIND_CXX_FLAGS LIBUNWIND_HAS_EHSC_FLAG -EHsc) 183 184append_if(LIBUNWIND_C_FLAGS LIBUNWIND_HAS_FUNWIND_TABLES -funwind-tables) 185 186# Assert 187string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 188if (LIBUNWIND_ENABLE_ASSERTIONS) 189 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 190 if (NOT MSVC) 191 list(APPEND LIBUNWIND_COMPILE_FLAGS -D_DEBUG) 192 endif () 193 194 # On Release builds cmake automatically defines NDEBUG, so we 195 # explicitly undefine it: 196 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 197 list(APPEND LIBUNWIND_COMPILE_FLAGS -UNDEBUG) 198 endif () 199else() 200 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 201 list(APPEND LIBUNWIND_COMPILE_FLAGS -DNDEBUG) 202 endif () 203endif () 204 205# This is the _ONLY_ place where add_definitions is called. 206if (MSVC) 207 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 208endif () 209 210append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_TARGET_TRIPLE 211 "-target ${LIBUNWIND_TARGET_TRIPLE}") 212append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_GCC_TOOLCHAIN 213 "-gcc-toolchain ${LIBUNWIND_GCC_TOOLCHAIN}") 214append_if(LIBUNWIND_COMPILE_FLAGS LIBUNWIND_SYSROOT 215 "--sysroot=${LIBUNWIND_SYSROOT}") 216 217#=============================================================================== 218# Setup Source Code 219#=============================================================================== 220 221include_directories(include) 222 223add_subdirectory(src) 224 225