summaryrefslogtreecommitdiffstats
path: root/cmake/modules/CheckCxxAtomic.cmake
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /cmake/modules/CheckCxxAtomic.cmake
parentInitial commit. (diff)
downloadceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.tar.xz
ceph-483eb2f56657e8e7f419ab1a4fab8dce9ade8609.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'cmake/modules/CheckCxxAtomic.cmake')
-rw-r--r--cmake/modules/CheckCxxAtomic.cmake55
1 files changed, 55 insertions, 0 deletions
diff --git a/cmake/modules/CheckCxxAtomic.cmake b/cmake/modules/CheckCxxAtomic.cmake
new file mode 100644
index 00000000..f2d89cf3
--- /dev/null
+++ b/cmake/modules/CheckCxxAtomic.cmake
@@ -0,0 +1,55 @@
+# some platforms do not offer support for atomic primitive for all integer
+# types, in that case we need to link against libatomic
+
+include(CheckCXXSourceCompiles)
+include(CMakePushCheckState)
+
+
+function(check_cxx_atomics var)
+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11")
+ check_cxx_source_compiles("
+#include <atomic>
+#include <cstdint>
+
+#if __s390x__
+// Boost needs 16-byte atomics for tagged pointers.
+// These are implemented via inline instructions on the platform
+// if 16-byte alignment can be proven, and are delegated to libatomic
+// library routines otherwise. Whether or not alignment is provably
+// OK for a std::atomic unfortunately depends on compiler version and
+// optimization levels, and also on the details of the expression.
+// We specifically test access via an otherwise unknown pointer here
+// to ensure we get the most complex case. If this access can be
+// done without libatomic, then all accesses can be done.
+bool atomic16(std::atomic<unsigned __int128> *ptr)
+{
+ return *ptr != 0;
+}
+#endif
+
+int main() {
+ std::atomic<uint8_t> w1;
+ std::atomic<uint16_t> w2;
+ std::atomic<uint32_t> w4;
+ std::atomic<uint64_t> w8;
+ return w1 + w2 + w4 + w8;
+}
+" ${var})
+endfunction(check_cxx_atomics)
+
+cmake_push_check_state()
+check_cxx_atomics(HAVE_CXX11_ATOMIC)
+cmake_pop_check_state()
+
+if(NOT HAVE_CXX11_ATOMIC)
+ cmake_push_check_state()
+ set(CMAKE_REQUIRED_LIBRARIES "atomic")
+ check_cxx_atomics(HAVE_LIBATOMIC)
+ cmake_pop_check_state()
+ if(HAVE_LIBATOMIC)
+ set(LIBATOMIC_LINK_FLAGS "-Wl,--as-needed -latomic")
+ else()
+ message(FATAL_ERROR
+ "Host compiler ${CMAKE_CXX_COMPILER} requires libatomic, but it is not found")
+ endif()
+endif()