CMakeLists.txt 13 KB

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