OpenCVCompilerOptions.cmake 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  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 OR ARM))
  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. if(AARCH64 AND NOT MSVC_VERSION LESS 1930)
  290. set(OPENCV_EXTRA_FLAGS "${OPENCV_EXTRA_FLAGS} /D _ARM64_DISTINCT_NEON_TYPES")
  291. endif()
  292. endif()
  293. if(PROJECT_NAME STREQUAL "OpenCV")
  294. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVCompilerOptimizations.cmake")
  295. endif()
  296. if(COMMAND ocv_compiler_optimization_options)
  297. ocv_compiler_optimization_options()
  298. endif()
  299. if(COMMAND ocv_compiler_optimization_options_finalize)
  300. ocv_compiler_optimization_options_finalize()
  301. endif()
  302. # set default visibility to hidden
  303. if((CV_GCC OR CV_CLANG)
  304. AND NOT MSVC
  305. AND NOT OPENCV_SKIP_VISIBILITY_HIDDEN
  306. AND NOT " ${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}" MATCHES " -fvisibility")
  307. add_extra_compiler_option(-fvisibility=hidden)
  308. add_extra_compiler_option(-fvisibility-inlines-hidden)
  309. endif()
  310. # workaround gcc bug for aligned ld/st
  311. # https://github.com/opencv/opencv/issues/13211
  312. if((PPC64LE AND NOT CMAKE_CROSSCOMPILING) OR OPENCV_FORCE_COMPILER_CHECK_VSX_ALIGNED)
  313. ocv_check_runtime_flag("${CPU_BASELINE_FLAGS}" OPENCV_CHECK_VSX_ALIGNED "${OpenCV_SOURCE_DIR}/cmake/checks/runtime/cpu_vsx_aligned.cpp")
  314. if(NOT OPENCV_CHECK_VSX_ALIGNED)
  315. add_extra_compiler_option_force(-DCV_COMPILER_VSX_BROKEN_ALIGNED)
  316. endif()
  317. endif()
  318. # validate inline asm with fixes register number and constraints wa, wd, wf
  319. if(PPC64LE)
  320. ocv_check_compiler_flag(CXX "${CPU_BASELINE_FLAGS}" OPENCV_CHECK_VSX_ASM "${OpenCV_SOURCE_DIR}/cmake/checks/cpu_vsx_asm.cpp")
  321. if(NOT OPENCV_CHECK_VSX_ASM)
  322. add_extra_compiler_option_force(-DCV_COMPILER_VSX_BROKEN_ASM)
  323. endif()
  324. endif()
  325. # Apply "-Wl,--as-needed" linker flags: https://github.com/opencv/opencv/issues/7001
  326. if(NOT OPENCV_SKIP_LINK_AS_NEEDED)
  327. if(UNIX AND (NOT APPLE OR NOT CMAKE_VERSION VERSION_LESS "3.2"))
  328. set(_option "-Wl,--as-needed")
  329. set(_saved_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  330. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_option}") # requires CMake 3.2+ and CMP0056
  331. ocv_check_compiler_flag(CXX "" HAVE_LINK_AS_NEEDED)
  332. set(CMAKE_EXE_LINKER_FLAGS "${_saved_CMAKE_EXE_LINKER_FLAGS}")
  333. if(HAVE_LINK_AS_NEEDED)
  334. set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} ${_option}")
  335. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} ${_option}")
  336. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} ${_option}")
  337. endif()
  338. endif()
  339. endif()
  340. # Apply "-Wl,--no-undefined" linker flags: https://github.com/opencv/opencv/pull/21347
  341. if(NOT OPENCV_SKIP_LINK_NO_UNDEFINED)
  342. if(UNIX AND (NOT APPLE OR NOT CMAKE_VERSION VERSION_LESS "3.2"))
  343. set(_option "-Wl,--no-undefined")
  344. set(_saved_CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}")
  345. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${_option}") # requires CMake 3.2+ and CMP0056
  346. ocv_check_compiler_flag(CXX "" HAVE_LINK_NO_UNDEFINED)
  347. set(CMAKE_EXE_LINKER_FLAGS "${_saved_CMAKE_EXE_LINKER_FLAGS}")
  348. if(HAVE_LINK_NO_UNDEFINED)
  349. set(OPENCV_EXTRA_EXE_LINKER_FLAGS "${OPENCV_EXTRA_EXE_LINKER_FLAGS} ${_option}")
  350. set(OPENCV_EXTRA_SHARED_LINKER_FLAGS "${OPENCV_EXTRA_SHARED_LINKER_FLAGS} ${_option}")
  351. set(OPENCV_EXTRA_MODULE_LINKER_FLAGS "${OPENCV_EXTRA_MODULE_LINKER_FLAGS} ${_option}")
  352. endif()
  353. endif()
  354. endif()
  355. # combine all "extra" options
  356. if(NOT OPENCV_SKIP_EXTRA_COMPILER_FLAGS)
  357. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_C_FLAGS}")
  358. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS} ${OPENCV_EXTRA_CXX_FLAGS}")
  359. set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
  360. set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} ${OPENCV_EXTRA_FLAGS_RELEASE}")
  361. set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
  362. set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${OPENCV_EXTRA_FLAGS_DEBUG}")
  363. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OPENCV_EXTRA_EXE_LINKER_FLAGS}")
  364. set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_RELEASE}")
  365. set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_EXE_LINKER_FLAGS_DEBUG}")
  366. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS}")
  367. set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_RELEASE}")
  368. set(CMAKE_SHARED_LINKER_FLAGS_DEBUG "${CMAKE_SHARED_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_SHARED_LINKER_FLAGS_DEBUG}")
  369. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${OPENCV_EXTRA_MODULE_LINKER_FLAGS}")
  370. set(CMAKE_MODULE_LINKER_FLAGS_RELEASE "${CMAKE_MODULE_LINKER_FLAGS_RELEASE} ${OPENCV_EXTRA_MODULE_LINKER_FLAGS_RELEASE}")
  371. set(CMAKE_MODULE_LINKER_FLAGS_DEBUG "${CMAKE_MODULE_LINKER_FLAGS_DEBUG} ${OPENCV_EXTRA_MODULE_LINKER_FLAGS_DEBUG}")
  372. endif()
  373. if(MSVC)
  374. if(NOT ENABLE_NOISY_WARNINGS)
  375. if(MSVC_VERSION EQUAL 1400)
  376. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4510 /wd4610 /wd4312 /wd4201 /wd4244 /wd4328 /wd4267)
  377. endif()
  378. endif()
  379. foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  380. string(REPLACE "/Zm1000" "" ${flags} "${${flags}}")
  381. endforeach()
  382. # Enable 'extern "C"' and asynchronous (division by zero, access violation) exceptions
  383. if(NOT OPENCV_SKIP_MSVC_EXCEPTIONS_FLAG)
  384. foreach(flags CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_RELEASE CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  385. string(REGEX REPLACE " /EH[^ ]* " " " ${flags} " ${${flags}}")
  386. endforeach()
  387. if(NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${CMAKE_CXX_FLAGS_DEBUG}" MATCHES " /EH")
  388. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHa")
  389. endif()
  390. endif()
  391. # Enable [[attribute]] syntax checking to prevent silent failure: "attribute is ignored in this syntactic position"
  392. add_extra_compiler_option("/w15240")
  393. if(NOT ENABLE_NOISY_WARNINGS)
  394. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4127) # conditional expression is constant
  395. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4251) # class 'std::XXX' needs to have dll-interface to be used by clients of YYY
  396. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4324) # 'struct_name' : structure was padded due to __declspec(align())
  397. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4275) # non dll-interface class 'std::exception' used as base for dll-interface class 'cv::Exception'
  398. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4512) # Assignment operator could not be generated
  399. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4589) # Constructor of abstract class 'cv::ORB' ignores initializer for virtual base class 'cv::Algorithm'
  400. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4819) # Symbols like delta or epsilon cannot be represented
  401. endif()
  402. if(CV_ICC AND NOT ENABLE_NOISY_WARNINGS)
  403. foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG CMAKE_C_FLAGS CMAKE_C_FLAGS_RELEASE CMAKE_C_FLAGS_DEBUG)
  404. string(REGEX REPLACE "( |^)/W[0-9]+( |$)" "\\1\\2" ${flags} "${${flags}}")
  405. endforeach()
  406. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Qwd673") # PCH warning
  407. endif()
  408. endif()
  409. if(APPLE AND NOT CMAKE_CROSSCOMPILING AND NOT DEFINED ENV{LDFLAGS} AND EXISTS "/usr/local/lib")
  410. link_directories("/usr/local/lib")
  411. endif()
  412. if(ENABLE_BUILD_HARDENING)
  413. include("${CMAKE_CURRENT_LIST_DIR}/OpenCVCompilerDefenses.cmake")
  414. endif()
  415. if(MSVC)
  416. include("${CMAKE_CURRENT_LIST_DIR}/OpenCVCRTLinkage.cmake")
  417. add_definitions(-D_VARIADIC_MAX=10)
  418. endif()
  419. if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
  420. get_directory_property(__DIRECTORY_COMPILE_DEFINITIONS COMPILE_DEFINITIONS)
  421. if((NOT " ${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${OPENCV_EXTRA_CXX_FLAGS} ${OPENCV_EXTRA_FLAGS_RELEASE} ${__DIRECTORY_COMPILE_DEFINITIONS}" MATCHES "_WIN32_WINNT"
  422. AND NOT OPENCV_CMAKE_SKIP_MACRO_WIN32_WINNT)
  423. OR OPENCV_CMAKE_FORCE_MACRO_WIN32_WINNT
  424. )
  425. # https://docs.microsoft.com/en-us/cpp/porting/modifying-winver-and-win32-winnt
  426. # Target Windows 7 API
  427. set(OPENCV_CMAKE_MACRO_WIN32_WINNT "0x0601" CACHE STRING "Value of _WIN32_WINNT macro")
  428. add_definitions(-D_WIN32_WINNT=${OPENCV_CMAKE_MACRO_WIN32_WINNT})
  429. endif()
  430. endif()
  431. # Enable compiler options for OpenCV modules/apps/samples only (ignore 3rdparty)
  432. macro(ocv_add_modules_compiler_options)
  433. if(MSVC AND NOT OPENCV_SKIP_MSVC_W4_OPTION)
  434. foreach(flags CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_RELEASE CMAKE_CXX_FLAGS_DEBUG)
  435. string(REPLACE "/W3" "/W4" ${flags} "${${flags}}")
  436. endforeach()
  437. endif()
  438. if(OPENCV_ENABLE_MEMORY_SANITIZER)
  439. add_definitions(-DOPENCV_ENABLE_MEMORY_SANITIZER=1)
  440. endif()
  441. endmacro()
  442. # adjust -Wl,-rpath-link
  443. if(CMAKE_SKIP_RPATH)
  444. if((NOT CMAKE_CROSSCOMPILING OR OPENCV_ENABLE_LINKER_RPATH_LINK_ORIGIN) AND NOT OPENCV_SKIP_LINKER_RPATH_LINK_ORIGIN)
  445. if(DEFINED CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN)
  446. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "${CMAKE_SHARED_LIBRARY_RPATH_ORIGIN_TOKEN}")
  447. else()
  448. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "\$ORIGIN")
  449. endif()
  450. elseif(NOT OPENCV_SKIP_LINKER_RPATH_LINK_BINARY_LIB)
  451. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH "${LIBRARY_OUTPUT_PATH}")
  452. endif()
  453. endif()
  454. if(OPENCV_EXTRA_RPATH_LINK_PATH)
  455. string(REPLACE ":" ";" OPENCV_EXTRA_RPATH_LINK_PATH_ "${OPENCV_EXTRA_RPATH_LINK_PATH}")
  456. list(APPEND CMAKE_PLATFORM_RUNTIME_PATH ${OPENCV_EXTRA_RPATH_LINK_PATH_})
  457. if(NOT CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG)
  458. message(WARNING "OPENCV_EXTRA_RPATH_LINK_PATH may not work properly because CMAKE_EXECUTABLE_RPATH_LINK_CXX_FLAG is not defined (not supported)")
  459. endif()
  460. endif()
  461. # Control MSVC /MP flag
  462. # Input variables: OPENCV_MSVC_PARALLEL (ON,1,2,3,...) + OPENCV_SKIP_MSVC_PARALLEL
  463. # Details:
  464. # - https://docs.microsoft.com/en-us/cpp/build/reference/mp-build-with-multiple-processes
  465. # - https://docs.microsoft.com/en-us/cpp/build/reference/cl-environment-variables
  466. # - https://gitlab.kitware.com/cmake/cmake/merge_requests/1718/diffs
  467. if(CMAKE_GENERATOR MATCHES "Visual Studio" AND CMAKE_CXX_COMPILER_ID MATCHES "MSVC|Intel")
  468. ocv_check_environment_variables(OPENCV_SKIP_MSVC_PARALLEL)
  469. if(OPENCV_SKIP_MSVC_PARALLEL)
  470. # nothing
  471. elseif(" ${CMAKE_CXX_FLAGS}" MATCHES "/MP")
  472. # nothing, already defined in compiler flags
  473. elseif(DEFINED ENV{CL} AND " $ENV{CL}" MATCHES "/MP")
  474. # nothing, compiler will use CL environment variable
  475. elseif(DEFINED ENV{_CL_} AND " $ENV{_CL_}" MATCHES "/MP")
  476. # nothing, compiler will use _CL_ environment variable
  477. else()
  478. ocv_check_environment_variables(OPENCV_MSVC_PARALLEL)
  479. set(_mp_value "ON")
  480. if(DEFINED OPENCV_MSVC_PARALLEL)
  481. set(_mp_value "${OPENCV_MSVC_PARALLEL}")
  482. endif()
  483. set(OPENCV_MSVC_PARALLEL "${_mp_value}" CACHE STRING "Control MSVC /MP flag")
  484. if(_mp_value)
  485. if(_mp_value GREATER 0)
  486. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP${_mp_value}")
  487. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP${_mp_value}")
  488. else()
  489. set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /MP")
  490. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP")
  491. endif()
  492. endif()
  493. endif()
  494. endif()