1include(CheckLibraryExists) 2include(CheckCCompilerFlag) 3include(CheckCXXCompilerFlag) 4 5if(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) 9else() 10 check_library_exists(c fopen "" LIBCXX_HAS_C_LIB) 11endif() 12 13if (NOT LIBCXX_USE_COMPILER_RT) 14 if(WIN32 AND NOT MINGW) 15 set(LIBCXX_HAS_GCC_S_LIB NO) 16 else() 17 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB) 18 endif() 19endif() 20 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. 28check_c_compiler_flag(-nodefaultlibs LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) 29if (LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) 30 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs") 31 if (LIBCXX_HAS_C_LIB) 32 list(APPEND CMAKE_REQUIRED_LIBRARIES c) 33 endif () 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) 40 endif () 41 if (MINGW) 42 # Mingw64 requires quite a few "C" runtime libraries in order for basic 43 # programs to link successfully with -nodefaultlibs. 44 if (LIBCXX_USE_COMPILER_RT) 45 set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY}) 46 else () 47 set(MINGW_RUNTIME gcc_s gcc) 48 endif() 49 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32 50 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME} 51 moldname mingwex msvcrt) 52 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES}) 53 endif() 54 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) 55 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") 56 endif () 57 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) 58 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") 59 endif () 60endif () 61 62if(NOT WIN32 OR MINGW) 63 include(CheckLibcxxAtomic) 64endif() 65 66# Check compiler flags 67 68check_cxx_compiler_flag(/WX LIBCXX_HAS_WX_FLAG) 69check_cxx_compiler_flag(/WX- LIBCXX_HAS_NO_WX_FLAG) 70check_cxx_compiler_flag(/EHsc LIBCXX_HAS_EHSC_FLAG) 71check_cxx_compiler_flag(/EHs- LIBCXX_HAS_NO_EHS_FLAG) 72check_cxx_compiler_flag(/EHa- LIBCXX_HAS_NO_EHA_FLAG) 73check_cxx_compiler_flag(/GR- LIBCXX_HAS_NO_GR_FLAG) 74 75 76# Check libraries 77if(WIN32 AND NOT MINGW) 78 # TODO(compnerd) do we want to support an emulation layer that allows for the 79 # use of pthread-win32 or similar libraries to emulate pthreads on Windows? 80 set(LIBCXX_HAS_PTHREAD_LIB NO) 81 set(LIBCXX_HAS_M_LIB NO) 82 set(LIBCXX_HAS_RT_LIB NO) 83else() 84 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB) 85 check_library_exists(m ccos "" LIBCXX_HAS_M_LIB) 86 check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB) 87endif() 88