aboutsummaryrefslogtreecommitdiff
path: root/cmake
diff options
context:
space:
mode:
authorfuzzard <fuzzard@users.noreply.github.com>2022-09-05 17:15:30 +1000
committerGitHub <noreply@github.com>2022-09-05 17:15:30 +1000
commit1673f476b802da1da942cef256cae6272fdf9a4b (patch)
tree919c6c4301214b5d99be067b92f80522e6a13c20 /cmake
parentf074f063fc7d5d7c3a5e44af5104fd142f5e30ad (diff)
parentac3213e683e4c62c50dc02fef3b168d883245094 (diff)
Merge pull request #21743 from dlan17/master
[cmake] link atomic library for certain CPU architectures
Diffstat (limited to 'cmake')
-rw-r--r--cmake/modules/FindAtomic.cmake56
-rw-r--r--cmake/scripts/linux/ArchSetup.cmake3
2 files changed, 59 insertions, 0 deletions
diff --git a/cmake/modules/FindAtomic.cmake b/cmake/modules/FindAtomic.cmake
new file mode 100644
index 0000000000..8ea3c815d7
--- /dev/null
+++ b/cmake/modules/FindAtomic.cmake
@@ -0,0 +1,56 @@
+#.rst:
+# FindAtomic
+# -----
+# Finds the ATOMIC library
+#
+# This will define the following variables::
+#
+# ATOMIC_FOUND - system has ATOMIC
+# ATOMIC_LIBRARIES - the ATOMIC libraries
+#
+# and the following imported targets::
+#
+# ATOMIC::ATOMIC - The ATOMIC library
+
+
+include(CheckCXXSourceCompiles)
+
+set(atomic_code
+ "
+ #include <atomic>
+ #include <cstdint>
+ std::atomic<uint8_t> n8 (0); // riscv64
+ std::atomic<uint64_t> n64 (0); // armel, mipsel, powerpc
+ int main() {
+ ++n8;
+ ++n64;
+ return 0;
+ }")
+
+check_cxx_source_compiles("${atomic_code}" ATOMIC_LOCK_FREE_INSTRUCTIONS)
+
+if(ATOMIC_LOCK_FREE_INSTRUCTIONS)
+ set(ATOMIC_FOUND TRUE)
+ set(ATOMIC_LIBRARIES)
+else()
+ set(CMAKE_REQUIRED_LIBRARIES "-latomic")
+ check_cxx_source_compiles("${atomic_code}" ATOMIC_IN_LIBRARY)
+ set(CMAKE_REQUIRED_LIBRARIES)
+ if(ATOMIC_IN_LIBRARY)
+ set(ATOMIC_LIBRARY atomic)
+ include(FindPackageHandleStandardArgs)
+ find_package_handle_standard_args(Atomic DEFAULT_MSG ATOMIC_LIBRARY)
+ set(ATOMIC_LIBRARIES ${ATOMIC_LIBRARY})
+ if(NOT TARGET ATOMIC::ATOMIC)
+ add_library(ATOMIC::ATOMIC UNKNOWN IMPORTED)
+ set_target_properties(ATOMIC::ATOMIC PROPERTIES
+ IMPORTED_LOCATION "${ATOMIC_LIBRARY}")
+ endif()
+ unset(ATOMIC_LIBRARY)
+ else()
+ if(Atomic_FIND_REQUIRED)
+ message(FATAL_ERROR "Neither lock free instructions nor -latomic found.")
+ endif()
+ endif()
+endif()
+unset(atomic_code)
diff --git a/cmake/scripts/linux/ArchSetup.cmake b/cmake/scripts/linux/ArchSetup.cmake
index 35ab1402f5..848723af1f 100644
--- a/cmake/scripts/linux/ArchSetup.cmake
+++ b/cmake/scripts/linux/ArchSetup.cmake
@@ -199,3 +199,6 @@ if(NOT USE_INTERNAL_LIBS)
set(USE_INTERNAL_LIBS OFF)
endif()
endif()
+
+# Atomic library
+list(APPEND PLATFORM_REQUIRED_DEPS Atomic)