CheckAtomic.cmake 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # atomic builtins are required for threading support.
  2. INCLUDE(CheckCXXSourceCompiles)
  3. INCLUDE(CheckLibraryExists)
  4. # Sometimes linking against libatomic is required for atomic ops, if
  5. # the platform doesn't support lock-free atomics.
  6. function(check_working_cxx_atomics varname)
  7. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  8. set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
  9. CHECK_CXX_SOURCE_COMPILES("
  10. #include <atomic>
  11. std::atomic<int> x;
  12. int main() {
  13. return x;
  14. }
  15. " ${varname})
  16. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  17. endfunction(check_working_cxx_atomics)
  18. function(check_working_cxx_atomics64 varname)
  19. set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
  20. set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
  21. CHECK_CXX_SOURCE_COMPILES("
  22. #include <atomic>
  23. #include <cstdint>
  24. std::atomic<uint64_t> x (0);
  25. int main() {
  26. uint64_t i = x.load(std::memory_order_relaxed);
  27. return 0;
  28. }
  29. " ${varname})
  30. set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
  31. endfunction(check_working_cxx_atomics64)
  32. # This isn't necessary on MSVC, so avoid command-line switch annoyance
  33. # by only running on GCC-like hosts.
  34. if (LLVM_COMPILER_IS_GCC_COMPATIBLE)
  35. # First check if atomics work without the library.
  36. check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
  37. # If not, check if the library exists, and atomics work with it.
  38. if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
  39. check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
  40. if( HAVE_LIBATOMIC )
  41. list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
  42. check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
  43. if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
  44. message(FATAL_ERROR "Host compiler must support std::atomic!")
  45. endif()
  46. else()
  47. message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
  48. endif()
  49. endif()
  50. endif()
  51. # Check for 64 bit atomic operations.
  52. if(MSVC)
  53. set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
  54. else()
  55. check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
  56. endif()
  57. # If not, check if the library exists, and atomics work with it.
  58. if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
  59. check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
  60. if(HAVE_CXX_LIBATOMICS64)
  61. list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
  62. check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
  63. if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
  64. message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
  65. endif()
  66. else()
  67. message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
  68. endif()
  69. endif()
  70. ## TODO: This define is only used for the legacy atomic operations in
  71. ## llvm's Atomic.h, which should be replaced. Other code simply
  72. ## assumes C++11 <atomic> works.
  73. CHECK_CXX_SOURCE_COMPILES("
  74. #ifdef _MSC_VER
  75. #include <windows.h>
  76. #endif
  77. int main() {
  78. #ifdef _MSC_VER
  79. volatile LONG val = 1;
  80. MemoryBarrier();
  81. InterlockedCompareExchange(&val, 0, 1);
  82. InterlockedIncrement(&val);
  83. InterlockedDecrement(&val);
  84. #else
  85. volatile unsigned long val = 1;
  86. __sync_synchronize();
  87. __sync_val_compare_and_swap(&val, 1, 0);
  88. __sync_add_and_fetch(&val, 1);
  89. __sync_sub_and_fetch(&val, 1);
  90. #endif
  91. return 0;
  92. }
  93. " LLVM_HAS_ATOMICS)
  94. if( NOT LLVM_HAS_ATOMICS )
  95. message(STATUS "Warning: LLVM will be built thread-unsafe because atomic builtins are missing")
  96. endif()