llvm-project
129 строк · 4.8 Кб
1include(CMakePushCheckState)
2include(CheckCCompilerFlag)
3include(CheckCXXCompilerFlag)
4include(CheckLibraryExists)
5include(LLVMCheckCompilerLinkerFlag)
6include(CheckSymbolExists)
7include(CheckCSourceCompiles)
8
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.
12llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
13
14if (HAIKU)
15check_library_exists(root fopen "" LIBUNWIND_HAS_ROOT_LIB)
16else()
17check_library_exists(c fopen "" LIBUNWIND_HAS_C_LIB)
18endif()
19
20if (NOT LIBUNWIND_USE_COMPILER_RT)
21if (ANDROID)
22check_library_exists(gcc __gcc_personality_v0 "" LIBUNWIND_HAS_GCC_LIB)
23else ()
24check_library_exists(gcc_s __gcc_personality_v0 "" LIBUNWIND_HAS_GCC_S_LIB)
25check_library_exists(gcc __absvdi2 "" LIBUNWIND_HAS_GCC_LIB)
26endif ()
27endif()
28
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.
37
38llvm_check_compiler_linker_flag(CXX "-nostdlib++" CXX_SUPPORTS_NOSTDLIBXX_FLAG)
39if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
40set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
41else()
42llvm_check_compiler_linker_flag(C "-nodefaultlibs" C_SUPPORTS_NODEFAULTLIBS_FLAG)
43if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
44set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
45endif()
46endif()
47
48# Only link against compiler-rt manually if we use -nodefaultlibs, since
49# otherwise the compiler will do the right thing on its own.
50if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
51if (LIBUNWIND_HAS_C_LIB)
52list(APPEND CMAKE_REQUIRED_LIBRARIES c)
53endif ()
54if (LIBUNWIND_HAS_ROOT_LIB)
55list(APPEND CMAKE_REQUIRED_LIBRARIES root)
56endif ()
57if (LIBUNWIND_USE_COMPILER_RT)
58include(HandleCompilerRT)
59find_compiler_rt_library(builtins LIBUNWIND_BUILTINS_LIBRARY
60FLAGS ${LIBUNWIND_COMPILE_FLAGS})
61list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBUNWIND_BUILTINS_LIBRARY}")
62else ()
63if (LIBUNWIND_HAS_GCC_S_LIB)
64list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
65endif ()
66if (LIBUNWIND_HAS_GCC_LIB)
67list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
68endif ()
69endif ()
70if (MINGW)
71# Mingw64 requires quite a few "C" runtime libraries in order for basic
72# programs to link successfully with -nodefaultlibs.
73if (LIBUNWIND_USE_COMPILER_RT)
74set(MINGW_RUNTIME ${LIBUNWIND_BUILTINS_LIBRARY})
75else ()
76set(MINGW_RUNTIME gcc_s gcc)
77endif()
78set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
79shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
80moldname mingwex msvcrt)
81list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
82endif()
83endif()
84
85if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
86if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
87set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
88endif ()
89if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
90set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
91endif ()
92endif ()
93
94# Check compiler pragmas
95if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
96cmake_push_check_state()
97set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
98check_c_source_compiles("
99#pragma comment(lib, \"c\")
100int main(void) { return 0; }
101" C_SUPPORTS_COMMENT_LIB_PRAGMA)
102cmake_pop_check_state()
103endif()
104
105# Check compiler flags
106check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
107
108# Check symbols
109check_symbol_exists(__arm__ "" LIBUNWIND_TARGET_ARM)
110check_symbol_exists(__USING_SJLJ_EXCEPTIONS__ "" LIBUNWIND_USES_SJLJ_EXCEPTIONS)
111check_symbol_exists(__ARM_DWARF_EH__ "" LIBUNWIND_USES_DWARF_EH)
112
113if(LIBUNWIND_TARGET_ARM AND NOT LIBUNWIND_USES_SJLJ_EXCEPTIONS AND NOT LIBUNWIND_USES_DWARF_EH)
114# This condition is copied from __libunwind_config.h
115set(LIBUNWIND_USES_ARM_EHABI ON)
116endif()
117
118# Check libraries
119if(FUCHSIA)
120set(LIBUNWIND_HAS_DL_LIB NO)
121set(LIBUNWIND_HAS_PTHREAD_LIB NO)
122else()
123check_library_exists(dl dladdr "" LIBUNWIND_HAS_DL_LIB)
124check_library_exists(pthread pthread_once "" LIBUNWIND_HAS_PTHREAD_LIB)
125endif()
126
127if(HAIKU)
128check_library_exists(bsd dl_iterate_phdr "" LIBUNWIND_HAS_BSD_LIB)
129endif()
130