OpenCVDetectCXXCompiler.cmake 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(loongarch64.*|LOONGARCH64.*)")
  96. set(LOONGARCH64 1)
  97. else()
  98. if(NOT OPENCV_SUPPRESS_MESSAGE_UNRECOGNIZED_SYSTEM_PROCESSOR)
  99. message(WARNING "OpenCV: unrecognized target processor configuration")
  100. endif()
  101. endif()
  102. # Workaround for 32-bit operating systems on x86_64
  103. if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND X86_64
  104. AND NOT FORCE_X86_64 # deprecated (2019-12)
  105. AND NOT OPENCV_FORCE_X86_64
  106. )
  107. message(STATUS "sizeof(void) = 4 on 64 bit processor. Assume 32-bit compilation mode")
  108. if(X86_64)
  109. unset(X86_64)
  110. set(X86 1)
  111. endif()
  112. endif()
  113. # Workaround for 32-bit operating systems on aarch64 processor
  114. if(CMAKE_SIZEOF_VOID_P EQUAL 4 AND AARCH64
  115. AND NOT OPENCV_FORCE_AARCH64
  116. )
  117. message(STATUS "sizeof(void) = 4 on 64 bit processor. Assume 32-bit compilation mode")
  118. if(AARCH64)
  119. unset(AARCH64)
  120. set(ARM 1)
  121. endif()
  122. endif()
  123. # Similar code exists in OpenCVConfig.cmake
  124. if(NOT DEFINED OpenCV_STATIC)
  125. # look for global setting
  126. if(NOT DEFINED BUILD_SHARED_LIBS OR BUILD_SHARED_LIBS)
  127. set(OpenCV_STATIC OFF)
  128. else()
  129. set(OpenCV_STATIC ON)
  130. endif()
  131. endif()
  132. if(DEFINED OpenCV_ARCH AND DEFINED OpenCV_RUNTIME)
  133. # custom overridden values
  134. elseif(MSVC)
  135. # see Modules/CMakeGenericSystem.cmake
  136. if("${CMAKE_GENERATOR}" MATCHES "(Win64|IA64)")
  137. set(OpenCV_ARCH "x64")
  138. elseif("${CMAKE_GENERATOR_PLATFORM}" MATCHES "ARM64")
  139. set(OpenCV_ARCH "ARM64")
  140. elseif("${CMAKE_GENERATOR}" MATCHES "ARM")
  141. set(OpenCV_ARCH "ARM")
  142. elseif("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
  143. set(OpenCV_ARCH "x64")
  144. else()
  145. set(OpenCV_ARCH x86)
  146. endif()
  147. if(MSVC_VERSION EQUAL 1400)
  148. set(OpenCV_RUNTIME vc8)
  149. elseif(MSVC_VERSION EQUAL 1500)
  150. set(OpenCV_RUNTIME vc9)
  151. elseif(MSVC_VERSION EQUAL 1600)
  152. set(OpenCV_RUNTIME vc10)
  153. elseif(MSVC_VERSION EQUAL 1700)
  154. set(OpenCV_RUNTIME vc11)
  155. elseif(MSVC_VERSION EQUAL 1800)
  156. set(OpenCV_RUNTIME vc12)
  157. elseif(MSVC_VERSION EQUAL 1900)
  158. set(OpenCV_RUNTIME vc14)
  159. elseif(MSVC_VERSION MATCHES "^191[0-9]$")
  160. set(OpenCV_RUNTIME vc15)
  161. elseif(MSVC_VERSION MATCHES "^192[0-9]$")
  162. set(OpenCV_RUNTIME vc16)
  163. elseif(MSVC_VERSION MATCHES "^193[0-9]$")
  164. set(OpenCV_RUNTIME vc17)
  165. else()
  166. message(WARNING "OpenCV does not recognize MSVC_VERSION \"${MSVC_VERSION}\". Cannot set OpenCV_RUNTIME")
  167. endif()
  168. elseif(MINGW)
  169. set(OpenCV_RUNTIME mingw)
  170. if(CMAKE_SYSTEM_PROCESSOR MATCHES "amd64.*|x86_64.*|AMD64.*")
  171. set(OpenCV_ARCH x64)
  172. else()
  173. set(OpenCV_ARCH x86)
  174. endif()
  175. endif()
  176. # Fix handling of duplicated files in the same static library:
  177. # https://public.kitware.com/Bug/view.php?id=14874
  178. if(CMAKE_VERSION VERSION_LESS "3.1")
  179. foreach(var CMAKE_C_ARCHIVE_APPEND CMAKE_CXX_ARCHIVE_APPEND)
  180. if(${var} MATCHES "^<CMAKE_AR> r")
  181. string(REPLACE "<CMAKE_AR> r" "<CMAKE_AR> q" ${var} "${${var}}")
  182. endif()
  183. endforeach()
  184. endif()
  185. if(NOT OPENCV_SKIP_CMAKE_CXX_STANDARD)
  186. ocv_update(CMAKE_CXX_STANDARD 11)
  187. ocv_update(CMAKE_CXX_STANDARD_REQUIRED TRUE)
  188. ocv_update(CMAKE_CXX_EXTENSIONS OFF) # use -std=c++11 instead of -std=gnu++11
  189. if(CMAKE_CXX11_COMPILE_FEATURES)
  190. set(HAVE_CXX11 ON)
  191. endif()
  192. endif()
  193. if(NOT HAVE_CXX11)
  194. ocv_check_compiler_flag(CXX "" HAVE_CXX11 "${OpenCV_SOURCE_DIR}/cmake/checks/cxx11.cpp")
  195. if(NOT HAVE_CXX11)
  196. ocv_check_compiler_flag(CXX "-std=c++11" HAVE_STD_CXX11 "${OpenCV_SOURCE_DIR}/cmake/checks/cxx11.cpp")
  197. if(HAVE_STD_CXX11)
  198. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
  199. set(HAVE_CXX11 ON)
  200. endif()
  201. endif()
  202. endif()
  203. if(NOT HAVE_CXX11)
  204. message(FATAL_ERROR "OpenCV 4.x requires C++11")
  205. endif()
  206. set(__OPENCV_ENABLE_ATOMIC_LONG_LONG OFF)
  207. if(HAVE_CXX11 AND (X86 OR X86_64))
  208. set(__OPENCV_ENABLE_ATOMIC_LONG_LONG ON)
  209. endif()
  210. option(OPENCV_ENABLE_ATOMIC_LONG_LONG "Enable C++ compiler support for atomic<long long>" ${__OPENCV_ENABLE_ATOMIC_LONG_LONG})
  211. if((HAVE_CXX11 AND OPENCV_ENABLE_ATOMIC_LONG_LONG
  212. AND NOT MSVC
  213. AND NOT (X86 OR X86_64)
  214. AND NOT OPENCV_SKIP_LIBATOMIC_COMPILER_CHECK)
  215. OR OPENCV_FORCE_LIBATOMIC_COMPILER_CHECK
  216. )
  217. ocv_check_compiler_flag(CXX "" HAVE_CXX_ATOMICS_WITHOUT_LIB "${OpenCV_SOURCE_DIR}/cmake/checks/atomic_check.cpp")
  218. if(NOT HAVE_CXX_ATOMICS_WITHOUT_LIB)
  219. list(APPEND CMAKE_REQUIRED_LIBRARIES atomic)
  220. ocv_check_compiler_flag(CXX "" HAVE_CXX_ATOMICS_WITH_LIB "${OpenCV_SOURCE_DIR}/cmake/checks/atomic_check.cpp")
  221. if(HAVE_CXX_ATOMICS_WITH_LIB)
  222. set(HAVE_ATOMIC_LONG_LONG ON)
  223. list(APPEND OPENCV_LINKER_LIBS atomic)
  224. else()
  225. message(STATUS "Compiler doesn't support std::atomic<long long>")
  226. endif()
  227. else()
  228. set(HAVE_ATOMIC_LONG_LONG ON)
  229. endif()
  230. else(HAVE_CXX11 AND OPENCV_ENABLE_ATOMIC_LONG_LONG)
  231. set(HAVE_ATOMIC_LONG_LONG ${OPENCV_ENABLE_ATOMIC_LONG_LONG})
  232. endif()