brintos

brintos / llvm-project-archived public Read only

0
0
Text · 13.9 KiB · 97edff0 Raw
354 lines · plain
1#===============================================================================2# Setup Project3#===============================================================================4 5cmake_minimum_required(VERSION 3.20.0)6set(LLVM_SUBPROJECT_TITLE "libunwind")7 8set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")9 10# Add path for custom modules11list(INSERT CMAKE_MODULE_PATH 012  "${CMAKE_CURRENT_SOURCE_DIR}/cmake"13  "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"14  "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"15  "${LLVM_COMMON_CMAKE_UTILS}"16  "${LLVM_COMMON_CMAKE_UTILS}/Modules"17  )18 19set(LIBUNWIND_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})20set(LIBUNWIND_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})21set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH22        "Specify path to libc++ source.")23 24include(GNUInstallDirs)25include(CheckSymbolExists)26 27if (MSVC)28  message(FATAL_ERROR "Libunwind doesn't build for MSVC targets, and that is almost certainly not what you want to do "29                      "anyway since libunwind is tied to the Itanium C++ ABI, and MSVC targets must use the MS C++ ABI.")30endif()31 32#===============================================================================33# Setup CMake Options34#===============================================================================35include(CMakeDependentOption)36include(HandleCompilerRT)37 38# Define options.39option(LIBUNWIND_ENABLE_CET "Build libunwind with CET enabled." OFF)40option(LIBUNWIND_ENABLE_GCS "Build libunwind with GCS enabled." OFF)41option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)42option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)43option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)44option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)45option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)46option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)47option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)48option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)49option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)50option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)51option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})52option(LIBUNWIND_INCLUDE_TESTS "Build the libunwind tests." ${LLVM_INCLUDE_TESTS})53option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)54option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)55option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)56option(LIBUNWIND_INSTALL_HEADERS "Install the libunwind headers." ON)57option(LIBUNWIND_ENABLE_FRAME_APIS "Include libgcc-compatible frame apis." OFF)58 59set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING60    "Define suffix of library directory name (32/64)")61option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)62cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY63  "Install the static libunwind library." ON64  "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)65cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY66  "Install the shared libunwind library." ON67  "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)68 69set(LIBUNWIND_LIBRARY_VERSION "1.0" CACHE STRING70  "Version of libunwind. This will be reflected in the name of the shared library produced.71   For example, -DLIBUNWIND_LIBRARY_VERSION=x.y will result in the library being named72   libunwind.x.y.dylib, along with the usual symlinks pointing to that. On Apple platforms,73   this also controls the linker's 'current_version' property.")74 75if(MINGW)76  if (LIBUNWIND_ENABLE_SHARED)77    set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared-mingw.cfg.in")78  else()79    set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static-mingw.cfg.in")80  endif()81elseif (LIBUNWIND_ENABLE_SHARED)82  set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")83else()84  set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")85endif()86set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING87  "The path to the Lit testing configuration to use when running the tests.88   If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")89if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")90  set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")91endif()92message(STATUS "Using libunwind testing configuration: ${LIBUNWIND_TEST_CONFIG}")93set(LIBUNWIND_TEST_PARAMS "" CACHE STRING94    "A list of parameters to run the Lit test suite with.")95 96if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)97  message(FATAL_ERROR "libunwind must be built as either a shared or static library.")98endif()99 100if (LIBUNWIND_ENABLE_CET AND MSVC)101  message(FATAL_ERROR "libunwind CET support is not available for MSVC!")102endif()103 104if (WIN32)105  set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS TRUE)106else()107  set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS FALSE)108endif()109option(LIBUNWIND_HIDE_SYMBOLS110  "Do not export any symbols from the static library." ${LIBUNWIND_DEFAULT_HIDE_SYMBOLS})111 112# If toolchain is FPXX, we switch to FP64 to save the full FPRs. See:113# https://web.archive.org/web/20180828210612/https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking114check_symbol_exists(__mips_hard_float "" __MIPSHF)115check_symbol_exists(_ABIO32 "" __MIPS_O32)116if (__MIPSHF AND __MIPS_O32)117  file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/mips_is_fpxx.c118    "#if __mips_fpr != 0\n"119    "# error\n"120    "#endif\n")121  try_compile(MIPS_FPABI_FPXX ${CMAKE_BINARY_DIR}122    ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/mips_is_fpxx.c123    CMAKE_FLAGS -DCMAKE_C_LINK_EXECUTABLE='echo')124endif()125 126#===============================================================================127# Configure System128#===============================================================================129 130# Add path for custom modules131set(CMAKE_MODULE_PATH132    "${CMAKE_CURRENT_SOURCE_DIR}/cmake"133    ${CMAKE_MODULE_PATH})134 135set(LIBUNWIND_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE STRING136    "Path where built libunwind headers should be installed.")137set(LIBUNWIND_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING138    "Path where built libunwind runtime libraries should be installed.")139 140set(LIBUNWIND_SHARED_OUTPUT_NAME "unwind" CACHE STRING "Output name for the shared libunwind runtime library.")141set(LIBUNWIND_STATIC_OUTPUT_NAME "unwind" CACHE STRING "Output name for the static libunwind runtime library.")142 143if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)144  set(LIBUNWIND_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})145  if(LIBUNWIND_LIBDIR_SUBDIR)146    string(APPEND LIBUNWIND_TARGET_SUBDIR /${LIBUNWIND_LIBDIR_SUBDIR})147  endif()148  cmake_path(NORMAL_PATH LIBUNWIND_TARGET_SUBDIR)149  set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBUNWIND_TARGET_SUBDIR})150  set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBUNWIND_TARGET_SUBDIR} CACHE STRING151      "Path where built libunwind libraries should be installed.")152  unset(LIBUNWIND_TARGET_SUBDIR)153else()154  if(LLVM_LIBRARY_OUTPUT_INTDIR)155    set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})156  else()157    set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})158  endif()159  set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE STRING160      "Path where built libunwind libraries should be installed.")161endif()162 163set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})164set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})165set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})166 167set(LIBUNWIND_C_FLAGS "")168set(LIBUNWIND_CXX_FLAGS "")169set(LIBUNWIND_COMPILE_FLAGS "")170set(LIBUNWIND_LINK_FLAGS "")171set(LIBUNWIND_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING "See documentation for LIBCXX_ADDITIONAL_COMPILE_FLAGS")172set(LIBUNWIND_ADDITIONAL_LIBRARIES "" CACHE STRING173    "Additional libraries libunwind is linked to which can be provided in cache")174 175# Include macros for adding and removing libunwind flags.176include(HandleLibunwindFlags)177 178#===============================================================================179# Setup Compiler Flags180#===============================================================================181 182# Configure compiler.183include(config-ix)184 185include(HandleLibC) # Setup the C library flags186 187if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)188  list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")189endif()190 191add_compile_flags_if_supported(-Werror=return-type)192 193if (LIBUNWIND_ENABLE_CET)194  add_compile_flags_if_supported(-fcf-protection=full)195  add_compile_flags_if_supported(-mshstk)196  if (NOT CXX_SUPPORTS_FCF_PROTECTION_EQ_FULL_FLAG)197    message(SEND_ERROR "Compiler doesn't support CET -fcf-protection option!")198  endif()199  if (NOT CXX_SUPPORTS_MSHSTK_FLAG)200    message(SEND_ERROR "Compiler doesn't support CET -mshstk option!")201  endif()202endif()203 204if (LIBUNWIND_ENABLE_GCS)205  add_compile_flags_if_supported(-mbranch-protection=standard)206  if (NOT CXX_SUPPORTS_MBRANCH_PROTECTION_EQ_STANDARD_FLAG)207    message(SEND_ERROR "Compiler doesn't support GCS -mbranch-protection option!")208  endif()209endif()210 211if (WIN32)212  # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);213  # silence the warning instead of cluttering the headers (which aren't214  # necessarily the ones that the callers will use anyway) with the215  # attributes.216  add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)217endif()218 219if (MIPS_FPABI_FPXX)220  add_compile_flags(-mfp64)221endif()222 223# Get feature flags.224# Exceptions225# Catches C++ exceptions only and tells the compiler to assume that extern C226# functions never throw a C++ exception.227add_cxx_compile_flags_if_supported(-fstrict-aliasing)228add_cxx_compile_flags_if_supported(-EHsc)229 230# Don't run the linker in this CMake check.231#232# The reason why this was added is that when building libunwind for233# ARM Linux, we need to pass the -funwind-tables flag in order for it to234# work properly with ARM EHABI.235#236# However, when performing CMake checks, adding this flag causes the check237# to produce a false negative, because the compiler generates calls238# to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,239# which isn't built yet, so the linker complains about undefined symbols.240#241# This leads to libunwind not being built with this flag, which makes242# libunwind quite useless in this setup.243set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})244set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)245add_compile_flags_if_supported(-funwind-tables)246set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})247 248if (LIBUNWIND_USES_ARM_EHABI AND NOT CXX_SUPPORTS_FUNWIND_TABLES_FLAG)249  message(SEND_ERROR "The -funwind-tables flag must be supported "250                     "because this target uses ARM Exception Handling ABI")251endif()252 253add_cxx_compile_flags_if_supported(-fno-exceptions)254add_cxx_compile_flags_if_supported(-fno-rtti)255 256# Ensure that we don't depend on C++ standard library.257if (CXX_SUPPORTS_NOSTDINCXX_FLAG)258  list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)259  # Remove -stdlib flags to prevent them from causing an unused flag warning.260  string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")261  string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")262  string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")263  string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")264endif()265 266# Assert267string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)268if (LIBUNWIND_ENABLE_ASSERTIONS)269  # MSVC doesn't like _DEBUG on release builds. See PR 4379.270  if (NOT MSVC)271    add_compile_flags(-D_DEBUG)272  endif()273 274  # On Release builds cmake automatically defines NDEBUG, so we275  # explicitly undefine it:276  if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")277    add_compile_flags(-UNDEBUG)278  endif()279else()280  if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")281    add_compile_flags(-DNDEBUG)282  endif()283endif()284 285# Cross-unwinding286if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)287  add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)288endif()289 290# Include stubs for __register_frame_info_bases and related291if (LIBUNWIND_ENABLE_FRAME_APIS)292  add_compile_flags(-D_LIBUNWIND_SUPPORT_FRAME_APIS)293endif()294 295# Threading-support296if (NOT LIBUNWIND_ENABLE_THREADS)297  add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)298endif()299 300# ARM WMMX register support301if (LIBUNWIND_ENABLE_ARM_WMMX)302  # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not303  # define this macro for any supported target at present. Therefore, here we304  # provide the option to explicitly enable support for WMMX registers in the305  # unwinder.306  add_compile_flags(-D__ARM_WMMX)307endif()308 309if(LIBUNWIND_IS_BAREMETAL)310  add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)311endif()312 313if(LIBUNWIND_USE_FRAME_HEADER_CACHE)314  add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)315endif()316 317if(LIBUNWIND_REMEMBER_HEAP_ALLOC)318  add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)319endif()320 321# This is the _ONLY_ place where add_definitions is called.322if (MSVC)323  add_definitions(-D_CRT_SECURE_NO_WARNINGS)324endif()325 326if (C_SUPPORTS_COMMENT_LIB_PRAGMA)327  if (LIBUNWIND_HAS_DL_LIB)328    add_definitions(-D_LIBUNWIND_LINK_DL_LIB)329  endif()330  if (LIBUNWIND_HAS_PTHREAD_LIB)331    add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)332  endif()333endif()334 335if (RUNTIMES_EXECUTE_ONLY_CODE)336  add_compile_definitions(_LIBUNWIND_EXECUTE_ONLY_CODE)337endif()338 339#===============================================================================340# Setup Source Code341#===============================================================================342 343add_subdirectory(include)344 345add_subdirectory(src)346 347if (LIBUNWIND_INCLUDE_DOCS)348  add_subdirectory(docs)349endif()350 351if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})352  add_subdirectory(test)353endif()354