CheckLibcxxAtomic.cmake 2.4 KB

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