OpenCVDetectCXXCompiler.cmake 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. # Compilers:
  2. # - CV_GCC - GNU compiler (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
  3. # - CV_CLANG - Clang-compatible compiler (CMAKE_CXX_COMPILER_ID MATCHES "Clang" - Clang or AppleClang, see CMP0025)
  4. # - CV_ICC - Intel compiler
  5. # - MSVC - Microsoft Visual Compiler (CMake variable)
  6. # - MINGW / CYGWIN / CMAKE_COMPILER_IS_MINGW / CMAKE_COMPILER_IS_CYGWIN (CMake original variables)
  7. #
  8. # CPU Platforms:
  9. # - X86 / X86_64
  10. # - ARM - ARM CPU, not defined for AArch64
  11. # - AARCH64 - ARMv8+ (64-bit)
  12. # - PPC64 / PPC64LE - PowerPC
  13. # - MIPS
  14. #
  15. # OS:
  16. # - WIN32 - Windows | MINGW
  17. # - UNIX - Linux | MacOSX | ANDROID
  18. # - ANDROID
  19. # - IOS
  20. # - APPLE - MacOSX | iOS
  21. # ----------------------------------------------------------------------------
  22. ocv_declare_removed_variables(MINGW64 MSVC64)
  23. # do not use (CMake variables): CMAKE_CL_64
  24. if(NOT DEFINED CV_GCC AND CMAKE_CXX_COMPILER_ID MATCHES "GNU")
  25. set(CV_GCC 1)
  26. endif()
  27. if(NOT DEFINED CV_CLANG AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") # Clang or AppleClang (see CMP0025)
  28. set(CV_CLANG 1)
  29. set(CMAKE_COMPILER_IS_CLANGCXX 1) # TODO next release: remove this
  30. set(CMAKE_COMPILER_IS_CLANGCC 1) # TODO next release: remove this
  31. endif()
  32. function(access_CMAKE_COMPILER_IS_CLANGCXX)
  33. if(NOT OPENCV_SUPPRESS_DEPRECATIONS)
  34. message(WARNING "DEPRECATED: CMAKE_COMPILER_IS_CLANGCXX support is deprecated in OpenCV.
  35. Consider using:
  36. - CV_GCC # GCC
  37. - CV_CLANG # Clang or AppleClang (see CMP0025)
  38. ")
  39. endif()
  40. endfunction()
  41. variable_watch(CMAKE_COMPILER_IS_CLANGCXX access_CMAKE_COMPILER_IS_CLANGCXX)
  42. variable_watch(CMAKE_COMPILER_IS_CLANGCC access_CMAKE_COMPILER_IS_CLANGCXX)
  43. # ----------------------------------------------------------------------------
  44. # Detect Intel ICC compiler
  45. # ----------------------------------------------------------------------------
  46. if(UNIX)
  47. if(__ICL)
  48. set(CV_ICC __ICL)
  49. elseif(__ICC)
  50. set(CV_ICC __ICC)
  51. elseif(__ECL)
  52. set(CV_ICC __ECL)
  53. elseif(__ECC)
  54. set(CV_ICC __ECC)
  55. elseif(__INTEL_COMPILER)
  56. set(CV_ICC __INTEL_COMPILER)
  57. elseif(CMAKE_C_COMPILER MATCHES "icc")
  58. set(CV_ICC icc_matches_c_compiler)
  59. endif()
  60. endif()
  61. if(MSVC AND CMAKE_C_COMPILER MATCHES "icc|icl")
  62. set(CV_ICC __INTEL_COMPILER_FOR_WINDOWS)
  63. endif()
  64. if(NOT DEFINED CMAKE_CXX_COMPILER_VERSION
  65. AND NOT OPENCV_SUPPRESS_MESSAGE_MISSING_COMPILER_VERSION)
  66. message(WARNING "OpenCV: Compiler version is not available: CMAKE_CXX_COMPILER_VERSION is not set")
  67. endif()
  68. if((NOT DEFINED CMAKE_SYSTEM_PROCESSOR OR CMAKE_SYSTEM_PROCESSOR STREQUAL "")
  69. AND NOT OPENCV_SUPPRESS_MESSAGE_MISSING_CMAKE_SYSTEM_PROCESSOR)
  70. message(WARNING "OpenCV: CMAKE_SYSTEM_PROCESSOR is not defined. Perhaps CMake toolchain is broken")
  71. endif()
  72. if(NOT DEFINED CMAKE_SIZEOF_VOID_P
  73. AND NOT OPENCV_SUPPRESS_MESSAGE_MISSING_CMAKE_SIZEOF_VOID_P)
  74. message(WARNING "OpenCV: CMAKE_SIZEOF_VOID_P is not defined. Perhaps CMake toolchain is broken")
  75. endif()
  76. message(STATUS "Detected processor: ${CMAKE_SYSTEM_PROCESSOR}")
  77. if(OPENCV_SKIP_SYSTEM_PROCESSOR_DETECTION)
  78. # custom setup: required variables are passed through cache / CMake's command-line
  79. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
  80. set(X86_64 1)
  81. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i686.*|i386.*|x86.*")
  82. set(X86 1)
  83. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64.*|AARCH64.*|arm64.*|ARM64.*)")
  84. set(AARCH64 1)
  85. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(arm.*|ARM.*)")
  86. set(ARM 1)
  87. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64le")
  88. set(PPC64LE 1)
  89. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(powerpc|ppc)64")
  90. set(PPC64 1)
  91. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(mips.*|MIPS.*)")
  92. set(MIPS 1)
  93. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(riscv.*|RISCV.*)")
  94. set(RISCV 1)
  95. else()
  96. if(NOT OPENCV_SUPPRESS_MESSAGE_UNRECOGNIZED_SYSTEM_PROCESSOR)
  97. message(WARNING "OpenCV: unrecognized target processor configuration")
  98. endif()
  99. endif()
  100. # Workaround for 32-bit operating systems on x86_64
  101. if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND X86_64
  102. AND NOT FORCE_X86_64 # deprecated (2019-12)
  103. AND NOT OPENCV_FORCE_X86_64
  104. )
  105. message(STATUS "sizeof(void) = 4 on 64 bit processor. Assume 32-bit compilation mode")
  106. if(X86_64)
  107. unset(X86_64)
  108. set(X86 1)
  109. endif()
  110. endif()
  111. # Workaround for 32-bit operating systems on aarch64 processor
  112. if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND AARCH64
  113. AND NOT OPENCV_FORCE_AARCH64
  114. )
  115. message(STATUS "sizeof(void) = 4 on 64 bit processor. Assume 32-bit compilation mode")
  116. if(AARCH64)
  117. unset(AARCH64)
  118. set(ARM 1)
  119. endif()
  120. endif()
  121. # Similar code exists in OpenCVConfig.cmake
  122. if(NOT DEFINED OpenCV_STATIC)
  123. # look for global setting
  124. if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
  125. set(OpenCV_STATIC OFF)
  126. else()
  127. set(OpenCV_STATIC ON)
  128. endif()
  129. endif()
  130. if(DEFINED OpenCV_ARCH AND DEFINED OpenCV_RUNTIME)
  131. # custom overridden values
  132. elseif(MSVC)
  133. # see Modules/CMakeGenericSystem.cmake
  134. if("${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)")
  135. set(OpenCV_ARCH "x64")
  136. elseif("${CMAKE_GENERATOR_PLATFORM}" MATCHES "ARM64")
  137. set(OpenCV_ARCH "ARM64")
  138. elseif("${CMAKE_GENERATOR}" MATCHES "ARM")
  139. set(OpenCV_ARCH "ARM")
  140. elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  141. set(OpenCV_ARCH "x64")
  142. else()
  143. set(OpenCV_ARCH x86)
  144. endif()
  145. if(MSVC_VERSION EQUAL 1400)
  146. set(OpenCV_RUNTIME vc8)
  147. elseif(MSVC_VERSION EQUAL 1500)
  148. set(OpenCV_RUNTIME vc9)
  149. elseif(MSVC_VERSION EQUAL 1600)
  150. set(OpenCV_RUNTIME vc10)
  151. elseif(MSVC_VERSION EQUAL 1700)
  152. set(OpenCV_RUNTIME vc11)
  153. elseif(MSVC_VERSION EQUAL 1800)
  154. set(OpenCV_RUNTIME vc12)
  155. elseif(MSVC_VERSION EQUAL 1900)
  156. set(OpenCV_RUNTIME vc14)
  157. elseif(MSVC_VERSION MATCHES "^191[0-9]$")
  158. set(OpenCV_RUNTIME vc15)
  159. elseif(MSVC_VERSION MATCHES "^192[0-9]$")
  160. set(OpenCV_RUNTIME vc16)
  161. elseif(MSVC_VERSION MATCHES "^193[0-9]$")
  162. set(OpenCV_RUNTIME vc17)
  163. else()
  164. message(WARNING "OpenCV does not recognize MSVC_VERSION \"${MSVC_VERSION}\". Cannot set OpenCV_RUNTIME")
  165. endif()
  166. elseif(MINGW)
  167. set(OpenCV_RUNTIME mingw)
  168. if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
  169. set(OpenCV_ARCH x64)
  170. else()
  171. set(OpenCV_ARCH x86)
  172. endif()
  173. endif()
  174. # Fix handling of duplicated files in the same static library:
  175. # https://public.kitware.com/Bug/view.php?id=14874
  176. if(CMAKE_VERSION VERSION_LESS "3.1")
  177. foreach(var CMAKE_C_ARCHIVE_APPEND CMAKE_CXX_ARCHIVE_APPEND)
  178. if(${var} MATCHES "^<CMAKE_AR> r")
  179. string(REPLACE "<CMAKE_AR> r" "<CMAKE_AR> q" ${var} "${${var}}")
  180. endif()
  181. endforeach()
  182. endif()
  183. if(NOT OPENCV_SKIP_CMAKE_CXX_STANDARD)
  184. ocv_update(CMAKE_CXX_STANDARD 11)
  185. ocv_update(CMAKE_CXX_STANDARD_REQUIRED TRUE)
  186. ocv_update(CMAKE_CXX_EXTENSIONS OFF) # use -std=c++11 instead of -std=gnu++11
  187. if(CMAKE_CXX11_COMPILE_FEATURES)
  188. set(HAVE_CXX11 ON)
  189. endif()
  190. endif()
  191. if(NOT HAVE_CXX11)
  192. ocv_check_compiler_flag(CXX "" HAVE_CXX11 "${OpenCV_SOURCE_DIR}/cmake/checks/cxx11.cpp")
  193. if(NOT HAVE_CXX11)
  194. ocv_check_compiler_flag(CXX "-std=c++11" HAVE_STD_CXX11 "${OpenCV_SOURCE_DIR}/cmake/checks/cxx11.cpp")
  195. if(HAVE_STD_CXX11)
  196. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  197. set(HAVE_CXX11 ON)
  198. endif()
  199. endif()
  200. endif()
  201. if(NOT HAVE_CXX11)
  202. message(FATAL_ERROR "OpenCV 4.x requires C++11")
  203. endif()
  204. set(__OPENCV_ENABLE_ATOMIC_LONG_LONG OFF)
  205. if(HAVE_CXX11 AND (X86 OR X86_64))
  206. set(__OPENCV_ENABLE_ATOMIC_LONG_LONG ON)
  207. endif()
  208. option(OPENCV_ENABLE_ATOMIC_LONG_LONG "Enable C++ compiler support for atomic<long long>" ${__OPENCV_ENABLE_ATOMIC_LONG_LONG})
  209. if((HAVE_CXX11 AND OPENCV_ENABLE_ATOMIC_LONG_LONG
  210. AND NOT MSVC
  211. AND NOT (X86 OR X86_64)
  212. AND NOT OPENCV_SKIP_LIBATOMIC_COMPILER_CHECK)
  213. OR OPENCV_FORCE_LIBATOMIC_COMPILER_CHECK
  214. )
  215. ocv_check_compiler_flag(CXX "" HAVE_CXX_ATOMICS_WITHOUT_LIB "${OpenCV_SOURCE_DIR}/cmake/checks/atomic_check.cpp")
  216. if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
  217. list(APPEND CMAKE_REQUIRED_LIBRARIES atomic)
  218. ocv_check_compiler_flag(CXX "" HAVE_CXX_ATOMICS_WITH_LIB "${OpenCV_SOURCE_DIR}/cmake/checks/atomic_check.cpp")
  219. if(HAVE_CXX_ATOMICS_WITH_LIB)
  220. set(HAVE_ATOMIC_LONG_LONG ON)
  221. list(APPEND OPENCV_LINKER_LIBS atomic)
  222. else()
  223. message(STATUS "Compiler doesn't support std::atomic<long long>")
  224. endif()
  225. else()
  226. set(HAVE_ATOMIC_LONG_LONG ON)
  227. endif()
  228. else(HAVE_CXX11 AND OPENCV_ENABLE_ATOMIC_LONG_LONG)
  229. set(HAVE_ATOMIC_LONG_LONG ${OPENCV_ENABLE_ATOMIC_LONG_LONG})
  230. endif()