From fe3e176e2813956000bb947479de2c3d80fe34be Mon Sep 17 00:00:00 2001 From: rkaminsk Date: Fri, 25 Aug 2017 20:15:02 +0200 Subject: [PATCH] add libatomic if clang and pthreads are used (#11) --- CMakeLists.txt | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 15da2d1..ae7490b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,32 @@ if (CLASP_BUILD_WITH_THREADS) # some versions of findThreads will fail if C is not enabled enable_language(C) find_package(Threads REQUIRED) + + # Add libatomic if necessary + if (CMAKE_CXX_COMPILER_ID MATCHES "Clang" AND CMAKE_USE_PTHREADS_INIT) + include (CheckCXXSourceCompiles) + set (OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) + set (OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES}) + list(APPEND CMAKE_REQUIRED_FLAGS "-std=c++11") + list(APPEND CMAKE_REQUIRED_LIBRARIES Threads::Threads) + check_cxx_source_compiles(" +#include +#include +std::atomic x (0); +int main() { + uint64_t i = x.load(std::memory_order_relaxed); + return 0; +} +" CLASP_HAS_WORKING_LIBATOMIC) + set (CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) + set (CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES}) + if (NOT CLASP_HAS_WORKING_LIBATOMIC) + check_library_exists(atomic __atomic_fetch_add_4 "" CLASP_HAS_LIBATOMIC) + if (CLASP_HAS_LIBATOMIC) + set_property(TARGET Threads::Threads APPEND PROPERTY INTERFACE_LINK_LIBRARIES "atomic") + endif() + endif() + endif() endif() # Check for and optionally build external dependency