cmake: add libatomic check
authorAlexander Alekhin <alexander.alekhin@intel.com>
Tue, 20 Aug 2019 13:19:37 +0000 (16:19 +0300)
committerAlexander Alekhin <alexander.alekhin@intel.com>
Wed, 21 Aug 2019 10:02:36 +0000 (13:02 +0300)
CMakeLists.txt
cmake/OpenCVDetectCXXCompiler.cmake
cmake/OpenCVUtils.cmake
cmake/checks/atomic_check.cpp [new file with mode: 0644]

index 4ac553b..894656c 100644 (file)
@@ -64,6 +64,10 @@ if(POLICY CMP0068)
   cmake_policy(SET CMP0068 NEW)  # CMake 3.9+: `RPATH` settings on macOS do not affect `install_name`.
 endif()
 
+if(POLICY CMP0075)
+  cmake_policy(SET CMP0075 NEW)  # CMake 3.12+: Include file check macros honor `CMAKE_REQUIRED_LIBRARIES`
+endif()
+
 
 #
 # Configure OpenCV CMake hooks
index b17895a..be64e55 100644 (file)
@@ -194,3 +194,21 @@ if(NOT HAVE_CXX11)
     endif()
   endif()
 endif()
+
+if((HAVE_CXX11
+        AND NOT MSVC
+        AND NOT (X86 OR X86_64)
+    AND NOT OPENCV_SKIP_LIBATOMIC_COMPILER_CHECK)
+    OR OPENCV_FORCE_LIBATOMIC_COMPILER_CHECK
+)
+  ocv_check_compiler_flag(CXX "" HAVE_CXX_ATOMICS_WITHOUT_LIB "${OpenCV_SOURCE_DIR}/cmake/checks/atomic_check.cpp")
+  if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
+    list(APPEND CMAKE_REQUIRED_LIBRARIES atomic)
+    ocv_check_compiler_flag(CXX "" HAVE_CXX_ATOMICS_WITH_LIB "${OpenCV_SOURCE_DIR}/cmake/checks/atomic_check.cpp")
+    if(HAVE_CXX_ATOMICS_WITH_LIB)
+      list(APPEND OPENCV_LINKER_LIBS atomic)
+    else()
+      message(FATAL_ERROR "C++11 compiler must support std::atomic")
+    endif()
+  endif()
+endif()
index 7317398..a876d9c 100644 (file)
@@ -415,12 +415,34 @@ MACRO(ocv_check_compiler_flag LANG FLAG RESULT)
       else()
         set(__msg "")
       endif()
+      if(CMAKE_REQUIRED_LIBRARIES)
+        set(__link_libs LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
+      else()
+        set(__link_libs)
+      endif()
+      set(__cmake_flags "")
+      if(CMAKE_EXE_LINKER_FLAGS)  # CMP0056 do this on new CMake
+        list(APPEND __cmake_flags "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}")
+      endif()
+
+      # CMP0067 do this on new CMake
+      if(DEFINED CMAKE_CXX_STANDARD)
+        list(APPEND __cmake_flags "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
+      endif()
+      if(DEFINED CMAKE_CXX_STANDARD_REQUIRED)
+        list(APPEND __cmake_flags "-DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}")
+      endif()
+      if(DEFINED CMAKE_CXX_EXTENSIONS)
+        list(APPEND __cmake_flags "-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}")
+      endif()
+
       MESSAGE(STATUS "Performing Test ${RESULT}${__msg}")
       TRY_COMPILE(${RESULT}
         "${CMAKE_BINARY_DIR}"
         "${_fname}"
-        CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}"   # CMP0056 do this on new CMake
+        CMAKE_FLAGS ${__cmake_flags}
         COMPILE_DEFINITIONS "${FLAG}"
+        ${__link_libs}
         OUTPUT_VARIABLE OUTPUT)
 
       if(${RESULT})
diff --git a/cmake/checks/atomic_check.cpp b/cmake/checks/atomic_check.cpp
new file mode 100644 (file)
index 0000000..c9770a0
--- /dev/null
@@ -0,0 +1,12 @@
+#include <atomic>
+
+static int test()
+{
+    std::atomic<int> x;
+    return x;
+}
+
+int main()
+{
+    return test();
+}