1From cf168ae0b7bceab8432d096719b331f18428fe39 Mon Sep 17 00:00:00 2001 2From: Khem Raj <raj.khem@gmail.com> 3Date: Wed, 18 Mar 2020 15:10:37 -0700 4Subject: [PATCH] cmake: Add check for atomic support 5 6Detect if libatomic should be linked in or compiler and platform can 7provide the needed atomic instrinsics, this helps build on certain 8platforms like mips or clang/i386 9 10Fixes 11 12| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_exchange_8' 13| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_fetch_or_8' 14| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_compare_exchange_8' 15| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_fetch_sub_8' 16| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_load_8' 17| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_store_8' 18| /mnt/b/yoe/build/tmp/work/mips32r2-yoe-linux/rocksdb/6.6.4-r0/recipe-sysroot-native/usr/bin/mips-yoe-linux/mips-yoe-linux-ld: librocksdb.so.6.6.4: undefined reference to `__atomic_fetch_add_8' 19 20Upstream-Status: Submitted [https://github.com/facebook/rocksdb/pull/6555] 21Signed-off-by: Khem Raj <raj.khem@gmail.com> 22--- 23 CMakeLists.txt | 5 +++ 24 cmake/modules/CheckAtomic.cmake | 69 +++++++++++++++++++++++++++++++++ 25 2 files changed, 74 insertions(+) 26 create mode 100644 cmake/modules/CheckAtomic.cmake 27 28diff --git a/CMakeLists.txt b/CMakeLists.txt 29index f9c8f3346..4d604004b 100644 30--- a/CMakeLists.txt 31+++ b/CMakeLists.txt 32@@ -852,7 +852,12 @@ endif() 33 if(WIN32) 34 set(SYSTEM_LIBS ${SYSTEM_LIBS} shlwapi.lib rpcrt4.lib) 35 else() 36+ # check if linking against libatomic is necessary 37+ include(CheckAtomic) 38 set(SYSTEM_LIBS ${CMAKE_THREAD_LIBS_INIT}) 39+ if(HAVE_CXX_ATOMIC_WITH_LIB OR HAVE_CXX_ATOMICS64_WITH_LIB) 40+ set(SYSTEM_LIBS ${SYSTEM_LIBS} atomic) 41+ endif() 42 endif() 43 44 add_library(${ROCKSDB_STATIC_LIB} STATIC ${SOURCES}) 45diff --git a/cmake/modules/CheckAtomic.cmake b/cmake/modules/CheckAtomic.cmake 46new file mode 100644 47index 000000000..8b7dc8a37 48--- /dev/null 49+++ b/cmake/modules/CheckAtomic.cmake 50@@ -0,0 +1,69 @@ 51+# Checks if atomic operations are supported natively or if linking against 52+# libatomic is needed. 53+ 54+# Check inspired by LLVMs cmake/modules/CheckAtomic.cmake 55+ 56+INCLUDE(CheckCXXSourceCompiles) 57+INCLUDE(CheckLibraryExists) 58+ 59+function(check_working_cxx_atomics varname) 60+ set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 61+ set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -std=c++11") 62+ CHECK_CXX_SOURCE_COMPILES(" 63+#include <atomic> 64+std::atomic<int> x; 65+int main() { 66+ return x; 67+} 68+" ${varname}) 69+ set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 70+endfunction(check_working_cxx_atomics) 71+ 72+function(check_working_cxx_atomics64 varname) 73+ set(OLD_CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS}) 74+ set(CMAKE_REQUIRED_FLAGS "-std=c++11 ${CMAKE_REQUIRED_FLAGS}") 75+ CHECK_CXX_SOURCE_COMPILES(" 76+#include <atomic> 77+#include <cstdint> 78+std::atomic<uint64_t> x (0); 79+std::atomic<double> y (0); 80+int main() { 81+ uint64_t i = x.load(std::memory_order_relaxed); 82+ return int(y); 83+} 84+" ${varname}) 85+ set(CMAKE_REQUIRED_FLAGS ${OLD_CMAKE_REQUIRED_FLAGS}) 86+endfunction(check_working_cxx_atomics64) 87+ 88+# Check if atomics work without libatomic 89+check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITHOUT_LIB) 90+ 91+if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB) 92+ check_library_exists(atomic __atomic_fetch_add_4 "" HAVE_LIBATOMIC) 93+ if( HAVE_LIBATOMIC ) 94+ list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") 95+ check_working_cxx_atomics(HAVE_CXX_ATOMICS_WITH_LIB) 96+ if (NOT HAVE_CXX_ATOMICS_WITH_LIB) 97+ message(FATAL_ERROR "Host compiler must support std::atomic!") 98+ endif() 99+ else() 100+ message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") 101+ endif() 102+endif() 103+ 104+# Check if 64bit atomics work without libatomic 105+check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITHOUT_LIB) 106+ 107+if(NOT HAVE_CXX_ATOMICS64_WITHOUT_LIB) 108+ check_library_exists(atomic __atomic_load_8 "" HAVE_CXX_LIBATOMICS64) 109+ if(HAVE_CXX_LIBATOMICS64) 110+ list(APPEND CMAKE_REQUIRED_LIBRARIES "atomic") 111+ check_working_cxx_atomics64(HAVE_CXX_ATOMICS64_WITH_LIB) 112+ if (NOT HAVE_CXX_ATOMICS64_WITH_LIB) 113+ message(FATAL_ERROR "Host compiler must support std::atomic!") 114+ endif() 115+ else() 116+ message(FATAL_ERROR "Host compiler appears to require libatomic, but cannot find it.") 117+ endif() 118+endif() 119+ 120-- 1212.28.0 122 123