1 #===============================================================================
3 #===============================================================================
5 cmake_minimum_required(VERSION 3.13.4)
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)
55 set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
56 "Define suffix of library directory name (32/64)")
57 option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
58 cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
59 "Install the static libunwind library." ON
60 "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
61 cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
62 "Install the shared libunwind library." ON
63 "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
65 # TODO: Remove this after branching for LLVM 15
66 if(LIBUNWIND_SYSROOT OR LIBUNWIND_TARGET_TRIPLE OR LIBUNWIND_GCC_TOOLCHAIN)
67 message(WARNING "LIBUNWIND_SYSROOT, LIBUNWIND_TARGET_TRIPLE and LIBUNWIND_GCC_TOOLCHAIN are not supported anymore, please use the native CMake equivalents instead")
71 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-mingw.cfg.in")
72 elseif (LIBUNWIND_ENABLE_SHARED)
73 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")
75 set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")
77 set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING
78 "The path to the Lit testing configuration to use when running the tests.
79 If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
80 if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
81 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
83 message(STATUS "Using libunwind testing configuration: ${LIBUNWIND_TEST_CONFIG}")
84 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
85 "A list of parameters to run the Lit test suite with.")
87 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
88 message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
91 if (LIBUNWIND_ENABLE_CET AND MSVC)
92 message(FATAL_ERROR "libunwind CET support is not available for MSVC!")
96 set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS TRUE)
98 set(LIBUNWIND_DEFAULT_HIDE_SYMBOLS FALSE)
100 option(LIBUNWIND_HIDE_SYMBOLS
101 "Do not export any symbols from the static library." ${LIBUNWIND_DEFAULT_HIDE_SYMBOLS})
103 #===============================================================================
105 #===============================================================================
107 # Add path for custom modules
108 set(CMAKE_MODULE_PATH
109 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
110 ${CMAKE_MODULE_PATH})
112 set(LIBUNWIND_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE PATH
113 "Path where built libunwind headers should be installed.")
114 set(LIBUNWIND_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
115 "Path where built libunwind runtime libraries should be installed.")
117 set(LIBUNWIND_SHARED_OUTPUT_NAME "unwind" CACHE STRING "Output name for the shared libunwind runtime library.")
118 set(LIBUNWIND_STATIC_OUTPUT_NAME "unwind" CACHE STRING "Output name for the static libunwind runtime library.")
120 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
121 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
122 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
123 "Path where built libunwind libraries should be installed.")
124 if(LIBCXX_LIBDIR_SUBDIR)
125 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
126 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
129 if(LLVM_LIBRARY_OUTPUT_INTDIR)
130 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
132 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
134 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
135 "Path where built libunwind libraries should be installed.")
138 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
139 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
140 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
142 set(LIBUNWIND_C_FLAGS "")
143 set(LIBUNWIND_CXX_FLAGS "")
144 set(LIBUNWIND_COMPILE_FLAGS "")
145 set(LIBUNWIND_LINK_FLAGS "")
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)
250 if (NOT LIBUNWIND_ENABLE_THREADS)
251 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
254 # ARM WMMX register support
255 if (LIBUNWIND_ENABLE_ARM_WMMX)
256 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
257 # define this macro for any supported target at present. Therefore, here we
258 # provide the option to explicitly enable support for WMMX registers in the
260 add_compile_flags(-D__ARM_WMMX)
263 if(LIBUNWIND_IS_BAREMETAL)
264 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
267 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
268 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
271 if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
272 add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
275 # This is the _ONLY_ place where add_definitions is called.
277 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
280 if (C_SUPPORTS_COMMENT_LIB_PRAGMA)
281 if (LIBUNWIND_HAS_DL_LIB)
282 add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
284 if (LIBUNWIND_HAS_PTHREAD_LIB)
285 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
289 #===============================================================================
291 #===============================================================================
293 add_subdirectory(include)
295 add_subdirectory(src)
297 if (LIBUNWIND_INCLUDE_DOCS)
298 add_subdirectory(docs)
301 if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})
302 add_subdirectory(test)