Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libunwind / cmake / config-ix.cmake
blob7df8974b0a9254006d0eb9893264a90f894afef4
1 include(CMakePushCheckState)
2 include(CheckCCompilerFlag)
3 include(CheckCXXCompilerFlag)
4 include(CheckLibraryExists)
5 include(LLVMCheckCompilerLinkerFlag)
6 include(CheckSymbolExists)
7 include(CheckCSourceCompiles)
9 # The compiler driver may be implicitly trying to link against libunwind, which
10 # might not work if libunwind doesn't exist yet. Try to check if
11 # --unwindlib=none is supported, and use that if possible.
12 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
14 if (HAIKU)
15   check_library_exists(root fopen "" LIBUNWIND_HAS_ROOT_LIB)
16 else()
17   check_library_exists(c fopen "" LIBUNWIND_HAS_C_LIB)
18 endif()
20 if (NOT LIBUNWIND_USE_COMPILER_RT)
21   if (ANDROID)
22     check_library_exists(gcc __gcc_personality_v0 "" LIBUNWIND_HAS_GCC_LIB)
23   else ()
24     check_library_exists(gcc_s __gcc_personality_v0 "" LIBUNWIND_HAS_GCC_S_LIB)
25     check_library_exists(gcc __absvdi2 "" LIBUNWIND_HAS_GCC_LIB)
26   endif ()
27 endif()
29 # libunwind is using -nostdlib++ at the link step when available,
30 # otherwise -nodefaultlibs is used. We want all our checks to also
31 # use one of these options, otherwise we may end up with an inconsistency between
32 # the flags we think we require during configuration (if the checks are
33 # performed without one of those options) and the flags that are actually
34 # required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
35 # required for the link to go through. We remove sanitizers from the
36 # configuration checks to avoid spurious link errors.
38 llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
39 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
40   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
41 else()
42   llvm_check_compiler_linker_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG)
43   if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
44     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
45   endif()
46 endif()
48 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
49   if (LIBUNWIND_HAS_C_LIB)
50     list(APPEND CMAKE_REQUIRED_LIBRARIES c)
51   endif ()
52   if (LIBUNWIND_HAS_ROOT_LIB)
53     list(APPEND CMAKE_REQUIRED_LIBRARIES root)
54   endif ()
55   if (LIBUNWIND_USE_COMPILER_RT)
56     include(HandleCompilerRT)
57     find_compiler_rt_library(builtins LIBUNWIND_BUILTINS_LIBRARY
58                              FLAGS ${LIBUNWIND_COMPILE_FLAGS})
59     list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBUNWIND_BUILTINS_LIBRARY}")
60   else ()
61     if (LIBUNWIND_HAS_GCC_S_LIB)
62       list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
63     endif ()
64     if (LIBUNWIND_HAS_GCC_LIB)
65       list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
66     endif ()
67   endif ()
68   if (MINGW)
69     # Mingw64 requires quite a few "C" runtime libraries in order for basic
70     # programs to link successfully with -nodefaultlibs.
71     if (LIBUNWIND_USE_COMPILER_RT)
72       set(MINGW_RUNTIME ${LIBUNWIND_BUILTINS_LIBRARY})
73     else ()
74       set(MINGW_RUNTIME gcc_s gcc)
75     endif()
76     set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
77                         shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
78                         moldname mingwex msvcrt)
79     list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
80   endif()
81   if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
82     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
83   endif ()
84   if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
85     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
86   endif ()
87 endif ()
89 # Check compiler pragmas
90 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
91   cmake_push_check_state()
92   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
93   check_c_source_compiles("
94 #pragma comment(lib, \"c\")
95 int main(void) { return 0; }
96 " C_SUPPORTS_COMMENT_LIB_PRAGMA)
97   cmake_pop_check_state()
98 endif()
100 # Check compiler flags
101 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
103 # Check symbols
104 check_symbol_exists(__arm__ "" LIBUNWIND_TARGET_ARM)
105 check_symbol_exists(__USING_SJLJ_EXCEPTIONS__ "" LIBUNWIND_USES_SJLJ_EXCEPTIONS)
106 check_symbol_exists(__ARM_DWARF_EH__ "" LIBUNWIND_USES_DWARF_EH)
108 if(LIBUNWIND_TARGET_ARM AND NOT LIBUNWIND_USES_SJLJ_EXCEPTIONS AND NOT LIBUNWIND_USES_DWARF_EH)
109   # This condition is copied from __libunwind_config.h
110   set(LIBUNWIND_USES_ARM_EHABI ON)
111 endif()
113 # Check libraries
114 if(FUCHSIA)
115   set(LIBUNWIND_HAS_DL_LIB NO)
116   set(LIBUNWIND_HAS_PTHREAD_LIB NO)
117 else()
118   check_library_exists(dl dladdr "" LIBUNWIND_HAS_DL_LIB)
119   check_library_exists(pthread pthread_once "" LIBUNWIND_HAS_PTHREAD_LIB)
120 endif()
122 if(HAIKU)
123   check_library_exists(bsd dl_iterate_phdr "" LIBUNWIND_HAS_BSD_LIB)
124 endif()