1 include(CMakePushCheckState)
2 include(CheckLibraryExists)
3 include(CheckSymbolExists)
4 include(LLVMCheckCompilerLinkerFlag)
5 include(CheckCCompilerFlag)
6 include(CheckCXXCompilerFlag)
7 include(CheckCSourceCompiles)
9 # The compiler driver may be implicitly trying to link against libunwind.
10 # This is normally ok (libcxx relies on an unwinder), but if libunwind is
11 # built in the same cmake invocation as libcxx and we've got
12 # LIBCXXABI_USE_LLVM_UNWINDER set, we'd be linking against the just-built
13 # libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly
14 # built libunwind isn't installed yet). For those cases, it'd be good to
15 # link with --unwindlib=none. Check if that option works.
16 llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)
18 if (NOT LIBCXX_USE_COMPILER_RT)
19 if(WIN32 AND NOT MINGW)
20 set(LIBCXX_HAS_GCC_S_LIB NO)
23 check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB)
25 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB)
30 check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)
31 check_cxx_compiler_flag(-nolibc CXX_SUPPORTS_NOLIBC_FLAG)
33 # libc++ is using -nostdlib++ at the link step when available,
34 # otherwise -nodefaultlibs is used. We want all our checks to also
35 # use one of these options, otherwise we may end up with an inconsistency between
36 # the flags we think we require during configuration (if the checks are
37 # performed without one of those options) and the flags that are actually
38 # required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is
39 # required for the link to go through. We remove sanitizers from the
40 # configuration checks to avoid spurious link errors.
42 check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)
43 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)
44 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")
46 check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)
47 if (C_SUPPORTS_NODEFAULTLIBS_FLAG)
48 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
52 # Only link against compiler-rt manually if we use -nodefaultlibs, since
53 # otherwise the compiler will do the right thing on its own.
54 if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)
55 if (LIBCXX_USE_COMPILER_RT)
56 include(HandleCompilerRT)
57 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY
58 FLAGS ${LIBCXX_COMPILE_FLAGS})
59 if (LIBCXX_BUILTINS_LIBRARY)
60 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
62 message(WARNING "Could not find builtins library from libc++")
64 elseif (LIBCXX_HAS_GCC_LIB)
65 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)
66 elseif (LIBCXX_HAS_GCC_S_LIB)
67 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
70 # Mingw64 requires quite a few "C" runtime libraries in order for basic
71 # programs to link successfully with -nodefaultlibs.
72 if (LIBCXX_USE_COMPILER_RT)
73 set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY})
75 set(MINGW_RUNTIME gcc_s gcc)
77 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32
78 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}
79 moldname mingwex msvcrt)
80 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})
84 if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)
85 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
86 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
88 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
89 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")
93 # Check compiler pragmas
94 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
95 cmake_push_check_state()
96 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")
97 check_c_source_compiles("
98 #pragma comment(lib, \"c\")
99 int main(void) { return 0; }
100 " C_SUPPORTS_COMMENT_LIB_PRAGMA)
101 cmake_pop_check_state()
104 check_symbol_exists(__PICOLIBC__ "string.h" PICOLIBC)
107 if(WIN32 AND NOT MINGW)
108 # TODO(compnerd) do we want to support an emulation layer that allows for the
109 # use of pthread-win32 or similar libraries to emulate pthreads on Windows?
110 set(LIBCXX_HAS_PTHREAD_LIB NO)
111 set(LIBCXX_HAS_RT_LIB NO)
112 set(LIBCXX_HAS_ATOMIC_LIB NO)
114 set(LIBCXX_HAS_PTHREAD_LIB NO)
115 set(LIBCXX_HAS_RT_LIB NO)
116 set(LIBCXX_HAS_ATOMIC_LIB NO)
118 set(LIBCXX_HAS_PTHREAD_LIB NO)
119 set(LIBCXX_HAS_RT_LIB NO)
120 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
122 set(LIBCXX_HAS_PTHREAD_LIB NO)
123 set(LIBCXX_HAS_RT_LIB NO)
124 set(LIBCXX_HAS_ATOMIC_LIB NO)
126 set(LIBCXX_HAS_PTHREAD_LIB NO)
127 set(LIBCXX_HAS_RT_LIB NO)
128 set(LIBCXX_HAS_ATOMIC_LIB NO)
130 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
131 check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)
132 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)