1 include(CheckLibraryExists)
2 include(CheckCCompilerFlag)
3 include(CheckCXXCompilerFlag)
5 if(WIN32 AND NOT MINGW)
6 # NOTE(compnerd) this is technically a lie, there is msvcrt, but for now, lets
7 # let the default linking take care of that.
8 set(LIBCXX_HAS_C_LIB NO)
10 check_library_exists(c fopen "" LIBCXX_HAS_C_LIB)
13 if (NOT LIBCXX_USE_COMPILER_RT)
14 if(WIN32 AND NOT MINGW)
15 set(LIBCXX_HAS_GCC_S_LIB NO)
17 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB)
21 # libc++ is built with -nodefaultlibs, so we want all our checks to also
22 # use this option, otherwise we may end up with an inconsistency between
23 # the flags we think we require during configuration (if the checks are
24 # performed without -nodefaultlibs) and the flags that are actually
25 # required during compilation (which has the -nodefaultlibs). libc is
26 # required for the link to go through. We remove sanitizers from the
27 # configuration checks to avoid spurious link errors.
28 check_c_compiler_flag(-nodefaultlibs LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG)
29 if (LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG)
30 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")
32 list(APPEND CMAKE_REQUIRED_LIBRARIES c)
34 if (LIBCXX_USE_COMPILER_RT)
35 list(APPEND CMAKE_REQUIRED_FLAGS -rtlib=compiler-rt)
36 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY)
37 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")
38 elseif (LIBCXX_HAS_GCC_S_LIB)
39 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)
42 # Mingw64 requires quite a few "C" runtime libraries in order for basic
43 # programs to link successfully with -nodefaultlibs.
44 list(APPEND CMAKE_REQUIRED_LIBRARIES mingw32 gcc gcc_eh mingwex msvcrt gcc)
46 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
47 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
49 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
50 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters")
54 if(NOT WIN32 OR MINGW)
55 include(CheckLibcxxAtomic)
58 # Check compiler flags
60 check_cxx_compiler_flag(/WX LIBCXX_HAS_WX_FLAG)
61 check_cxx_compiler_flag(/WX- LIBCXX_HAS_NO_WX_FLAG)
62 check_cxx_compiler_flag(/EHsc LIBCXX_HAS_EHSC_FLAG)
63 check_cxx_compiler_flag(/EHs- LIBCXX_HAS_NO_EHS_FLAG)
64 check_cxx_compiler_flag(/EHa- LIBCXX_HAS_NO_EHA_FLAG)
65 check_cxx_compiler_flag(/GR- LIBCXX_HAS_NO_GR_FLAG)
69 if(WIN32 AND NOT MINGW)
70 # TODO(compnerd) do we want to support an emulation layer that allows for the
71 # use of pthread-win32 or similar libraries to emulate pthreads on Windows?
72 set(LIBCXX_HAS_PTHREAD_LIB NO)
73 set(LIBCXX_HAS_M_LIB NO)
74 set(LIBCXX_HAS_RT_LIB NO)
76 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)
77 check_library_exists(m ccos "" LIBCXX_HAS_M_LIB)
78 check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)