OpenCVCompilerOptions.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. if("${CMAKE_CXX_COMPILER};${CMAKE_C_COMPILER};${CMAKE_CXX_COMPILER_LAUNCHER}" MATCHES "ccache")
  2. set(CMAKE_COMPILER_IS_CCACHE 1) # TODO: FIXIT Avoid setting of CMAKE_ variables
  3. set(OPENCV_COMPILER_IS_CCACHE 1)
  4. endif()
  5. function(access_CMAKE_COMPILER_IS_CCACHE)
  6. if(NOT OPENCV_SUPPRESS_DEPRECATIONS)
  7. message(WARNING "DEPRECATED: CMAKE_COMPILER_IS_CCACHE is replaced to OPENCV_COMPILER_IS_CCACHE.")
  8. endif()
  9. endfunction()
  10. variable_watch(CMAKE_COMPILER_IS_CCACHE access_CMAKE_COMPILER_IS_CCACHE)
  11. if(ENABLE_CCACHE AND NOT OPENCV_COMPILER_IS_CCACHE)
  12. # This works fine with Unix Makefiles and Ninja generators
  13. find_host_program(CCACHE_PROGRAM ccache)
  14. if(CCACHE_PROGRAM)
  15. message(STATUS "Looking for ccache - found (${CCACHE_PROGRAM})")
  16. get_property(__OLD_RULE_LAUNCH_COMPILE GLOBAL PROPERTY RULE_LAUNCH_COMPILE)
  17. if(CMAKE_GENERATOR MATCHES "Xcode")
  18. configure_file("${CMAKE_CURRENT_LIST_DIR}/templates/xcode-launch-c.in" "${CMAKE_BINARY_DIR}/xcode-launch-c")
  19. configure_file("${CMAKE_CURRENT_LIST_DIR}/templates/xcode-launch-cxx.in" "${CMAKE_BINARY_DIR}/xcode-launch-cxx")
  20. execute_process(COMMAND chmod a+rx
  21. "${CMAKE_BINARY_DIR}/xcode-launch-c"
  22. "${CMAKE_BINARY_DIR}/xcode-launch-cxx"
  23. )
  24. # Xcode project attributes
  25. set(CMAKE_XCODE_ATTRIBUTE_CC "${CMAKE_BINARY_DIR}/xcode-launch-c")
  26. set(CMAKE_XCODE_ATTRIBUTE_CXX "${CMAKE_BINARY_DIR}/xcode-launch-cxx")
  27. set(CMAKE_XCODE_ATTRIBUTE_LD "${CMAKE_BINARY_DIR}/xcode-launch-c")
  28. set(CMAKE_XCODE_ATTRIBUTE_LDPLUSPLUS "${CMAKE_BINARY_DIR}/xcode-launch-cxx")
  29. set(OPENCV_COMPILER_IS_CCACHE 1)
  30. message(STATUS "ccache: enable support through Xcode project properties")
  31. elseif(__OLD_RULE_LAUNCH_COMPILE)
  32. message(STATUS "Can't replace CMake compiler launcher")
  33. else()
  34. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
  35. # NOTE: Actually this check doesn't work as expected.
  36. # "RULE_LAUNCH_COMPILE" is ignored by CMake during try_compile() step.
  37. # ocv_check_compiler_flag(CXX "" IS_CCACHE_WORKS)
  38. set(IS_CCACHE_WORKS 1)
  39. if(IS_CCACHE_WORKS)
  40. set(OPENCV_COMPILER_IS_CCACHE 1)
  41. else()
  42. message(STATUS "Unable to compile program with enabled ccache, reverting...")
  43. set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${__OLD_RULE_LAUNCH_COMPILE}")
  44. endif()
  45. endif()
  46. else()
  47. message(STATUS "Looking for ccache - not found")
  48. endif()
  49. endif()
  50. if((CV_CLANG AND NOT CMAKE_GENERATOR MATCHES "Xcode") # PCH has no support for Clang
  51. OR OPENCV_COMPILER_IS_CCACHE
  52. )
  53. set(ENABLE_PRECOMPILED_HEADERS OFF CACHE BOOL "" FORCE)
  54. endif()
  55. macro(add_extra_compiler_option option)
  56. ocv_check_flag_support(CXX "${option}" _varname "${OPENCV_EXTRA_CXX_FLAGS} ${ARGN}")
  57. if(${_varname})
  58. set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
  59. endif()
  60. ocv_check_flag_support(C "${option}" _varname "${OPENCV_EXTRA_C_FLAGS} ${ARGN}")
  61. if(${_varname})
  62. set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
  63. endif()
  64. endmacro()
  65. macro(add_extra_compiler_option_force option)
  66. set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} ${option}")
  67. set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} ${option}")
  68. endmacro()
  69. # Gets environment variable and puts its value to the corresponding preprocessor definition
  70. # Useful for WINRT that has no access to environment variables
  71. macro(add_env_definitions option)
  72. set(value $ENV{${option}})
  73. if("${value}" STREQUAL "")
  74. message(WARNING "${option} environment variable is empty. Please set it to appropriate location to get correct results")
  75. else()
  76. string(REPLACE "\\" "\\\\" value ${value})
  77. endif()
  78. add_definitions("-D${option}=\"${value}\"")
  79. endmacro()
  80. if(NOT MSVC)
  81. # OpenCV fails some tests when 'char' is 'unsigned' by default
  82. add_extra_compiler_option(-fsigned-char)
  83. endif()
  84. if(MSVC)
  85. if(NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " /fp:")
  86. if(ENABLE_FAST_MATH)
  87. add_extra_compiler_option("/fp:fast")
  88. else()
  89. add_extra_compiler_option("/fp:precise")
  90. endif()
  91. endif()
  92. elseif(CV_ICC)
  93. if(NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " /fp:"
  94. AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -fp-model"
  95. )
  96. if(NOT ENABLE_FAST_MATH)
  97. add_extra_compiler_option("-fp-model precise")
  98. endif()
  99. endif()
  100. elseif(CV_GCC OR CV_CLANG)
  101. if(ENABLE_FAST_MATH)
  102. add_extra_compiler_option(-ffast-math)
  103. endif()
  104. endif()
  105. if(CV_GCC OR CV_CLANG)
  106. # High level of warnings.
  107. add_extra_compiler_option(-W)
  108. if (NOT MSVC)
  109. # clang-cl interprets -Wall as MSVC would: -Weverything, which is more than
  110. # we want.
  111. add_extra_compiler_option(-Wall)
  112. endif()
  113. add_extra_compiler_option(-Wreturn-type)
  114. add_extra_compiler_option(-Wnon-virtual-dtor)
  115. add_extra_compiler_option(-Waddress)
  116. add_extra_compiler_option(-Wsequence-point)
  117. add_extra_compiler_option(-Wformat)
  118. add_extra_compiler_option(-Wformat-security -Wformat)
  119. add_extra_compiler_option(-Wmissing-declarations)
  120. add_extra_compiler_option(-Wmissing-prototypes)
  121. add_extra_compiler_option(-Wstrict-prototypes)
  122. add_extra_compiler_option(-Wundef)
  123. add_extra_compiler_option(-Winit-self)
  124. add_extra_compiler_option(-Wpointer-arith)
  125. if(NOT (CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "5.0"))
  126. add_extra_compiler_option(-Wshadow) # old GCC emits warnings for variables + methods combination
  127. endif()
  128. add_extra_compiler_option(-Wsign-promo)
  129. add_extra_compiler_option(-Wuninitialized)
  130. if(CV_GCC AND (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0) AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0))
  131. add_extra_compiler_option(-Wno-psabi)
  132. endif()
  133. if(HAVE_CXX11)
  134. if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT ENABLE_PRECOMPILED_HEADERS)
  135. add_extra_compiler_option(-Wsuggest-override)
  136. elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
  137. add_extra_compiler_option(-Winconsistent-missing-override)
  138. endif()
  139. endif()
  140. if(ENABLE_NOISY_WARNINGS)
  141. add_extra_compiler_option(-Wcast-align)
  142. add_extra_compiler_option(-Wstrict-aliasing=2)
  143. else()
  144. add_extra_compiler_option(-Wno-delete-non-virtual-dtor)
  145. add_extra_compiler_option(-Wno-unnamed-type-template-args)
  146. add_extra_compiler_option(-Wno-comment)
  147. if(NOT OPENCV_SKIP_IMPLICIT_FALLTHROUGH
  148. AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES "implicit-fallthrough"
  149. AND (CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0.0)
  150. )
  151. add_extra_compiler_option(-Wimplicit-fallthrough=3)
  152. endif()
  153. if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0)
  154. add_extra_compiler_option(-Wno-strict-overflow) # Issue appears when compiling surf.cpp from opencv_contrib/modules/xfeatures2d
  155. endif()
  156. if(CV_GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 5.0)
  157. add_extra_compiler_option(-Wno-missing-field-initializers) # GCC 4.x emits warnings about {}, fixed in GCC 5+
  158. endif()
  159. if(CV_CLANG AND NOT CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS 10.0)
  160. add_extra_compiler_option(-Wno-deprecated-enum-enum-conversion)
  161. add_extra_compiler_option(-Wno-deprecated-anon-enum-enum-conversion)
  162. endif()
  163. endif()
  164. add_extra_compiler_option(-fdiagnostics-show-option)
  165. # The -Wno-long-long is required in 64bit systems when including system headers.
  166. if(X86_64)
  167. add_extra_compiler_option(-Wno-long-long)
  168. endif()
  169. # We need pthread's, unless we have explicitly disabled multi-thread execution.
  170. if(NOT OPENCV_DISABLE_THREAD_SUPPORT
  171. AND (
  172. (UNIX
  173. AND NOT ANDROID
  174. AND NOT (APPLE AND CV_CLANG)
  175. AND NOT EMSCRIPTEN
  176. )
  177. OR (EMSCRIPTEN AND WITH_PTHREADS_PF) # https://github.com/opencv/opencv/issues/20285
  178. )
  179. ) # TODO
  180. add_extra_compiler_option(-pthread)
  181. endif()
  182. if(CV_CLANG)
  183. add_extra_compiler_option(-Qunused-arguments)
  184. endif()
  185. if(OPENCV_WARNINGS_ARE_ERRORS)
  186. add_extra_compiler_option(-Werror)
  187. endif()
  188. if(APPLE)
  189. add_extra_compiler_option(-Wno-semicolon-before-method-body)
  190. endif()
  191. # Other optimizations
  192. if(ENABLE_OMIT_FRAME_POINTER)
  193. add_extra_compiler_option(-fomit-frame-pointer)
  194. elseif(DEFINED ENABLE_OMIT_FRAME_POINTER)
  195. add_extra_compiler_option(-fno-omit-frame-pointer)
  196. endif()
  197. # Profiling?
  198. if(ENABLE_PROFILING)
  199. # turn off incompatible options
  200. foreach(flags CMAKE_CXX_FLAGS CMAKE_C_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS_DEBUG
  201. OPENCV_EXTRA_FLAGS_RELEASE OPENCV_EXTRA_FLAGS_DEBUG OPENCV_EXTRA_C_FLAGS OPENCV_EXTRA_CXX_FLAGS)
  202. string(REPLACE "-fomit-frame-pointer" "" ${flags} "${${flags}}")
  203. string(REPLACE "-ffunction-sections" "" ${flags} "${${flags}}")
  204. string(REPLACE "-fdata-sections" "" ${flags} "${${flags}}")
  205. endforeach()
  206. # -pg should be placed both in the linker and in the compiler settings
  207. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -pg")
  208. add_extra_compiler_option("-pg -g")
  209. else()
  210. if(MSVC)
  211. # TODO: Clang/C2 is not supported
  212. elseif(((IOS OR ANDROID) AND NOT BUILD_SHARED_LIBS) AND NOT OPENCV_FORCE_FUNCTIONS_SECTIONS)
  213. # don't create separate sections for functions/data, reduce package size
  214. else()
  215. # Remove unreferenced functions: function level linking
  216. add_extra_compiler_option(-ffunction-sections)
  217. add_extra_compiler_option(-fdata-sections)
  218. if(NOT OPENCV_SKIP_GC_SECTIONS)
  219. if(APPLE)
  220. set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,-dead_strip")
  221. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} -Wl,-dead_strip")
  222. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} -Wl,-dead_strip")
  223. else()
  224. set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} -Wl,--gc-sections")
  225. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
  226. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} -Wl,--gc-sections")
  227. endif()
  228. endif()
  229. endif()
  230. endif()
  231. if(ENABLE_COVERAGE)
  232. set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} --coverage")
  233. set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} --coverage")
  234. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} --coverage")
  235. endif()
  236. if(ENABLE_INSTRUMENTATION)
  237. if(NOT HAVE_CXX11)
  238. message(WARNING "ENABLE_INSTRUMENTATION requires C++11 support")
  239. endif()
  240. set(WITH_VTK OFF) # There are issues with VTK 6.0
  241. endif()
  242. if(ENABLE_LTO)
  243. add_extra_compiler_option(-flto)
  244. endif()
  245. if(ENABLE_THIN_LTO)
  246. add_extra_compiler_option(-flto=thin)
  247. endif()
  248. set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} -DNDEBUG")
  249. if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG} " MATCHES "-O")
  250. set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -O0")
  251. endif()
  252. set(OPENCV_EXTRA_FLAGS_DEBUG "${OPENCV_EXTRA_FLAGS_DEBUG} -DDEBUG -D_DEBUG")
  253. if(BUILD_WITH_DEBUG_INFO)
  254. # https://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html
  255. # '-g' is equal to '-g2', '-g1' produces minimal information, enough for making backtraces
  256. ocv_update(OPENCV_DEBUG_OPTION "-g1")
  257. if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS_RELEASE}" MATCHES " -g")
  258. set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} ${OPENCV_DEBUG_OPTION}")
  259. endif()
  260. endif()
  261. endif()
  262. if(MSVC)
  263. #TODO Code refactoring is required to resolve security warnings
  264. #if(NOT ENABLE_BUILD_HARDENING)
  265. set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS")
  266. #endif()
  267. if(BUILD_WITH_DEBUG_INFO)
  268. set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /Zi")
  269. set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /debug")
  270. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE} /debug")
  271. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_MODULE_LINKER_FLAGS_RELEASE} /debug")
  272. endif()
  273. # Remove unreferenced functions: function level linking
  274. set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /Gy")
  275. if(NOT MSVC_VERSION LESS 1400)
  276. set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /bigobj")
  277. endif()
  278. if(OPENCV_WARNINGS_ARE_ERRORS)
  279. set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /WX")
  280. endif()
  281. if(ENABLE_LTO)
  282. set(OPENCV_EXTRA_FLAGS_RELEASE "${OPENCV_EXTRA_FLAGS_RELEASE} /GL")
  283. set(OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE "${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE} /LTCG")
  284. endif()
  285. if(NOT MSVC_VERSION LESS 1800 AND NOT CMAKE_GENERATOR MATCHES "Visual Studio")
  286. set(OPENCV_EXTRA_C_FLAGS "${OPENCV_EXTRA_C_FLAGS} /FS")
  287. set(OPENCV_EXTRA_CXX_FLAGS "${OPENCV_EXTRA_CXX_FLAGS} /FS")
  288. endif()
  289. endif()
  290. if(PROJECT_NAME STREQUAL "OpenCV")
  291. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVCompilerOptimizations.cmake")
  292. endif()
  293. if(COMMAND ocv_compiler_optimization_options)
  294. ocv_compiler_optimization_options()
  295. endif()
  296. if(COMMAND ocv_compiler_optimization_options_finalize)
  297. ocv_compiler_optimization_options_finalize()
  298. endif()
  299. # set default visibility to hidden
  300. if((CV_GCC OR CV_CLANG)
  301. AND NOT MSVC
  302. AND NOT OPENCV_SKIP_VISIBILITY_HIDDEN
  303. AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -fvisibility")
  304. add_extra_compiler_option(-fvisibility=hidden)
  305. add_extra_compiler_option(-fvisibility-inlines-hidden)
  306. endif()
  307. # workaround gcc bug for aligned ld/st
  308. # https://github.com/opencv/opencv/issues/13211
  309. if((PPC64LE AND NOT CMAKE_CROSSCOMPILING) OR OPENCV_FORCE_COMPILER_CHECK_VSX_ALIGNED)
  310. ocv_check_runtime_flag("${CPU_BASELINE_FLAGS}" OPENCV_CHECK_VSX_ALIGNED "${OpenCV_SOURCE_DIR}/cmake/checks/runtime/cpu_vsx_aligned.cpp")
  311. if(NOT OPENCV_CHECK_VSX_ALIGNED)
  312. add_extra_compiler_option_force(-DCV_COMPILER_VSX_BROKEN_ALIGNED)
  313. endif()
  314. endif()
  315. # validate inline asm with fixes register number and constraints wa, wd, wf
  316. if(PPC64LE)
  317. ocv_check_compiler_flag(CXX "${CPU_BASELINE_FLAGS}" OPENCV_CHECK_VSX_ASM "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx_asm.cpp")
  318. if(NOT OPENCV_CHECK_VSX_ASM)
  319. add_extra_compiler_option_force(-DCV_COMPILER_VSX_BROKEN_ASM)
  320. endif()
  321. endif()
  322. # Apply "-Wl,--as-needed" linker flags: https://github.com/opencv/opencv/issues/7001
  323. if(NOT OPENCV_SKIP_LINK_AS_NEEDED)
  324. if(UNIX AND (NOT APPLE OR NOT CMAKE_VERSION VERSION_LESS "3.2"))
  325. set(_option "-Wl,--as-needed")
  326. set(_saved_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  327. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_option}") # requires CMake 3.2+ and CMP0056
  328. ocv_check_compiler_flag(CXX "" HAVE_LINK_AS_NEEDED)
  329. set(CMAKE_EXE_LINKER_FLAGS "${_saved_CMAKE_EXE_LINKER_FLAGS}")
  330. if(HAVE_LINK_AS_NEEDED)
  331. set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} ${_option}")
  332. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} ${_option}")
  333. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} ${_option}")
  334. endif()
  335. endif()
  336. endif()
  337. # Apply "-Wl,--no-undefined" linker flags: https://github.com/opencv/opencv/pull/21347
  338. if(NOT OPENCV_SKIP_LINK_NO_UNDEFINED)
  339. if(UNIX AND (NOT APPLE OR NOT CMAKE_VERSION VERSION_LESS "3.2"))
  340. set(_option "-Wl,--no-undefined")
  341. set(_saved_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  342. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_option}") # requires CMake 3.2+ and CMP0056
  343. ocv_check_compiler_flag(CXX "" HAVE_LINK_NO_UNDEFINED)
  344. set(CMAKE_EXE_LINKER_FLAGS "${_saved_CMAKE_EXE_LINKER_FLAGS}")
  345. if(HAVE_LINK_NO_UNDEFINED)
  346. set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} ${_option}")
  347. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} ${_option}")
  348. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} ${_option}")
  349. endif()
  350. endif()
  351. endif()
  352. # combine all "extra" options
  353. if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
  354. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
  355. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
  356. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
  357. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
  358. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
  359. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
  360. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
  361. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
  362. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
  363. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS}")
  364. set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE}")
  365. set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_DEBUG}")
  366. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${OPENCV_EXTRA_MODULE_LINKER_FLAGS}")
  367. set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_MODULE_LINKER_FLAGS_RELEASE}")
  368. set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_MODULE_LINKER_FLAGS_DEBUG}")
  369. endif()
  370. if(MSVC)
  371. if(NOT ENABLE_NOISY_WARNINGS)
  372. if(MSVC_VERSION EQUAL 1400)
  373. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
  374. endif()
  375. endif()
  376. foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  377. string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
  378. endforeach()
  379. # Enable 'extern "C"' and asynchronous (division by zero, access violation) exceptions
  380. if(NOT OPENCV_SKIP_MSVC_EXCEPTIONS_FLAG)
  381. foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  382. string(REGEX REPLACE " /EH[^ ]* " " " ${flags} " ${${flags}}")
  383. endforeach()
  384. if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " /EH")
  385. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
  386. endif()
  387. endif()
  388. # Enable [[attribute]] syntax checking to prevent silent failure: "attribute is ignored in this syntactic position"
  389. add_extra_compiler_option("/w15240")
  390. if(NOT ENABLE_NOISY_WARNINGS)
  391. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127) # conditional expression is constant
  392. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
  393. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
  394. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
  395. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4512) # Assignment operator could not be generated
  396. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'
  397. endif()
  398. if(CV_ICC AND NOT ENABLE_NOISY_WARNINGS)
  399. foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
  400. string(REGEX REPLACE "( |^)/W[0-9]+( |$)" "\\1\\2" ${flags} "${${flags}}")
  401. endforeach()
  402. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
  403. endif()
  404. endif()
  405. if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
  406. link_directories("/usr/local/lib")
  407. endif()
  408. if(ENABLE_BUILD_HARDENING)
  409. include("${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake")
  410. endif()
  411. if(MSVC)
  412. include("${CMAKE_CURRENT_LIST_DIR}/OpenCVCRTLinkage.cmake")
  413. add_definitions(-D_VARIADIC_MAX=10)
  414. endif()
  415. if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  416. get_directory_property(__DIRECTORY_COMPILE_DEFINITIONS COMPILE_DEFINITIONS)
  417. if((NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS_RELEASE} ${__DIRECTORY_COMPILE_DEFINITIONS}" MATCHES "_WIN32_WINNT"
  418. AND NOT OPENCV_CMAKE_SKIP_MACRO_WIN32_WINNT)
  419. OR OPENCV_CMAKE_FORCE_MACRO_WIN32_WINNT
  420. )
  421. # https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt
  422. # Target Windows 7 API
  423. set(OPENCV_CMAKE_MACRO_WIN32_WINNT "0x0601" CACHE STRING "Value of _WIN32_WINNT macro")
  424. add_definitions(-D_WIN32_WINNT=${OPENCV_CMAKE_MACRO_WIN32_WINNT})
  425. endif()
  426. endif()
  427. # Enable compiler options for OpenCV modules/apps/samples only (ignore 3rdparty)
  428. macro(ocv_add_modules_compiler_options)
  429. if(MSVC AND NOT OPENCV_SKIP_MSVC_W4_OPTION)
  430. foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  431. string(REPLACE "/W3" "/W4" ${flags} "${${flags}}")
  432. endforeach()
  433. endif()
  434. if(OPENCV_ENABLE_MEMORY_SANITIZER)
  435. add_definitions(-DOPENCV_ENABLE_MEMORY_SANITIZER=1)
  436. endif()
  437. endmacro()
  438. # adjust -Wl,-rpath-link
  439. if(CMAKE_SKIP_RPATH)
  440. if((NOT CMAKE_CROSSCOMPILING OR OPENCV_ENABLE_LINKER_RPATH_LINK_ORIGIN) AND NOT OPENCV_SKIP_LINKER_RPATH_LINK_ORIGIN)
  441. if(DEFINED CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN)
  442. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "${CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN}")
  443. else()
  444. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "\$ORIGIN")
  445. endif()
  446. elseif(NOT OPENCV_SKIP_LINKER_RPATH_LINK_BINARY_LIB)
  447. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "${LIBRARY_OUTPUT_PATH}")
  448. endif()
  449. endif()
  450. if(OPENCV_EXTRA_RPATH_LINK_PATH)
  451. string(REPLACE ":" ";" OPENCV_EXTRA_RPATH_LINK_PATH_ "${OPENCV_EXTRA_RPATH_LINK_PATH}")
  452. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH ${OPENCV_EXTRA_RPATH_LINK_PATH_})
  453. if(NOT CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG)
  454. message(WARNING "OPENCV_EXTRA_RPATH_LINK_PATH may not work properly because CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG is not defined (not supported)")
  455. endif()
  456. endif()
  457. # Control MSVC /MP flag
  458. # Input variables: OPENCV_MSVC_PARALLEL (ON,1,2,3,...) + OPENCV_SKIP_MSVC_PARALLEL
  459. # Details:
  460. # - https://docs.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes
  461. # - https://docs.microsoft.com/en-us/cpp/build/reference/cl-environment-variables
  462. # - https://gitlab.kitware.com/cmake/cmake/merge_requests/1718/diffs
  463. if(CMAKE_GENERATOR MATCHES "Visual Studio" AND CMAKE_CXX_COMPILER_ID MATCHES "MSVC|Intel")
  464. ocv_check_environment_variables(OPENCV_SKIP_MSVC_PARALLEL)
  465. if(OPENCV_SKIP_MSVC_PARALLEL)
  466. # nothing
  467. elseif(" ${CMAKE_CXX_FLAGS}" MATCHES "/MP")
  468. # nothing, already defined in compiler flags
  469. elseif(DEFINED ENV{CL} AND " $ENV{CL}" MATCHES "/MP")
  470. # nothing, compiler will use CL environment variable
  471. elseif(DEFINED ENV{_CL_} AND " $ENV{_CL_}" MATCHES "/MP")
  472. # nothing, compiler will use _CL_ environment variable
  473. else()
  474. ocv_check_environment_variables(OPENCV_MSVC_PARALLEL)
  475. set(_mp_value "ON")
  476. if(DEFINED OPENCV_MSVC_PARALLEL)
  477. set(_mp_value "${OPENCV_MSVC_PARALLEL}")
  478. endif()
  479. set(OPENCV_MSVC_PARALLEL "${_mp_value}" CACHE STRING "Control MSVC /MP flag")
  480. if(_mp_value)
  481. if(_mp_value GREATER 0)
  482. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP${_mp_value}")
  483. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP${_mp_value}")
  484. else()
  485. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
  486. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
  487. endif()
  488. endif()
  489. endif()
  490. endif()