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)
67 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-mingw.cfg.in")
68 elseif (LIBUNWIND_ENABLE_SHARED)
69 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")
71 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")
73 set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING
74 "The path to the Lit testing configuration to use when running the tests.
75 If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
76 if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
77 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
79 message(STATUS "Using libunwind testing configuration: ${LIBUNWIND_TEST_CONFIG}")
80 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
81 "A list of parameters to run the Lit test suite with.")
83 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
84 message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
87 if (LIBUNWIND_ENABLE_CET AND MSVC)
88 message(FATAL_ERROR "libunwind CET support is not available for MSVC!")
92 set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS TRUE)
94 set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS FALSE)
96 option(LIBUNWIND_HIDE_SYMBOLS
97 "Do not export any symbols from the static library." ${LIBUNWIND_DEFAULT_HIDE_SYMBOLS})
99 #===============================================================================
101 #===============================================================================
103 # Add path for custom modules
104 set(CMAKE_MODULE_PATH
105 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
106 ${CMAKE_MODULE_PATH})
108 set(LIBUNWIND_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE STRING
109 "Path where built libunwind headers should be installed.")
110 set(LIBUNWIND_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE STRING
111 "Path where built libunwind runtime libraries should be installed.")
113 set(LIBUNWIND_SHARED_OUTPUT_NAME "unwind" CACHE STRING "Output name for the shared libunwind runtime library.")
114 set(LIBUNWIND_STATIC_OUTPUT_NAME "unwind" CACHE STRING "Output name for the static libunwind runtime library.")
116 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
117 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
118 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE STRING
119 "Path where built libunwind libraries should be installed.")
120 if(LIBCXX_LIBDIR_SUBDIR)
121 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
122 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
125 if(LLVM_LIBRARY_OUTPUT_INTDIR)
126 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
128 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
130 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE STRING
131 "Path where built libunwind libraries should be installed.")
134 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
135 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
136 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
138 set(LIBUNWIND_C_FLAGS "")
139 set(LIBUNWIND_CXX_FLAGS "")
140 set(LIBUNWIND_COMPILE_FLAGS "")
141 set(LIBUNWIND_LINK_FLAGS "")
142 set(LIBUNWIND_ADDITIONAL_COMPILE_FLAGS "" CACHE STRING
143 "Additional Compile only flags which can be provided in cache")
144 set(LIBUNWIND_ADDITIONAL_LIBRARIES "" CACHE STRING
145 "Additional libraries libunwind is linked to which can be provided in cache")
147 # Include macros for adding and removing libunwind flags.
148 include(HandleLibunwindFlags)
150 #===============================================================================
151 # Setup Compiler Flags
152 #===============================================================================
154 # Configure compiler.
157 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
158 list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
161 add_compile_flags_if_supported(-Werror=return-type)
163 if (LIBUNWIND_ENABLE_CET)
164 add_compile_flags_if_supported(-fcf-protection=full)
165 add_compile_flags_if_supported(-mshstk)
166 if (NOT CXX_SUPPORTS_FCF_PROTECTION_EQ_FULL_FLAG)
167 message(SEND_ERROR "Compiler doesn't support CET -fcf-protection option!")
169 if (NOT CXX_SUPPORTS_MSHSTK_FLAG)
170 message(SEND_ERROR "Compiler doesn't support CET -mshstk option!")
175 # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
176 # silence the warning instead of cluttering the headers (which aren't
177 # necessarily the ones that the callers will use anyway) with the
179 add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
184 # Catches C++ exceptions only and tells the compiler to assume that extern C
185 # functions never throw a C++ exception.
186 add_cxx_compile_flags_if_supported(-fstrict-aliasing)
187 add_cxx_compile_flags_if_supported(-EHsc)
189 # Don't run the linker in this CMake check.
191 # The reason why this was added is that when building libunwind for
192 # ARM Linux, we need to pass the -funwind-tables flag in order for it to
193 # work properly with ARM EHABI.
195 # However, when performing CMake checks, adding this flag causes the check
196 # to produce a false negative, because the compiler generates calls
197 # to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
198 # which isn't built yet, so the linker complains about undefined symbols.
200 # This leads to libunwind not being built with this flag, which makes
201 # libunwind quite useless in this setup.
202 set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
203 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
204 add_compile_flags_if_supported(-funwind-tables)
205 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
207 if (LIBUNWIND_USES_ARM_EHABI AND NOT CXX_SUPPORTS_FUNWIND_TABLES_FLAG)
208 message(SEND_ERROR "The -funwind-tables flag must be supported "
209 "because this target uses ARM Exception Handling ABI")
212 add_cxx_compile_flags_if_supported(-fno-exceptions)
213 add_cxx_compile_flags_if_supported(-fno-rtti)
215 # Ensure that we don't depend on C++ standard library.
216 if (CXX_SUPPORTS_NOSTDINCXX_FLAG)
217 list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
218 # Remove -stdlib flags to prevent them from causing an unused flag warning.
219 string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
220 string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
221 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
222 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
226 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
227 if (LIBUNWIND_ENABLE_ASSERTIONS)
228 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
230 add_compile_flags(-D_DEBUG)
233 # On Release builds cmake automatically defines NDEBUG, so we
234 # explicitly undefine it:
235 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
236 add_compile_flags(-UNDEBUG)
239 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG")
240 add_compile_flags(-DNDEBUG)
245 if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
246 add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
249 # Include stubs for __register_frame_info_bases and related
250 if (LIBUNWIND_ENABLE_FRAME_APIS)
251 add_compile_flags(-D_LIBUNWIND_SUPPORT_FRAME_APIS)
255 if (NOT LIBUNWIND_ENABLE_THREADS)
256 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
259 # ARM WMMX register support
260 if (LIBUNWIND_ENABLE_ARM_WMMX)
261 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
262 # define this macro for any supported target at present. Therefore, here we
263 # provide the option to explicitly enable support for WMMX registers in the
265 add_compile_flags(-D__ARM_WMMX)
268 if(LIBUNWIND_IS_BAREMETAL)
269 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
272 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
273 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
276 if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
277 add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
280 # This is the _ONLY_ place where add_definitions is called.
282 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
285 if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
286 if (LIBUNWIND_HAS_DL_LIB)
287 add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
289 if (LIBUNWIND_HAS_PTHREAD_LIB)
290 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
294 #===============================================================================
296 #===============================================================================
298 add_subdirectory(include)
300 add_subdirectory(src)
302 if (LIBUNWIND_INCLUDE_DOCS)
303 add_subdirectory(docs)
306 if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})
307 add_subdirectory(test)