|
| 1 | +# - Try to find if atomics need -latomic linking |
| 2 | +# Once done this will define |
| 3 | +# HAVE_CXX_ATOMICS_WITHOUT_LIB - Wether atomic types work without -latomic |
| 4 | +# HAVE_CXX_ATOMICS64_WITHOUT_LIB - Wether 64 bit atomic types work without -latomic |
| 5 | + |
| 6 | +INCLUDE(CheckCXXSourceCompiles) |
| 7 | +INCLUDE(CheckLibraryExists) |
| 8 | + |
| 9 | +# Sometimes linking against libatomic is required for atomic ops, if |
| 10 | +# the platform doesn't support lock-free atomics. |
| 11 | + |
| 12 | +function(check_working_cxx_atomics varname) |
| 13 | +set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 14 | +set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") |
| 15 | +CHECK_CXX_SOURCE_COMPILES(" |
| 16 | +#include <atomic> |
| 17 | +std::atomic<int> x; |
| 18 | +int main() { |
| 19 | +return std::atomic_is_lock_free(&x); |
| 20 | +} |
| 21 | +" ${varname}) |
| 22 | +set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 23 | +endfunction(check_working_cxx_atomics) |
| 24 | + |
| 25 | +function(check_working_cxx_atomics64 varname) |
| 26 | +set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 27 | +set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}") |
| 28 | +CHECK_CXX_SOURCE_COMPILES(" |
| 29 | +#include <atomic> |
| 30 | +#include <cstdint> |
| 31 | +std::atomic<uint64_t> x (0); |
| 32 | +int main() { |
| 33 | +uint64_t i = x.load(std::memory_order_relaxed); |
| 34 | +return std::atomic_is_lock_free(&x); |
| 35 | +} |
| 36 | +" ${varname}) |
| 37 | +set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 38 | +endfunction(check_working_cxx_atomics64) |
| 39 | + |
| 40 | +# Check for atomic operations. |
| 41 | +if(MSVC) |
| 42 | + # This isn't necessary on MSVC. |
| 43 | + set(HAVE_CXX_ATOMICS_WITHOUT_LIB True) |
| 44 | +else() |
| 45 | + # First check if atomics work without the library. |
| 46 | + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 47 | +endif() |
| 48 | + |
| 49 | +# If not, check if the library exists, and atomics work with it. |
| 50 | +if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 51 | + check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC) |
| 52 | + if(NOT HAVE_LIBATOMIC) |
| 53 | + message(STATUS "Host compiler appears to require libatomic, but cannot locate it.") |
| 54 | + endif() |
| 55 | + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") |
| 56 | + check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB) |
| 57 | + if (NOT HAVE_CXX_ATOMICS_WITH_LIB) |
| 58 | + message(FATAL_ERROR "Host compiler must support std::atomic!") |
| 59 | + endif() |
| 60 | +endif() |
| 61 | + |
| 62 | +# Check for 64 bit atomic operations. |
| 63 | +if(MSVC) |
| 64 | + set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True) |
| 65 | +else() |
| 66 | + check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB) |
| 67 | +endif() |
| 68 | + |
| 69 | +# If not, check if the library exists, and atomics work with it. |
| 70 | +if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) |
| 71 | + check_library_exists(atomic __atomic_load_8 "" HAVE_LIBATOMIC64) |
| 72 | + if(NOT HAVE_LIBATOMIC64) |
| 73 | + message(STATUS "Host compiler appears to require libatomic, but cannot locate it.") |
| 74 | + endif() |
| 75 | + list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") |
| 76 | + check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB) |
| 77 | + if (NOT HAVE_CXX_ATOMICS64_WITH_LIB) |
| 78 | + message(FATAL_ERROR "Host compiler must support std::atomic!") |
| 79 | + endif() |
| 80 | +endif() |
| 81 | + |
0 commit comments