1include(CMakePushCheckState) 2include(CheckLibraryExists) 3include(CheckCCompilerFlag) 4include(CheckCXXCompilerFlag) 5include(CheckCSourceCompiles) 6 7if(WIN32 AND NOT MINGW) 8 # NOTE(compnerd) this is technically a lie, there is msvcrt, but for now, lets 9 # let the default linking take care of that. 10 set(LIBCXX_HAS_C_LIB NO) 11else() 12 check_library_exists(c fopen "" LIBCXX_HAS_C_LIB) 13endif() 14 15if (NOT LIBCXX_USE_COMPILER_RT) 16 if(WIN32 AND NOT MINGW) 17 set(LIBCXX_HAS_GCC_S_LIB NO) 18 else() 19 if(ANDROID) 20 check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB) 21 else() 22 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB) 23 endif() 24 endif() 25endif() 26 27# libc++ is built with -nodefaultlibs, so we want all our checks to also 28# use this option, otherwise we may end up with an inconsistency between 29# the flags we think we require during configuration (if the checks are 30# performed without -nodefaultlibs) and the flags that are actually 31# required during compilation (which has the -nodefaultlibs). libc is 32# required for the link to go through. We remove sanitizers from the 33# configuration checks to avoid spurious link errors. 34check_c_compiler_flag(-nodefaultlibs LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) 35if (LIBCXX_SUPPORTS_NODEFAULTLIBS_FLAG) 36 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs") 37 if (LIBCXX_HAS_C_LIB) 38 list(APPEND CMAKE_REQUIRED_LIBRARIES c) 39 endif () 40 if (LIBCXX_USE_COMPILER_RT) 41 list(APPEND CMAKE_REQUIRED_FLAGS -rtlib=compiler-rt) 42 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY) 43 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}") 44 elseif (LIBCXX_HAS_GCC_LIB) 45 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc) 46 elseif (LIBCXX_HAS_GCC_S_LIB) 47 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s) 48 endif () 49 if (MINGW) 50 # Mingw64 requires quite a few "C" runtime libraries in order for basic 51 # programs to link successfully with -nodefaultlibs. 52 if (LIBCXX_USE_COMPILER_RT) 53 set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY}) 54 else () 55 set(MINGW_RUNTIME gcc_s gcc) 56 endif() 57 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32 58 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME} 59 moldname mingwex msvcrt) 60 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES}) 61 endif() 62 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) 63 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") 64 endif () 65 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) 66 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") 67 endif () 68endif () 69 70# Check compiler pragmas 71if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 72 cmake_push_check_state() 73 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas") 74 check_c_source_compiles(" 75#pragma comment(lib, \"c\") 76int main() { return 0; } 77" LIBCXX_HAS_COMMENT_LIB_PRAGMA) 78 cmake_pop_check_state() 79endif() 80 81# Check libraries 82if(WIN32 AND NOT MINGW) 83 # TODO(compnerd) do we want to support an emulation layer that allows for the 84 # use of pthread-win32 or similar libraries to emulate pthreads on Windows? 85 set(LIBCXX_HAS_PTHREAD_LIB NO) 86 set(LIBCXX_HAS_M_LIB NO) 87 set(LIBCXX_HAS_RT_LIB NO) 88 set(LIBCXX_HAS_SYSTEM_LIB NO) 89 set(LIBCXX_HAS_ATOMIC_LIB NO) 90elseif(APPLE) 91 check_library_exists(System write "" LIBCXX_HAS_SYSTEM_LIB) 92 set(LIBCXX_HAS_PTHREAD_LIB NO) 93 set(LIBCXX_HAS_M_LIB NO) 94 set(LIBCXX_HAS_RT_LIB NO) 95 set(LIBCXX_HAS_ATOMIC_LIB NO) 96elseif(FUCHSIA) 97 set(LIBCXX_HAS_M_LIB NO) 98 set(LIBCXX_HAS_PTHREAD_LIB NO) 99 set(LIBCXX_HAS_RT_LIB NO) 100 set(LIBCXX_HAS_SYSTEM_LIB NO) 101 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) 102else() 103 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB) 104 check_library_exists(m ccos "" LIBCXX_HAS_M_LIB) 105 check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB) 106 set(LIBCXX_HAS_SYSTEM_LIB NO) 107 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) 108endif() 109