1+ #
2+ # Copyright 2013-2021 Software Radio Systems Limited
3+ #
4+ # By using this file, you agree to the terms and conditions set
5+ # forth in the LICENSE file which can be found at the top level of
6+ # the distribution.
7+ #
8+
9+ # Adopted from https://github.com/pothosware/SoapyRTLSDR
10+ # Copyright: 2015, Charles J. Cliffe
11+ # License: MIT
12+
13+ # - Try to find if atomics need -latomic linking
14+ # Once done this will define
15+ # HAVE_CXX_ATOMICS_WITHOUT_LIB - Wether atomic types work without -latomic
16+ # HAVE_CXX_ATOMICS64_WITHOUT_LIB - Wether 64 bit atomic types work without -latomic
17+
18+ INCLUDE (CheckCXXSourceCompiles)
19+ INCLUDE (CheckLibraryExists)
20+
21+ # Sometimes linking against libatomic is required for atomic ops, if
22+ # the platform doesn't support lock-free atomics.
23+
24+ function (check_working_cxx_atomics varname)
25+ set (OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} )
26+ set (CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11" )
27+ CHECK_CXX_SOURCE_COMPILES("
28+ #include <atomic>
29+ std::atomic<int> x;
30+ int main() {
31+ return std::atomic_is_lock_free(&x);
32+ }
33+ " ${varname} )
34+ set (CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS} )
35+ endfunction (check_working_cxx_atomics)
36+
37+ function (check_working_cxx_atomics64 varname)
38+ set (OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS} )
39+ set (CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS} " )
40+ CHECK_CXX_SOURCE_COMPILES("
41+ #include <atomic>
42+ #include <cstdint>
43+ std::atomic<uint64_t> x (0);
44+ int main() {
45+ uint64_t i = x.load(std::memory_order_relaxed);
46+ return std::atomic_is_lock_free(&x);
47+ }
48+ " ${varname} )
49+ set (CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS} )
50+ endfunction (check_working_cxx_atomics64)
51+
52+ # Check for atomic operations.
53+ if (MSVC )
54+ # This isn't necessary on MSVC.
55+ set (HAVE_CXX_ATOMICS_WITHOUT_LIB True )
56+ else ()
57+ # First check if atomics work without the library.
58+ check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
59+ endif ()
60+
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 (NOT HAVE_LIBATOMIC)
65+ message (STATUS "Host compiler appears to require libatomic, but cannot locate it." )
66+ endif ()
67+ list (APPEND CMAKE_REQUIRED_LIBRARIES "atomic" )
68+ check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
69+ if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
70+ message (FATAL_ERROR "Host compiler must support std::atomic!" )
71+ endif ()
72+ endif ()
73+
74+ # Check for 64 bit atomic operations.
75+ if (MSVC )
76+ set (HAVE_CXX_ATOMICS64_WITHOUT_LIB True )
77+ else ()
78+ check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
79+ endif ()
80+
81+ # If not, check if the library exists, and atomics work with it.
82+ if (NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
83+ check_library_exists(atomic __atomic_load_8 "" HAVE_LIBATOMIC64)
84+ if (NOT HAVE_LIBATOMIC64)
85+ message (STATUS "Host compiler appears to require libatomic, but cannot locate it." )
86+ endif ()
87+ list (APPEND CMAKE_REQUIRED_LIBRARIES "atomic" )
88+ check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
89+ if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
90+ message (FATAL_ERROR "Host compiler must support std::atomic!" )
91+ endif ()
92+ endif ()
0 commit comments