| 1 |
# SPDX-FileCopyrightText: 2003-2018 University of Illinois at Urbana-Champaign. |
| 2 |
# |
| 3 |
# SPDX-License-Identifier: BSD-3-Clause |
| 4 |
|
| 5 |
#[=======================================================================[.rst: |
| 6 |
CheckAtomic |
| 7 |
----------- |
| 8 |
|
| 9 |
Check if the compiler supports std:atomic out of the box or if libatomic is |
| 10 |
needed for atomic support. If it is needed, ``HAVE_CXX_ATOMICS_WITH_LIB`` |
| 11 |
or ``HAVE_CXX_ATOMICS64_WITH_LIB`` set to ``ON``. |
| 12 |
|
| 13 |
Since 5.75.0. |
| 14 |
#]=======================================================================] |
| 15 |
|
| 16 |
include(CheckCXXSourceCompiles) |
| 17 |
include(CheckLibraryExists) |
| 18 |
|
| 19 |
# Sometimes linking against libatomic is required for atomic ops, if |
| 20 |
# the platform doesn't support lock-free atomics. |
| 21 |
|
| 22 |
function(check_working_cxx_atomics varname) |
| 23 |
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 24 |
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") |
| 25 |
check_cxx_source_compiles(" |
| 26 |
#include <atomic> |
| 27 |
std::atomic<int> x; |
| 28 |
std::atomic<short> y; |
| 29 |
std::atomic<char> z; |
| 30 |
int main() { |
| 31 |
++z; |
| 32 |
++y; |
| 33 |
return ++x; |
| 34 |
} |
| 35 |
" ${varname}) |
| 36 |
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 37 |
endfunction() |
| 38 |
|
| 39 |
function(check_working_cxx_atomics64 varname) |
| 40 |
set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) |
| 41 |
set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}") |
| 42 |
check_cxx_source_compiles(" |
| 43 |
#include <atomic> |
| 44 |
#include <cstdint> |
| 45 |
std::atomic<uint64_t> x (0); |
| 46 |
int main() { |
| 47 |
uint64_t i = x.load(std::memory_order_relaxed); |
| 48 |
x.is_lock_free(); |
| 49 |
return 0; |
| 50 |
} |
| 51 |
" ${varname}) |
| 52 |
set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) |
| 53 |
endfunction() |
| 54 |
|
| 55 |
# Check for (non-64-bit) atomic operations. |
| 56 |
if(MSVC) |
| 57 |
set(HAVE_CXX_ATOMICS_WITHOUT_LIB True) |
| 58 |
else() |
| 59 |
# First check if atomics work without the library. |
| 60 |
check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 61 |
# If not, check if the library exists, and atomics work with it. |
| 62 |
if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB) |
| 63 |
check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC) |
| 64 |
if(HAVE_LIBATOMIC) |
| 65 |
list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") |
| 66 |
check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB) |
| 67 |
if (NOT HAVE_CXX_ATOMICS_WITH_LIB) |
| 68 |
message(FATAL_ERROR "Host compiler must support std::atomic!") |
| 69 |
endif() |
| 70 |
else() |
| 71 |
message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") |
| 72 |
endif() |
| 73 |
endif() |
| 74 |
endif() |
| 75 |
|
| 76 |
# Check for 64 bit atomic operations. |
| 77 |
if(MSVC) |
| 78 |
set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True) |
| 79 |
else() |
| 80 |
# First check if atomics work without the library. |
| 81 |
check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB) |
| 82 |
# If not, check if the library exists, and atomics work with it. |
| 83 |
if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) |
| 84 |
check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64) |
| 85 |
if(HAVE_CXX_LIBATOMICS64) |
| 86 |
list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") |
| 87 |
check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB) |
| 88 |
if (NOT HAVE_CXX_ATOMICS64_WITH_LIB) |
| 89 |
message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!") |
| 90 |
endif() |
| 91 |
else() |
| 92 |
message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.") |
| 93 |
endif() |
| 94 |
endif() |
| 95 |
endif() |