1include(CMakePushCheckState) 2include(CheckLibraryExists) 3include(CheckCCompilerFlag) 4include(CheckCXXCompilerFlag) 5include(CheckCSourceCompiles) 6 7check_library_exists(c fopen "" LIBCXXABI_HAS_C_LIB) 8if (NOT LIBCXXABI_USE_COMPILER_RT) 9 if (ANDROID) 10 check_library_exists(gcc __gcc_personality_v0 "" LIBCXXABI_HAS_GCC_LIB) 11 else () 12 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXXABI_HAS_GCC_S_LIB) 13 check_library_exists(gcc __aeabi_uldivmod "" LIBCXXABI_HAS_GCC_LIB) 14 endif () 15endif () 16 17# libc++abi is using -nostdlib++ at the link step when available, 18# otherwise -nodefaultlibs is used. We want all our checks to also 19# use one of these options, otherwise we may end up with an inconsistency between 20# the flags we think we require during configuration (if the checks are 21# performed without -nodefaultlibs) and the flags that are actually 22# required during compilation (which has the -nodefaultlibs). libc is 23# required for the link to go through. We remove sanitizers from the 24# configuration checks to avoid spurious link errors. 25 26check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG) 27if (CXX_SUPPORTS_NOSTDLIBXX_FLAG) 28 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++") 29else() 30 check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG) 31 if (C_SUPPORTS_NODEFAULTLIBS_FLAG) 32 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs") 33 endif() 34endif() 35 36# Only link against compiler-rt manually if we use -nodefaultlibs, since 37# otherwise the compiler will do the right thing on its own. 38if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG) 39 if (LIBCXXABI_HAS_C_LIB) 40 list(APPEND CMAKE_REQUIRED_LIBRARIES c) 41 endif () 42 if (LIBCXXABI_USE_COMPILER_RT) 43 include(HandleCompilerRT) 44 find_compiler_rt_library(builtins LIBCXXABI_BUILTINS_LIBRARY 45 FLAGS "${LIBCXXABI_COMPILE_FLAGS}") 46 if (LIBCXXABI_BUILTINS_LIBRARY) 47 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXXABI_BUILTINS_LIBRARY}") 48 else() 49 message(WARNING "Could not find builtins library from libc++abi") 50 endif() 51 else () 52 if (LIBCXXABI_HAS_GCC_S_LIB) 53 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s) 54 endif () 55 if (LIBCXXABI_HAS_GCC_LIB) 56 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc) 57 endif () 58 endif () 59 if (MINGW) 60 # Mingw64 requires quite a few "C" runtime libraries in order for basic 61 # programs to link successfully with -nodefaultlibs. 62 if (LIBCXXABI_USE_COMPILER_RT) 63 set(MINGW_RUNTIME ${LIBCXXABI_BUILTINS_LIBRARY}) 64 else () 65 set(MINGW_RUNTIME gcc_s gcc) 66 endif() 67 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi32 68 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME} 69 moldname mingwex msvcrt) 70 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES}) 71 endif() 72endif() 73 74if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG) 75 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) 76 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") 77 endif () 78 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) 79 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0") 80 endif () 81endif () 82 83# Check compiler pragmas 84if(CMAKE_CXX_COMPILER_ID MATCHES "Clang") 85 cmake_push_check_state() 86 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas") 87 check_c_source_compiles(" 88#pragma comment(lib, \"c\") 89int main(void) { return 0; } 90" C_SUPPORTS_COMMENT_LIB_PRAGMA) 91 cmake_pop_check_state() 92endif() 93 94# Check compiler flags 95check_cxx_compiler_flag(-nostdinc++ CXX_SUPPORTS_NOSTDINCXX_FLAG) 96 97# Check libraries 98if(FUCHSIA) 99 set(LIBCXXABI_HAS_DL_LIB NO) 100 set(LIBCXXABI_HAS_PTHREAD_LIB NO) 101 check_library_exists(c __cxa_thread_atexit_impl "" 102 LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL) 103elseif(ANDROID) 104 set(LIBCXXABI_HAS_DL_LIB YES) 105 set(LIBCXXABI_HAS_PTHREAD_LIB NO) 106 check_library_exists(c __cxa_thread_atexit_impl "" 107 LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL) 108else() 109 check_library_exists(dl dladdr "" LIBCXXABI_HAS_DL_LIB) 110 check_library_exists(pthread pthread_once "" LIBCXXABI_HAS_PTHREAD_LIB) 111 check_library_exists(c __cxa_thread_atexit_impl "" 112 LIBCXXABI_HAS_CXA_THREAD_ATEXIT_IMPL) 113endif() 114