[WebAssembly] Add new target feature in support of 'extended-const' proposal
[llvm-project.git] / libunwind / CMakeLists.txt
blob068074cef28e822e5693a948148adde8ad567330
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")
3 endif()
5 #===============================================================================
6 # Setup Project
7 #===============================================================================
9 cmake_minimum_required(VERSION 3.13.4)
11 set(LLVM_COMMON_CMAKE_UTILS "${CMAKE_CURRENT_SOURCE_DIR}/../cmake")
13 # Add path for custom modules
14 list(INSERT CMAKE_MODULE_PATH 0
15   "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
16   "${CMAKE_CURRENT_SOURCE_DIR}/cmake/Modules"
17   "${LLVM_COMMON_CMAKE_UTILS}"
18   "${LLVM_COMMON_CMAKE_UTILS}/Modules"
19   )
21 set(LIBUNWIND_SOURCE_DIR  ${CMAKE_CURRENT_SOURCE_DIR})
22 set(LIBUNWIND_BINARY_DIR  ${CMAKE_CURRENT_BINARY_DIR})
23 set(LIBUNWIND_LIBCXX_PATH "${CMAKE_CURRENT_LIST_DIR}/../libcxx" CACHE PATH
24         "Specify path to libc++ source.")
26 if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR OR LIBUNWIND_STANDALONE_BUILD)
27   message(FATAL_ERROR "The Standalone build has been deprecated since LLVM 14, and it is not supported anymore. "
28                       "Please use one of the ways described at https://libcxx.llvm.org/BuildingLibcxx.html for "
29                       "building libunwind.")
31   # We may have an incomplete toolchain - do language support tests without
32   # linking.
33   include(EnableLanguageNolink)
34   project(libunwind LANGUAGES NONE)
36   set(PACKAGE_NAME libunwind)
37   set(PACKAGE_VERSION 15.0.0git)
38   set(PACKAGE_STRING "${PACKAGE_NAME} ${PACKAGE_VERSION}")
39   set(PACKAGE_BUGREPORT "llvm-bugs@lists.llvm.org")
41   set(LIBUNWIND_STANDALONE_BUILD TRUE)
42 endif()
44 # Must go below project(..)
45 include(GNUInstallDirs)
47 if(LIBUNWIND_STANDALONE_BUILD)
48   llvm_enable_language_nolink(C CXX ASM)
50   # Find the LLVM sources and simulate LLVM CMake options.
51   include(HandleOutOfTreeLLVM)
53   # In a standalone build, we don't have llvm to automatically generate the
54   # llvm-lit script for us.  So we need to provide an explicit directory that
55   # the configurator should write the script into.
56   set(LLVM_LIT_OUTPUT_DIR "${LIBUNWIND_BINARY_DIR}/bin")
57 else()
58   set(LLVM_LIT "${CMAKE_SOURCE_DIR}/utils/lit/lit.py")
59 endif()
61 #===============================================================================
62 # Setup CMake Options
63 #===============================================================================
64 include(CMakeDependentOption)
65 include(HandleCompilerRT)
67 # Define options.
68 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})
69 if (LIBUNWIND_BUILD_32_BITS)
70   message(FATAL_ERROR "LIBUNWIND_BUILD_32_BITS is not supported anymore when building the runtimes, please specify a full triple instead.")
71 endif()
73 option(LIBUNWIND_ENABLE_CET "Build libunwind with CET enabled." OFF)
74 option(LIBUNWIND_ENABLE_ASSERTIONS "Enable assertions independent of build mode." ON)
75 option(LIBUNWIND_ENABLE_PEDANTIC "Compile with pedantic enabled." ON)
76 option(LIBUNWIND_ENABLE_WERROR "Fail and stop if a warning is triggered." OFF)
77 option(LIBUNWIND_ENABLE_SHARED "Build libunwind as a shared library." ON)
78 option(LIBUNWIND_ENABLE_STATIC "Build libunwind as a static library." ON)
79 option(LIBUNWIND_ENABLE_CROSS_UNWINDING "Enable cross-platform unwinding support." OFF)
80 option(LIBUNWIND_ENABLE_ARM_WMMX "Enable unwinding support for ARM WMMX registers." OFF)
81 option(LIBUNWIND_ENABLE_THREADS "Build libunwind with threading support." ON)
82 option(LIBUNWIND_WEAK_PTHREAD_LIB "Use weak references to refer to pthread functions." OFF)
83 option(LIBUNWIND_USE_COMPILER_RT "Use compiler-rt instead of libgcc" OFF)
84 option(LIBUNWIND_INCLUDE_DOCS "Build the libunwind documentation." ${LLVM_INCLUDE_DOCS})
85 option(LIBUNWIND_INCLUDE_TESTS "Build the libunwind tests." ${LLVM_INCLUDE_TESTS})
86 option(LIBUNWIND_IS_BAREMETAL "Build libunwind for baremetal targets." OFF)
87 option(LIBUNWIND_USE_FRAME_HEADER_CACHE "Cache frame headers for unwinding. Requires locking dl_iterate_phdr." OFF)
88 option(LIBUNWIND_REMEMBER_HEAP_ALLOC "Use heap instead of the stack for .cfi_remember_state." OFF)
89 option(LIBUNWIND_INSTALL_HEADERS "Install the libunwind headers." OFF)
91 set(LIBUNWIND_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" CACHE STRING
92     "Define suffix of library directory name (32/64)")
93 option(LIBUNWIND_INSTALL_LIBRARY "Install the libunwind library." ON)
94 cmake_dependent_option(LIBUNWIND_INSTALL_STATIC_LIBRARY
95   "Install the static libunwind library." ON
96   "LIBUNWIND_ENABLE_STATIC;LIBUNWIND_INSTALL_LIBRARY" OFF)
97 cmake_dependent_option(LIBUNWIND_INSTALL_SHARED_LIBRARY
98   "Install the shared libunwind library." ON
99   "LIBUNWIND_ENABLE_SHARED;LIBUNWIND_INSTALL_LIBRARY" OFF)
100 set(LIBUNWIND_TEST_LINKER_FLAGS "" CACHE STRING
101     "Additional linker flags for test programs.")
102 set(LIBUNWIND_TEST_COMPILER_FLAGS "" CACHE STRING
103     "Additional compiler flags for test programs.")
105 # TODO: Remove this after branching for LLVM 15
106 if(LIBUNWIND_SYSROOT OR LIBUNWIND_TARGET_TRIPLE OR LIBUNWIND_GCC_TOOLCHAIN)
107   message(WARNING "LIBUNWIND_SYSROOT, LIBUNWIND_TARGET_TRIPLE and LIBUNWIND_GCC_TOOLCHAIN are not supported anymore, please use the native CMake equivalents instead")
108 endif()
110 if (LIBUNWIND_ENABLE_SHARED)
111   set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-shared.cfg.in")
112 else()
113   set(LIBUNWIND_DEFAULT_TEST_CONFIG "llvm-libunwind-static.cfg.in")
114 endif()
115 set(LIBUNWIND_TEST_CONFIG "${LIBUNWIND_DEFAULT_TEST_CONFIG}" CACHE STRING
116   "The path to the Lit testing configuration to use when running the tests.
117    If a relative path is provided, it is assumed to be relative to '<monorepo>/libunwind/test/configs'.")
118 if (NOT IS_ABSOLUTE "${LIBUNWIND_TEST_CONFIG}")
119   set(LIBUNWIND_TEST_CONFIG "${CMAKE_CURRENT_SOURCE_DIR}/test/configs/${LIBUNWIND_TEST_CONFIG}")
120 endif()
121 set(LIBUNWIND_TEST_PARAMS "" CACHE STRING
122     "A list of parameters to run the Lit test suite with.")
124 if (NOT LIBUNWIND_ENABLE_SHARED AND NOT LIBUNWIND_ENABLE_STATIC)
125   message(FATAL_ERROR "libunwind must be built as either a shared or static library.")
126 endif()
128 if (LIBUNWIND_ENABLE_CET AND MSVC)
129   message(FATAL_ERROR "libunwind CET support is not available for MSVC!")
130 endif()
132 option(LIBUNWIND_HIDE_SYMBOLS
133   "Do not export any symbols from the static library." OFF)
135 #===============================================================================
136 # Configure System
137 #===============================================================================
139 # Add path for custom modules
140 set(CMAKE_MODULE_PATH
141     "${CMAKE_CURRENT_SOURCE_DIR}/cmake"
142     ${CMAKE_MODULE_PATH})
144 set(LIBUNWIND_INSTALL_INCLUDE_DIR "${CMAKE_INSTALL_INCLUDEDIR}" CACHE PATH
145     "Path where built libunwind headers should be installed.")
146 set(LIBUNWIND_INSTALL_RUNTIME_DIR "${CMAKE_INSTALL_BINDIR}" CACHE PATH
147     "Path where built libunwind runtime libraries should be installed.")
149 if(LLVM_ENABLE_PER_TARGET_RUNTIME_DIR AND NOT APPLE)
150   set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR}/${LLVM_DEFAULT_TARGET_TRIPLE})
151   set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LLVM_LIBDIR_SUFFIX}/${LLVM_DEFAULT_TARGET_TRIPLE} CACHE PATH
152       "Path where built libunwind libraries should be installed.")
153   if(LIBCXX_LIBDIR_SUBDIR)
154     string(APPEND LIBUNWIND_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
155     string(APPEND LIBUNWIND_INSTALL_LIBRARY_DIR /${LIBUNWIND_LIBDIR_SUBDIR})
156   endif()
157 else()
158   if(LLVM_LIBRARY_OUTPUT_INTDIR)
159     set(LIBUNWIND_LIBRARY_DIR ${LLVM_LIBRARY_OUTPUT_INTDIR})
160   else()
161     set(LIBUNWIND_LIBRARY_DIR ${CMAKE_BINARY_DIR}/lib${LIBUNWIND_LIBDIR_SUFFIX})
162   endif()
163   set(LIBUNWIND_INSTALL_LIBRARY_DIR lib${LIBUNWIND_LIBDIR_SUFFIX} CACHE PATH
164       "Path where built libunwind libraries should be installed.")
165 endif()
167 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
168 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
169 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LIBUNWIND_LIBRARY_DIR})
171 set(LIBUNWIND_C_FLAGS "")
172 set(LIBUNWIND_CXX_FLAGS "")
173 set(LIBUNWIND_COMPILE_FLAGS "")
174 set(LIBUNWIND_LINK_FLAGS "")
176 # Include macros for adding and removing libunwind flags.
177 include(HandleLibunwindFlags)
179 #===============================================================================
180 # Setup Compiler Flags
181 #===============================================================================
183 # Compiler tests may be failing if the compiler implicitly links in libunwind,
184 # which doesn't exist yet. This gets waived by --unwindlib=none
185 # later in config-ix below, but the tests for --target etc before that may
186 # be failing due to it. Only test compilation, not linking, for these
187 # tests here now.
188 set(CMAKE_TRY_COMPILE_TARGET_TYPE_ORIG ${CMAKE_TRY_COMPILE_TARGET_TYPE})
189 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
190 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE_ORIG})
192 # Configure compiler.
193 include(config-ix)
195 if (LIBUNWIND_USE_COMPILER_RT AND NOT LIBUNWIND_HAS_NODEFAULTLIBS_FLAG)
196   list(APPEND LIBUNWIND_LINK_FLAGS "-rtlib=compiler-rt")
197 endif()
199 add_compile_flags_if_supported(-Werror=return-type)
201 if (LIBUNWIND_ENABLE_CET)
202   add_compile_flags_if_supported(-fcf-protection=full)
203   add_compile_flags_if_supported(-mshstk)
204   if (NOT LIBUNWIND_SUPPORTS_FCF_PROTECTION_EQ_FULL_FLAG)
205     message(SEND_ERROR "Compiler doesn't support CET -fcf-protection option!")
206   endif()
207   if (NOT LIBUNWIND_SUPPORTS_MSHSTK_FLAG)
208     message(SEND_ERROR "Compiler doesn't support CET -mshstk option!")
209   endif()
210 endif()
212 # Get warning flags
213 add_compile_flags_if_supported(-W)
214 add_compile_flags_if_supported(-Wall)
215 add_compile_flags_if_supported(-Wchar-subscripts)
216 add_compile_flags_if_supported(-Wconversion)
217 add_compile_flags_if_supported(-Wmismatched-tags)
218 add_compile_flags_if_supported(-Wmissing-braces)
219 add_compile_flags_if_supported(-Wnewline-eof)
220 add_compile_flags_if_supported(-Wno-unused-function)
221 add_compile_flags_if_supported(-Wshadow)
222 add_compile_flags_if_supported(-Wshorten-64-to-32)
223 add_compile_flags_if_supported(-Wsign-compare)
224 add_compile_flags_if_supported(-Wsign-conversion)
225 add_compile_flags_if_supported(-Wstrict-aliasing=2)
226 add_compile_flags_if_supported(-Wstrict-overflow=4)
227 add_compile_flags_if_supported(-Wunused-parameter)
228 add_compile_flags_if_supported(-Wunused-variable)
229 add_compile_flags_if_supported(-Wwrite-strings)
230 add_compile_flags_if_supported(-Wundef)
232 add_compile_flags_if_supported(-Wno-suggest-override)
234 if (WIN32)
235   # The headers lack matching dllexport attributes (_LIBUNWIND_EXPORT);
236   # silence the warning instead of cluttering the headers (which aren't
237   # necessarily the ones that the callers will use anyway) with the
238   # attributes.
239   add_compile_flags_if_supported(-Wno-dll-attribute-on-redeclaration)
240 endif()
242 if (LIBUNWIND_ENABLE_WERROR)
243   add_compile_flags_if_supported(-Werror)
244   add_compile_flags_if_supported(-WX)
245 else()
246   add_compile_flags_if_supported(-Wno-error)
247   add_compile_flags_if_supported(-WX-)
248 endif()
250 if (LIBUNWIND_ENABLE_PEDANTIC)
251   add_compile_flags_if_supported(-pedantic)
252 endif()
254 # Get feature flags.
255 # Exceptions
256 # Catches C++ exceptions only and tells the compiler to assume that extern C
257 # functions never throw a C++ exception.
258 add_cxx_compile_flags_if_supported(-fstrict-aliasing)
259 add_cxx_compile_flags_if_supported(-EHsc)
261 # Don't run the linker in this CMake check.
263 # The reason why this was added is that when building libunwind for
264 # ARM Linux, we need to pass the -funwind-tables flag in order for it to
265 # work properly with ARM EHABI.
267 # However, when performing CMake checks, adding this flag causes the check
268 # to produce a false negative, because the compiler generates calls
269 # to __aeabi_unwind_cpp_pr0, which is defined in libunwind itself,
270 # which isn't built yet, so the linker complains about undefined symbols.
272 # This leads to libunwind not being built with this flag, which makes
273 # libunwind quite useless in this setup.
274 set(_previous_CMAKE_TRY_COMPILE_TARGET_TYPE ${CMAKE_TRY_COMPILE_TARGET_TYPE})
275 set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
276 add_compile_flags_if_supported(-funwind-tables)
277 set(CMAKE_TRY_COMPILE_TARGET_TYPE ${_previous_CMAKE_TRY_COMPILE_TARGET_TYPE})
279 if (LIBUNWIND_USES_ARM_EHABI AND NOT LIBUNWIND_SUPPORTS_FUNWIND_TABLES_FLAG)
280   message(SEND_ERROR "The -funwind-tables flag must be supported "
281                      "because this target uses ARM Exception Handling ABI")
282 endif()
284 add_cxx_compile_flags_if_supported(-fno-exceptions)
285 add_cxx_compile_flags_if_supported(-fno-rtti)
287 # Ensure that we don't depend on C++ standard library.
288 if (LIBUNWIND_HAS_NOSTDINCXX_FLAG)
289   list(APPEND LIBUNWIND_COMPILE_FLAGS -nostdinc++)
290   # Remove -stdlib flags to prevent them from causing an unused flag warning.
291   string(REPLACE "--stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
292   string(REPLACE "--stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
293   string(REPLACE "-stdlib=libc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
294   string(REPLACE "-stdlib=libstdc++" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
295 endif()
297 # Assert
298 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
299 if (LIBUNWIND_ENABLE_ASSERTIONS)
300   # MSVC doesn't like _DEBUG on release builds. See PR 4379.
301   if (NOT MSVC)
302     add_compile_flags(-D_DEBUG)
303   endif()
305   # On Release builds cmake automatically defines NDEBUG, so we
306   # explicitly undefine it:
307   if (uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
308     add_compile_flags(-UNDEBUG)
309   endif()
310 else()
311   if (NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "RELEASE")
312     add_compile_flags(-DNDEBUG)
313   endif()
314 endif()
316 # Cross-unwinding
317 if (NOT LIBUNWIND_ENABLE_CROSS_UNWINDING)
318   add_compile_flags(-D_LIBUNWIND_IS_NATIVE_ONLY)
319 endif()
321 # Threading-support
322 if (NOT LIBUNWIND_ENABLE_THREADS)
323   add_compile_flags(-D_LIBUNWIND_HAS_NO_THREADS)
324 endif()
326 # ARM WMMX register support
327 if (LIBUNWIND_ENABLE_ARM_WMMX)
328   # __ARM_WMMX is a compiler pre-define (as per the ACLE 2.0). Clang does not
329   # define this macro for any supported target at present. Therefore, here we
330   # provide the option to explicitly enable support for WMMX registers in the
331   # unwinder.
332   add_compile_flags(-D__ARM_WMMX)
333 endif()
335 if(LIBUNWIND_IS_BAREMETAL)
336   add_compile_definitions(_LIBUNWIND_IS_BAREMETAL)
337 endif()
339 if(LIBUNWIND_USE_FRAME_HEADER_CACHE)
340   add_compile_definitions(_LIBUNWIND_USE_FRAME_HEADER_CACHE)
341 endif()
343 if(LIBUNWIND_REMEMBER_HEAP_ALLOC)
344   add_compile_definitions(_LIBUNWIND_REMEMBER_HEAP_ALLOC)
345 endif()
347 # This is the _ONLY_ place where add_definitions is called.
348 if (MSVC)
349   add_definitions(-D_CRT_SECURE_NO_WARNINGS)
350 endif()
352 # Disable DLL annotations on Windows for static builds.
353 if (WIN32 AND LIBUNWIND_ENABLE_STATIC AND NOT LIBUNWIND_ENABLE_SHARED)
354   add_definitions(-D_LIBUNWIND_HIDE_SYMBOLS)
355 endif()
357 if (LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
358   if (LIBUNWIND_HAS_DL_LIB)
359     add_definitions(-D_LIBUNWIND_LINK_DL_LIB)
360   endif()
361   if (LIBUNWIND_HAS_PTHREAD_LIB)
362     add_definitions(-D_LIBUNWIND_LINK_PTHREAD_LIB)
363   endif()
364 endif()
366 #===============================================================================
367 # Setup Source Code
368 #===============================================================================
370 add_subdirectory(include)
372 add_subdirectory(src)
374 if (LIBUNWIND_INCLUDE_DOCS)
375   add_subdirectory(docs)
376 endif()
378 if (LIBUNWIND_INCLUDE_TESTS AND EXISTS ${LLVM_CMAKE_DIR})
379   add_subdirectory(test)
380 endif()