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)
11 # Add path for custom modules
13 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
14 "${CMAKE_CURRENT_SOURCE_DIR}/cmake/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 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBUNWIND_STANDALONE_BUILD)
24 project(libunwind LANGUAGES C CXX ASM)
26 set(PACKAGE_NAME libunwind)
27 set(PACKAGE_VERSION 14.0.0git)
28 set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
29 set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
31 # Add the CMake module path of libcxx so we can reuse HandleOutOfTreeLLVM.cmake
32 set(LIBUNWIND_LIBCXX_CMAKE_PATH "${LIBUNWIND_LIBCXX_PATH}/cmake/Modules")
33 list(APPEND CMAKE_MODULE_PATH "${LIBUNWIND_LIBCXX_CMAKE_PATH}")
35 # In a standalone build, we don't have llvm to automatically generate the
36 # llvm-lit script for us. So we need to provide an explicit directory that
37 # the configurator should write the script into.
38 set(LIBUNWIND_STANDALONE_BUILD 1)
39 set(LLVM_LIT_OUTPUT_DIR "${LIBUNWIND_BINARY_DIR}/bin")
41 # Find the LLVM sources and simulate LLVM CMake options.
42 include(HandleOutOfTreeLLVM)
44 set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
47 #===============================================================================
49 #===============================================================================
50 include(CMakeDependentOption)
51 include(HandleCompilerRT)
54 option(LIBUNWIND_BUILD_32_BITS "Build 32 bit libunwind" ${LLVM_BUILD_32_BITS})
55 option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
56 option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
57 option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
58 option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
59 option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
60 option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
61 option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
62 option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
63 option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)
64 option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
65 option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
66 option(LIBUNWIND_INCLUDE_TESTS "Build the libunwind tests." ${LLVM_INCLUDE_TESTS})
67 option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)
68 option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)
69 option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)
71 set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
72 "Define suffix of library directory name (32/64)")
73 option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
74 cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
75 "Install the static libunwind library." ON
76 "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
77 cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
78 "Install the shared libunwind library." ON
79 "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
80 set(LIBUNWIND_TARGET_TRIPLE "${LLVM_DEFAULT_TARGET_TRIPLE}" CACHE STRING "Target triple for cross compiling.")
81 set(LIBUNWIND_GCC_TOOLCHAIN "" CACHE PATH "GCC toolchain for cross compiling.")
82 set(LIBUNWIND_SYSROOT "" CACHE PATH "Sysroot for cross compiling.")
83 set(LIBUNWIND_TEST_LINKER_FLAGS "" CACHE STRING
84 "Additional linker flags for test programs.")
85 set(LIBUNWIND_TEST_COMPILER_FLAGS "" CACHE STRING
86 "Additional compiler flags for test programs.")
87 set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/lit.site.cfg.in" CACHE STRING
88 "The Lit testing configuration to use when running the tests.")
89 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
90 "A list of parameters to run the Lit test suite with.")
92 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
93 message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
96 # Check that we can build with 32 bits if requested.
97 if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND NOT WIN32)
98 if (LIBUNWIND_BUILD_32_BITS AND NOT LLVM_BUILD_32_BITS) # Don't duplicate the output from LLVM
99 message(STATUS "Building 32 bits executables and libraries.")
101 elseif(LIBUNWIND_BUILD_32_BITS)
102 message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS=ON is not supported on this platform.")
105 option(LIBUNWIND_HIDE_SYMBOLS
106 "Do not export any symbols from the static library." OFF)
108 #===============================================================================
110 #===============================================================================
112 # Add path for custom modules
113 set(CMAKE_MODULE_PATH
114 "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
115 ${CMAKE_MODULE_PATH})
117 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
118 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
119 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
120 "Path where built libunwind libraries should be installed.")
121 if(LIBCXX_LIBDIR_SUBDIR)
122 string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
123 string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
125 elseif(LLVM_LIBRARY_OUTPUT_INTDIR)
126 set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
127 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
128 "Path where built libunwind libraries should be installed.")
130 set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
131 set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
132 "Path where built libunwind libraries should be installed.")
135 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
136 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
137 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
139 set(LIBUNWIND_C_FLAGS "")
140 set(LIBUNWIND_CXX_FLAGS "")
141 set(LIBUNWIND_COMPILE_FLAGS "")
142 set(LIBUNWIND_LINK_FLAGS "")
144 # Include macros for adding and removing libunwind flags.
145 include(HandleLibunwindFlags)
147 #===============================================================================
148 # Setup Compiler Flags
149 #===============================================================================
151 # Get required flags.
152 add_target_flags_if(LIBUNWIND_BUILD_32_BITS "-m32")
154 if(LIBUNWIND_TARGET_TRIPLE)
155 add_target_flags_if_supported("--target=${LIBUNWIND_TARGET_TRIPLE}")
156 elseif(CMAKE_CXX_COMPILER_TARGET)
157 set(LIBUNWIND_TARGET_TRIPLE "${CMAKE_CXX_COMPILER_TARGET}")
159 if(LIBUNWIND_GCC_TOOLCHAIN)
160 add_target_flags_if_supported("--gcc-toolchain=${LIBUNWIND_GCC_TOOLCHAIN}")
161 elseif(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN)
162 set(LIBUNWIND_GCC_TOOLCHAIN "${CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN}")
164 if(LIBUNWIND_SYSROOT)
165 add_target_flags_if_supported("--sysroot=${LIBUNWIND_SYSROOT}")
166 elseif(CMAKE_SYSROOT)
167 set(LIBUNWIND_SYSROOT "${CMAKE_SYSROOT}")
170 # Configure compiler.
173 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
174 list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
177 add_compile_flags_if_supported(-Werror=return-type)
180 add_compile_flags_if_supported(-W)
181 add_compile_flags_if_supported(-Wall)
182 add_compile_flags_if_supported(-Wchar-subscripts)
183 add_compile_flags_if_supported(-Wconversion)
184 add_compile_flags_if_supported(-Wmismatched-tags)
185 add_compile_flags_if_supported(-Wmissing-braces)
186 add_compile_flags_if_supported(-Wnewline-eof)
187 add_compile_flags_if_supported(-Wno-unused-function)
188 add_compile_flags_if_supported(-Wshadow)
189 add_compile_flags_if_supported(-Wshorten-64-to-32)
190 add_compile_flags_if_supported(-Wsign-compare)
191 add_compile_flags_if_supported(-Wsign-conversion)
192 add_compile_flags_if_supported(-Wstrict-aliasing=2)
193 add_compile_flags_if_supported(-Wstrict-overflow=4)
194 add_compile_flags_if_supported(-Wunused-parameter)
195 add_compile_flags_if_supported(-Wunused-variable)
196 add_compile_flags_if_supported(-Wwrite-strings)
197 add_compile_flags_if_supported(-Wundef)
199 add_compile_flags_if_supported(-Wno-suggest-override)
202 # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
203 # silence the warning instead of cluttering the headers (which aren't
204 # necessarily the ones that the callers will use anyway) with the
206 add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
209 if (LIBUNWIND_ENABLE_WERROR)
210 add_compile_flags_if_supported(-Werror)
211 add_compile_flags_if_supported(-WX)
213 add_compile_flags_if_supported(-Wno-error)
214 add_compile_flags_if_supported(-WX-)
217 if (LIBUNWIND_ENABLE_PEDANTIC)
218 add_compile_flags_if_supported(-pedantic)
223 # Catches C++ exceptions only and tells the compiler to assume that extern C
224 # functions never throw a C++ exception.
225 add_cxx_compile_flags_if_supported(-fstrict-aliasing)
226 add_cxx_compile_flags_if_supported(-EHsc)
228 # Don't run the linker in this CMake check.
230 # The reason why this was added is that when building libunwind for
231 # ARM Linux, we need to pass the -funwind-tables flag in order for it to
232 # work properly with ARM EHABI.
234 # However, when performing CMake checks, adding this flag causes the check
235 # to produce a false negative, because the compiler generates calls
236 # to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
237 # which isn't built yet, so the linker complains about undefined symbols.
239 # This leads to libunwind not being built with this flag, which makes
240 # libunwind quite useless in this setup.
241 set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
242 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
243 add_compile_flags_if_supported(-funwind-tables)
244 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
246 if (LIBUNWIND_USES_ARM_EHABI AND NOT LIBUNWIND_SUPPORTS_FUNWIND_TABLES_FLAG)
247 message(SEND_ERROR "The -funwind-tables flag must be supported "
248 "because this target uses ARM Exception Handling ABI")
251 add_cxx_compile_flags_if_supported(-fno-exceptions)
252 add_cxx_compile_flags_if_supported(-fno-rtti)
254 # Ensure that we don't depend on C++ standard library.
255 if (LIBUNWIND_HAS_NOSTDINCXX_FLAG)
256 list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
257 # Remove -stdlib flags to prevent them from causing an unused flag warning.
258 string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
259 string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
260 string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
261 string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
265 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
266 if (LIBUNWIND_ENABLE_ASSERTIONS)
267 # MSVC doesn't like _DEBUG on release builds. See PR 4379.
269 add_compile_flags(-D_DEBUG)
272 # On Release builds cmake automatically defines NDEBUG, so we
273 # explicitly undefine it:
274 if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
275 add_compile_flags(-UNDEBUG)
278 if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
279 add_compile_flags(-DNDEBUG)
284 if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
285 add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
289 if (NOT LIBUNWIND_ENABLE_THREADS)
290 add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
293 # ARM WMMX register support
294 if (LIBUNWIND_ENABLE_ARM_WMMX)
295 # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
296 # define this macro for any supported target at present. Therefore, here we
297 # provide the option to explicitly enable support for WMMX registers in the
299 add_compile_flags(-D__ARM_WMMX)
302 if(LIBUNWIND_IS_BAREMETAL)
303 add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
306 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
307 add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
310 if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
311 add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
314 # This is the _ONLY_ place where add_definitions is called.
316 add_definitions(-D_CRT_SECURE_NO_WARNINGS)
319 # Disable DLL annotations on Windows for static builds.
320 if (WIN32 AND LIBUNWIND_ENABLE_STATIC AND NOT LIBUNWIND_ENABLE_SHARED)
321 add_definitions(-D_LIBUNWIND_HIDE_SYMBOLS)
324 if (LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
325 if (LIBUNWIND_HAS_DL_LIB)
326 add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
328 if (LIBUNWIND_HAS_PTHREAD_LIB)
329 add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
333 #===============================================================================
335 #===============================================================================
337 include_directories(include)
339 add_subdirectory(src)
341 if (LIBUNWIND_INCLUDE_DOCS)
342 add_subdirectory(docs)
345 if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_PATH})
346 add_subdirectory(test)