[bolt][tests] Skip tests that use perf when perf counters are unavailable (#107892)
[llvm-project.git] / libunwind / CMakeLists.txt
blob28d67b0fef92cc1fdcd61e7051da2df438529b60
1 #===============================================================================
2 # Setup Project
3 #===============================================================================
5 cmake_minimum_required(VERSION 3.20.0)
6 set(LLVM_SUBPROJECT_TITLE "libunwind")
8 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
10 # Add path for custom modules
11 list(INSERT CMAKE_MODULE_PATH 0
12   "${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   )
19 set(LIBUNWIND_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
20 set(LIBUNWIND_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
21 set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
22         "Specify path to libc++ source.")
24 include(GNUInstallDirs)
25 include(CheckSymbolExists)
27 #===============================================================================
28 # Setup CMake Options
29 #===============================================================================
30 include(CMakeDependentOption)
31 include(HandleCompilerRT)
33 # Define options.
34 option(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})
35 if (LIBUNWIND_BUILD_32_BITS)
36   message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS is not supported anymore when building the runtimes, please specify a full triple instead.")
37 endif()
39 option(LIBUNWIND_ENABLE_CET "Build libunwind with CET enabled." OFF)
40 option(LIBUNWIND_ENABLE_GCS "Build libunwind with GCS enabled." OFF)
41 option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
42 option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
43 option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
44 option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
45 option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
46 option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
47 option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
48 option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
49 option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)
50 option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
51 option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
52 option(LIBUNWIND_INCLUDE_TESTS "Build the libunwind tests." ${LLVM_INCLUDE_TESTS})
53 option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)
54 option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)
55 option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)
56 option(LIBUNWIND_INSTALL_HEADERS "Install the libunwind headers." ON)
57 option(LIBUNWIND_ENABLE_FRAME_APIS "Include libgcc-compatible frame apis." 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)
69 if(MINGW)
70   set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-mingw.cfg.in")
71 elseif (LIBUNWIND_ENABLE_SHARED)
72   set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")
73 else()
74   set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")
75 endif()
76 set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING
77   "The path to the Lit testing configuration to use when running the tests.
78    If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
79 if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
80   set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
81 endif()
82 message(STATUS "Using libunwind testing configuration: ${LIBUNWIND_TEST_CONFIG}")
83 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
84     "A list of parameters to run the Lit test suite with.")
86 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
87   message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
88 endif()
90 if (LIBUNWIND_ENABLE_CET AND MSVC)
91   message(FATAL_ERROR "libunwind CET support is not available for MSVC!")
92 endif()
94 if (WIN32)
95   set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS TRUE)
96 else()
97   set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS FALSE)
98 endif()
99 option(LIBUNWIND_HIDE_SYMBOLS
100   "Do not export any symbols from the static library." ${LIBUNWIND_DEFAULT_HIDE_SYMBOLS})
102 # If toolchain is FPXX, we switch to FP64 to save the full FPRs. See:
103 # https://web.archive.org/web/20180828210612/https://dmz-portal.mips.com/wiki/MIPS_O32_ABI_-_FR0_and_FR1_Interlinking
104 check_symbol_exists(__mips_hard_float "" __MIPSHF)
105 check_symbol_exists(_ABIO32 "" __MIPS_O32)
106 if (__MIPSHF AND __MIPS_O32)
107   file(WRITE ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/mips_is_fpxx.c
108     "#if __mips_fpr != 0\n"
109     "# error\n"
110     "#endif\n")
111   try_compile(MIPS_FPABI_FPXX ${CMAKE_BINARY_DIR}
112     ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/mips_is_fpxx.c
113     CMAKE_FLAGS -DCMAKE_C_LINK_EXECUTABLE='echo')
114 endif()
116 #===============================================================================
117 # Configure System
118 #===============================================================================
120 # Add path for custom modules
121 set(CMAKE_MODULE_PATH
122     "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
123     ${CMAKE_MODULE_PATH})
125 set(LIBUNWIND_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE STRING
126     "Path where built libunwind headers should be installed.")
127 set(LIBUNWIND_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING
128     "Path where built libunwind runtime libraries should be installed.")
130 set(LIBUNWIND_SHARED_OUTPUT_NAME "unwind" CACHE STRING "Output name for the shared libunwind runtime library.")
131 set(LIBUNWIND_STATIC_OUTPUT_NAME "unwind" CACHE STRING "Output name for the static libunwind runtime library.")
133 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
134   set(LIBUNWIND_TARGET_SUBDIR ${LLVM_DEFAULT_TARGET_TRIPLE})
135   if(LIBUNWIND_LIBDIR_SUBDIR)
136     string(APPEND LIBUNWIND_TARGET_SUBDIR /${LIBUNWIND_LIBDIR_SUBDIR})
137   endif()
138   set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LIBUNWIND_TARGET_SUBDIR})
139   set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LIBUNWIND_TARGET_SUBDIR} CACHE STRING
140       "Path where built libunwind libraries should be installed.")
141   unset(LIBUNWIND_TARGET_SUBDIR)
142 else()
143   if(LLVM_LIBRARY_OUTPUT_INTDIR)
144     set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
145   else()
146     set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
147   endif()
148   set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE STRING
149       "Path where built libunwind libraries should be installed.")
150 endif()
152 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
153 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
154 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
156 set(LIBUNWIND_C_FLAGS "")
157 set(LIBUNWIND_CXX_FLAGS "")
158 set(LIBUNWIND_COMPILE_FLAGS "")
159 set(LIBUNWIND_LINK_FLAGS "")
160 set(LIBUNWIND_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
161     "Additional Compile only flags which can be provided in cache")
162 set(LIBUNWIND_ADDITIONAL_LIBRARIES "" CACHE STRING
163     "Additional libraries libunwind is linked to which can be provided in cache")
165 # Include macros for adding and removing libunwind flags.
166 include(HandleLibunwindFlags)
168 #===============================================================================
169 # Setup Compiler Flags
170 #===============================================================================
172 # Configure compiler.
173 include(config-ix)
175 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
176   list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
177 endif()
179 add_compile_flags_if_supported(-Werror=return-type)
181 if (LIBUNWIND_ENABLE_CET)
182   add_compile_flags_if_supported(-fcf-protection=full)
183   add_compile_flags_if_supported(-mshstk)
184   if (NOT CXX_SUPPORTS_FCF_PROTECTION_EQ_FULL_FLAG)
185     message(SEND_ERROR "Compiler doesn't support CET -fcf-protection option!")
186   endif()
187   if (NOT CXX_SUPPORTS_MSHSTK_FLAG)
188     message(SEND_ERROR "Compiler doesn't support CET -mshstk option!")
189   endif()
190 endif()
192 if (LIBUNWIND_ENABLE_GCS)
193   add_compile_flags_if_supported(-mbranch-protection=standard)
194   if (NOT CXX_SUPPORTS_MBRANCH_PROTECTION_EQ_STANDARD_FLAG)
195     message(SEND_ERROR "Compiler doesn't support GCS -mbranch-protection option!")
196   endif()
197 endif()
199 if (WIN32)
200   # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
201   # silence the warning instead of cluttering the headers (which aren't
202   # necessarily the ones that the callers will use anyway) with the
203   # attributes.
204   add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
205 endif()
207 if (MIPS_FPABI_FPXX)
208   add_compile_flags(-mfp64)
209 endif()
211 # Get feature flags.
212 # Exceptions
213 # Catches C++ exceptions only and tells the compiler to assume that extern C
214 # functions never throw a C++ exception.
215 add_cxx_compile_flags_if_supported(-fstrict-aliasing)
216 add_cxx_compile_flags_if_supported(-EHsc)
218 # Don't run the linker in this CMake check.
220 # The reason why this was added is that when building libunwind for
221 # ARM Linux, we need to pass the -funwind-tables flag in order for it to
222 # work properly with ARM EHABI.
224 # However, when performing CMake checks, adding this flag causes the check
225 # to produce a false negative, because the compiler generates calls
226 # to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
227 # which isn't built yet, so the linker complains about undefined symbols.
229 # This leads to libunwind not being built with this flag, which makes
230 # libunwind quite useless in this setup.
231 set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
232 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
233 add_compile_flags_if_supported(-funwind-tables)
234 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
236 if (LIBUNWIND_USES_ARM_EHABI AND NOT CXX_SUPPORTS_FUNWIND_TABLES_FLAG)
237   message(SEND_ERROR "The -funwind-tables flag must be supported "
238                      "because this target uses ARM Exception Handling ABI")
239 endif()
241 add_cxx_compile_flags_if_supported(-fno-exceptions)
242 add_cxx_compile_flags_if_supported(-fno-rtti)
244 # Ensure that we don't depend on C++ standard library.
245 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
246   list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
247   # Remove -stdlib flags to prevent them from causing an unused flag warning.
248   string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
249   string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
250   string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
251   string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
252 endif()
254 # Assert
255 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
256 if (LIBUNWIND_ENABLE_ASSERTIONS)
257   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
258   if (NOT MSVC)
259     add_compile_flags(-D_DEBUG)
260   endif()
262   # On Release builds cmake automatically defines NDEBUG, so we
263   # explicitly undefine it:
264   if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
265     add_compile_flags(-UNDEBUG)
266   endif()
267 else()
268   if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
269     add_compile_flags(-DNDEBUG)
270   endif()
271 endif()
273 # Cross-unwinding
274 if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
275   add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
276 endif()
278 # Include stubs for __register_frame_info_bases and related
279 if (LIBUNWIND_ENABLE_FRAME_APIS)
280   add_compile_flags(-D_LIBUNWIND_SUPPORT_FRAME_APIS)
281 endif()
283 # Threading-support
284 if (NOT LIBUNWIND_ENABLE_THREADS)
285   add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
286 endif()
288 # ARM WMMX register support
289 if (LIBUNWIND_ENABLE_ARM_WMMX)
290   # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
291   # define this macro for any supported target at present. Therefore, here we
292   # provide the option to explicitly enable support for WMMX registers in the
293   # unwinder.
294   add_compile_flags(-D__ARM_WMMX)
295 endif()
297 if(LIBUNWIND_IS_BAREMETAL)
298   add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
299 endif()
301 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
302   add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
303 endif()
305 if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
306   add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
307 endif()
309 # This is the _ONLY_ place where add_definitions is called.
310 if (MSVC)
311   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
312 endif()
314 if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
315   if (LIBUNWIND_HAS_DL_LIB)
316     add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
317   endif()
318   if (LIBUNWIND_HAS_PTHREAD_LIB)
319     add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
320   endif()
321 endif()
323 #===============================================================================
324 # Setup Source Code
325 #===============================================================================
327 add_subdirectory(include)
329 add_subdirectory(src)
331 if (LIBUNWIND_INCLUDE_DOCS)
332   add_subdirectory(docs)
333 endif()
335 if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})
336   add_subdirectory(test)
337 endif()