xref: /aosp_15_r20/external/libcxx/cmake/Modules/CheckLibcxxAtomic.cmake (revision 58b9f456b02922dfdb1fad8a988d5fd8765ecb80)
1*58b9f456SAndroid Build Coastguard WorkerINCLUDE(CheckCXXSourceCompiles)
2*58b9f456SAndroid Build Coastguard Worker
3*58b9f456SAndroid Build Coastguard Worker# Sometimes linking against libatomic is required for atomic ops, if
4*58b9f456SAndroid Build Coastguard Worker# the platform doesn't support lock-free atomics.
5*58b9f456SAndroid Build Coastguard Worker#
6*58b9f456SAndroid Build Coastguard Worker# We could modify LLVM's CheckAtomic module and have it check for 64-bit
7*58b9f456SAndroid Build Coastguard Worker# atomics instead. However, we would like to avoid careless uses of 64-bit
8*58b9f456SAndroid Build Coastguard Worker# atomics inside LLVM over time on 32-bit platforms.
9*58b9f456SAndroid Build Coastguard Worker
10*58b9f456SAndroid Build Coastguard Workerfunction(check_cxx_atomics varname)
11*58b9f456SAndroid Build Coastguard Worker  set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
12*58b9f456SAndroid Build Coastguard Worker  set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -nodefaultlibs -std=c++11 -nostdinc++ -isystem ${LIBCXX_SOURCE_DIR}/include")
13*58b9f456SAndroid Build Coastguard Worker  if (${LIBCXX_GCC_TOOLCHAIN})
14*58b9f456SAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} --gcc-toolchain=${LIBCXX_GCC_TOOLCHAIN}")
15*58b9f456SAndroid Build Coastguard Worker  endif()
16*58b9f456SAndroid Build Coastguard Worker  if (CMAKE_C_FLAGS MATCHES -fsanitize OR CMAKE_CXX_FLAGS MATCHES -fsanitize)
17*58b9f456SAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize=all")
18*58b9f456SAndroid Build Coastguard Worker  endif()
19*58b9f456SAndroid Build Coastguard Worker  if (CMAKE_C_FLAGS MATCHES -fsanitize-coverage OR CMAKE_CXX_FLAGS MATCHES -fsanitize-coverage)
20*58b9f456SAndroid Build Coastguard Worker    set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -fno-sanitize-coverage=edge,trace-cmp,indirect-calls,8bit-counters")
21*58b9f456SAndroid Build Coastguard Worker  endif()
22*58b9f456SAndroid Build Coastguard Worker  check_cxx_source_compiles("
23*58b9f456SAndroid Build Coastguard Worker#include <cstdint>
24*58b9f456SAndroid Build Coastguard Worker#include <atomic>
25*58b9f456SAndroid Build Coastguard Workerstd::atomic<uintptr_t> x;
26*58b9f456SAndroid Build Coastguard Workerstd::atomic<uintmax_t> y;
27*58b9f456SAndroid Build Coastguard Workerint main() {
28*58b9f456SAndroid Build Coastguard Worker  return x + y;
29*58b9f456SAndroid Build Coastguard Worker}
30*58b9f456SAndroid Build Coastguard Worker" ${varname})
31*58b9f456SAndroid Build Coastguard Worker  set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
32*58b9f456SAndroid Build Coastguard Workerendfunction(check_cxx_atomics)
33*58b9f456SAndroid Build Coastguard Worker
34*58b9f456SAndroid Build Coastguard Worker# Perform the check for 64bit atomics without libatomic. It may have been
35*58b9f456SAndroid Build Coastguard Worker# added to the required libraries during in the configuration of LLVM, which
36*58b9f456SAndroid Build Coastguard Worker# would cause the check for CXX atomics without libatomic to incorrectly pass.
37*58b9f456SAndroid Build Coastguard Workerset(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
38*58b9f456SAndroid Build Coastguard Workerlist(REMOVE_ITEM CMAKE_REQUIRED_LIBRARIES "atomic")
39*58b9f456SAndroid Build Coastguard Workercheck_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB)
40*58b9f456SAndroid Build Coastguard Workerset(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
41*58b9f456SAndroid Build Coastguard Worker
42*58b9f456SAndroid Build Coastguard Workercheck_library_exists(atomic __atomic_fetch_add_8 "" LIBCXX_HAS_ATOMIC_LIB)
43*58b9f456SAndroid Build Coastguard Worker# If not, check if the library exists, and atomics work with it.
44*58b9f456SAndroid Build Coastguard Workerif(NOT LIBCXX_HAVE_CXX_ATOMICS_WITHOUT_LIB)
45*58b9f456SAndroid Build Coastguard Worker  if(LIBCXX_HAS_ATOMIC_LIB)
46*58b9f456SAndroid Build Coastguard Worker    list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
47*58b9f456SAndroid Build Coastguard Worker    check_cxx_atomics(LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB)
48*58b9f456SAndroid Build Coastguard Worker    if (NOT LIBCXX_HAVE_CXX_ATOMICS_WITH_LIB)
49*58b9f456SAndroid Build Coastguard Worker      message(WARNING "Host compiler must support std::atomic!")
50*58b9f456SAndroid Build Coastguard Worker    endif()
51*58b9f456SAndroid Build Coastguard Worker  else()
52*58b9f456SAndroid Build Coastguard Worker    message(WARNING "Host compiler appears to require libatomic, but cannot find it.")
53*58b9f456SAndroid Build Coastguard Worker  endif()
54*58b9f456SAndroid Build Coastguard Workerendif()
55