1 include(CMakePushCheckState)
2 include(CheckLibraryExists)
3 include(CheckCCompilerFlag)
4 include(CheckCXXCompilerFlag)
5 include(CheckCSourceCompiles)
7 check_library_exists(c fopen "" LIBCXXABI_HAS_C_LIB)
8 if (NOT LIBCXXABI_USE_COMPILER_RT)
10 check_library_exists(gcc __gcc_personality_v0 "" LIBCXXABI_HAS_GCC_LIB)
12 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXXABI_HAS_GCC_S_LIB)
13 check_library_exists(gcc __aeabi_uldivmod "" LIBCXXABI_HAS_GCC_LIB)
17 # libc++abi is using -nostdlib++ at the link step when available,
18 # otherwise -nodefaultlibs is used. We want all our checks to also
19 # use one of these options, otherwise we may end up with an inconsistency between
20 # the flags we think we require during configuration (if the checks are
21 # performed without -nodefaultlibs) and the flags that are actually
22 # required during compilation (which has the -nodefaultlibs). libc is
23 # required for the link to go through. We remove sanitizers from the
24 # configuration checks to avoid spurious link errors.
26 check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
27 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
28 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
30 check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
31 if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
32 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
36 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
37 if (LIBCXXABI_HAS_C_LIB)
38 list(APPEND CMAKE_REQUIRED_LIBRARIES c)
40 if (LIBCXXABI_USE_COMPILER_RT)
41 include(HandleCompilerRT)
42 find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY
43 FLAGS "${LIBCXXABI_COMPILE_FLAGS}")
44 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}")
46 if (LIBCXXABI_HAS_GCC_S_LIB)
47 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
49 if (LIBCXXABI_HAS_GCC_LIB)
50 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
54 # Mingw64 requires quite a few "C" runtime libraries in order for basic
55 # programs to link successfully with -nodefaultlibs.
56 if (LIBCXXABI_USE_COMPILER_RT)
57 set(MINGW_RUNTIME ${LIBCXXABI_BUILTINS_LIBRARY})
59 set(MINGW_RUNTIME gcc_s gcc)
61 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
62 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
63 moldname mingwex msvcrt)
64 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
66 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
67 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
69 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
70 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
74 # Check compiler pragmas
75 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
76 cmake_push_check_state()
77 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
78 check_c_source_compiles("
79 #pragma comment(lib, \"c\")
80 int main() { return 0; }
81 " C_SUPPORTS_COMMENT_LIB_PRAGMA)
82 cmake_pop_check_state()
85 # Check compiler flags
86 check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG)
90 set(LIBCXXABI_HAS_DL_LIB NO)
91 set(LIBCXXABI_HAS_PTHREAD_LIB NO)
92 check_library_exists(c __cxa_thread_atexit_impl ""
93 LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
94 set(LIBCXXABI_HAS_SYSTEM_LIB NO)
96 check_library_exists(dl dladdr "" LIBCXXABI_HAS_DL_LIB)
97 check_library_exists(pthread pthread_once "" LIBCXXABI_HAS_PTHREAD_LIB)
98 check_library_exists(c __cxa_thread_atexit_impl ""
99 LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL)
100 check_library_exists(System write "" LIBCXXABI_HAS_SYSTEM_LIB)