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