Warning, /graphics/krita/cmake/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, ``HAVE_CXX_ATOMICS_WITH_LIB``
0011 or ``HAVE_CXX_ATOMICS64_WITH_LIB`` set to ``ON``.
0012 
0013 Since 5.75.0.
0014 #]=======================================================================]
0015 
0016 include(CheckCXXSourceCompiles)
0017 include(CheckLibraryExists)
0018 
0019 # Sometimes linking against libatomic is required for atomic ops, if
0020 # the platform doesn't support lock-free atomics.
0021 
0022 function(check_working_cxx_atomics varname)
0023         set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
0024         set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
0025         check_cxx_source_compiles("
0026         #include <atomic>
0027         std::atomic<int> x;
0028         std::atomic<short> y;
0029         std::atomic<char> z;
0030         int main() {
0031                 ++z;
0032                 ++y;
0033                 return ++x;
0034         }
0035         " ${varname})
0036         set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
0037 endfunction()
0038 
0039 function(check_working_cxx_atomics64 varname)
0040         set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
0041         set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}")
0042         check_cxx_source_compiles("
0043         #include <atomic>
0044         #include <cstdint>
0045         std::atomic<uint64_t> x (0);
0046         int main() {
0047                 uint64_t i = x.load(std::memory_order_relaxed);
0048                 x.is_lock_free();
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 else()
0059         # First check if atomics work without the library.
0060         if (CMAKE_COMPILER_IS_GNUCXX
0061                 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang"
0062                 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"
0063                 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
0064                 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
0065         endif()
0066         # If not, check if the library exists, and atomics work with it.
0067         if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
0068                 check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
0069                 if(HAVE_LIBATOMIC)
0070                         list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
0071                         check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
0072                         if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
0073                                 message(FATAL_ERROR "Host compiler must support std::atomic!")
0074                         endif()
0075                 else()
0076                         message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
0077                 endif()
0078         endif()
0079 endif()
0080 
0081 # Check for 64 bit atomic operations.
0082 if(MSVC)
0083         set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
0084 else()
0085         # First check if atomics work without the library.
0086         if (CMAKE_COMPILER_IS_GNUCXX
0087                 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "AppleClang"
0088                 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang"
0089                 OR "${CMAKE_CXX_COMPILER_ID}" MATCHES "Intel")
0090                 check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
0091         endif()
0092         # If not, check if the library exists, and atomics work with it.
0093         if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
0094                 check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
0095                 if(HAVE_CXX_LIBATOMICS64)
0096                         list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
0097                         check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
0098                         if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
0099                                 message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
0100                         endif()
0101                 else()
0102                         message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
0103                 endif()
0104         endif()
0105 endif()