Debian Codegraph @

cmake/modules/CheckAtomic.cmake

raw
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 if (CMAKE_COMPILER_IS_GNUCXX
61 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang"
62 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"
63 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
64 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
65 endif()
66 # If not, check if the library exists, and atomics work with it.
67 if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
68 check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
69 if(HAVE_LIBATOMIC)
70 list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
71 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
72 if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
73 message(FATAL_ERROR "Host compiler must support std::atomic!")
74 endif()
75 else()
76 message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
77 endif()
78 endif()
79 endif()
80
81 # Check for 64 bit atomic operations.
82 if(MSVC)
83 set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
84 else()
85 # First check if atomics work without the library.
86 if (CMAKE_COMPILER_IS_GNUCXX
87 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang"
88 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"
89 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
90 check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
91 endif()
92 # If not, check if the library exists, and atomics work with it.
93 if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
94 check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
95 if(HAVE_CXX_LIBATOMICS64)
96 list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
97 check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
98 if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
99 message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
100 endif()
101 else()
102 message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
103 endif()
104 endif()
105 endif()