1 include(CMakePushCheckState)
2 include(CheckLibraryExists)
3 include(LLVMCheckCompilerLinkerFlag)
4 include(CheckCCompilerFlag)
5 include(CheckCXXCompilerFlag)
6 include(CheckCSourceCompiles)
8 # The compiler driver may be implicitly trying to link against libunwind.
9 # This is normally ok (libcxx relies on an unwinder), but if libunwind is
10 # built in the same cmake invocation as libcxx and we've got
11 # LIBCXXABI_USE_LLVM_UNWINDER set, we'd be linking against the just-built
12 # libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
13 # built libunwind isn't installed yet). For those cases, it'd be good to
14 # link with --uwnindlib=none. Check if that option works.
15 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
17 if (NOT LIBCXX_USE_COMPILER_RT)
18 if(WIN32 AND NOT MINGW)
19 set(LIBCXX_HAS_GCC_S_LIB NO)
22 check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB)
24 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB)
29 # libc++ 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 check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
39 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
40 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
42 check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
43 if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
44 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
48 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
49 if (LIBCXX_USE_COMPILER_RT)
50 include(HandleCompilerRT)
51 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
52 FLAGS ${LIBCXX_COMPILE_FLAGS})
53 if (LIBCXX_BUILTINS_LIBRARY)
54 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
56 message(WARNING "Could not find builtins library from libc++")
58 elseif (LIBCXX_HAS_GCC_LIB)
59 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
60 elseif (LIBCXX_HAS_GCC_S_LIB)
61 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
64 # Mingw64 requires quite a few "C" runtime libraries in order for basic
65 # programs to link successfully with -nodefaultlibs.
66 if (LIBCXX_USE_COMPILER_RT)
67 set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY})
69 set(MINGW_RUNTIME gcc_s gcc)
71 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
72 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
73 moldname mingwex msvcrt)
74 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
76 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
77 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
79 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
80 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
84 # Check compiler pragmas
85 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
86 cmake_push_check_state()
87 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
88 check_c_source_compiles("
89 #pragma comment(lib, \"c\")
90 int main(void) { return 0; }
91 " C_SUPPORTS_COMMENT_LIB_PRAGMA)
92 cmake_pop_check_state()
96 if(WIN32 AND NOT MINGW)
97 # TODO(compnerd) do we want to support an emulation layer that allows for the
98 # use of pthread-win32 or similar libraries to emulate pthreads on Windows?
99 set(LIBCXX_HAS_PTHREAD_LIB NO)
100 set(LIBCXX_HAS_RT_LIB NO)
101 set(LIBCXX_HAS_ATOMIC_LIB NO)
103 set(LIBCXX_HAS_PTHREAD_LIB NO)
104 set(LIBCXX_HAS_RT_LIB NO)
105 set(LIBCXX_HAS_ATOMIC_LIB NO)
107 set(LIBCXX_HAS_PTHREAD_LIB NO)
108 set(LIBCXX_HAS_RT_LIB NO)
109 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
111 set(LIBCXX_HAS_PTHREAD_LIB NO)
112 set(LIBCXX_HAS_RT_LIB NO)
113 set(LIBCXX_HAS_ATOMIC_LIB NO)
115 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
116 check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)
117 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)