1 if (NOT IS_DIRECTORY "${CMAKE_CURRENT_LIST_DIR}/../libcxx")
2 message(FATAL_ERROR "libunwind requires being built in a monorepo layout with libcxx available")
5 #===============================================================================
7 #===============================================================================
9 cmake_minimum_required(VERSION 3.13.4)
12 cmake_policy(SET CMP0042 NEW) # Set MACOSX_RPATH=YES by default
15 # Add path for custom modules
17 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
18 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
22 set(LIBUNWIND_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
23 set(LIBUNWIND_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
24 set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
25 "Specify path to libc++ source.")
27 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBUNWIND_STANDALONE_BUILD)
28 project(libunwind LANGUAGES C CXX ASM)
30 set(PACKAGE_NAME libunwind)
31 set(PACKAGE_VERSION 12.0.0git)
32 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
33 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
35 # Add the CMake module path of libcxx so we can reuse HandleOutOfTreeLLVM.cmake
36 set(LIBUNWIND_LIBCXX_CMAKE_PATH "${LIBUNWIND_LIBCXX_PATH}/cmake/Modules")
37 list(APPEND CMAKE_MODULE_PATH "${LIBUNWIND_LIBCXX_CMAKE_PATH}")
39 # In a standalone build, we don't have llvm to automatically generate the
40 # llvm-lit script for us. So we need to provide an explicit directory that
41 # the configurator should write the script into.
42 set(LIBUNWIND_STANDALONE_BUILD 1)
43 set(LLVM_LIT_OUTPUT_DIR "${LIBUNWIND_BINARY_DIR}/bin")
45 # Find the LLVM sources and simulate LLVM CMake options.
46 include(HandleOutOfTreeLLVM)
48 set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
51 #===============================================================================
53 #===============================================================================
54 include(CMakeDependentOption)
55 include(HandleCompilerRT)
58 option(LIBUNWIND_BUILD_32_BITS "Build 32 bit libunwind" ${LLVM_BUILD_32_BITS})
59 option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
60 option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
61 option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
62 option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
63 option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
64 option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
65 option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
66 option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
67 option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)
68 option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
69 option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
70 option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)
71 option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)
73 set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
74 "Define suffix of library directory name (32/64)")
75 option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
76 cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
77 "Install the static libunwind library." ON
78 "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
79 cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
80 "Install the shared libunwind library." ON
81 "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
82 set(LIBUNWIND_TARGET_TRIPLE "" CACHE STRING "Target triple for cross compiling.")
83 set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
84 set(LIBUNWIND_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
85 set(LIBUNWIND_TEST_LINKER_FLAGS "" CACHE STRING
86 "Additional linker flags for test programs.")
87 set(LIBUNWIND_TEST_COMPILER_FLAGS "" CACHE STRING
88 "Additional compiler flags for test programs.")
89 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/lit.site.cfg.in" CACHE STRING
90 "The Lit testing configuration to use when running the tests.")
91 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
92 "A list of parameters to run the Lit test suite with.")
94 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
95 message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
98 # Check that we can build with 32 bits if requested.
99 if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
100 if (LIBUNWIND_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
101 message(STATUS "Building 32 bits executables and libraries.")
103 elseif(LIBUNWIND_BUILD_32_BITS)
104 message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS=ON is not supported on this platform.")
107 option(LIBUNWIND_HERMETIC_STATIC_LIBRARY
108 "Do not export any symbols from the static library." OFF)
110 #===============================================================================
112 #===============================================================================
114 # Add path for custom modules
115 set(CMAKE_MODULE_PATH
116 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
117 ${CMAKE_MODULE_PATH})
119 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
120 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++)
121 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE}/c++)
122 if(LIBCXX_LIBDIR_SUBDIR)
123 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
124 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
126 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
127 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
128 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX})
130 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
131 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX})
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_INSTALL_PREFIX "" CACHE STRING "Define libunwind destination prefix.")
140 set(LIBUNWIND_C_FLAGS "")
141 set(LIBUNWIND_CXX_FLAGS "")
142 set(LIBUNWIND_COMPILE_FLAGS "")
143 set(LIBUNWIND_LINK_FLAGS "")
145 # Include macros for adding and removing libunwind flags.
146 include(HandleLibunwindFlags)
148 #===============================================================================
149 # Setup Compiler Flags
150 #===============================================================================
152 # Get required flags.
153 add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32")
155 if(LIBUNWIND_TARGET_TRIPLE)
156 add_target_flags("--target=${LIBUNWIND_TARGET_TRIPLE}")
157 elseif(CMAKE_CXX_COMPILER_TARGET)
158 set(LIBUNWIND_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
160 if(LIBUNWIND_GCC_TOOLCHAIN)
161 add_target_flags("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}")
162 elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
163 set(LIBUNWIND_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
165 if(LIBUNWIND_SYSROOT)
166 add_target_flags("--sysroot=${LIBUNWIND_SYSROOT}")
167 elseif(CMAKE_SYSROOT)
168 set(LIBUNWIND_SYSROOT "${CMAKE_SYSROOT}")
171 if (LIBUNWIND_TARGET_TRIPLE)
172 set(TARGET_TRIPLE "${LIBUNWIND_TARGET_TRIPLE}")
175 # Configure compiler.
178 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
179 list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
182 add_compile_flags_if_supported(-Werror=return-type)
185 add_compile_flags_if_supported(-W)
186 add_compile_flags_if_supported(-Wall)
187 add_compile_flags_if_supported(-Wchar-subscripts)
188 add_compile_flags_if_supported(-Wconversion)
189 add_compile_flags_if_supported(-Wmismatched-tags)
190 add_compile_flags_if_supported(-Wmissing-braces)
191 add_compile_flags_if_supported(-Wnewline-eof)
192 add_compile_flags_if_supported(-Wno-unused-function)
193 add_compile_flags_if_supported(-Wshadow)
194 add_compile_flags_if_supported(-Wshorten-64-to-32)
195 add_compile_flags_if_supported(-Wsign-compare)
196 add_compile_flags_if_supported(-Wsign-conversion)
197 add_compile_flags_if_supported(-Wstrict-aliasing=2)
198 add_compile_flags_if_supported(-Wstrict-overflow=4)
199 add_compile_flags_if_supported(-Wunused-parameter)
200 add_compile_flags_if_supported(-Wunused-variable)
201 add_compile_flags_if_supported(-Wwrite-strings)
202 add_compile_flags_if_supported(-Wundef)
204 add_compile_flags_if_supported(-Wno-suggest-override)
207 # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
208 # silence the warning instead of cluttering the headers (which aren't
209 # necessarily the ones that the callers will use anyway) with the
211 add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
214 if (LIBUNWIND_ENABLE_WERROR)
215 add_compile_flags_if_supported(-Werror)
216 add_compile_flags_if_supported(-WX)
218 add_compile_flags_if_supported(-Wno-error)
219 add_compile_flags_if_supported(-WX-)
222 if (LIBUNWIND_ENABLE_PEDANTIC)
223 add_compile_flags_if_supported(-pedantic)
228 # Catches C++ exceptions only and tells the compiler to assume that extern C
229 # functions never throw a C++ exception.
230 add_cxx_compile_flags_if_supported(-fstrict-aliasing)
231 add_cxx_compile_flags_if_supported(-EHsc)
233 # Don't run the linker in this CMake check.
235 # The reason why this was added is that when building libunwind for
236 # ARM Linux, we need to pass the -funwind-tables flag in order for it to
237 # work properly with ARM EHABI.
239 # However, when performing CMake checks, adding this flag causes the check
240 # to produce a false negative, because the compiler generates calls
241 # to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
242 # which isn't built yet, so the linker complains about undefined symbols.
244 # This leads to libunwind not being built with this flag, which makes
245 # libunwind quite useless in this setup.
246 set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
247 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
248 add_compile_flags_if_supported(-funwind-tables)
249 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
251 if (LIBUNWIND_USES_ARM_EHABI AND NOT LIBUNWIND_SUPPORTS_FUNWIND_TABLES_FLAG)
252 message(SEND_ERROR "The -funwind-tables flag must be supported "
253 "because this target uses ARM Exception Handling ABI")
256 add_cxx_compile_flags_if_supported(-fno-exceptions)
257 add_cxx_compile_flags_if_supported(-fno-rtti)
259 # Ensure that we don't depend on C++ standard library.
260 if (LIBUNWIND_HAS_NOSTDINCXX_FLAG)
261 list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
262 # Remove -stdlib flags to prevent them from causing an unused flag warning.
263 string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
264 string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
265 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
266 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
270 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
271 if (LIBUNWIND_ENABLE_ASSERTIONS)
272 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
274 add_compile_flags(-D_DEBUG)
277 # On Release builds cmake automatically defines NDEBUG, so we
278 # explicitly undefine it:
279 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
280 add_compile_flags(-UNDEBUG)
283 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
284 add_compile_flags(-DNDEBUG)
289 if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
290 add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
294 if (NOT LIBUNWIND_ENABLE_THREADS)
295 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
298 # ARM WMMX register support
299 if (LIBUNWIND_ENABLE_ARM_WMMX)
300 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
301 # define this macro for any supported target at present. Therefore, here we
302 # provide the option to explicitly enable support for WMMX registers in the
304 add_compile_flags(-D__ARM_WMMX)
307 if(LIBUNWIND_IS_BAREMETAL)
308 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
311 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
312 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
315 # This is the _ONLY_ place where add_definitions is called.
317 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
320 # Disable DLL annotations on Windows for static builds.
321 if (WIN32 AND LIBUNWIND_ENABLE_STATIC AND NOT LIBUNWIND_ENABLE_SHARED)
322 add_definitions(-D_LIBUNWIND_DISABLE_VISIBILITY_ANNOTATIONS)
325 if (LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
326 if (LIBUNWIND_HAS_DL_LIB)
327 add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
329 if (LIBUNWIND_HAS_PTHREAD_LIB)
330 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
334 #===============================================================================
336 #===============================================================================
338 include_directories(include)
340 add_subdirectory(src)
342 if (LIBUNWIND_INCLUDE_DOCS)
343 add_subdirectory(docs)
346 if (EXISTS ${LLVM_CMAKE_PATH})
347 add_subdirectory(test)