link with libatomic on architectures that require it
authorThomas Krennwallner <tkren@kr.tuwien.ac.at>
Wed, 11 Mar 2020 10:50:20 +0000 (06:50 -0400)
committerAdrian Bunk <bunk@debian.org>
Sat, 19 Nov 2022 10:33:48 +0000 (10:33 +0000)
Last-Update: 2020-03-11
Forwarded: no

clingo requires c++11 threads with 64bit __atomic_exchange, we need to
link with libatomic on armel, powerpc, powerpcspe, m68k, mips, mipsel,
and sh4, see also https://gcc.gnu.org/wiki/Atomic and
https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary

This patch was shamelessly adapted from
https://github.com/potassco/clasp/blob/master/CMakeLists.txt
https://github.com/potassco/clasp/commit/fe3e176e2813956000bb947479de2c3d80fe34be

Gbp-Pq: Name link-libatomic-check.patch

CMakeLists.txt

index 243469aca3f1775d6ebc9ea007e6994182951e0e..df92db857d6a01274737cc066c441fb2c726cd34 100644 (file)
@@ -17,6 +17,35 @@ include(CMakeDependentOption)
 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
 include(clingo_functions)
 
+# Add libatomic if necessary
+include (CheckCXXSourceCompiles)
+set (OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS})
+set (OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
+find_package(Threads REQUIRED)
+list(APPEND CMAKE_REQUIRED_FLAGS "-std=c++11")
+list(APPEND CMAKE_REQUIRED_LIBRARIES Threads::Threads)
+check_cxx_source_compiles("
+#include <atomic>
+#include <cstdint>
+std::atomic<uint64_t> x (0);
+int main() {
+       uint64_t i = x.load(std::memory_order_relaxed);
+       return 0;
+}
+" CLINGO_HAS_WORKING_LIBATOMIC)
+set (CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS})
+set (CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
+if (NOT CLINGO_HAS_WORKING_LIBATOMIC)
+       # clingo requires c++11 threads with 64bit __atomic_exchange, we
+       # need to link with libatomic on armel, powerpc, powerpcspe, m68k,
+       # mips, mipsel, and sh4, see also https://gcc.gnu.org/wiki/Atomic
+       # https://gcc.gnu.org/wiki/Atomic/GCCMM/LIbrary
+       check_library_exists(atomic __atomic_fetch_add_4 "" CLINGO_HAS_LIBATOMIC)
+       if (CLINGO_HAS_LIBATOMIC)
+               set_property(TARGET Threads::Threads APPEND PROPERTY INTERFACE_LINK_LIBRARIES "atomic")
+       endif()
+endif()
+
 # Enable folders in IDEs like Visual Studio
 set_property(GLOBAL PROPERTY USE_FOLDERS ON)