134 lines · plain
1include(CMakePushCheckState)2include(CheckLibraryExists)3include(CheckSymbolExists)4include(LLVMCheckCompilerLinkerFlag)5include(CheckCCompilerFlag)6include(CheckCXXCompilerFlag)7include(CheckCSourceCompiles)8 9# The compiler driver may be implicitly trying to link against libunwind.10# This is normally ok (libcxx relies on an unwinder), but if libunwind is11# built in the same cmake invocation as libcxx and we've got12# LIBCXXABI_USE_LLVM_UNWINDER set, we'd be linking against the just-built13# libunwind (and the compiler implicit -lunwind wouldn't succeed as the newly14# built libunwind isn't installed yet). For those cases, it'd be good to15# link with --unwindlib=none. Check if that option works.16llvm_check_compiler_linker_flag(C "--unwindlib=none" CXX_SUPPORTS_UNWINDLIB_EQ_NONE_FLAG)17 18if (NOT LIBCXX_USE_COMPILER_RT)19 if(WIN32 AND NOT MINGW)20 set(LIBCXX_HAS_GCC_S_LIB NO)21 else()22 if(ANDROID)23 check_library_exists(gcc __gcc_personality_v0 "" LIBCXX_HAS_GCC_LIB)24 else()25 check_library_exists(gcc_s __gcc_personality_v0 "" LIBCXX_HAS_GCC_S_LIB)26 endif()27 endif()28endif()29 30check_cxx_compiler_flag(-nostdlibinc CXX_SUPPORTS_NOSTDLIBINC_FLAG)31check_cxx_compiler_flag(-nolibc CXX_SUPPORTS_NOLIBC_FLAG)32 33# libc++ is using -nostdlib++ at the link step when available,34# otherwise -nodefaultlibs is used. We want all our checks to also35# use one of these options, otherwise we may end up with an inconsistency between36# the flags we think we require during configuration (if the checks are37# performed without one of those options) and the flags that are actually38# required during compilation (which has the -nostdlib++ or -nodefaultlibs). libc is39# required for the link to go through. We remove sanitizers from the40# configuration checks to avoid spurious link errors.41 42check_cxx_compiler_flag(-nostdlib++ CXX_SUPPORTS_NOSTDLIBXX_FLAG)43if (CXX_SUPPORTS_NOSTDLIBXX_FLAG)44 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nostdlib++")45else()46 check_c_compiler_flag(-nodefaultlibs C_SUPPORTS_NODEFAULTLIBS_FLAG)47 if (C_SUPPORTS_NODEFAULTLIBS_FLAG)48 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs")49 endif()50endif()51 52# Only link against compiler-rt manually if we use -nodefaultlibs, since53# otherwise the compiler will do the right thing on its own.54if (NOT CXX_SUPPORTS_NOSTDLIBXX_FLAG AND C_SUPPORTS_NODEFAULTLIBS_FLAG)55 if (LIBCXX_USE_COMPILER_RT)56 include(HandleCompilerRT)57 find_compiler_rt_library(builtins LIBCXX_BUILTINS_LIBRARY58 FLAGS ${LIBCXX_COMPILE_FLAGS})59 if (LIBCXX_BUILTINS_LIBRARY)60 list(APPEND CMAKE_REQUIRED_LIBRARIES "${LIBCXX_BUILTINS_LIBRARY}")61 else()62 message(WARNING "Could not find builtins library from libc++")63 endif()64 elseif (LIBCXX_HAS_GCC_LIB)65 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc)66 elseif (LIBCXX_HAS_GCC_S_LIB)67 list(APPEND CMAKE_REQUIRED_LIBRARIES gcc_s)68 endif ()69 if (MINGW)70 # Mingw64 requires quite a few "C" runtime libraries in order for basic71 # programs to link successfully with -nodefaultlibs.72 if (LIBCXX_USE_COMPILER_RT)73 set(MINGW_RUNTIME ${LIBCXX_BUILTINS_LIBRARY})74 else ()75 set(MINGW_RUNTIME gcc_s gcc)76 endif()77 set(MINGW_LIBRARIES mingw32 ${MINGW_RUNTIME} moldname mingwex msvcrt advapi3278 shell32 user32 kernel32 mingw32 ${MINGW_RUNTIME}79 moldname mingwex msvcrt)80 list(APPEND CMAKE_REQUIRED_LIBRARIES ${MINGW_LIBRARIES})81 endif()82endif()83 84if (CXX_SUPPORTS_NOSTDLIBXX_FLAG OR C_SUPPORTS_NODEFAULTLIBS_FLAG)85 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)86 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")87 endif ()88 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)89 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fsanitize-coverage=0")90 endif ()91endif ()92 93# Check compiler pragmas94if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")95 cmake_push_check_state()96 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -Werror=unknown-pragmas")97 check_c_source_compiles("98#pragma comment(lib, \"c\")99int main(void) { return 0; }100" C_SUPPORTS_COMMENT_LIB_PRAGMA)101 cmake_pop_check_state()102endif()103 104check_symbol_exists(__PICOLIBC__ "string.h" PICOLIBC)105 106# Check libraries107if(WIN32 AND NOT MINGW)108 # TODO(compnerd) do we want to support an emulation layer that allows for the109 # use of pthread-win32 or similar libraries to emulate pthreads on Windows?110 set(LIBCXX_HAS_PTHREAD_LIB NO)111 set(LIBCXX_HAS_RT_LIB NO)112 set(LIBCXX_HAS_ATOMIC_LIB NO)113elseif(APPLE)114 set(LIBCXX_HAS_PTHREAD_LIB NO)115 set(LIBCXX_HAS_RT_LIB NO)116 set(LIBCXX_HAS_ATOMIC_LIB NO)117elseif(FUCHSIA)118 set(LIBCXX_HAS_PTHREAD_LIB NO)119 set(LIBCXX_HAS_RT_LIB NO)120 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)121elseif(ANDROID)122 set(LIBCXX_HAS_PTHREAD_LIB NO)123 set(LIBCXX_HAS_RT_LIB NO)124 set(LIBCXX_HAS_ATOMIC_LIB NO)125elseif(PICOLIBC)126 set(LIBCXX_HAS_PTHREAD_LIB NO)127 set(LIBCXX_HAS_RT_LIB NO)128 set(LIBCXX_HAS_ATOMIC_LIB NO)129else()130 check_library_exists(pthread pthread_create "" LIBCXX_HAS_PTHREAD_LIB)131 check_library_exists(rt clock_gettime "" LIBCXX_HAS_RT_LIB)132 check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)133endif()134