[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / libunwind / cmake / config-ix.cmake
blobc814611d43785db7f61a561bb8584251e8a02f44
1 include(CMakePushCheckState)
2 include(CheckCCompilerFlag)
3 include(CheckCXXCompilerFlag)
4 include(CheckLibraryExists)
5 include(CheckLinkerFlag)
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_linker_flag("--unwindlib=none" LIBUNWIND_SUPPORTS_UNWINDLIB_NONE_FLAG)
13 if (LIBUNWIND_SUPPORTS_UNWINDLIB_NONE_FLAG)
14   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --unwindlib=none")
15 endif()
17 check_library_exists(c fopen "" LIBUNWIND_HAS_C_LIB)
19 if (NOT LIBUNWIND_USE_COMPILER_RT)
20   if (ANDROID)
21     check_library_exists(gcc __gcc_personality_v0 "" LIBUNWIND_HAS_GCC_LIB)
22   else ()
23     check_library_exists(gcc_s __gcc_personality_v0 "" LIBUNWIND_HAS_GCC_S_LIB)
24     check_library_exists(gcc __absvdi2 "" LIBUNWIND_HAS_GCC_LIB)
25   endif ()
26 endif()
28 # libunwind is using -nostdlib++ at the link step when available,
29 # otherwise -nodefaultlibs is used. We want all our checks to also
30 # use one of these options, otherwise we may end up with an inconsistency between
31 # the flags we think we require during configuration (if the checks are
32 # performed without one of those options) and the flags that are actually
33 # required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
34 # required for the link to go through. We remove sanitizers from the
35 # configuration checks to avoid spurious link errors.
37 llvm_check_linker_flag(-nostdlib++ LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG)
38 if (LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG)
39   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
40 else()
41   llvm_check_linker_flag(-nodefaultlibs LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
42   if (LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
43     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
44   endif()
45 endif()
47 if (LIBUNWIND_SUPPORTS_NOSTDLIBXX_FLAG OR LIBUNWIND_SUPPORTS_NODEFAULTLIBS_FLAG)
48   if (LIBUNWIND_HAS_C_LIB)
49     list(APPEND CMAKE_REQUIRED_LIBRARIES c)
50   endif ()
51   if (LIBUNWIND_USE_COMPILER_RT)
52     include(HandleCompilerRT)
53     find_compiler_rt_library(builtins LIBUNWIND_BUILTINS_LIBRARY
54                              FLAGS ${LIBUNWIND_COMPILE_FLAGS})
55     list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBUNWIND_BUILTINS_LIBRARY}")
56   else ()
57     if (LIBUNWIND_HAS_GCC_S_LIB)
58       list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
59     endif ()
60     if (LIBUNWIND_HAS_GCC_LIB)
61       list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
62     endif ()
63   endif ()
64   if (MINGW)
65     # Mingw64 requires quite a few "C" runtime libraries in order for basic
66     # programs to link successfully with -nodefaultlibs.
67     if (LIBUNWIND_USE_COMPILER_RT)
68       set(MINGW_RUNTIME ${LIBUNWIND_BUILTINS_LIBRARY})
69     else ()
70       set(MINGW_RUNTIME gcc_s gcc)
71     endif()
72     set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
73                         shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
74                         moldname mingwex msvcrt)
75     list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
76   endif()
77   if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
78     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
79   endif ()
80   if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
81     set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
82   endif ()
83 endif ()
85 # Check compiler pragmas
86 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
87   cmake_push_check_state()
88   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
89   check_c_source_compiles("
90 #pragma comment(lib, \"c\")
91 int main() { return 0; }
92 " LIBUNWIND_HAS_COMMENT_LIB_PRAGMA)
93   cmake_pop_check_state()
94 endif()
96 # Check compiler flags
97 check_cxx_compiler_flag(-nostdinc++ LIBUNWIND_HAS_NOSTDINCXX_FLAG)
99 # Check symbols
100 check_symbol_exists(__arm__ "" LIBUNWIND_TARGET_ARM)
101 check_symbol_exists(__USING_SJLJ_EXCEPTIONS__ "" LIBUNWIND_USES_SJLJ_EXCEPTIONS)
102 check_symbol_exists(__ARM_DWARF_EH__ "" LIBUNWIND_USES_DWARF_EH)
104 if(LIBUNWIND_TARGET_ARM AND NOT LIBUNWIND_USES_SJLJ_EXCEPTIONS AND NOT LIBUNWIND_USES_DWARF_EH)
105   # This condition is copied from __libunwind_config.h
106   set(LIBUNWIND_USES_ARM_EHABI ON)
107 endif()
109 # Check libraries
110 if(FUCHSIA)
111   set(LIBUNWIND_HAS_DL_LIB NO)
112   set(LIBUNWIND_HAS_PTHREAD_LIB NO)
113 else()
114   check_library_exists(dl dladdr "" LIBUNWIND_HAS_DL_LIB)
115   check_library_exists(pthread pthread_once "" LIBUNWIND_HAS_PTHREAD_LIB)
116 endif()