blob: 3727d11598366b98ceaba6e2e179844b2a6afd63 [file] [log] [blame]
Dominik Montada9e379242020-08-24 11:01:05 +02001if (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
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +00005#===============================================================================
Tanya Lattnerfdfd2c12015-08-06 23:31:37 +00006# Setup Project
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +00007#===============================================================================
8
Louis Dionne7ee89bc2020-04-22 11:15:05 -04009cmake_minimum_required(VERSION 3.13.4)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000010
Petr Hosek9bbcefb2017-04-12 02:28:07 +000011# Add path for custom modules
12set(CMAKE_MODULE_PATH
13 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
14 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
Petr Hosek90f32c52020-09-28 17:37:20 -070015 "${CMAKE_CURRENT_SOURCE_DIR}/../cmake"
16 "${CMAKE_CURRENT_SOURCE_DIR}/../cmake/Modules"
Petr Hosek9bbcefb2017-04-12 02:28:07 +000017 ${CMAKE_MODULE_PATH}
18 )
19
Dominik Montada9e379242020-08-24 11:01:05 +020020set(LIBUNWIND_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
21set(LIBUNWIND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
22set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
23 "Specify path to libc++ source.")
24
Petr Hosek7aecb762019-05-30 07:34:39 +000025if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBUNWIND_STANDALONE_BUILD)
Martin Storsjö51ffc5e2021-09-10 22:14:48 +030026 # We may have an incomplete toolchain - do language support tests without
27 # linking.
28 include(EnableLanguageNolink)
29 project(libunwind LANGUAGES NONE)
30 llvm_enable_language_nolink(C CXX ASM)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000031
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000032 set(PACKAGE_NAME libunwind)
Tom Stellardcdb04dc2021-07-27 21:51:07 -070033 set(PACKAGE_VERSION 14.0.0git)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000034 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
Tanya Lattnera7abd9b2015-08-05 04:01:47 +000035 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000036
Dominik Montada9e379242020-08-24 11:01:05 +020037 # Add the CMake module path of libcxx so we can reuse HandleOutOfTreeLLVM.cmake
38 set(LIBUNWIND_LIBCXX_CMAKE_PATH "${LIBUNWIND_LIBCXX_PATH}/cmake/Modules")
39 list(APPEND CMAKE_MODULE_PATH "${LIBUNWIND_LIBCXX_CMAKE_PATH}")
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000040
Dominik Montada9e379242020-08-24 11:01:05 +020041 # In a standalone build, we don't have llvm to automatically generate the
42 # llvm-lit script for us. So we need to provide an explicit directory that
43 # the configurator should write the script into.
44 set(LIBUNWIND_STANDALONE_BUILD 1)
45 set(LLVM_LIT_OUTPUT_DIR "${LIBUNWIND_BINARY_DIR}/bin")
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000046
Dominik Montada9e379242020-08-24 11:01:05 +020047 # Find the LLVM sources and simulate LLVM CMake options.
48 include(HandleOutOfTreeLLVM)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000049else()
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000050 set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
51endif()
52
53#===============================================================================
54# Setup CMake Options
55#===============================================================================
Petr Hosek68b2a0f2018-07-24 23:42:51 +000056include(CMakeDependentOption)
Petr Hosek9bbcefb2017-04-12 02:28:07 +000057include(HandleCompilerRT)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000058
59# Define options.
Louis Dionnea281fd32021-11-23 16:34:09 -050060option(LIBUNWIND_BUILD_32_BITS "Build 32 bit multilib libunwind. This option is not supported anymore when building the runtimes. Please specify a full triple instead." ${LLVM_BUILD_32_BITS})
61if (LIBUNWIND_BUILD_32_BITS)
62 message(WARNING "LIBUNWIND_BUILD_32_BITS is not supported anymore when building the runtimes, please specify a full triple instead.")
63endif()
64
gejin7f493162021-08-26 16:20:38 +080065option(LIBUNWIND_ENABLE_CET "Build libunwind with CET enabled." OFF)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000066option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
67option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
68option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000069option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
Petr Hosek35f87502016-08-08 22:55:48 +000070option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
Asiri Rathnayake8c94db92016-05-27 08:29:27 +000071option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
Asiri Rathnayaked90f6812016-07-07 10:55:39 +000072option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
Asiri Rathnayake6d434c82016-09-28 10:57:15 +000073option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
Sterling Augustine7f90e762019-05-13 18:45:03 +000074option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)
Petr Hosek9bbcefb2017-04-12 02:28:07 +000075option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
Jonathan Roelofsa9bd4562017-03-28 15:21:43 +000076option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
Kristina Bessonovadc676eb2021-02-13 12:49:47 +020077option(LIBUNWIND_INCLUDE_TESTS "Build the libunwind tests." ${LLVM_INCLUDE_TESTS})
Hafiz Abid Qadeer1fd0b1b2020-07-29 11:39:41 +010078option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)
Sterling Augustineb778c912020-08-18 12:05:07 -070079option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)
Daniel Kisscc1fc4e2020-11-11 10:58:41 +010080option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +000081
Petr Hosek285a8f32017-08-08 00:37:59 +000082set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
83 "Define suffix of library directory name (32/64)")
Petr Hosek94bbfa62017-11-17 23:29:46 +000084option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
Petr Hosek106866a2018-07-24 23:27:51 +000085cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
86 "Install the static libunwind library." ON
87 "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
88cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
89 "Install the shared libunwind library." ON
90 "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
Louis Dionne023d2662021-10-12 15:59:08 -040091if(CMAKE_CXX_COMPILER_TARGET)
92 set(LIBUNWIND_DEFAULT_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
93else()
94 set(LIBUNWIND_DEFAULT_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}")
95endif()
96set(LIBUNWIND_TARGET_TRIPLE "${LIBUNWIND_DEFAULT_TARGET_TRIPLE}" CACHE STRING "Target triple for cross compiling.")
97set(LIBUNWIND_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}" CACHE PATH "GCC toolchain for cross compiling.")
98set(LIBUNWIND_SYSROOT "${CMAKE_SYSROOT}" CACHE PATH "Sysroot for cross compiling.")
John Baldwin22b1bfd2018-02-27 18:40:04 +000099set(LIBUNWIND_TEST_LINKER_FLAGS "" CACHE STRING
100 "Additional linker flags for test programs.")
101set(LIBUNWIND_TEST_COMPILER_FLAGS "" CACHE STRING
102 "Additional compiler flags for test programs.")
Louis Dionnebbd954d2021-10-12 14:10:02 -0400103
104if (LIBUNWIND_ENABLE_SHARED)
105 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")
106else()
107 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")
108endif()
109set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING
Louis Dionne15999e72021-09-29 15:26:05 -0400110 "The path to the Lit testing configuration to use when running the tests.
Louis Dionne10f4e4a2021-10-12 12:46:21 -0400111 If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
Louis Dionne15999e72021-09-29 15:26:05 -0400112if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
Louis Dionne10f4e4a2021-10-12 12:46:21 -0400113 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
Louis Dionne15999e72021-09-29 15:26:05 -0400114endif()
Dominik Montada9e379242020-08-24 11:01:05 +0200115set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
116 "A list of parameters to run the Lit test suite with.")
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000117
Petr Hosek35f87502016-08-08 22:55:48 +0000118if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
119 message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
120endif()
121
gejin7f493162021-08-26 16:20:38 +0800122if (LIBUNWIND_ENABLE_CET AND MSVC)
123 message(FATAL_ERROR "libunwind CET support is not available for MSVC!")
124endif()
125
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800126option(LIBUNWIND_HIDE_SYMBOLS
Petr Hosek809c2b52019-01-29 23:01:08 +0000127 "Do not export any symbols from the static library." OFF)
128
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000129#===============================================================================
130# Configure System
131#===============================================================================
132
133# Add path for custom modules
134set(CMAKE_MODULE_PATH
135 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
136 ${CMAKE_MODULE_PATH})
137
Eric Christopher92b75a22018-06-29 20:27:40 +0000138if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
Petr Hosek5844fc62021-04-16 21:33:18 -0700139 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
John Ericsona5feaf62021-04-28 22:36:47 +0000140 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
141 "Path where built libunwind libraries should be installed.")
Martin Storsjö44ea7ab2021-08-11 13:44:06 +0300142 set(LIBUNWIND_INSTALL_RUNTIME_DIR bin CACHE PATH
143 "Path where built libunwind runtime libraries should be installed.")
Petr Hosek1f9c24d2019-05-22 21:08:33 +0000144 if(LIBCXX_LIBDIR_SUBDIR)
145 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
146 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
147 endif()
Eric Christopher92b75a22018-06-29 20:27:40 +0000148elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
Petr Hosekae8b8512017-07-18 21:30:18 +0000149 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
John Ericsona5feaf62021-04-28 22:36:47 +0000150 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
151 "Path where built libunwind libraries should be installed.")
Martin Storsjö44ea7ab2021-08-11 13:44:06 +0300152 set(LIBUNWIND_INSTALL_RUNTIME_DIR bin CACHE PATH
153 "Path where built libunwind runtime libraries should be installed.")
Petr Hosekae8b8512017-07-18 21:30:18 +0000154else()
Petr Hosek285a8f32017-08-08 00:37:59 +0000155 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
John Ericsona5feaf62021-04-28 22:36:47 +0000156 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
157 "Path where built libunwind libraries should be installed.")
Martin Storsjö44ea7ab2021-08-11 13:44:06 +0300158 set(LIBUNWIND_INSTALL_RUNTIME_DIR bin CACHE PATH
159 "Path where built libunwind runtime libraries should be installed.")
Petr Hosekae8b8512017-07-18 21:30:18 +0000160endif()
161
162set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
163set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
164set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000165
Eric Fiselier077b6822016-06-02 01:02:10 +0000166set(LIBUNWIND_C_FLAGS "")
167set(LIBUNWIND_CXX_FLAGS "")
168set(LIBUNWIND_COMPILE_FLAGS "")
169set(LIBUNWIND_LINK_FLAGS "")
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000170
Petr Hosekcfb6df82019-10-11 22:22:29 +0000171# Include macros for adding and removing libunwind flags.
172include(HandleLibunwindFlags)
173
174#===============================================================================
175# Setup Compiler Flags
176#===============================================================================
177
Martin Storsjö51ffc5e2021-09-10 22:14:48 +0300178# Compiler tests may be failing if the compiler implicitly links in libunwind,
179# which doesn't exist yet. This gets waived by --unwindlib=none
180# later in config-ix below, but the tests for --target etc before that may
181# be failing due to it. Only test compilation, not linking, for these
182# tests here now.
183set(CMAKE_TRY_COMPILE_TARGET_TYPE_ORIG ${CMAKE_TRY_COMPILE_TARGET_TYPE})
184set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
185
Petr Hoseka6a888a2019-02-04 20:02:26 +0000186if(LIBUNWIND_TARGET_TRIPLE)
Louis Dionne37be9902021-07-15 13:02:43 -0400187 add_target_flags_if_supported("--target=${LIBUNWIND_TARGET_TRIPLE}")
Petr Hoseka6a888a2019-02-04 20:02:26 +0000188endif()
189if(LIBUNWIND_GCC_TOOLCHAIN)
Louis Dionne37be9902021-07-15 13:02:43 -0400190 add_target_flags_if_supported("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}")
Petr Hoseka6a888a2019-02-04 20:02:26 +0000191endif()
192if(LIBUNWIND_SYSROOT)
Louis Dionne37be9902021-07-15 13:02:43 -0400193 add_target_flags_if_supported("--sysroot=${LIBUNWIND_SYSROOT}")
Petr Hoseka6a888a2019-02-04 20:02:26 +0000194endif()
Martin Storsjö51ffc5e2021-09-10 22:14:48 +0300195set(CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE_ORIG})
Eric Fiselier077b6822016-06-02 01:02:10 +0000196
197# Configure compiler.
198include(config-ix)
199
Charles Davisddde2e12018-09-04 20:57:50 +0000200if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
Petr Hosek9bbcefb2017-04-12 02:28:07 +0000201 list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
202endif()
203
Petr Hosekcfb6df82019-10-11 22:22:29 +0000204add_compile_flags_if_supported(-Werror=return-type)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000205
gejin7f493162021-08-26 16:20:38 +0800206if (LIBUNWIND_ENABLE_CET)
207 add_compile_flags_if_supported(-fcf-protection=full)
208 add_compile_flags_if_supported(-mshstk)
209 if (NOT LIBUNWIND_SUPPORTS_FCF_PROTECTION_EQ_FULL_FLAG)
210 message(SEND_ERROR "Compiler doesn't support CET -fcf-protection option!")
211 endif()
212 if (NOT LIBUNWIND_SUPPORTS_MSHSTK_FLAG)
213 message(SEND_ERROR "Compiler doesn't support CET -mshstk option!")
214 endif()
215endif()
216
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000217# Get warning flags
Petr Hosekcfb6df82019-10-11 22:22:29 +0000218add_compile_flags_if_supported(-W)
219add_compile_flags_if_supported(-Wall)
220add_compile_flags_if_supported(-Wchar-subscripts)
221add_compile_flags_if_supported(-Wconversion)
222add_compile_flags_if_supported(-Wmismatched-tags)
223add_compile_flags_if_supported(-Wmissing-braces)
224add_compile_flags_if_supported(-Wnewline-eof)
225add_compile_flags_if_supported(-Wno-unused-function)
226add_compile_flags_if_supported(-Wshadow)
227add_compile_flags_if_supported(-Wshorten-64-to-32)
228add_compile_flags_if_supported(-Wsign-compare)
229add_compile_flags_if_supported(-Wsign-conversion)
230add_compile_flags_if_supported(-Wstrict-aliasing=2)
231add_compile_flags_if_supported(-Wstrict-overflow=4)
232add_compile_flags_if_supported(-Wunused-parameter)
233add_compile_flags_if_supported(-Wunused-variable)
234add_compile_flags_if_supported(-Wwrite-strings)
235add_compile_flags_if_supported(-Wundef)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000236
kristinaa95e23b2020-07-31 18:44:02 +0100237add_compile_flags_if_supported(-Wno-suggest-override)
238
Martin Storsjö66b29762020-10-23 22:51:21 +0300239if (WIN32)
240 # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
241 # silence the warning instead of cluttering the headers (which aren't
242 # necessarily the ones that the callers will use anyway) with the
243 # attributes.
244 add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
245endif()
246
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000247if (LIBUNWIND_ENABLE_WERROR)
Petr Hosekcfb6df82019-10-11 22:22:29 +0000248 add_compile_flags_if_supported(-Werror)
249 add_compile_flags_if_supported(-WX)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000250else()
Petr Hosekcfb6df82019-10-11 22:22:29 +0000251 add_compile_flags_if_supported(-Wno-error)
252 add_compile_flags_if_supported(-WX-)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000253endif()
254
255if (LIBUNWIND_ENABLE_PEDANTIC)
Petr Hosekcfb6df82019-10-11 22:22:29 +0000256 add_compile_flags_if_supported(-pedantic)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000257endif()
258
259# Get feature flags.
260# Exceptions
261# Catches C++ exceptions only and tells the compiler to assume that extern C
262# functions never throw a C++ exception.
Petr Hosekcfb6df82019-10-11 22:22:29 +0000263add_cxx_compile_flags_if_supported(-fstrict-aliasing)
264add_cxx_compile_flags_if_supported(-EHsc)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000265
Sergej Jaskiewiczbaf07ac2019-12-06 17:26:35 +0300266# Don't run the linker in this CMake check.
267#
268# The reason why this was added is that when building libunwind for
269# ARM Linux, we need to pass the -funwind-tables flag in order for it to
270# work properly with ARM EHABI.
271#
272# However, when performing CMake checks, adding this flag causes the check
273# to produce a false negative, because the compiler generates calls
274# to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
275# which isn't built yet, so the linker complains about undefined symbols.
276#
277# This leads to libunwind not being built with this flag, which makes
278# libunwind quite useless in this setup.
279set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
280set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
Petr Hosek1073ad92019-10-12 01:50:57 +0000281add_compile_flags_if_supported(-funwind-tables)
Sergej Jaskiewiczbaf07ac2019-12-06 17:26:35 +0300282set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
283
284if (LIBUNWIND_USES_ARM_EHABI AND NOT LIBUNWIND_SUPPORTS_FUNWIND_TABLES_FLAG)
285 message(SEND_ERROR "The -funwind-tables flag must be supported "
286 "because this target uses ARM Exception Handling ABI")
287endif()
288
Petr Hosekcfb6df82019-10-11 22:22:29 +0000289add_cxx_compile_flags_if_supported(-fno-exceptions)
290add_cxx_compile_flags_if_supported(-fno-rtti)
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000291
Petr Hosekac0d9e02019-01-29 22:26:18 +0000292# Ensure that we don't depend on C++ standard library.
Petr Hosek1073ad92019-10-12 01:50:57 +0000293if (LIBUNWIND_HAS_NOSTDINCXX_FLAG)
Petr Hosekcfb6df82019-10-11 22:22:29 +0000294 list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
295 # Remove -stdlib flags to prevent them from causing an unused flag warning.
Raphael Isemanneb5b92d2020-10-13 16:05:00 +0200296 string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
297 string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
Petr Hosekcfb6df82019-10-11 22:22:29 +0000298 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
299 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
300endif()
Petr Hosekac0d9e02019-01-29 22:26:18 +0000301
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000302# Assert
303string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
304if (LIBUNWIND_ENABLE_ASSERTIONS)
305 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
306 if (NOT MSVC)
Petr Hosekcfb6df82019-10-11 22:22:29 +0000307 add_compile_flags(-D_DEBUG)
Eugene Zelenko12b5d282016-08-08 17:56:28 +0000308 endif()
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000309
310 # On Release builds cmake automatically defines NDEBUG, so we
311 # explicitly undefine it:
312 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
Petr Hosekcfb6df82019-10-11 22:22:29 +0000313 add_compile_flags(-UNDEBUG)
Eugene Zelenko12b5d282016-08-08 17:56:28 +0000314 endif()
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000315else()
316 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
Petr Hosekcfb6df82019-10-11 22:22:29 +0000317 add_compile_flags(-DNDEBUG)
Eugene Zelenko12b5d282016-08-08 17:56:28 +0000318 endif()
319endif()
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000320
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000321# Cross-unwinding
322if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
Petr Hosekcfb6df82019-10-11 22:22:29 +0000323 add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
Eugene Zelenko12b5d282016-08-08 17:56:28 +0000324endif()
Asiri Rathnayakec00dcef2016-05-25 12:36:34 +0000325
Asiri Rathnayake6d434c82016-09-28 10:57:15 +0000326# Threading-support
327if (NOT LIBUNWIND_ENABLE_THREADS)
Petr Hosekcfb6df82019-10-11 22:22:29 +0000328 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
Asiri Rathnayake6d434c82016-09-28 10:57:15 +0000329endif()
330
Asiri Rathnayaked90f6812016-07-07 10:55:39 +0000331# ARM WMMX register support
332if (LIBUNWIND_ENABLE_ARM_WMMX)
333 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
334 # define this macro for any supported target at present. Therefore, here we
335 # provide the option to explicitly enable support for WMMX registers in the
336 # unwinder.
Petr Hosekcfb6df82019-10-11 22:22:29 +0000337 add_compile_flags(-D__ARM_WMMX)
Eugene Zelenko12b5d282016-08-08 17:56:28 +0000338endif()
Asiri Rathnayaked90f6812016-07-07 10:55:39 +0000339
Hafiz Abid Qadeer1fd0b1b2020-07-29 11:39:41 +0100340if(LIBUNWIND_IS_BAREMETAL)
341 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
342endif()
343
Sterling Augustineb778c912020-08-18 12:05:07 -0700344if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
345 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
346endif()
347
Daniel Kisscc1fc4e2020-11-11 10:58:41 +0100348if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
349 add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
350endif()
351
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000352# This is the _ONLY_ place where add_definitions is called.
353if (MSVC)
354 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
Eugene Zelenko12b5d282016-08-08 17:56:28 +0000355endif()
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000356
Martin Storsjo74e3b4d2017-11-29 08:21:12 +0000357# Disable DLL annotations on Windows for static builds.
358if (WIN32 AND LIBUNWIND_ENABLE_STATIC AND NOT LIBUNWIND_ENABLE_SHARED)
Ryan Prichard8d5fb6f2021-02-22 16:35:38 -0800359 add_definitions(-D_LIBUNWIND_HIDE_SYMBOLS)
Martin Storsjo74e3b4d2017-11-29 08:21:12 +0000360endif()
361
Petr Hosek0e595162019-05-30 04:40:21 +0000362if (LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
Michał Górnyf939ab72019-11-30 15:13:56 +0100363 if (LIBUNWIND_HAS_DL_LIB)
364 add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
365 endif()
366 if (LIBUNWIND_HAS_PTHREAD_LIB)
367 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
368 endif()
Petr Hosek0e595162019-05-30 04:40:21 +0000369endif()
370
Saleem Abdulrasoolf058d5e2015-04-25 01:46:35 +0000371#===============================================================================
372# Setup Source Code
373#===============================================================================
374
375include_directories(include)
376
377add_subdirectory(src)
Jonathan Roelofsa9bd4562017-03-28 15:21:43 +0000378
379if (LIBUNWIND_INCLUDE_DOCS)
380 add_subdirectory(docs)
381endif()
Jonathan Roelofs0fedff12017-07-06 15:20:12 +0000382
Alfonso Gregorya7e4ce02021-09-16 18:27:53 +0200383if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})
Martin Storsjo41118c42018-08-03 05:51:31 +0000384 add_subdirectory(test)
385endif()