1if (NOT IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libcxx") 2 message(FATAL_ERROR "libunwind requires being built in a monorepo layout with libcxx available") 3endif() 4 5#=============================================================================== 6# Setup Project 7#=============================================================================== 8 9cmake_minimum_required(VERSION 3.13.4) 10 11if (POLICY CMP0042) 12 cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default 13endif() 14 15# Add path for custom modules 16set(CMAKE_MODULE_PATH 17 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules" 19 ${CMAKE_MODULE_PATH} 20 ) 21 22set(LIBUNWIND_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) 23set(LIBUNWIND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) 24set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH 25 "Specify path to libc++ source.") 26 27if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBUNWIND_STANDALONE_BUILD) 28 project(libunwind LANGUAGES C CXX ASM) 29 30 set(PACKAGE_NAME libunwind) 31 set(PACKAGE_VERSION 12.0.0git) 32 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}") 33 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org") 34 35 # Add the CMake module path of libcxx so we can reuse HandleOutOfTreeLLVM.cmake 36 set(LIBUNWIND_LIBCXX_CMAKE_PATH "${LIBUNWIND_LIBCXX_PATH}/cmake/Modules") 37 list(APPEND CMAKE_MODULE_PATH "${LIBUNWIND_LIBCXX_CMAKE_PATH}") 38 39 # In a standalone build, we don't have llvm to automatically generate the 40 # llvm-lit script for us. So we need to provide an explicit directory that 41 # the configurator should write the script into. 42 set(LIBUNWIND_STANDALONE_BUILD 1) 43 set(LLVM_LIT_OUTPUT_DIR "${LIBUNWIND_BINARY_DIR}/bin") 44 45 # Find the LLVM sources and simulate LLVM CMake options. 46 include(HandleOutOfTreeLLVM) 47else() 48 set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py") 49endif() 50 51#=============================================================================== 52# Setup CMake Options 53#=============================================================================== 54include(CMakeDependentOption) 55include(HandleCompilerRT) 56 57# Define options. 58option(LIBUNWIND_BUILD_32_BITS "Build 32 bit libunwind" ${LLVM_BUILD_32_BITS}) 59option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON) 60option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON) 61option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF) 62option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON) 63option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON) 64option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF) 65option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF) 66option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON) 67option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF) 68option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF) 69option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS}) 70option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF) 71option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF) 72option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF) 73 74set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING 75 "Define suffix of library directory name (32/64)") 76option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON) 77cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY 78 "Install the static libunwind library." ON 79 "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF) 80cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY 81 "Install the shared libunwind library." ON 82 "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF) 83set(LIBUNWIND_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling.") 84set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.") 85set(LIBUNWIND_SYSROOT "" CACHE PATH "Sysroot for cross compiling.") 86set(LIBUNWIND_TEST_LINKER_FLAGS "" CACHE STRING 87 "Additional linker flags for test programs.") 88set(LIBUNWIND_TEST_COMPILER_FLAGS "" CACHE STRING 89 "Additional compiler flags for test programs.") 90set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/lit.site.cfg.in" CACHE STRING 91 "The Lit testing configuration to use when running the tests.") 92set(LIBUNWIND_TEST_PARAMS "" CACHE STRING 93 "A list of parameters to run the Lit test suite with.") 94 95if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC) 96 message(FATAL_ERROR "libunwind must be built as either a shared or static library.") 97endif() 98 99# Check that we can build with 32 bits if requested. 100if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32) 101 if (LIBUNWIND_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM 102 message(STATUS "Building 32 bits executables and libraries.") 103 endif() 104elseif(LIBUNWIND_BUILD_32_BITS) 105 message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS=ON is not supported on this platform.") 106endif() 107 108option(LIBUNWIND_HERMETIC_STATIC_LIBRARY 109 "Do not export any symbols from the static library." OFF) 110 111#=============================================================================== 112# Configure System 113#=============================================================================== 114 115# Add path for custom modules 116set(CMAKE_MODULE_PATH 117 "${CMAKE_CURRENT_SOURCE_DIR}/cmake" 118 ${CMAKE_MODULE_PATH}) 119 120if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE) 121 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++) 122 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++) 123 if(LIBCXX_LIBDIR_SUBDIR) 124 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR}) 125 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR}) 126 endif() 127elseif(LLVM_LIBRARY_OUTPUT_INTDIR) 128 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}) 129 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX}) 130else() 131 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX}) 132 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX}) 133endif() 134 135set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR}) 136set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR}) 137set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR}) 138 139set(LIBUNWIND_INSTALL_PREFIX "" CACHE STRING "Define libunwind destination prefix.") 140 141set(LIBUNWIND_C_FLAGS "") 142set(LIBUNWIND_CXX_FLAGS "") 143set(LIBUNWIND_COMPILE_FLAGS "") 144set(LIBUNWIND_LINK_FLAGS "") 145 146# Include macros for adding and removing libunwind flags. 147include(HandleLibunwindFlags) 148 149#=============================================================================== 150# Setup Compiler Flags 151#=============================================================================== 152 153# Get required flags. 154add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32") 155 156if(LIBUNWIND_TARGET_TRIPLE) 157 add_target_flags("--target=${LIBUNWIND_TARGET_TRIPLE}") 158elseif(CMAKE_CXX_COMPILER_TARGET) 159 set(LIBUNWIND_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}") 160endif() 161if(LIBUNWIND_GCC_TOOLCHAIN) 162 add_target_flags("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}") 163elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN) 164 set(LIBUNWIND_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}") 165endif() 166if(LIBUNWIND_SYSROOT) 167 add_target_flags("--sysroot=${LIBUNWIND_SYSROOT}") 168elseif(CMAKE_SYSROOT) 169 set(LIBUNWIND_SYSROOT "${CMAKE_SYSROOT}") 170endif() 171 172if (LIBUNWIND_TARGET_TRIPLE) 173 set(TARGET_TRIPLE "${LIBUNWIND_TARGET_TRIPLE}") 174endif() 175 176# Configure compiler. 177include(config-ix) 178 179if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG) 180 list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt") 181endif() 182 183add_compile_flags_if_supported(-Werror=return-type) 184 185# Get warning flags 186add_compile_flags_if_supported(-W) 187add_compile_flags_if_supported(-Wall) 188add_compile_flags_if_supported(-Wchar-subscripts) 189add_compile_flags_if_supported(-Wconversion) 190add_compile_flags_if_supported(-Wmismatched-tags) 191add_compile_flags_if_supported(-Wmissing-braces) 192add_compile_flags_if_supported(-Wnewline-eof) 193add_compile_flags_if_supported(-Wno-unused-function) 194add_compile_flags_if_supported(-Wshadow) 195add_compile_flags_if_supported(-Wshorten-64-to-32) 196add_compile_flags_if_supported(-Wsign-compare) 197add_compile_flags_if_supported(-Wsign-conversion) 198add_compile_flags_if_supported(-Wstrict-aliasing=2) 199add_compile_flags_if_supported(-Wstrict-overflow=4) 200add_compile_flags_if_supported(-Wunused-parameter) 201add_compile_flags_if_supported(-Wunused-variable) 202add_compile_flags_if_supported(-Wwrite-strings) 203add_compile_flags_if_supported(-Wundef) 204 205add_compile_flags_if_supported(-Wno-suggest-override) 206 207if (WIN32) 208 # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT); 209 # silence the warning instead of cluttering the headers (which aren't 210 # necessarily the ones that the callers will use anyway) with the 211 # attributes. 212 add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration) 213endif() 214 215if (LIBUNWIND_ENABLE_WERROR) 216 add_compile_flags_if_supported(-Werror) 217 add_compile_flags_if_supported(-WX) 218else() 219 add_compile_flags_if_supported(-Wno-error) 220 add_compile_flags_if_supported(-WX-) 221endif() 222 223if (LIBUNWIND_ENABLE_PEDANTIC) 224 add_compile_flags_if_supported(-pedantic) 225endif() 226 227# Get feature flags. 228# Exceptions 229# Catches C++ exceptions only and tells the compiler to assume that extern C 230# functions never throw a C++ exception. 231add_cxx_compile_flags_if_supported(-fstrict-aliasing) 232add_cxx_compile_flags_if_supported(-EHsc) 233 234# Don't run the linker in this CMake check. 235# 236# The reason why this was added is that when building libunwind for 237# ARM Linux, we need to pass the -funwind-tables flag in order for it to 238# work properly with ARM EHABI. 239# 240# However, when performing CMake checks, adding this flag causes the check 241# to produce a false negative, because the compiler generates calls 242# to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself, 243# which isn't built yet, so the linker complains about undefined symbols. 244# 245# This leads to libunwind not being built with this flag, which makes 246# libunwind quite useless in this setup. 247set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE}) 248set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) 249add_compile_flags_if_supported(-funwind-tables) 250set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE}) 251 252if (LIBUNWIND_USES_ARM_EHABI AND NOT LIBUNWIND_SUPPORTS_FUNWIND_TABLES_FLAG) 253 message(SEND_ERROR "The -funwind-tables flag must be supported " 254 "because this target uses ARM Exception Handling ABI") 255endif() 256 257add_cxx_compile_flags_if_supported(-fno-exceptions) 258add_cxx_compile_flags_if_supported(-fno-rtti) 259 260# Ensure that we don't depend on C++ standard library. 261if (LIBUNWIND_HAS_NOSTDINCXX_FLAG) 262 list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++) 263 # Remove -stdlib flags to prevent them from causing an unused flag warning. 264 string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 265 string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 266 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 267 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") 268endif() 269 270# Assert 271string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 272if (LIBUNWIND_ENABLE_ASSERTIONS) 273 # MSVC doesn't like _DEBUG on release builds. See PR 4379. 274 if (NOT MSVC) 275 add_compile_flags(-D_DEBUG) 276 endif() 277 278 # On Release builds cmake automatically defines NDEBUG, so we 279 # explicitly undefine it: 280 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 281 add_compile_flags(-UNDEBUG) 282 endif() 283else() 284 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE") 285 add_compile_flags(-DNDEBUG) 286 endif() 287endif() 288 289# Cross-unwinding 290if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING) 291 add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY) 292endif() 293 294# Threading-support 295if (NOT LIBUNWIND_ENABLE_THREADS) 296 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS) 297endif() 298 299# ARM WMMX register support 300if (LIBUNWIND_ENABLE_ARM_WMMX) 301 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not 302 # define this macro for any supported target at present. Therefore, here we 303 # provide the option to explicitly enable support for WMMX registers in the 304 # unwinder. 305 add_compile_flags(-D__ARM_WMMX) 306endif() 307 308if(LIBUNWIND_IS_BAREMETAL) 309 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL) 310endif() 311 312if(LIBUNWIND_USE_FRAME_HEADER_CACHE) 313 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE) 314endif() 315 316if(LIBUNWIND_REMEMBER_HEAP_ALLOC) 317 add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC) 318endif() 319 320# This is the _ONLY_ place where add_definitions is called. 321if (MSVC) 322 add_definitions(-D_CRT_SECURE_NO_WARNINGS) 323endif() 324 325# Disable DLL annotations on Windows for static builds. 326if (WIN32 AND LIBUNWIND_ENABLE_STATIC AND NOT LIBUNWIND_ENABLE_SHARED) 327 add_definitions(-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS) 328endif() 329 330if (LIBUNWIND_HAS_COMMENT_LIB_PRAGMA) 331 if (LIBUNWIND_HAS_DL_LIB) 332 add_definitions(-D_LIBUNWIND_LINK_DL_LIB) 333 endif() 334 if (LIBUNWIND_HAS_PTHREAD_LIB) 335 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB) 336 endif() 337endif() 338 339#=============================================================================== 340# Setup Source Code 341#=============================================================================== 342 343include_directories(include) 344 345add_subdirectory(src) 346 347if (LIBUNWIND_INCLUDE_DOCS) 348 add_subdirectory(docs) 349endif() 350 351if (EXISTS ${LLVM_CMAKE_PATH}) 352 add_subdirectory(test) 353endif() 354