From 976468458d53d8bb71acf48ddfc852a60557acb9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Vicen=C8=9Biu=20Ciorbaru?= <vicentiu@mariadb.org>
Date: Thu, 23 Jul 2020 00:02:21 -0700
Subject: [PATCH] Link with libatomic to enable C11 atomics support

   Some architectures (mips) require libatomic to support proper
   atomic operations. Check first if support is available without
   linking, otherwise use the library.

Upstream-Status: Pending
Signed-off-by: Khem Raj <raj.khem@gmail.com>
Signed-off-by: Mingli Yu <mingli.yu@windriver.com>
---
 configure.cmake      | 20 +++++++++++++++++++-
 mysys/CMakeLists.txt |  4 ++++
 sql/CMakeLists.txt   |  4 ++++
 3 files changed, 27 insertions(+), 1 deletion(-)

diff --git a/configure.cmake b/configure.cmake
index a355214..6ee595f 100644
--- a/configure.cmake
+++ b/configure.cmake
@@ -865,7 +865,25 @@ int main()
   long long int *ptr= &var;
   return (int)__atomic_load_n(ptr, __ATOMIC_SEQ_CST);
 }"
-HAVE_GCC_C11_ATOMICS)
+HAVE_GCC_C11_ATOMICS_WITHOUT_LIBATOMIC)
+IF (HAVE_GCC_C11_ATOMICS_WITHOUT_LIBATOMIC)
+  SET(HAVE_GCC_C11_ATOMICS True)
+ELSE()
+  SET(OLD_CMAKE_REQUIRED_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
+  LIST(APPEND CMAKE_REQUIRED_LIBRARIES "atomic")
+  CHECK_CXX_SOURCE_COMPILES("
+  int main()
+  {
+    long long int var= 1;
+    long long int *ptr= &var;
+    return (int)__atomic_load_n(ptr, __ATOMIC_SEQ_CST);
+  }"
+  HAVE_GCC_C11_ATOMICS_WITH_LIBATOMIC)
+  IF(HAVE_GCC_C11_ATOMICS_WITH_LIBATOMIC)
+    SET(HAVE_GCC_C11_ATOMICS True)
+  ENDIF()
+  SET(CMAKE_REQUIRED_LIBRARIES ${OLD_CMAKE_REQUIRED_LIBRARIES})
+ENDIF()
 
 IF(WITH_VALGRIND)
   SET(HAVE_valgrind 1)
diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt
index 3d8a20e..42f1441 100644
--- a/mysys/CMakeLists.txt
+++ b/mysys/CMakeLists.txt
@@ -129,6 +129,10 @@ TARGET_LINK_LIBRARIES(mysys dbug strings ${ZLIB_LIBRARY}
  ${LIBNSL} ${LIBM} ${LIBRT} ${LIBDL} ${LIBSOCKET} ${LIBEXECINFO})
 DTRACE_INSTRUMENT(mysys)
 
+IF (HAVE_GCC_C11_ATOMICS_WITH_LIBATOMIC)
+  TARGET_LINK_LIBRARIES(mysys atomic)
+ENDIF()
+
 IF(HAVE_BFD_H)
   TARGET_LINK_LIBRARIES(mysys bfd)  
 ENDIF(HAVE_BFD_H)
diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt
index 616017b..53575f6 100644
--- a/sql/CMakeLists.txt
+++ b/sql/CMakeLists.txt
@@ -219,6 +219,10 @@ ELSE()
   SET(MYSQLD_SOURCE main.cc ${DTRACE_PROBES_ALL})
 ENDIF()
 
+IF (HAVE_GCC_C11_ATOMICS_WITH_LIBATOMIC)
+  TARGET_LINK_LIBRARIES(sql atomic)
+ENDIF()
+
 IF(MSVC)
   SET(libs_to_export_symbols sql mysys dbug strings)
   # Create shared library of already compiled object
-- 
2.17.1

