1INCLUDE(CheckCXXSourceCompiles) 2 3# Sometimes linking against libatomic is required for atomic ops, if 4# the platform doesn't support lock-free atomics. 5# 6# We could modify LLVM's CheckAtomic module and have it check for 64-bit 7# atomics instead. However, we would like to avoid careless uses of 64-bit 8# atomics inside LLVM over time on 32-bit platforms. 9 10function(check_cxx_atomics varname) 11 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 12 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs -std=c++11 -nostdinc++ -isystem ${LIBCXX_SOURCE_DIR}/include") 13 if (${LIBCXX_GCC_TOOLCHAIN}) 14 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}") 15 endif() 16 if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize) 17 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all") 18 endif() 19 if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage) 20 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters") 21 endif() 22 check_cxx_source_compiles(" 23#include <cstdint> 24#include <atomic> 25std::atomic<uintptr_t> x; 26std::atomic<uintmax_t> y; 27int main() { 28 return x + y; 29} 30" ${varname}) 31 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 32endfunction(check_cxx_atomics) 33 34# Perform the check for 64bit atomics without libatomic. It may have been 35# added to the required libraries during in the configuration of LLVM, which 36# would cause the check for CXX atomics without libatomic to incorrectly pass. 37set(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) 38list(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "atomic") 39check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) 40set(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) 41 42check_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB) 43# If not, check if the library exists, and atomics work with it. 44if(NOT LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB) 45 if(LIBCXX_HAS_ATOMIC_LIB) 46 list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") 47 check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) 48 if (NOT LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB) 49 message(WARNING "Host compiler must support std::atomic!") 50 endif() 51 else() 52 message(WARNING "Host compiler appears to require libatomic, but cannot find it.") 53 endif() 54endif() 55