1 #===============================================================================
3 #===============================================================================
5 cmake_minimum_required(VERSION 3.20.0)
7 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
9 # Add path for custom modules
10 list(INSERT CMAKE_MODULE_PATH 0
11 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
12 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
13 "${CMAKE_CURRENT_SOURCE_DIR}/../runtimes/cmake/Modules"
14 "${LLVM_COMMON_CMAKE_UTILS}"
15 "${LLVM_COMMON_CMAKE_UTILS}/Modules"
18 set(LIBUNWIND_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
19 set(LIBUNWIND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
20 set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
21 "Specify path to libc++ source.")
23 include(GNUInstallDirs)
25 #===============================================================================
27 #===============================================================================
28 include(CMakeDependentOption)
29 include(HandleCompilerRT)
32 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})
33 if (LIBUNWIND_BUILD_32_BITS)
34 message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS is not supported anymore when building the runtimes, please specify a full triple instead.")
37 option(LIBUNWIND_ENABLE_CET "Build libunwind with CET enabled." OFF)
38 option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
39 option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
40 option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
41 option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
42 option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
43 option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
44 option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
45 option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
46 option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)
47 option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
48 option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
49 option(LIBUNWIND_INCLUDE_TESTS "Build the libunwind tests." ${LLVM_INCLUDE_TESTS})
50 option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)
51 option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)
52 option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)
53 option(LIBUNWIND_INSTALL_HEADERS "Install the libunwind headers." ON)
54 option(LIBUNWIND_ENABLE_FRAME_APIS "Include libgcc-compatible frame apis." OFF)
56 set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
57 "Define suffix of library directory name (32/64)")
58 option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
59 cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
60 "Install the static libunwind library." ON
61 "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
62 cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
63 "Install the shared libunwind library." ON
64 "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
66 # TODO: Remove this after branching for LLVM 15
67 if(LIBUNWIND_SYSROOT OR LIBUNWIND_TARGET_TRIPLE OR LIBUNWIND_GCC_TOOLCHAIN)
68 message(WARNING "LIBUNWIND_SYSROOT, LIBUNWIND_TARGET_TRIPLE and LIBUNWIND_GCC_TOOLCHAIN are not supported anymore, please use the native CMake equivalents instead")
72 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-mingw.cfg.in")
73 elseif (LIBUNWIND_ENABLE_SHARED)
74 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")
76 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")
78 set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING
79 "The path to the Lit testing configuration to use when running the tests.
80 If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
81 if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
82 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
84 message(STATUS "Using libunwind testing configuration: ${LIBUNWIND_TEST_CONFIG}")
85 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
86 "A list of parameters to run the Lit test suite with.")
88 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
89 message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
92 if (LIBUNWIND_ENABLE_CET AND MSVC)
93 message(FATAL_ERROR "libunwind CET support is not available for MSVC!")
97 set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS TRUE)
99 set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS FALSE)
101 option(LIBUNWIND_HIDE_SYMBOLS
102 "Do not export any symbols from the static library." ${LIBUNWIND_DEFAULT_HIDE_SYMBOLS})
104 #===============================================================================
106 #===============================================================================
108 # Add path for custom modules
109 set(CMAKE_MODULE_PATH
110 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
111 ${CMAKE_MODULE_PATH})
113 set(LIBUNWIND_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE PATH
114 "Path where built libunwind headers should be installed.")
115 set(LIBUNWIND_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
116 "Path where built libunwind runtime libraries should be installed.")
118 set(LIBUNWIND_SHARED_OUTPUT_NAME "unwind" CACHE STRING "Output name for the shared libunwind runtime library.")
119 set(LIBUNWIND_STATIC_OUTPUT_NAME "unwind" CACHE STRING "Output name for the static libunwind runtime library.")
121 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
122 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
123 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
124 "Path where built libunwind libraries should be installed.")
125 if(LIBCXX_LIBDIR_SUBDIR)
126 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
127 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
130 if(LLVM_LIBRARY_OUTPUT_INTDIR)
131 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
133 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
135 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
136 "Path where built libunwind libraries should be installed.")
139 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
140 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
141 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
143 set(LIBUNWIND_C_FLAGS "")
144 set(LIBUNWIND_CXX_FLAGS "")
145 set(LIBUNWIND_COMPILE_FLAGS "")
146 set(LIBUNWIND_LINK_FLAGS "")
147 set(LIBUNWIND_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
148 "Additional Compile only flags which can be provided in cache")
149 set(LIBUNWIND_ADDITIONAL_LIBRARIES "" CACHE STRING
150 "Additional libraries libunwind is linked to which can be provided in cache")
152 # Include macros for adding and removing libunwind flags.
153 include(HandleLibunwindFlags)
155 #===============================================================================
156 # Setup Compiler Flags
157 #===============================================================================
159 # Configure compiler.
162 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
163 list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
166 add_compile_flags_if_supported(-Werror=return-type)
168 if (LIBUNWIND_ENABLE_CET)
169 add_compile_flags_if_supported(-fcf-protection=full)
170 add_compile_flags_if_supported(-mshstk)
171 if (NOT CXX_SUPPORTS_FCF_PROTECTION_EQ_FULL_FLAG)
172 message(SEND_ERROR "Compiler doesn't support CET -fcf-protection option!")
174 if (NOT CXX_SUPPORTS_MSHSTK_FLAG)
175 message(SEND_ERROR "Compiler doesn't support CET -mshstk option!")
180 # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
181 # silence the warning instead of cluttering the headers (which aren't
182 # necessarily the ones that the callers will use anyway) with the
184 add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
189 # Catches C++ exceptions only and tells the compiler to assume that extern C
190 # functions never throw a C++ exception.
191 add_cxx_compile_flags_if_supported(-fstrict-aliasing)
192 add_cxx_compile_flags_if_supported(-EHsc)
194 # Don't run the linker in this CMake check.
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.
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.
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})
212 if (LIBUNWIND_USES_ARM_EHABI AND NOT CXX_SUPPORTS_FUNWIND_TABLES_FLAG)
213 message(SEND_ERROR "The -funwind-tables flag must be supported "
214 "because this target uses ARM Exception Handling ABI")
217 add_cxx_compile_flags_if_supported(-fno-exceptions)
218 add_cxx_compile_flags_if_supported(-fno-rtti)
220 # Ensure that we don't depend on C++ standard library.
221 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
222 list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
223 # Remove -stdlib flags to prevent them from causing an unused flag warning.
224 string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
225 string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
226 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
227 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
231 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
232 if (LIBUNWIND_ENABLE_ASSERTIONS)
233 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
235 add_compile_flags(-D_DEBUG)
238 # On Release builds cmake automatically defines NDEBUG, so we
239 # explicitly undefine it:
240 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
241 add_compile_flags(-UNDEBUG)
244 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
245 add_compile_flags(-DNDEBUG)
250 if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
251 add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
254 # Include stubs for __register_frame_info_bases and related
255 if (LIBUNWIND_ENABLE_FRAME_APIS)
256 add_compile_flags(-D_LIBUNWIND_SUPPORT_FRAME_APIS)
260 if (NOT LIBUNWIND_ENABLE_THREADS)
261 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
264 # ARM WMMX register support
265 if (LIBUNWIND_ENABLE_ARM_WMMX)
266 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
267 # define this macro for any supported target at present. Therefore, here we
268 # provide the option to explicitly enable support for WMMX registers in the
270 add_compile_flags(-D__ARM_WMMX)
273 if(LIBUNWIND_IS_BAREMETAL)
274 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
277 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
278 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
281 if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
282 add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
285 # This is the _ONLY_ place where add_definitions is called.
287 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
290 if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
291 if (LIBUNWIND_HAS_DL_LIB)
292 add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
294 if (LIBUNWIND_HAS_PTHREAD_LIB)
295 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
299 #===============================================================================
301 #===============================================================================
303 add_subdirectory(include)
305 add_subdirectory(src)
307 if (LIBUNWIND_INCLUDE_DOCS)
308 add_subdirectory(docs)
311 if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})
312 add_subdirectory(test)