Warning, /frameworks/extra-cmake-modules/modules/CheckAtomic.cmake is written in an unsupported language. File is not indexed.

0001 # SPDX-FileCopyrightText: 2003-2018 University of Illinois at Urbana-Champaign.
0002 #
0003 # SPDX-License-Identifier: BSD-3-Clause
0004 
0005 #[=======================================================================[.rst:
0006 CheckAtomic
0007 -----------
0008 
0009 Check if the compiler supports std:atomic out of the box or if libatomic is
0010 needed for atomic support. If it is needed libatomicis added to
0011 ``CMAKE_REQUIRED_LIBRARIES``. So after running CheckAtomic you can use
0012 std:atomic.
0013 
0014 Since 5.75.0.
0015 #]=======================================================================]
0016 
0017 include(CheckCXXSourceCompiles)
0018 include(CheckLibraryExists)
0019 
0020 # Sometimes linking against libatomic is required for atomic ops, if
0021 # the platform doesn't support lock-free atomics.
0022 
0023 function(check_working_cxx_atomics varname)
0024   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
0025   set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
0026   check_cxx_source_compiles("
0027   #include <atomic>
0028   std::atomic<int> x;
0029   std::atomic<short> y;
0030   std::atomic<char> z;
0031   int main() {
0032     ++z;
0033     ++y;
0034     return ++x;
0035   }
0036   " ${varname})
0037   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
0038 endfunction()
0039 
0040 function(check_working_cxx_atomics64 varname)
0041   set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
0042   set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
0043   check_cxx_source_compiles("
0044   #include <atomic>
0045   #include <cstdint>
0046   std::atomic<uint64_t> x (0);
0047   int main() {
0048     uint64_t i = x.load(std::memory_order_relaxed);
0049     return 0;
0050   }
0051   " ${varname})
0052   set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
0053 endfunction()
0054 
0055 # Check for (non-64-bit) atomic operations.
0056 if(MSVC)
0057   set(HAVE_CXX_ATOMICS_WITHOUT_LIB True)
0058 elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE)
0059   # First check if atomics work without the library.
0060   check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
0061   # If not, check if the library exists, and atomics work with it.
0062   if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
0063     check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
0064     if(HAVE_LIBATOMIC)
0065       list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
0066       check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
0067       if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
0068         message(FATAL_ERROR "Host compiler must support std::atomic!")
0069       endif()
0070     else()
0071       message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
0072     endif()
0073   endif()
0074 endif()
0075 
0076 # Check for 64 bit atomic operations.
0077 if(MSVC)
0078   set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
0079 elseif(LLVM_COMPILER_IS_GCC_COMPATIBLE)
0080   # First check if atomics work without the library.
0081   check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
0082   # If not, check if the library exists, and atomics work with it.
0083   if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
0084     check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
0085     if(HAVE_CXX_LIBATOMICS64)
0086       list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
0087       check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
0088       if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
0089         message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
0090       endif()
0091     else()
0092       message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
0093     endif()
0094   endif()
0095 endif()