blob: d3b9fa466ed62696cc04f11b7b04c48e48a2edb5 [file] [log] [blame]
Vasileios Kalintirisd5ff3202016-02-11 12:43:04 +00001INCLUDE(CheckCXXSourceCompiles)
2
3# Sometimes linking against libatomic is required for atomic ops, if
4# the platform doesn't support lock-free atomics.
5#
6# We could modify LLVM's CheckAtomic module and have it check for 64-bit
7# atomics instead. However, we would like to avoid careless uses of 64-bit
8# atomics inside LLVM over time on 32-bit platforms.
9
10function(check_cxx_atomics varname)
11 set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
12 set(CMAKE_REQUIRED_FLAGS "-std=c++11")
13 if (${LIBCXX_GCC_TOOLCHAIN})
14 set(CMAKE_REQUIRED_FLAGS "-std=c++11 --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}")
15 endif()
16 check_cxx_source_compiles("
17#include <cstdint>
18#include <atomic>
19std::atomic<uintptr_t> x;
20std::atomic<uintmax_t> y;
21int main() {
22 return x + y;
23}
24" ${varname})
25 set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
26endfunction(check_cxx_atomics)
27
28check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB)
29# If not, check if the library exists, and atomics work with it.
30if(NOT LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB)
31 check_library_exists(atomic __atomic_fetch_add_8 "" HAVE_LIBATOMIC)
32 if(HAVE_LIBATOMIC)
33 list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
34 check_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB)
35 if (NOT HAVE_CXX_ATOMICS_WITH_LIB)
36 message(FATAL_ERROR "Host compiler must support std::atomic!")
37 endif()
38 else()
39 message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.")
40 endif()
41endif()