1 # atomic builtins are required for threading support.
3 INCLUDE(CheckCXXSourceCompiles)
4 INCLUDE(CheckLibraryExists)
6 # Sometimes linking against libatomic is required for atomic ops, if
7 # the platform doesn't support lock-free atomics.
9 function(check_working_cxx_atomics varname)
10 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
11 set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++17")
12 check_cxx_source_compiles("
23 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
24 endfunction(check_working_cxx_atomics)
26 function(check_working_cxx_atomics64 varname)
27 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
28 set(CMAKE_REQUIRED_FLAGS "-std=c++17 ${CMAKE_REQUIRED_FLAGS}")
29 check_cxx_source_compiles("
32 std::atomic<uint64_t> x (0);
34 uint64_t i = x.load(std::memory_order_relaxed);
39 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
40 endfunction(check_working_cxx_atomics64)
43 # Check for (non-64-bit) atomic operations.
45 set(HAVE_CXX_ATOMICS_WITHOUT_LIB True)
47 # First check if atomics work without the library.
48 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB)
49 # If not, check if the library exists, and atomics work with it.
50 if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
51 check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC)
53 list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
54 check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
55 if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
56 message(FATAL_ERROR "Host compiler must support std::atomic!")
59 message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
64 # Check for 64 bit atomic operations.
66 set(HAVE_CXX_ATOMICS64_WITHOUT_LIB True)
68 # First check if atomics work without the library.
69 check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB)
70 # If not, check if the library exists, and atomics work with it.
71 if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB)
72 check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64)
73 if(HAVE_CXX_LIBATOMICS64)
74 list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
75 check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB)
76 if (NOT HAVE_CXX_ATOMICS64_WITH_LIB)
77 message(FATAL_ERROR "Host compiler must support 64-bit std::atomic!")
80 message(FATAL_ERROR "Host compiler appears to require libatomic for 64-bit operations, but cannot find it.")
85 if(HAVE_CXX_ATOMICS_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB)
86 target_link_libraries(openttd_lib atomic)