OpenCVUtils.cmake 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018
  1. if(COMMAND ocv_cmake_dump_vars) # include guard
  2. return()
  3. endif()
  4. include(CMakeParseArguments)
  5. # Debugging function
  6. function(ocv_cmake_dump_vars)
  7. set(OPENCV_SUPPRESS_DEPRECATIONS 1) # suppress deprecation warnings from variable_watch() guards
  8. get_cmake_property(__variableNames VARIABLES)
  9. cmake_parse_arguments(DUMP "FORCE" "TOFILE" "" ${ARGN})
  10. # avoid generation of excessive logs with "--trace" or "--trace-expand" parameters
  11. # Note: `-DCMAKE_TRACE_MODE=1` should be passed to CMake through command line. It is not a CMake buildin variable for now (2020-12)
  12. # Use `cmake . -UCMAKE_TRACE_MODE` to remove this variable from cache
  13. if(CMAKE_TRACE_MODE AND NOT DUMP_FORCE)
  14. if(DUMP_TOFILE)
  15. file(WRITE ${CMAKE_BINARY_DIR}/${DUMP_TOFILE} "Skipped due to enabled CMAKE_TRACE_MODE")
  16. else()
  17. message(AUTHOR_WARNING "ocv_cmake_dump_vars() is skipped due to enabled CMAKE_TRACE_MODE")
  18. endif()
  19. return()
  20. endif()
  21. set(regex "${DUMP_UNPARSED_ARGUMENTS}")
  22. string(TOLOWER "${regex}" regex_lower)
  23. set(__VARS "")
  24. foreach(__variableName ${__variableNames})
  25. string(TOLOWER "${__variableName}" __variableName_lower)
  26. if((__variableName MATCHES "${regex}" OR __variableName_lower MATCHES "${regex_lower}")
  27. AND NOT __variableName_lower MATCHES "^__")
  28. get_property(__value VARIABLE PROPERTY "${__variableName}")
  29. set(__VARS "${__VARS}${__variableName}=${__value}\n")
  30. endif()
  31. endforeach()
  32. if(DUMP_TOFILE)
  33. file(WRITE ${CMAKE_BINARY_DIR}/${DUMP_TOFILE} "${__VARS}")
  34. else()
  35. message(AUTHOR_WARNING "${__VARS}")
  36. endif()
  37. endfunction()
  38. #
  39. # CMake script hooks support
  40. #
  41. option(OPENCV_DUMP_HOOKS_FLOW "Dump called OpenCV hooks" OFF)
  42. macro(ocv_cmake_hook_append hook_name)
  43. set(__var_name "__OPENCV_CMAKE_HOOKS_${hook_name}")
  44. set(__value "${${__var_name}}")
  45. message(STATUS "Registering hook '${hook_name}': ${ARGN}")
  46. list(APPEND __value ${ARGN})
  47. set(${__var_name} "${__value}" CACHE INTERNAL "")
  48. endmacro()
  49. macro(ocv_cmake_hook hook_name)
  50. set(__var_name "__OPENCV_CMAKE_HOOKS_${hook_name}")
  51. if(OPENCV_DUMP_HOOKS_FLOW)
  52. message(STATUS "Hook ${hook_name} ...")
  53. endif()
  54. foreach(__hook ${${__var_name}})
  55. #message(STATUS "Hook ${hook_name}: calling '${__hook}' ...")
  56. if(COMMAND "${__hook}")
  57. message(FATAL_ERROR "Indirect calling of CMake commands is not supported yet")
  58. else()
  59. include("${__hook}")
  60. endif()
  61. endforeach()
  62. endmacro()
  63. macro(ocv_cmake_reset_hooks)
  64. get_cmake_property(__variableNames VARIABLES)
  65. foreach(__variableName ${__variableNames})
  66. if(__variableName MATCHES "^__OPENCV_CMAKE_HOOKS_")
  67. unset(${__variableName})
  68. unset(${__variableName} CACHE)
  69. endif()
  70. endforeach()
  71. endmacro()
  72. macro(ocv_cmake_hook_register_dir dir)
  73. file(GLOB hook_files RELATIVE "${dir}" "${dir}/*.cmake")
  74. foreach(f ${hook_files})
  75. if(f MATCHES "^(.+)\\.cmake$")
  76. set(hook_name "${CMAKE_MATCH_1}")
  77. ocv_cmake_hook_append(${hook_name} "${dir}/${f}")
  78. endif()
  79. endforeach()
  80. endmacro()
  81. function(ocv_cmake_eval var_name)
  82. if(DEFINED ${var_name})
  83. file(WRITE "${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake" ${${var_name}})
  84. include("${CMAKE_BINARY_DIR}/CMakeCommand-${var_name}.cmake")
  85. endif()
  86. if(";${ARGN};" MATCHES ";ONCE;")
  87. unset(${var_name} CACHE)
  88. endif()
  89. endfunction()
  90. macro(ocv_cmake_configure file_name var_name)
  91. file(READ "${file_name}" __config)
  92. string(CONFIGURE "${__config}" ${var_name} ${ARGN})
  93. endmacro()
  94. macro(ocv_update VAR)
  95. if(NOT DEFINED ${VAR})
  96. if("x${ARGN}" STREQUAL "x")
  97. set(${VAR} "")
  98. else()
  99. set(${VAR} ${ARGN})
  100. endif()
  101. else()
  102. #ocv_debug_message("Preserve old value for ${VAR}: ${${VAR}}")
  103. endif()
  104. endmacro()
  105. function(_ocv_access_removed_variable VAR ACCESS)
  106. if(ACCESS STREQUAL "MODIFIED_ACCESS")
  107. set(OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE_${VAR} 1 PARENT_SCOPE)
  108. return()
  109. endif()
  110. if(ACCESS MATCHES "UNKNOWN_.*"
  111. AND NOT OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE
  112. AND NOT OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE_${VAR}
  113. )
  114. message(WARNING "OpenCV: Variable has been removed from CMake scripts: ${VAR}")
  115. set(OPENCV_SUPPRESS_MESSAGE_REMOVED_VARIABLE_${VAR} 1 PARENT_SCOPE) # suppress similar messages
  116. endif()
  117. endfunction()
  118. macro(ocv_declare_removed_variable VAR)
  119. if(NOT DEFINED ${VAR}) # don't hit external variables
  120. variable_watch(${VAR} _ocv_access_removed_variable)
  121. endif()
  122. endmacro()
  123. macro(ocv_declare_removed_variables)
  124. foreach(_var ${ARGN})
  125. ocv_declare_removed_variable(${_var})
  126. endforeach()
  127. endmacro()
  128. # Search packages for the host system instead of packages for the target system
  129. # in case of cross compilation these macros should be defined by the toolchain file
  130. if(NOT COMMAND find_host_package)
  131. macro(find_host_package)
  132. find_package(${ARGN})
  133. endmacro()
  134. endif()
  135. if(NOT COMMAND find_host_program)
  136. macro(find_host_program)
  137. find_program(${ARGN})
  138. endmacro()
  139. endif()
  140. # assert macro
  141. # Note: it doesn't support lists in arguments
  142. # Usage samples:
  143. # ocv_assert(MyLib_FOUND)
  144. # ocv_assert(DEFINED MyLib_INCLUDE_DIRS)
  145. macro(ocv_assert)
  146. if(NOT (${ARGN}))
  147. string(REPLACE ";" " " __assert_msg "${ARGN}")
  148. message(AUTHOR_WARNING "Assertion failed: ${__assert_msg}")
  149. endif()
  150. endmacro()
  151. macro(ocv_debug_message)
  152. if(OPENCV_CMAKE_DEBUG_MESSAGES)
  153. string(REPLACE ";" " " __msg "${ARGN}")
  154. message(STATUS "${__msg}")
  155. endif()
  156. endmacro()
  157. macro(ocv_check_environment_variables)
  158. foreach(_var ${ARGN})
  159. if(" ${${_var}}" STREQUAL " " AND DEFINED ENV{${_var}})
  160. set(__value "$ENV{${_var}}")
  161. file(TO_CMAKE_PATH "${__value}" __value) # Assume that we receive paths
  162. set(${_var} "${__value}")
  163. message(STATUS "Update variable ${_var} from environment: ${${_var}}")
  164. endif()
  165. endforeach()
  166. endmacro()
  167. macro(ocv_path_join result_var P1 P2_)
  168. string(REGEX REPLACE "^[/]+" "" P2 "${P2_}")
  169. if("${P1}" STREQUAL "" OR "${P1}" STREQUAL ".")
  170. set(${result_var} "${P2}")
  171. elseif("${P1}" STREQUAL "/")
  172. set(${result_var} "/${P2}")
  173. elseif("${P2}" STREQUAL "")
  174. set(${result_var} "${P1}")
  175. else()
  176. set(${result_var} "${P1}/${P2}")
  177. endif()
  178. string(REPLACE "\\\\" "\\" ${result_var} "${${result_var}}")
  179. string(REPLACE "//" "/" ${result_var} "${${result_var}}")
  180. string(REGEX REPLACE "(^|[/\\])[\\.][/\\]" "\\1" ${result_var} "${${result_var}}")
  181. if("${${result_var}}" STREQUAL "")
  182. set(${result_var} ".")
  183. endif()
  184. #message(STATUS "'${P1}' '${P2_}' => '${${result_var}}'")
  185. endmacro()
  186. # Used to parse Android SDK 'source.properties' files
  187. # File lines format:
  188. # - '<var_name>=<value>' (with possible 'space' symbols around '=')
  189. # - '#<any comment>'
  190. # Parsed values are saved into CMake variables:
  191. # - '${var_prefix}_${var_name}'
  192. # Flags:
  193. # - 'CACHE_VAR <var1> <var2>' - put these properties into CMake internal cache
  194. # - 'MSG_PREFIX <msg>' - prefix string for emitted messages
  195. # - flag 'VALIDATE' - emit messages about missing values from required cached variables
  196. # - flag 'WARNING' - emit CMake WARNING instead of STATUS messages
  197. function(ocv_parse_properties_file file var_prefix)
  198. cmake_parse_arguments(PARSE_PROPERTIES_PARAM "VALIDATE;WARNING" "" "CACHE_VAR;MSG_PREFIX" ${ARGN})
  199. set(__msg_type STATUS)
  200. if(PARSE_PROPERTIES_PARAM_WARNING)
  201. set(__msg_type WARNING)
  202. endif()
  203. if(EXISTS "${file}")
  204. set(SOURCE_PROPERTIES_REGEX "^[ ]*([^=:\n\"' ]+)[ ]*=[ ]*(.*)$")
  205. file(STRINGS "${file}" SOURCE_PROPERTIES_LINES REGEX "^[ ]*[^#].*$")
  206. foreach(line ${SOURCE_PROPERTIES_LINES})
  207. if(line MATCHES "${SOURCE_PROPERTIES_REGEX}")
  208. set(__name "${CMAKE_MATCH_1}")
  209. set(__value "${CMAKE_MATCH_2}")
  210. string(REGEX REPLACE "[^a-zA-Z0-9_]" "_" __name ${__name})
  211. if(";${PARSE_PROPERTIES_PARAM_CACHE_VAR};" MATCHES ";${__name};")
  212. set(${var_prefix}_${__name} "${__value}" CACHE INTERNAL "from ${file}")
  213. else()
  214. set(${var_prefix}_${__name} "${__value}" PARENT_SCOPE)
  215. endif()
  216. else()
  217. message(${__msg_type} "${PARSE_PROPERTIES_PARAM_MSG_PREFIX}Can't parse source property: '${line}' (from ${file})")
  218. endif()
  219. endforeach()
  220. if(PARSE_PROPERTIES_PARAM_VALIDATE)
  221. set(__missing "")
  222. foreach(__name ${PARSE_PROPERTIES_PARAM_CACHE_VAR})
  223. if(NOT DEFINED ${var_prefix}_${__name})
  224. list(APPEND __missing ${__name})
  225. endif()
  226. endforeach()
  227. if(__missing)
  228. message(${__msg_type} "${PARSE_PROPERTIES_PARAM_MSG_PREFIX}Can't read properties '${__missing}' from '${file}'")
  229. endif()
  230. endif()
  231. else()
  232. message(${__msg_type} "${PARSE_PROPERTIES_PARAM_MSG_PREFIX}Can't find file: ${file}")
  233. endif()
  234. endfunction()
  235. # rename modules target to world if needed
  236. macro(_ocv_fix_target target_var)
  237. if(BUILD_opencv_world)
  238. if(OPENCV_MODULE_${${target_var}}_IS_PART_OF_WORLD)
  239. set(${target_var} opencv_world)
  240. endif()
  241. endif()
  242. endmacro()
  243. # check if "sub" (file or dir) is below "dir"
  244. function(ocv_is_subdir res dir sub )
  245. get_filename_component(dir "${dir}" ABSOLUTE)
  246. get_filename_component(sub "${sub}" ABSOLUTE)
  247. file(TO_CMAKE_PATH "${dir}" dir)
  248. file(TO_CMAKE_PATH "${sub}" sub)
  249. set(dir "${dir}/")
  250. string(LENGTH "${dir}" len)
  251. string(LENGTH "${sub}" len_sub)
  252. if(NOT len GREATER len_sub)
  253. string(SUBSTRING "${sub}" 0 ${len} prefix)
  254. endif()
  255. if(prefix AND prefix STREQUAL dir)
  256. set(${res} TRUE PARENT_SCOPE)
  257. else()
  258. set(${res} FALSE PARENT_SCOPE)
  259. endif()
  260. endfunction()
  261. function(ocv_is_opencv_directory result_var dir)
  262. set(result FALSE)
  263. foreach(parent ${OpenCV_SOURCE_DIR} ${OpenCV_BINARY_DIR} ${OPENCV_EXTRA_MODULES_PATH})
  264. ocv_is_subdir(result "${parent}" "${dir}")
  265. if(result)
  266. break()
  267. endif()
  268. endforeach()
  269. set(${result_var} ${result} PARENT_SCOPE)
  270. endfunction()
  271. # adds include directories in such a way that directories from the OpenCV source tree go first
  272. function(ocv_include_directories)
  273. ocv_debug_message("ocv_include_directories( ${ARGN} )")
  274. set(__add_before "")
  275. foreach(dir ${ARGN})
  276. ocv_is_opencv_directory(__is_opencv_dir "${dir}")
  277. if(__is_opencv_dir)
  278. list(APPEND __add_before "${dir}")
  279. elseif(((CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0") OR CV_CLANG) AND
  280. dir MATCHES "/usr/include$")
  281. # workaround for GCC 6.x bug
  282. else()
  283. include_directories(AFTER SYSTEM "${dir}")
  284. endif()
  285. endforeach()
  286. include_directories(BEFORE ${__add_before})
  287. endfunction()
  288. function(ocv_append_target_property target prop)
  289. get_target_property(val ${target} ${prop})
  290. if(val)
  291. set(val "${val} ${ARGN}")
  292. set_target_properties(${target} PROPERTIES ${prop} "${val}")
  293. else()
  294. set_target_properties(${target} PROPERTIES ${prop} "${ARGN}")
  295. endif()
  296. endfunction()
  297. if(DEFINED OPENCV_DEPENDANT_TARGETS_LIST)
  298. foreach(v ${OPENCV_DEPENDANT_TARGETS_LIST})
  299. unset(${v} CACHE)
  300. endforeach()
  301. unset(OPENCV_DEPENDANT_TARGETS_LIST CACHE)
  302. endif()
  303. function(ocv_append_dependant_targets target)
  304. #ocv_debug_message("ocv_append_dependant_targets(${target} ${ARGN})")
  305. _ocv_fix_target(target)
  306. list(FIND OPENCV_DEPENDANT_TARGETS_LIST "OPENCV_DEPENDANT_TARGETS_${target}" __id)
  307. if(__id EQUAL -1)
  308. list(APPEND OPENCV_DEPENDANT_TARGETS_LIST "OPENCV_DEPENDANT_TARGETS_${target}")
  309. list(SORT OPENCV_DEPENDANT_TARGETS_LIST)
  310. set(OPENCV_DEPENDANT_TARGETS_LIST "${OPENCV_DEPENDANT_TARGETS_LIST}" CACHE INTERNAL "")
  311. endif()
  312. set(OPENCV_DEPENDANT_TARGETS_${target} "${OPENCV_DEPENDANT_TARGETS_${target}};${ARGN}" CACHE INTERNAL "" FORCE)
  313. endfunction()
  314. # adds include directories in such a way that directories from the OpenCV source tree go first
  315. function(ocv_target_include_directories target)
  316. #ocv_debug_message("ocv_target_include_directories(${target} ${ARGN})")
  317. _ocv_fix_target(target)
  318. set(__params "")
  319. if(CV_GCC AND NOT CMAKE_CXX_COMPILER_VERSION VERSION_LESS "6.0" AND
  320. ";${ARGN};" MATCHES "/usr/include;")
  321. return() # workaround for GCC 6.x bug
  322. endif()
  323. set(__params "")
  324. set(__system_params "")
  325. set(__var_name __params)
  326. foreach(dir ${ARGN})
  327. if("${dir}" STREQUAL "SYSTEM")
  328. set(__var_name __system_params)
  329. else()
  330. get_filename_component(__abs_dir "${dir}" ABSOLUTE)
  331. ocv_is_opencv_directory(__is_opencv_dir "${dir}")
  332. if(__is_opencv_dir)
  333. list(APPEND ${__var_name} "${__abs_dir}")
  334. else()
  335. list(APPEND ${__var_name} "${dir}")
  336. endif()
  337. endif()
  338. endforeach()
  339. if(HAVE_CUDA OR CMAKE_VERSION VERSION_LESS 2.8.11)
  340. include_directories(${__params})
  341. include_directories(SYSTEM ${__system_params})
  342. else()
  343. if(TARGET ${target})
  344. if(__params)
  345. target_include_directories(${target} PRIVATE ${__params})
  346. if(OPENCV_DEPENDANT_TARGETS_${target})
  347. foreach(t ${OPENCV_DEPENDANT_TARGETS_${target}})
  348. target_include_directories(${t} PRIVATE ${__params})
  349. endforeach()
  350. endif()
  351. endif()
  352. if(__system_params)
  353. target_include_directories(${target} SYSTEM PRIVATE ${__system_params})
  354. if(OPENCV_DEPENDANT_TARGETS_${target})
  355. foreach(t ${OPENCV_DEPENDANT_TARGETS_${target}})
  356. target_include_directories(${t} SYSTEM PRIVATE ${__system_params})
  357. endforeach()
  358. endif()
  359. endif()
  360. else()
  361. if(__params)
  362. set(__new_inc ${OCV_TARGET_INCLUDE_DIRS_${target}})
  363. list(APPEND __new_inc ${__params})
  364. set(OCV_TARGET_INCLUDE_DIRS_${target} "${__new_inc}" CACHE INTERNAL "")
  365. endif()
  366. if(__system_params)
  367. set(__new_inc ${OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target}})
  368. list(APPEND __new_inc ${__system_params})
  369. set(OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target} "${__new_inc}" CACHE INTERNAL "")
  370. endif()
  371. endif()
  372. endif()
  373. endfunction()
  374. # clears all passed variables
  375. macro(ocv_clear_vars)
  376. foreach(_var ${ARGN})
  377. unset(${_var})
  378. unset(${_var} CACHE)
  379. endforeach()
  380. endmacro()
  381. # Clears passed variables with INTERNAL type from CMake cache
  382. macro(ocv_clear_internal_cache_vars)
  383. foreach(_var ${ARGN})
  384. get_property(_propertySet CACHE ${_var} PROPERTY TYPE SET)
  385. if(_propertySet)
  386. get_property(_type CACHE ${_var} PROPERTY TYPE)
  387. if(_type STREQUAL "INTERNAL")
  388. message("Cleaning INTERNAL cached variable: ${_var}")
  389. unset(${_var} CACHE)
  390. endif()
  391. endif()
  392. endforeach()
  393. unset(_propertySet)
  394. unset(_type)
  395. endmacro()
  396. set(OCV_COMPILER_FAIL_REGEX
  397. "argument .* is not valid" # GCC 9+ (including support of unicode quotes)
  398. "command[- ]line option .* is valid for .* but not for C\\+\\+" # GNU
  399. "command[- ]line option .* is valid for .* but not for C" # GNU
  400. "unrecognized .*option" # GNU
  401. "unknown .*option" # Clang
  402. "ignoring unknown option" # MSVC
  403. "warning D9002" # MSVC, any lang
  404. "option .*not supported" # Intel
  405. "[Uu]nknown option" # HP
  406. "[Ww]arning: [Oo]ption" # SunPro
  407. "command option .* is not recognized" # XL
  408. "not supported in this configuration, ignored" # AIX (';' is replaced with ',')
  409. "File with unknown suffix passed to linker" # PGI
  410. "WARNING: unknown flag:" # Open64
  411. )
  412. MACRO(ocv_check_compiler_flag LANG FLAG RESULT)
  413. set(_fname "${ARGN}")
  414. if(NOT DEFINED ${RESULT})
  415. if(_fname)
  416. # nothing
  417. elseif("_${LANG}_" MATCHES "_CXX_")
  418. set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.cxx")
  419. if("${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
  420. FILE(WRITE "${_fname}" "int main() { return 0; }\n")
  421. else()
  422. FILE(WRITE "${_fname}" "#pragma\nint main() { return 0; }\n")
  423. endif()
  424. elseif("_${LANG}_" MATCHES "_C_")
  425. set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.c")
  426. if("${CMAKE_C_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_C_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
  427. FILE(WRITE "${_fname}" "int main(void) { return 0; }\n")
  428. else()
  429. FILE(WRITE "${_fname}" "#pragma\nint main(void) { return 0; }\n")
  430. endif()
  431. elseif("_${LANG}_" MATCHES "_OBJCXX_")
  432. set(_fname "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/src.mm")
  433. if("${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror " OR "${CMAKE_CXX_FLAGS} ${FLAG} " MATCHES "-Werror=unknown-pragmas ")
  434. FILE(WRITE "${_fname}" "int main() { return 0; }\n")
  435. else()
  436. FILE(WRITE "${_fname}" "#pragma\nint main() { return 0; }\n")
  437. endif()
  438. else()
  439. unset(_fname)
  440. endif()
  441. if(_fname)
  442. if(NOT "x${ARGN}" STREQUAL "x")
  443. file(RELATIVE_PATH __msg "${CMAKE_SOURCE_DIR}" "${ARGN}")
  444. set(__msg " (check file: ${__msg})")
  445. else()
  446. set(__msg "")
  447. endif()
  448. if(CMAKE_REQUIRED_LIBRARIES)
  449. set(__link_libs LINK_LIBRARIES ${CMAKE_REQUIRED_LIBRARIES})
  450. else()
  451. set(__link_libs)
  452. endif()
  453. set(__cmake_flags "")
  454. if(CMAKE_EXE_LINKER_FLAGS) # CMP0056 do this on new CMake
  455. list(APPEND __cmake_flags "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}")
  456. endif()
  457. # CMP0067 do this on new CMake
  458. if(DEFINED CMAKE_CXX_STANDARD)
  459. list(APPEND __cmake_flags "-DCMAKE_CXX_STANDARD=${CMAKE_CXX_STANDARD}")
  460. endif()
  461. if(DEFINED CMAKE_CXX_STANDARD_REQUIRED)
  462. list(APPEND __cmake_flags "-DCMAKE_CXX_STANDARD_REQUIRED=${CMAKE_CXX_STANDARD_REQUIRED}")
  463. endif()
  464. if(DEFINED CMAKE_CXX_EXTENSIONS)
  465. list(APPEND __cmake_flags "-DCMAKE_CXX_EXTENSIONS=${CMAKE_CXX_EXTENSIONS}")
  466. endif()
  467. MESSAGE(STATUS "Performing Test ${RESULT}${__msg}")
  468. TRY_COMPILE(${RESULT}
  469. "${CMAKE_BINARY_DIR}"
  470. "${_fname}"
  471. CMAKE_FLAGS ${__cmake_flags}
  472. COMPILE_DEFINITIONS "${FLAG}"
  473. ${__link_libs}
  474. OUTPUT_VARIABLE OUTPUT)
  475. if(${RESULT})
  476. string(REPLACE ";" "," OUTPUT_LINES "${OUTPUT}")
  477. string(REPLACE "\n" ";" OUTPUT_LINES "${OUTPUT_LINES}")
  478. foreach(_regex ${OCV_COMPILER_FAIL_REGEX})
  479. if(NOT ${RESULT})
  480. break()
  481. endif()
  482. foreach(_line ${OUTPUT_LINES})
  483. if("${_line}" MATCHES "${_regex}")
  484. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  485. "Build output check failed:\n"
  486. " Regex: '${_regex}'\n"
  487. " Output line: '${_line}'\n")
  488. set(${RESULT} 0)
  489. break()
  490. endif()
  491. endforeach()
  492. endforeach()
  493. endif()
  494. IF(${RESULT})
  495. SET(${RESULT} 1 CACHE INTERNAL "Test ${RESULT}")
  496. MESSAGE(STATUS "Performing Test ${RESULT} - Success")
  497. ELSE(${RESULT})
  498. MESSAGE(STATUS "Performing Test ${RESULT} - Failed")
  499. SET(${RESULT} "" CACHE INTERNAL "Test ${RESULT}")
  500. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  501. "Compilation failed:\n"
  502. " source file: '${_fname}'\n"
  503. " check option: '${FLAG}'\n"
  504. "===== BUILD LOG =====\n"
  505. "${OUTPUT}\n"
  506. "===== END =====\n\n")
  507. ENDIF(${RESULT})
  508. else()
  509. SET(${RESULT} 0)
  510. endif()
  511. endif()
  512. ENDMACRO()
  513. macro(ocv_check_flag_support lang flag varname base_options)
  514. if(CMAKE_BUILD_TYPE)
  515. set(CMAKE_TRY_COMPILE_CONFIGURATION ${CMAKE_BUILD_TYPE})
  516. endif()
  517. if("_${lang}_" MATCHES "_CXX_")
  518. set(_lang CXX)
  519. elseif("_${lang}_" MATCHES "_C_")
  520. set(_lang C)
  521. elseif("_${lang}_" MATCHES "_OBJCXX_")
  522. if(DEFINED CMAKE_OBJCXX_COMPILER) # CMake 3.16+ and enable_language(OBJCXX) call are required
  523. set(_lang OBJCXX)
  524. else()
  525. set(_lang CXX)
  526. endif()
  527. else()
  528. set(_lang ${lang})
  529. endif()
  530. string(TOUPPER "${flag}" ${varname})
  531. string(REGEX REPLACE "^(/|-)" "HAVE_${_lang}_" ${varname} "${${varname}}")
  532. string(REGEX REPLACE " -|-|=| |\\.|," "_" ${varname} "${${varname}}")
  533. if(DEFINED CMAKE_${_lang}_COMPILER)
  534. ocv_check_compiler_flag("${_lang}" "${base_options} ${flag}" ${${varname}} ${ARGN})
  535. endif()
  536. endmacro()
  537. macro(ocv_check_runtime_flag flag result)
  538. set(_fname "${ARGN}")
  539. if(NOT DEFINED ${result})
  540. file(RELATIVE_PATH _rname "${CMAKE_SOURCE_DIR}" "${_fname}")
  541. message(STATUS "Performing Runtime Test ${result} (check file: ${_rname})")
  542. try_run(exec_return compile_result
  543. "${CMAKE_BINARY_DIR}"
  544. "${_fname}"
  545. CMAKE_FLAGS "-DCMAKE_EXE_LINKER_FLAGS=${CMAKE_EXE_LINKER_FLAGS}" # CMP0056 do this on new CMake
  546. COMPILE_DEFINITIONS "${flag}"
  547. OUTPUT_VARIABLE OUTPUT)
  548. if(${compile_result})
  549. if(exec_return EQUAL 0)
  550. set(${result} 1 CACHE INTERNAL "Runtime Test ${result}")
  551. message(STATUS "Performing Runtime Test ${result} - Success")
  552. else()
  553. message(STATUS "Performing Runtime Test ${result} - Failed(${exec_return})")
  554. set(${result} 0 CACHE INTERNAL "Runtime Test ${result}")
  555. endif()
  556. else()
  557. set(${result} 0 CACHE INTERNAL "Runtime Test ${result}")
  558. message(STATUS "Performing Runtime Test ${result} - Compiling Failed")
  559. endif()
  560. if(NOT ${result})
  561. file(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log
  562. "Runtime Test failed:\n"
  563. " source file: '${_fname}'\n"
  564. " check option: '${flag}'\n"
  565. " exec return: ${exec_return}\n"
  566. "===== BUILD AND RUNTIME LOG =====\n"
  567. "${OUTPUT}\n"
  568. "===== END =====\n\n")
  569. endif()
  570. endif()
  571. endmacro()
  572. # turns off warnings
  573. macro(ocv_warnings_disable)
  574. if(NOT ENABLE_NOISY_WARNINGS)
  575. set(_flag_vars "")
  576. set(_msvc_warnings "")
  577. set(_gxx_warnings "")
  578. set(_icc_warnings "")
  579. foreach(arg ${ARGN})
  580. if(arg MATCHES "^CMAKE_")
  581. list(APPEND _flag_vars ${arg})
  582. elseif(arg MATCHES "^/wd")
  583. list(APPEND _msvc_warnings ${arg})
  584. elseif(arg MATCHES "^-W")
  585. list(APPEND _gxx_warnings ${arg})
  586. elseif(arg MATCHES "^-wd" OR arg MATCHES "^-Qwd" OR arg MATCHES "^/Qwd")
  587. list(APPEND _icc_warnings ${arg})
  588. endif()
  589. endforeach()
  590. if(MSVC AND _msvc_warnings AND _flag_vars)
  591. foreach(var ${_flag_vars})
  592. foreach(warning ${_msvc_warnings})
  593. set(${var} "${${var}} ${warning}")
  594. endforeach()
  595. endforeach()
  596. elseif(((CV_GCC OR CV_CLANG) OR (UNIX AND CV_ICC)) AND _gxx_warnings AND _flag_vars)
  597. foreach(var ${_flag_vars})
  598. foreach(warning ${_gxx_warnings})
  599. if(NOT warning MATCHES "^-Wno-")
  600. string(REGEX REPLACE "(^|[ ]+)${warning}(=[^ ]*)?([ ]+|$)" " " ${var} "${${var}}")
  601. string(REPLACE "-W" "-Wno-" warning "${warning}")
  602. endif()
  603. ocv_check_flag_support(${var} "${warning}" _varname "")
  604. if(${_varname})
  605. set(${var} "${${var}} ${warning}")
  606. endif()
  607. endforeach()
  608. endforeach()
  609. endif()
  610. if(CV_ICC AND _icc_warnings AND _flag_vars)
  611. foreach(var ${_flag_vars})
  612. foreach(warning ${_icc_warnings})
  613. if(UNIX)
  614. string(REPLACE "-Qwd" "-wd" warning "${warning}")
  615. else()
  616. string(REPLACE "-wd" "-Qwd" warning "${warning}")
  617. endif()
  618. ocv_check_flag_support(${var} "${warning}" _varname "")
  619. if(${_varname})
  620. set(${var} "${${var}} ${warning}")
  621. endif()
  622. endforeach()
  623. endforeach()
  624. endif()
  625. unset(_flag_vars)
  626. unset(_msvc_warnings)
  627. unset(_gxx_warnings)
  628. unset(_icc_warnings)
  629. endif(NOT ENABLE_NOISY_WARNINGS)
  630. endmacro()
  631. macro(ocv_append_source_file_compile_definitions source)
  632. get_source_file_property(_value "${source}" COMPILE_DEFINITIONS)
  633. if(_value)
  634. set(_value ${_value} ${ARGN})
  635. else()
  636. set(_value ${ARGN})
  637. endif()
  638. set_source_files_properties("${source}" PROPERTIES COMPILE_DEFINITIONS "${_value}")
  639. endmacro()
  640. macro(add_apple_compiler_options the_module)
  641. ocv_check_flag_support(OBJCXX "-fobjc-exceptions" HAVE_OBJC_EXCEPTIONS "")
  642. if(HAVE_OBJC_EXCEPTIONS)
  643. foreach(source ${OPENCV_MODULE_${the_module}_SOURCES})
  644. if("${source}" MATCHES "\\.mm$")
  645. get_source_file_property(flags "${source}" COMPILE_FLAGS)
  646. if(flags)
  647. set(flags "${_flags} -fobjc-exceptions")
  648. else()
  649. set(flags "-fobjc-exceptions")
  650. endif()
  651. set_source_files_properties("${source}" PROPERTIES COMPILE_FLAGS "${flags}")
  652. endif()
  653. endforeach()
  654. endif()
  655. endmacro()
  656. # Provides an option that the user can optionally select.
  657. # Can accept condition to control when option is available for user.
  658. # Usage:
  659. # option(<option_variable>
  660. # "help string describing the option"
  661. # <initial value or boolean expression>
  662. # [VISIBLE_IF <condition>]
  663. # [VERIFY <condition>])
  664. macro(OCV_OPTION variable description value)
  665. set(__value ${value})
  666. set(__condition "")
  667. set(__verification)
  668. set(__varname "__value")
  669. foreach(arg ${ARGN})
  670. if(arg STREQUAL "IF" OR arg STREQUAL "if" OR arg STREQUAL "VISIBLE_IF")
  671. set(__varname "__condition")
  672. elseif(arg STREQUAL "VERIFY")
  673. set(__varname "__verification")
  674. else()
  675. list(APPEND ${__varname} ${arg})
  676. endif()
  677. endforeach()
  678. unset(__varname)
  679. if(__condition STREQUAL "")
  680. set(__condition 2 GREATER 1)
  681. endif()
  682. if(${__condition})
  683. if(__value MATCHES ";")
  684. if(${__value})
  685. option(${variable} "${description}" ON)
  686. else()
  687. option(${variable} "${description}" OFF)
  688. endif()
  689. elseif(DEFINED ${__value})
  690. if(${__value})
  691. option(${variable} "${description}" ON)
  692. else()
  693. option(${variable} "${description}" OFF)
  694. endif()
  695. else()
  696. option(${variable} "${description}" ${__value})
  697. endif()
  698. else()
  699. if(DEFINED ${variable} AND "${${variable}}" # emit warnings about turned ON options only.
  700. AND NOT (OPENCV_HIDE_WARNING_UNSUPPORTED_OPTION OR "$ENV{OPENCV_HIDE_WARNING_UNSUPPORTED_OPTION}")
  701. )
  702. message(WARNING "Unexpected option: ${variable} (=${${variable}})\nCondition: IF (${__condition})")
  703. endif()
  704. if(OPENCV_UNSET_UNSUPPORTED_OPTION)
  705. unset(${variable} CACHE)
  706. endif()
  707. endif()
  708. if(__verification)
  709. set(OPENCV_VERIFY_${variable} "${__verification}") # variable containing condition to verify
  710. list(APPEND OPENCV_VERIFICATIONS "${variable}") # list of variable names (WITH_XXX;WITH_YYY;...)
  711. endif()
  712. unset(__condition)
  713. unset(__value)
  714. endmacro()
  715. # Check that each variable stored in OPENCV_VERIFICATIONS list
  716. # is consistent with actual detection result (stored as condition in OPENCV_VERIFY_...) variables
  717. function(ocv_verify_config)
  718. set(broken_options)
  719. foreach(var ${OPENCV_VERIFICATIONS})
  720. set(evaluated FALSE)
  721. if(${OPENCV_VERIFY_${var}})
  722. set(evaluated TRUE)
  723. endif()
  724. status("Verifying ${var}=${${var}} => '${OPENCV_VERIFY_${var}}'=${evaluated}")
  725. if (${var} AND NOT evaluated)
  726. list(APPEND broken_options ${var})
  727. message(WARNING
  728. "Option ${var} is enabled but corresponding dependency "
  729. "have not been found: \"${OPENCV_VERIFY_${var}}\" is FALSE")
  730. elseif(NOT ${var} AND evaluated)
  731. list(APPEND broken_options ${var})
  732. message(WARNING
  733. "Option ${var} is disabled or unset but corresponding dependency "
  734. "have been explicitly turned on: \"${OPENCV_VERIFY_${var}}\" is TRUE")
  735. endif()
  736. endforeach()
  737. if(broken_options)
  738. string(REPLACE ";" "\n" broken_options "${broken_options}")
  739. message(FATAL_ERROR
  740. "Some dependencies have not been found or have been forced, "
  741. "unset ENABLE_CONFIG_VERIFICATION option to ignore these failures "
  742. "or change following options:\n${broken_options}")
  743. endif()
  744. endfunction()
  745. # Usage: ocv_append_build_options(HIGHGUI FFMPEG)
  746. macro(ocv_append_build_options var_prefix pkg_prefix)
  747. foreach(suffix INCLUDE_DIRS LIBRARIES LIBRARY_DIRS LINK_LIBRARIES)
  748. if(${pkg_prefix}_${suffix})
  749. list(APPEND ${var_prefix}_${suffix} ${${pkg_prefix}_${suffix}})
  750. list(REMOVE_DUPLICATES ${var_prefix}_${suffix})
  751. endif()
  752. endforeach()
  753. endmacro()
  754. function(ocv_append_source_files_cxx_compiler_options files_var)
  755. set(__flags "${ARGN}")
  756. ocv_check_flag_support(CXX "${__flags}" __HAVE_COMPILER_OPTIONS_VAR "")
  757. if(${__HAVE_COMPILER_OPTIONS_VAR})
  758. foreach(source ${${files_var}})
  759. if("${source}" MATCHES "\\.(cpp|cc|cxx)$")
  760. get_source_file_property(flags "${source}" COMPILE_FLAGS)
  761. if(flags)
  762. set(flags "${flags} ${__flags}")
  763. else()
  764. set(flags "${__flags}")
  765. endif()
  766. set_source_files_properties("${source}" PROPERTIES COMPILE_FLAGS "${flags}")
  767. endif()
  768. endforeach()
  769. endif()
  770. endfunction()
  771. # Usage is similar to CMake 'pkg_check_modules' command
  772. # It additionally controls HAVE_${define} and ${define}_${modname}_FOUND variables
  773. macro(ocv_check_modules define)
  774. unset(HAVE_${define})
  775. foreach(m ${ARGN})
  776. if (m MATCHES "(.*[^><])(>=|=|<=)(.*)")
  777. set(__modname "${CMAKE_MATCH_1}")
  778. else()
  779. set(__modname "${m}")
  780. endif()
  781. unset(${define}_${__modname}_FOUND)
  782. endforeach()
  783. if(PKG_CONFIG_FOUND OR PkgConfig_FOUND)
  784. pkg_check_modules(${define} ${ARGN})
  785. endif()
  786. if(${define}_FOUND)
  787. set(HAVE_${define} 1)
  788. endif()
  789. foreach(m ${ARGN})
  790. if (m MATCHES "(.*[^><])(>=|=|<=)(.*)")
  791. set(__modname "${CMAKE_MATCH_1}")
  792. else()
  793. set(__modname "${m}")
  794. endif()
  795. if(NOT DEFINED ${define}_${__modname}_FOUND AND ${define}_FOUND)
  796. set(${define}_${__modname}_FOUND 1)
  797. endif()
  798. endforeach()
  799. if(${define}_FOUND AND ${define}_LIBRARIES)
  800. if(${define}_LINK_LIBRARIES_XXXXX) # CMake 3.12+: https://gitlab.kitware.com/cmake/cmake/merge_requests/2068
  801. set(${define}_LIBRARIES "${${define}_LINK_LIBRARIES}" CACHE INTERNAL "")
  802. else()
  803. unset(_libs) # absolute paths
  804. unset(_libs_paths) # -L args
  805. foreach(flag ${${define}_LDFLAGS})
  806. if(flag MATCHES "^-L(.*)")
  807. list(APPEND _libs_paths ${CMAKE_MATCH_1})
  808. elseif(IS_ABSOLUTE "${flag}"
  809. OR flag STREQUAL "-lstdc++"
  810. OR flag STREQUAL "-latomic"
  811. )
  812. list(APPEND _libs "${flag}")
  813. elseif(flag MATCHES "^-l(.*)")
  814. set(_lib "${CMAKE_MATCH_1}")
  815. if(_libs_paths)
  816. find_library(pkgcfg_lib_${define}_${_lib} NAMES ${_lib}
  817. HINTS ${_libs_paths} NO_DEFAULT_PATH)
  818. endif()
  819. find_library(pkgcfg_lib_${define}_${_lib} NAMES ${_lib})
  820. mark_as_advanced(pkgcfg_lib_${define}_${_lib})
  821. if(pkgcfg_lib_${define}_${_lib})
  822. list(APPEND _libs "${pkgcfg_lib_${define}_${_lib}}")
  823. else()
  824. message(WARNING "ocv_check_modules(${define}): can't find library '${_lib}'. Specify 'pkgcfg_lib_${define}_${_lib}' manually")
  825. list(APPEND _libs "${_lib}")
  826. endif()
  827. else()
  828. # -pthread
  829. #message(WARNING "ocv_check_modules(${define}): unknown LDFLAG '${flag}'")
  830. endif()
  831. endforeach()
  832. set(${define}_LINK_LIBRARIES "${_libs}")
  833. set(${define}_LIBRARIES "${_libs}" CACHE INTERNAL "")
  834. unset(_lib)
  835. unset(_libs)
  836. unset(_libs_paths)
  837. endif()
  838. endif()
  839. endmacro()
  840. if(NOT DEFINED CMAKE_ARGC) # Guard CMake standalone invocations
  841. # Use this option carefully, CMake's install() will install symlinks instead of real files
  842. # It is fine for development, but should not be used by real installations
  843. set(__symlink_default OFF) # preprocessing is required for old CMake like 2.8.12
  844. if(DEFINED ENV{BUILD_USE_SYMLINKS})
  845. set(__symlink_default $ENV{BUILD_USE_SYMLINKS})
  846. endif()
  847. OCV_OPTION(BUILD_USE_SYMLINKS "Use symlinks instead of files copying during build (and !!INSTALL!!)" (${__symlink_default}) IF (UNIX OR DEFINED __symlink_default))
  848. if(CMAKE_VERSION VERSION_LESS "3.2")
  849. macro(ocv_cmake_byproducts var_name)
  850. set(${var_name}) # nothing
  851. endmacro()
  852. else()
  853. macro(ocv_cmake_byproducts var_name)
  854. set(${var_name} BYPRODUCTS ${ARGN})
  855. endmacro()
  856. endif()
  857. set(OPENCV_DEPHELPER "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/dephelper" CACHE INTERNAL "")
  858. file(MAKE_DIRECTORY ${OPENCV_DEPHELPER})
  859. if(BUILD_USE_SYMLINKS)
  860. set(__file0 "${CMAKE_CURRENT_LIST_FILE}")
  861. set(__file1 "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/symlink_test")
  862. if(NOT IS_SYMLINK "${__file1}")
  863. execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${__file0}" "${__file1}"
  864. RESULT_VARIABLE SYMLINK_RESULT)
  865. if(NOT SYMLINK_RESULT EQUAL 0)
  866. file(REMOVE "${__file1}")
  867. endif()
  868. if(NOT IS_SYMLINK "${__file1}")
  869. set(BUILD_USE_SYMLINKS 0 CACHE INTERNAL "")
  870. endif()
  871. endif()
  872. if(NOT BUILD_USE_SYMLINKS)
  873. message(STATUS "Build symlinks are not available (disabled)")
  874. endif()
  875. endif()
  876. set(OPENCV_BUILD_INFO_STR "" CACHE INTERNAL "")
  877. function(ocv_output_status msg)
  878. message(STATUS "${msg}")
  879. string(REPLACE "\\" "\\\\" msg "${msg}")
  880. string(REPLACE "\"" "\\\"" msg "${msg}")
  881. string(REGEX REPLACE "^\n+|\n+$" "" msg "${msg}")
  882. if(msg MATCHES "\n")
  883. message(WARNING "String to be inserted to version_string.inc has an unexpected line break: '${msg}'")
  884. string(REPLACE "\n" "\\n" msg "${msg}")
  885. endif()
  886. set(OPENCV_BUILD_INFO_STR "${OPENCV_BUILD_INFO_STR}\"${msg}\\n\"\n" CACHE INTERNAL "")
  887. endfunction()
  888. macro(ocv_finalize_status)
  889. set(OPENCV_BUILD_INFO_FILE "${CMAKE_BINARY_DIR}/version_string.tmp")
  890. if(EXISTS "${OPENCV_BUILD_INFO_FILE}")
  891. file(READ "${OPENCV_BUILD_INFO_FILE}" __content)
  892. else()
  893. set(__content "")
  894. endif()
  895. if("${__content}" STREQUAL "${OPENCV_BUILD_INFO_STR}")
  896. #message(STATUS "${OPENCV_BUILD_INFO_FILE} contains the same content")
  897. else()
  898. file(WRITE "${OPENCV_BUILD_INFO_FILE}" "${OPENCV_BUILD_INFO_STR}")
  899. endif()
  900. unset(__content)
  901. unset(OPENCV_BUILD_INFO_STR CACHE)
  902. if(NOT OPENCV_SKIP_STATUS_FINALIZATION)
  903. if(DEFINED OPENCV_MODULE_opencv_core_BINARY_DIR)
  904. execute_process(COMMAND ${CMAKE_COMMAND} -E copy_if_different "${OPENCV_BUILD_INFO_FILE}" "${OPENCV_MODULE_opencv_core_BINARY_DIR}/version_string.inc" OUTPUT_QUIET)
  905. endif()
  906. endif()
  907. if(UNIX)
  908. install(FILES "${OpenCV_SOURCE_DIR}/platforms/scripts/valgrind.supp"
  909. "${OpenCV_SOURCE_DIR}/platforms/scripts/valgrind_3rdparty.supp"
  910. DESTINATION "${OPENCV_OTHER_INSTALL_PATH}" COMPONENT "dev")
  911. endif()
  912. endmacro()
  913. # Status report function.
  914. # Automatically align right column and selects text based on condition.
  915. # Usage:
  916. # status(<text>)
  917. # status(<heading> <value1> [<value2> ...])
  918. # status(<heading> <condition> THEN <text for TRUE> ELSE <text for FALSE> )
  919. function(status text)
  920. set(status_cond)
  921. set(status_then)
  922. set(status_else)
  923. set(status_current_name "cond")
  924. foreach(arg ${ARGN})
  925. if(arg STREQUAL "THEN")
  926. set(status_current_name "then")
  927. elseif(arg STREQUAL "ELSE")
  928. set(status_current_name "else")
  929. else()
  930. list(APPEND status_${status_current_name} ${arg})
  931. endif()
  932. endforeach()
  933. if(DEFINED status_cond)
  934. set(status_placeholder_length 32)
  935. string(RANDOM LENGTH ${status_placeholder_length} ALPHABET " " status_placeholder)
  936. string(LENGTH "${text}" status_text_length)
  937. if(status_text_length LESS status_placeholder_length)
  938. string(SUBSTRING "${text}${status_placeholder}" 0 ${status_placeholder_length} status_text)
  939. elseif(DEFINED status_then OR DEFINED status_else)
  940. ocv_output_status("${text}")
  941. set(status_text "${status_placeholder}")
  942. else()
  943. set(status_text "${text}")
  944. endif()
  945. if(DEFINED status_then OR DEFINED status_else)
  946. if(${status_cond})
  947. string(REPLACE ";" " " status_then "${status_then}")
  948. string(REGEX REPLACE "^[ \t]+" "" status_then "${status_then}")
  949. ocv_output_status("${status_text} ${status_then}")
  950. else()
  951. string(REPLACE ";" " " status_else "${status_else}")
  952. string(REGEX REPLACE "^[ \t]+" "" status_else "${status_else}")
  953. ocv_output_status("${status_text} ${status_else}")
  954. endif()
  955. else()
  956. string(REPLACE ";" " " status_cond "${status_cond}")
  957. string(REGEX REPLACE "^[ \t]+" "" status_cond "${status_cond}")
  958. ocv_output_status("${status_text} ${status_cond}")
  959. endif()
  960. else()
  961. ocv_output_status("${text}")
  962. endif()
  963. endfunction()
  964. endif() # NOT DEFINED CMAKE_ARGC
  965. #
  966. # Generate a list of enabled features basing on conditions:
  967. # IF <cond> THEN <title>: check condition and append title to the result if it is true
  968. # ELSE <title>: return provided value instead of empty result
  969. # EXCLUSIVE: break after first successful condition
  970. #
  971. # Usage:
  972. # ocv_build_features_string(out [EXCLUSIVE] [IF feature THEN title] ... [ELSE title])
  973. #
  974. function(ocv_build_features_string out)
  975. set(result)
  976. list(REMOVE_AT ARGV 0)
  977. foreach(arg ${ARGV})
  978. if(arg STREQUAL "EXCLUSIVE")
  979. set(exclusive TRUE)
  980. elseif(arg STREQUAL "IF")
  981. set(then FALSE)
  982. set(cond)
  983. elseif(arg STREQUAL "THEN")
  984. set(then TRUE)
  985. set(title)
  986. elseif(arg STREQUAL "ELSE")
  987. set(then FALSE)
  988. set(else TRUE)
  989. else()
  990. if(then)
  991. if(${cond})
  992. list(APPEND result "${arg}")
  993. if(exclusive)
  994. break()
  995. endif()
  996. endif()
  997. elseif(else)
  998. if(NOT result)
  999. set(result "${arg}")
  1000. endif()
  1001. else()
  1002. list(APPEND cond ${arg})
  1003. endif()
  1004. endif()
  1005. endforeach()
  1006. set(${out} ${result} PARENT_SCOPE)
  1007. endfunction()
  1008. # remove all matching elements from the list
  1009. macro(ocv_list_filterout lst regex)
  1010. foreach(item ${${lst}})
  1011. if(item MATCHES "${regex}")
  1012. list(REMOVE_ITEM ${lst} "${item}")
  1013. endif()
  1014. endforeach()
  1015. endmacro()
  1016. # Usage: ocv_list_filterout_ex(list_name regex1 regex2 ...)
  1017. macro(ocv_list_filterout_ex lst)
  1018. foreach(regex ${ARGN})
  1019. foreach(item ${${lst}})
  1020. if(item MATCHES "${regex}")
  1021. list(REMOVE_ITEM ${lst} "${item}")
  1022. endif()
  1023. endforeach()
  1024. endforeach()
  1025. endmacro()
  1026. # filter matching elements from the list
  1027. macro(ocv_list_filter lst regex)
  1028. set(dst ${ARGN})
  1029. if(NOT dst)
  1030. set(dst ${lst})
  1031. endif()
  1032. set(__result ${${lst}})
  1033. foreach(item ${__result})
  1034. if(NOT item MATCHES "${regex}")
  1035. list(REMOVE_ITEM __result "${item}")
  1036. endif()
  1037. endforeach()
  1038. set(${dst} ${__result})
  1039. endmacro()
  1040. # stable & safe duplicates removal macro
  1041. macro(ocv_list_unique __lst)
  1042. if(${__lst})
  1043. list(REMOVE_DUPLICATES ${__lst})
  1044. endif()
  1045. endmacro()
  1046. # safe list reversal macro
  1047. macro(ocv_list_reverse __lst)
  1048. if(${__lst})
  1049. list(REVERSE ${__lst})
  1050. endif()
  1051. endmacro()
  1052. # safe list sorting macro
  1053. macro(ocv_list_sort __lst)
  1054. if(${__lst})
  1055. list(SORT ${__lst})
  1056. endif()
  1057. endmacro()
  1058. # add prefix to each item in the list
  1059. macro(ocv_list_add_prefix LST PREFIX)
  1060. set(__tmp "")
  1061. foreach(item ${${LST}})
  1062. list(APPEND __tmp "${PREFIX}${item}")
  1063. endforeach()
  1064. set(${LST} ${__tmp})
  1065. unset(__tmp)
  1066. endmacro()
  1067. # add suffix to each item in the list
  1068. macro(ocv_list_add_suffix LST SUFFIX)
  1069. set(__tmp "")
  1070. foreach(item ${${LST}})
  1071. list(APPEND __tmp "${item}${SUFFIX}")
  1072. endforeach()
  1073. set(${LST} ${__tmp})
  1074. unset(__tmp)
  1075. endmacro()
  1076. # gets and removes the first element from the list
  1077. macro(ocv_list_pop_front LST VAR)
  1078. if(${LST})
  1079. list(GET ${LST} 0 ${VAR})
  1080. list(REMOVE_AT ${LST} 0)
  1081. else()
  1082. set(${VAR} "")
  1083. endif()
  1084. endmacro()
  1085. # Get list of duplicates in the list of input items.
  1086. # ocv_get_duplicates(<output list> <element> [<element> ...])
  1087. function(ocv_get_duplicates res)
  1088. if(ARGC LESS 2)
  1089. message(FATAL_ERROR "Invalid call to ocv_get_duplicates")
  1090. endif()
  1091. set(lst ${ARGN})
  1092. list(SORT lst)
  1093. set(prev_item)
  1094. foreach(item ${lst})
  1095. if(item STREQUAL prev_item)
  1096. list(APPEND dups ${item})
  1097. endif()
  1098. set(prev_item ${item})
  1099. endforeach()
  1100. set(${res} ${dups} PARENT_SCOPE)
  1101. endfunction()
  1102. # simple regex escaping routine (does not cover all cases!!!)
  1103. macro(ocv_regex_escape var regex)
  1104. string(REGEX REPLACE "([+.*^$])" "\\\\1" ${var} "${regex}")
  1105. endmacro()
  1106. # convert list of paths to full paths
  1107. macro(ocv_convert_to_full_paths VAR)
  1108. if(${VAR})
  1109. set(__tmp "")
  1110. foreach(path ${${VAR}})
  1111. get_filename_component(${VAR} "${path}" ABSOLUTE)
  1112. list(APPEND __tmp "${${VAR}}")
  1113. endforeach()
  1114. set(${VAR} ${__tmp})
  1115. unset(__tmp)
  1116. endif()
  1117. endmacro()
  1118. # convert list of paths to libraries names without lib prefix
  1119. function(ocv_convert_to_lib_name var)
  1120. set(tmp "")
  1121. foreach(path ${ARGN})
  1122. get_filename_component(tmp_name "${path}" NAME)
  1123. ocv_get_libname(tmp_name "${tmp_name}")
  1124. list(APPEND tmp "${tmp_name}")
  1125. endforeach()
  1126. set(${var} ${tmp} PARENT_SCOPE)
  1127. endfunction()
  1128. # add install command
  1129. function(ocv_install_target)
  1130. if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
  1131. install(TARGETS ${ARGN} FRAMEWORK DESTINATION ${OPENCV_3P_LIB_INSTALL_PATH})
  1132. else()
  1133. install(TARGETS ${ARGN})
  1134. endif()
  1135. set(isPackage 0)
  1136. unset(__package)
  1137. unset(__target)
  1138. foreach(e ${ARGN})
  1139. if(NOT DEFINED __target)
  1140. set(__target "${e}")
  1141. endif()
  1142. if(isPackage EQUAL 1)
  1143. set(__package "${e}")
  1144. break()
  1145. endif()
  1146. if(e STREQUAL "EXPORT")
  1147. set(isPackage 1)
  1148. endif()
  1149. endforeach()
  1150. if(DEFINED __package)
  1151. list(APPEND ${__package}_TARGETS ${__target})
  1152. set(${__package}_TARGETS "${${__package}_TARGETS}" CACHE INTERNAL "List of ${__package} targets")
  1153. endif()
  1154. if(MSVC)
  1155. set(__target "${ARGV0}")
  1156. # don't move this into global scope of this file: compiler settings (like MSVC variable) are not available during processing
  1157. if(BUILD_SHARED_LIBS) # no defaults for static libs (modern CMake is required)
  1158. if(NOT CMAKE_VERSION VERSION_LESS 3.6.0)
  1159. option(INSTALL_PDB_COMPONENT_EXCLUDE_FROM_ALL "Don't install PDB files by default" ON)
  1160. option(INSTALL_PDB "Add install PDB rules" ON)
  1161. elseif(NOT CMAKE_VERSION VERSION_LESS 3.1.0)
  1162. option(INSTALL_PDB_COMPONENT_EXCLUDE_FROM_ALL "Don't install PDB files by default (not supported)" OFF)
  1163. option(INSTALL_PDB "Add install PDB rules" OFF)
  1164. endif()
  1165. endif()
  1166. if(INSTALL_PDB AND NOT INSTALL_IGNORE_PDB
  1167. AND NOT OPENCV_${__target}_PDB_SKIP
  1168. )
  1169. set(__location_key "ARCHIVE") # static libs
  1170. get_target_property(__target_type ${__target} TYPE)
  1171. if("${__target_type}" STREQUAL "SHARED_LIBRARY")
  1172. set(__location_key "RUNTIME") # shared libs (.DLL)
  1173. endif()
  1174. set(processDst 0)
  1175. set(isDst 0)
  1176. unset(__dst)
  1177. foreach(e ${ARGN})
  1178. if(isDst EQUAL 1)
  1179. set(__dst "${e}")
  1180. break()
  1181. endif()
  1182. if(processDst EQUAL 1 AND e STREQUAL "DESTINATION")
  1183. set(isDst 1)
  1184. endif()
  1185. if(e STREQUAL "${__location_key}")
  1186. set(processDst 1)
  1187. else()
  1188. set(processDst 0)
  1189. endif()
  1190. endforeach()
  1191. # message(STATUS "Process ${__target} dst=${__dst}...")
  1192. if(DEFINED __dst)
  1193. if(NOT CMAKE_VERSION VERSION_LESS 3.1.0)
  1194. set(__pdb_install_component "pdb")
  1195. if(DEFINED INSTALL_PDB_COMPONENT AND INSTALL_PDB_COMPONENT)
  1196. set(__pdb_install_component "${INSTALL_PDB_COMPONENT}")
  1197. endif()
  1198. set(__pdb_exclude_from_all "")
  1199. if(INSTALL_PDB_COMPONENT_EXCLUDE_FROM_ALL)
  1200. if(NOT CMAKE_VERSION VERSION_LESS 3.6.0)
  1201. set(__pdb_exclude_from_all EXCLUDE_FROM_ALL)
  1202. else()
  1203. message(WARNING "INSTALL_PDB_COMPONENT_EXCLUDE_FROM_ALL requires CMake 3.6+")
  1204. endif()
  1205. endif()
  1206. # message(STATUS "Adding PDB file installation rule: target=${__target} dst=${__dst} component=${__pdb_install_component}")
  1207. if("${__target_type}" STREQUAL "SHARED_LIBRARY" OR "${__target_type}" STREQUAL "MODULE_LIBRARY")
  1208. install(FILES "$<TARGET_PDB_FILE:${__target}>" DESTINATION "${__dst}"
  1209. COMPONENT ${__pdb_install_component} OPTIONAL ${__pdb_exclude_from_all})
  1210. else()
  1211. # There is no generator expression similar to TARGET_PDB_FILE and TARGET_PDB_FILE can't be used: https://gitlab.kitware.com/cmake/cmake/issues/16932
  1212. # However we still want .pdb files like: 'lib/Debug/opencv_core341d.pdb' or '3rdparty/lib/zlibd.pdb'
  1213. install(FILES "$<TARGET_PROPERTY:${__target},ARCHIVE_OUTPUT_DIRECTORY>/$<CONFIG>/$<IF:$<BOOL:$<TARGET_PROPERTY:${__target},COMPILE_PDB_NAME_DEBUG>>,$<TARGET_PROPERTY:${__target},COMPILE_PDB_NAME_DEBUG>,$<TARGET_PROPERTY:${__target},COMPILE_PDB_NAME>>.pdb"
  1214. DESTINATION "${__dst}" CONFIGURATIONS Debug
  1215. COMPONENT ${__pdb_install_component} OPTIONAL ${__pdb_exclude_from_all})
  1216. install(FILES "$<TARGET_PROPERTY:${__target},ARCHIVE_OUTPUT_DIRECTORY>/$<CONFIG>/$<IF:$<BOOL:$<TARGET_PROPERTY:${__target},COMPILE_PDB_NAME_RELEASE>>,$<TARGET_PROPERTY:${__target},COMPILE_PDB_NAME_RELEASE>,$<TARGET_PROPERTY:${__target},COMPILE_PDB_NAME>>.pdb"
  1217. DESTINATION "${__dst}" CONFIGURATIONS Release
  1218. COMPONENT ${__pdb_install_component} OPTIONAL ${__pdb_exclude_from_all})
  1219. endif()
  1220. else()
  1221. message(WARNING "PDB files installation is not supported (need CMake >= 3.1.0)")
  1222. endif()
  1223. endif()
  1224. endif()
  1225. endif()
  1226. endfunction()
  1227. # ocv_install_3rdparty_licenses(<library-name> <filename1> [<filename2> ..])
  1228. function(ocv_install_3rdparty_licenses library)
  1229. foreach(filename ${ARGN})
  1230. set(filepath "${filename}")
  1231. if(NOT IS_ABSOLUTE "${filepath}")
  1232. set(filepath "${CMAKE_CURRENT_LIST_DIR}/${filepath}")
  1233. endif()
  1234. get_filename_component(name "${filename}" NAME)
  1235. install(
  1236. FILES "${filepath}"
  1237. DESTINATION "${OPENCV_LICENSES_INSTALL_PATH}"
  1238. COMPONENT licenses
  1239. RENAME "${library}-${name}"
  1240. )
  1241. endforeach()
  1242. endfunction()
  1243. # read set of version defines from the header file
  1244. macro(ocv_parse_header FILENAME FILE_VAR)
  1245. set(vars_regex "")
  1246. set(__parnet_scope OFF)
  1247. set(__add_cache OFF)
  1248. foreach(name ${ARGN})
  1249. if(${name} STREQUAL "PARENT_SCOPE")
  1250. set(__parnet_scope ON)
  1251. elseif(${name} STREQUAL "CACHE")
  1252. set(__add_cache ON)
  1253. elseif(vars_regex)
  1254. set(vars_regex "${vars_regex}|${name}")
  1255. else()
  1256. set(vars_regex "${name}")
  1257. endif()
  1258. endforeach()
  1259. if(EXISTS "${FILENAME}")
  1260. file(STRINGS "${FILENAME}" ${FILE_VAR} REGEX "#define[ \t]+(${vars_regex})[ \t]+[0-9]+" )
  1261. else()
  1262. unset(${FILE_VAR})
  1263. endif()
  1264. foreach(name ${ARGN})
  1265. if(NOT ${name} STREQUAL "PARENT_SCOPE" AND NOT ${name} STREQUAL "CACHE")
  1266. if(${FILE_VAR})
  1267. if(${FILE_VAR} MATCHES ".+[ \t]${name}[ \t]+([0-9]+).*")
  1268. string(REGEX REPLACE ".+[ \t]${name}[ \t]+([0-9]+).*" "\\1" ${name} "${${FILE_VAR}}")
  1269. else()
  1270. set(${name} "")
  1271. endif()
  1272. if(__add_cache)
  1273. set(${name} ${${name}} CACHE INTERNAL "${name} parsed from ${FILENAME}" FORCE)
  1274. elseif(__parnet_scope)
  1275. set(${name} "${${name}}" PARENT_SCOPE)
  1276. endif()
  1277. else()
  1278. unset(${name} CACHE)
  1279. endif()
  1280. endif()
  1281. endforeach()
  1282. endmacro()
  1283. # read single version define from the header file
  1284. macro(ocv_parse_header2 LIBNAME HDR_PATH VARNAME)
  1285. ocv_clear_vars(${LIBNAME}_VERSION_MAJOR
  1286. ${LIBNAME}_VERSION_MAJOR
  1287. ${LIBNAME}_VERSION_MINOR
  1288. ${LIBNAME}_VERSION_PATCH
  1289. ${LIBNAME}_VERSION_TWEAK
  1290. ${LIBNAME}_VERSION_STRING)
  1291. set(${LIBNAME}_H "")
  1292. if(EXISTS "${HDR_PATH}")
  1293. file(STRINGS "${HDR_PATH}" ${LIBNAME}_H REGEX "^#define[ \t]+${VARNAME}[ \t]+\"[^\"]*\".*$" LIMIT_COUNT 1)
  1294. endif()
  1295. if(${LIBNAME}_H)
  1296. string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_MAJOR "${${LIBNAME}_H}")
  1297. string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_MINOR "${${LIBNAME}_H}")
  1298. string(REGEX REPLACE "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.[0-9]+\\.([0-9]+).*$" "\\1" ${LIBNAME}_VERSION_PATCH "${${LIBNAME}_H}")
  1299. set(${LIBNAME}_VERSION_MAJOR ${${LIBNAME}_VERSION_MAJOR} ${ARGN})
  1300. set(${LIBNAME}_VERSION_MINOR ${${LIBNAME}_VERSION_MINOR} ${ARGN})
  1301. set(${LIBNAME}_VERSION_PATCH ${${LIBNAME}_VERSION_PATCH} ${ARGN})
  1302. set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_MAJOR}.${${LIBNAME}_VERSION_MINOR}.${${LIBNAME}_VERSION_PATCH}")
  1303. # append a TWEAK version if it exists:
  1304. set(${LIBNAME}_VERSION_TWEAK "")
  1305. if("${${LIBNAME}_H}" MATCHES "^.*[ \t]${VARNAME}[ \t]+\"[0-9]+\\.[0-9]+\\.[0-9]+\\.([0-9]+).*$")
  1306. set(${LIBNAME}_VERSION_TWEAK "${CMAKE_MATCH_1}" ${ARGN})
  1307. endif()
  1308. if(${LIBNAME}_VERSION_TWEAK)
  1309. set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_STRING}.${${LIBNAME}_VERSION_TWEAK}" ${ARGN})
  1310. else()
  1311. set(${LIBNAME}_VERSION_STRING "${${LIBNAME}_VERSION_STRING}" ${ARGN})
  1312. endif()
  1313. endif()
  1314. endmacro()
  1315. ################################################################################################
  1316. # short command to setup source group
  1317. function(ocv_source_group group)
  1318. if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
  1319. set(group "${the_module}\\${group}")
  1320. endif()
  1321. cmake_parse_arguments(SG "" "DIRBASE" "GLOB;GLOB_RECURSE;FILES" ${ARGN})
  1322. set(files "")
  1323. if(SG_FILES)
  1324. list(APPEND files ${SG_FILES})
  1325. endif()
  1326. if(SG_GLOB)
  1327. file(GLOB srcs ${SG_GLOB})
  1328. list(APPEND files ${srcs})
  1329. endif()
  1330. if(SG_GLOB_RECURSE)
  1331. file(GLOB_RECURSE srcs ${SG_GLOB_RECURSE})
  1332. list(APPEND files ${srcs})
  1333. endif()
  1334. if(SG_DIRBASE)
  1335. foreach(f ${files})
  1336. file(RELATIVE_PATH fpart "${SG_DIRBASE}" "${f}")
  1337. if(fpart MATCHES "^\\.\\.")
  1338. message(AUTHOR_WARNING "Can't detect subpath for source_group command: Group=${group} FILE=${f} DIRBASE=${SG_DIRBASE}")
  1339. set(fpart "")
  1340. else()
  1341. get_filename_component(fpart "${fpart}" PATH)
  1342. if(fpart)
  1343. set(fpart "/${fpart}") # add '/'
  1344. string(REPLACE "/" "\\" fpart "${fpart}")
  1345. endif()
  1346. endif()
  1347. source_group("${group}${fpart}" FILES ${f})
  1348. endforeach()
  1349. else()
  1350. source_group(${group} FILES ${files})
  1351. endif()
  1352. endfunction()
  1353. macro(__ocv_push_target_link_libraries)
  1354. if(NOT TARGET ${target})
  1355. if(NOT DEFINED OPENCV_MODULE_${target}_LOCATION)
  1356. message(FATAL_ERROR "ocv_target_link_libraries: invalid target: '${target}'")
  1357. endif()
  1358. set(OPENCV_MODULE_${target}_LINK_DEPS ${OPENCV_MODULE_${target}_LINK_DEPS} ${ARGN} CACHE INTERNAL "" FORCE)
  1359. else()
  1360. target_link_libraries(${target} ${ARGN})
  1361. endif()
  1362. endmacro()
  1363. function(ocv_target_link_libraries target)
  1364. set(LINK_DEPS ${ARGN})
  1365. _ocv_fix_target(target)
  1366. set(LINK_MODE "PRIVATE")
  1367. set(LINK_PENDING "")
  1368. foreach(dep ${LINK_DEPS})
  1369. if(" ${dep}" STREQUAL " ${target}")
  1370. # prevent "link to itself" warning (world problem)
  1371. elseif(" ${dep}" STREQUAL " LINK_PRIVATE" OR " ${dep}" STREQUAL " LINK_PUBLIC" # deprecated
  1372. OR " ${dep}" STREQUAL " PRIVATE" OR " ${dep}" STREQUAL " PUBLIC" OR " ${dep}" STREQUAL " INTERFACE"
  1373. )
  1374. if(NOT LINK_PENDING STREQUAL "")
  1375. __ocv_push_target_link_libraries(${LINK_MODE} ${LINK_PENDING})
  1376. set(LINK_PENDING "")
  1377. endif()
  1378. set(LINK_MODE "${dep}")
  1379. else()
  1380. if(BUILD_opencv_world)
  1381. if(OPENCV_MODULE_${dep}_IS_PART_OF_WORLD)
  1382. set(dep opencv_world)
  1383. endif()
  1384. endif()
  1385. list(APPEND LINK_PENDING "${dep}")
  1386. endif()
  1387. endforeach()
  1388. if(NOT LINK_PENDING STREQUAL "")
  1389. __ocv_push_target_link_libraries(${LINK_MODE} ${LINK_PENDING})
  1390. endif()
  1391. endfunction()
  1392. function(ocv_target_compile_definitions target)
  1393. _ocv_fix_target(target)
  1394. if(NOT TARGET ${target})
  1395. if(NOT DEFINED OPENCV_MODULE_${target}_LOCATION)
  1396. message(FATAL_ERROR "ocv_target_compile_definitions: invalid target: '${target}'")
  1397. endif()
  1398. set(OPENCV_MODULE_${target}_COMPILE_DEFINITIONS ${OPENCV_MODULE_${target}_COMPILE_DEFINITIONS} ${ARGN} CACHE INTERNAL "" FORCE)
  1399. else()
  1400. target_compile_definitions(${target} ${ARGN})
  1401. endif()
  1402. endfunction()
  1403. function(_ocv_append_target_includes target)
  1404. if(DEFINED OCV_TARGET_INCLUDE_DIRS_${target})
  1405. target_include_directories(${target} PRIVATE ${OCV_TARGET_INCLUDE_DIRS_${target}})
  1406. if(OPENCV_DEPENDANT_TARGETS_${target})
  1407. foreach(t ${OPENCV_DEPENDANT_TARGETS_${target}})
  1408. target_include_directories(${t} PRIVATE ${OCV_TARGET_INCLUDE_DIRS_${target}})
  1409. endforeach()
  1410. endif()
  1411. unset(OCV_TARGET_INCLUDE_DIRS_${target} CACHE)
  1412. endif()
  1413. if(DEFINED OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target})
  1414. target_include_directories(${target} SYSTEM PRIVATE ${OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target}})
  1415. if(OPENCV_DEPENDANT_TARGETS_${target})
  1416. foreach(t ${OPENCV_DEPENDANT_TARGETS_${target}})
  1417. target_include_directories(${t} SYSTEM PRIVATE ${OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target}})
  1418. endforeach()
  1419. endif()
  1420. unset(OCV_TARGET_INCLUDE_SYSTEM_DIRS_${target} CACHE)
  1421. endif()
  1422. endfunction()
  1423. function(ocv_add_executable target)
  1424. add_executable(${target} ${ARGN})
  1425. _ocv_append_target_includes(${target})
  1426. endfunction()
  1427. function(ocv_add_library target)
  1428. if(HAVE_CUDA AND ARGN MATCHES "\\.cu")
  1429. ocv_include_directories(${CUDA_INCLUDE_DIRS})
  1430. ocv_cuda_compile(cuda_objs ${ARGN})
  1431. set(OPENCV_MODULE_${target}_CUDA_OBJECTS ${cuda_objs} CACHE INTERNAL "Compiled CUDA object files")
  1432. endif()
  1433. add_library(${target} ${ARGN} ${cuda_objs})
  1434. if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
  1435. message(STATUS "Setting Apple target properties for ${target}")
  1436. set(CMAKE_SHARED_LIBRARY_RUNTIME_C_FLAG 1)
  1437. if(IOS AND NOT MAC_CATALYST)
  1438. set(OPENCV_APPLE_INFO_PLIST "${CMAKE_BINARY_DIR}/ios/Info.plist")
  1439. else()
  1440. set(OPENCV_APPLE_INFO_PLIST "${CMAKE_BINARY_DIR}/osx/Info.plist")
  1441. endif()
  1442. set_target_properties(${target} PROPERTIES
  1443. FRAMEWORK TRUE
  1444. MACOSX_FRAMEWORK_IDENTIFIER org.opencv
  1445. MACOSX_FRAMEWORK_INFO_PLIST ${OPENCV_APPLE_INFO_PLIST}
  1446. # "current version" in semantic format in Mach-O binary file
  1447. VERSION ${OPENCV_LIBVERSION}
  1448. # "compatibility version" in semantic format in Mach-O binary file
  1449. SOVERSION ${OPENCV_LIBVERSION}
  1450. INSTALL_RPATH ""
  1451. INSTALL_NAME_DIR "@rpath"
  1452. BUILD_WITH_INSTALL_RPATH 1
  1453. LIBRARY_OUTPUT_NAME "opencv2"
  1454. XCODE_ATTRIBUTE_TARGETED_DEVICE_FAMILY "1,2"
  1455. #PUBLIC_HEADER "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h"
  1456. #XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer"
  1457. )
  1458. endif()
  1459. _ocv_append_target_includes(${target})
  1460. endfunction()
  1461. function(ocv_add_external_target name inc link def)
  1462. if(BUILD_SHARED_LIBS AND link)
  1463. set(imp IMPORTED)
  1464. endif()
  1465. add_library(ocv.3rdparty.${name} INTERFACE ${imp})
  1466. if(def)
  1467. if(NOT (CMAKE_VERSION VERSION_LESS "3.11.0")) # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/1264 : eliminates "Cannot specify compile definitions for imported target" error message
  1468. target_compile_definitions(ocv.3rdparty.${name} INTERFACE "${def}")
  1469. else()
  1470. set_target_properties(ocv.3rdparty.${name} PROPERTIES INTERFACE_COMPILE_DEFINITIONS "${def}")
  1471. endif()
  1472. endif()
  1473. if(inc)
  1474. if(NOT (CMAKE_VERSION VERSION_LESS "3.11.0")) # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/1264 : eliminates "Cannot specify compile definitions for imported target" error message
  1475. target_include_directories(ocv.3rdparty.${name} SYSTEM INTERFACE "$<BUILD_INTERFACE:${inc}>")
  1476. else()
  1477. set_target_properties(ocv.3rdparty.${name} PROPERTIES
  1478. INTERFACE_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${inc}>"
  1479. INTERFACE_SYSTEM_INCLUDE_DIRECTORIES "$<BUILD_INTERFACE:${inc}>"
  1480. )
  1481. endif()
  1482. endif()
  1483. if(link)
  1484. # When cmake version is greater than or equal to 3.11, INTERFACE_LINK_LIBRARIES no longer applies to interface library
  1485. # See https://github.com/opencv/opencv/pull/18658
  1486. if(CMAKE_VERSION VERSION_LESS 3.11)
  1487. set_target_properties(ocv.3rdparty.${name} PROPERTIES
  1488. INTERFACE_LINK_LIBRARIES "${link}")
  1489. else()
  1490. target_link_libraries(ocv.3rdparty.${name} INTERFACE ${link})
  1491. endif()
  1492. endif()
  1493. # to install used target only upgrade CMake
  1494. if(NOT BUILD_SHARED_LIBS
  1495. AND CMAKE_VERSION VERSION_LESS "3.13.0" # https://gitlab.kitware.com/cmake/cmake/-/merge_requests/2152
  1496. )
  1497. install(TARGETS ocv.3rdparty.${name} EXPORT OpenCVModules)
  1498. endif()
  1499. endfunction()
  1500. function(ocv_install_used_external_targets)
  1501. if(NOT BUILD_SHARED_LIBS
  1502. AND NOT (CMAKE_VERSION VERSION_LESS "3.13.0") # upgrade CMake: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/2152
  1503. )
  1504. foreach(tgt in ${ARGN})
  1505. if(tgt MATCHES "^ocv\.3rdparty\.")
  1506. install(TARGETS ${tgt} EXPORT OpenCVModules)
  1507. endif()
  1508. endforeach()
  1509. endif()
  1510. endfunction()
  1511. # Returns the first non-interface target
  1512. function(ocv_get_imported_target imported interface)
  1513. set(__result "${interface}")
  1514. get_target_property(__type "${__result}" TYPE)
  1515. if(__type STREQUAL "INTERFACE_LIBRARY")
  1516. get_target_property(__libs "${__result}" INTERFACE_LINK_LIBRARIES)
  1517. list(GET __libs 0 __interface)
  1518. ocv_get_imported_target(__result "${__interface}")
  1519. endif()
  1520. set(${imported} "${__result}" PARENT_SCOPE)
  1521. endfunction()
  1522. macro(ocv_get_libname var_name)
  1523. get_filename_component(__libname "${ARGN}" NAME)
  1524. # libopencv_core.so.3.3 -> opencv_core
  1525. string(REGEX REPLACE "^lib(.+)\\.(a|so|dll)(\\.[.0-9]+)?$" "\\1" __libname "${__libname}")
  1526. # MacOSX: libopencv_core.3.3.1.dylib -> opencv_core
  1527. string(REGEX REPLACE "^lib(.+[^.0-9])\\.([.0-9]+\\.)?dylib$" "\\1" __libname "${__libname}")
  1528. set(${var_name} "${__libname}")
  1529. endmacro()
  1530. # build the list of opencv libs and dependencies for all modules
  1531. # _modules - variable to hold list of all modules
  1532. # _extra - variable to hold list of extra dependencies
  1533. # _3rdparty - variable to hold list of prebuilt 3rdparty libraries
  1534. macro(ocv_get_all_libs _modules _extra _3rdparty)
  1535. set(${_modules} "")
  1536. set(${_extra} "")
  1537. set(${_3rdparty} "")
  1538. foreach(m ${OPENCV_MODULES_PUBLIC})
  1539. if(TARGET ${m})
  1540. get_target_property(deps ${m} INTERFACE_LINK_LIBRARIES)
  1541. if(NOT deps)
  1542. set(deps "")
  1543. endif()
  1544. else()
  1545. set(deps "")
  1546. endif()
  1547. set(_rev_deps "${deps};${m}")
  1548. ocv_list_reverse(_rev_deps)
  1549. foreach (dep ${_rev_deps})
  1550. if(DEFINED OPENCV_MODULE_${dep}_LOCATION)
  1551. list(INSERT ${_modules} 0 ${dep})
  1552. endif()
  1553. endforeach()
  1554. foreach (dep ${deps} ${OPENCV_LINKER_LIBS})
  1555. if (NOT DEFINED OPENCV_MODULE_${dep}_LOCATION)
  1556. if(dep MATCHES "^\\$<LINK_ONLY:([^>]+)>$")
  1557. set(dep "${CMAKE_MATCH_1}")
  1558. endif()
  1559. if(dep MATCHES "^\\$<")
  1560. message(WARNING "Unexpected CMake generator expression: ${dep}")
  1561. endif()
  1562. if (TARGET ${dep})
  1563. get_target_property(_type ${dep} TYPE)
  1564. if((_type STREQUAL "STATIC_LIBRARY" AND BUILD_SHARED_LIBS)
  1565. OR _type STREQUAL "INTERFACE_LIBRARY"
  1566. OR DEFINED OPENCV_MODULE_${dep}_LOCATION # OpenCV modules
  1567. )
  1568. # nothing
  1569. else()
  1570. get_target_property(_output ${dep} IMPORTED_LOCATION)
  1571. if(NOT _output)
  1572. get_target_property(_output ${dep} ARCHIVE_OUTPUT_DIRECTORY)
  1573. get_target_property(_output_name ${dep} OUTPUT_NAME)
  1574. if(NOT _output_name)
  1575. set(_output_name "${dep}")
  1576. endif()
  1577. else()
  1578. get_filename_component(_output_name "${_output}" NAME)
  1579. endif()
  1580. string(FIND "${_output}" "${CMAKE_BINARY_DIR}" _POS)
  1581. if (_POS EQUAL 0)
  1582. ocv_get_libname(_libname "${_output_name}")
  1583. list(INSERT ${_3rdparty} 0 ${dep})
  1584. else()
  1585. if(_output)
  1586. list(INSERT ${_extra} 0 ${_output})
  1587. else()
  1588. list(INSERT ${_extra} 0 ${dep})
  1589. endif()
  1590. endif()
  1591. endif()
  1592. else()
  1593. list(INSERT ${_extra} 0 ${dep})
  1594. endif()
  1595. endif()
  1596. endforeach()
  1597. endforeach()
  1598. ocv_list_filterout(${_modules} "^[\$]<")
  1599. ocv_list_filterout(${_3rdparty} "^[\$]<")
  1600. ocv_list_filterout(${_extra} "^[\$]<")
  1601. # convert CMake lists to makefile literals
  1602. foreach(lst ${_modules} ${_3rdparty} ${_extra})
  1603. ocv_list_unique(${lst})
  1604. ocv_list_reverse(${lst})
  1605. endforeach()
  1606. endmacro()
  1607. function(ocv_add_test_from_target test_name test_kind the_target)
  1608. if(CMAKE_VERSION VERSION_GREATER "2.8" AND NOT CMAKE_CROSSCOMPILING)
  1609. if(NOT "${test_kind}" MATCHES "^(Accuracy|Performance|Sanity)$")
  1610. message(FATAL_ERROR "Unknown test kind : ${test_kind}")
  1611. endif()
  1612. if(NOT TARGET "${the_target}")
  1613. message(FATAL_ERROR "${the_target} is not a CMake target")
  1614. endif()
  1615. string(TOLOWER "${test_kind}" test_kind_lower)
  1616. set(test_report_dir "${CMAKE_BINARY_DIR}/test-reports/${test_kind_lower}")
  1617. file(MAKE_DIRECTORY "${test_report_dir}")
  1618. add_test(NAME "${test_name}"
  1619. COMMAND "${the_target}"
  1620. "--gtest_output=xml:${the_target}.xml"
  1621. ${ARGN})
  1622. set_tests_properties("${test_name}" PROPERTIES
  1623. LABELS "${OPENCV_MODULE_${the_module}_LABEL};${test_kind}"
  1624. WORKING_DIRECTORY "${test_report_dir}")
  1625. if(OPENCV_TEST_DATA_PATH)
  1626. set_tests_properties("${test_name}" PROPERTIES
  1627. ENVIRONMENT "OPENCV_TEST_DATA_PATH=${OPENCV_TEST_DATA_PATH}")
  1628. endif()
  1629. endif()
  1630. endfunction()
  1631. macro(ocv_add_testdata basedir dest_subdir)
  1632. if(BUILD_TESTS)
  1633. if(NOT CMAKE_CROSSCOMPILING AND NOT INSTALL_TESTS)
  1634. file(COPY ${basedir}/
  1635. DESTINATION ${CMAKE_BINARY_DIR}/${OPENCV_TEST_DATA_INSTALL_PATH}/${dest_subdir}
  1636. ${ARGN}
  1637. )
  1638. endif()
  1639. if(INSTALL_TESTS)
  1640. install(DIRECTORY ${basedir}/
  1641. DESTINATION ${OPENCV_TEST_DATA_INSTALL_PATH}/${dest_subdir}
  1642. COMPONENT "tests"
  1643. ${ARGN}
  1644. )
  1645. endif()
  1646. endif()
  1647. endmacro()
  1648. macro(ocv_generate_vs_version_file DESTINATION)
  1649. cmake_parse_arguments(VS_VER "" "NAME;FILEDESCRIPTION;FILEVERSION;INTERNALNAME;COPYRIGHT;ORIGINALFILENAME;PRODUCTNAME;PRODUCTVERSION;COMMENTS;FILEVERSION_QUAD;PRODUCTVERSION_QUAD" "" ${ARGN})
  1650. macro(__vs_ver_update_variable name)
  1651. if(VS_VER_NAME AND DEFINED OPENCV_${VS_VER_NAME}_VS_VER_${name})
  1652. set(OPENCV_VS_VER_${name} "${OPENCV_${VS_VER_NAME}_VS_VER_${name}}")
  1653. elseif(VS_VER_${name})
  1654. set(OPENCV_VS_VER_${name} "${VS_VER_${name}}")
  1655. endif()
  1656. endmacro()
  1657. __vs_ver_update_variable(FILEVERSION_QUAD)
  1658. __vs_ver_update_variable(PRODUCTVERSION_QUAD)
  1659. macro(__vs_ver_update_str_variable name)
  1660. if(VS_VER_NAME AND DEFINED OPENCV_${VS_VER_NAME}_VS_VER_${name})
  1661. set(OPENCV_VS_VER_${name}_STR "${OPENCV_${VS_VER_NAME}_VS_VER_${name}}")
  1662. elseif(VS_VER_${name})
  1663. set(OPENCV_VS_VER_${name}_STR "${VS_VER_${name}}")
  1664. endif()
  1665. endmacro()
  1666. __vs_ver_update_str_variable(FILEDESCRIPTION)
  1667. __vs_ver_update_str_variable(FILEVERSION)
  1668. __vs_ver_update_str_variable(INTERNALNAME)
  1669. __vs_ver_update_str_variable(COPYRIGHT)
  1670. __vs_ver_update_str_variable(ORIGINALFILENAME)
  1671. __vs_ver_update_str_variable(PRODUCTNAME)
  1672. __vs_ver_update_str_variable(PRODUCTVERSION)
  1673. __vs_ver_update_str_variable(COMMENTS)
  1674. if(OPENCV_VS_VER_COPYRIGHT_STR)
  1675. set(OPENCV_VS_VER_HAVE_COPYRIGHT_STR 1)
  1676. else()
  1677. set(OPENCV_VS_VER_HAVE_COPYRIGHT_STR 0)
  1678. endif()
  1679. if(OPENCV_VS_VER_COMMENTS_STR)
  1680. set(OPENCV_VS_VER_HAVE_COMMENTS_STR 1)
  1681. else()
  1682. set(OPENCV_VS_VER_HAVE_COMMENTS_STR 0)
  1683. endif()
  1684. configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/vs_version.rc.in" "${DESTINATION}" @ONLY)
  1685. endmacro()
  1686. macro(ocv_cmake_script_append_var content_var)
  1687. foreach(var_name ${ARGN})
  1688. set(${content_var} "${${content_var}}
  1689. set(${var_name} \"${${var_name}}\")
  1690. ")
  1691. endforeach()
  1692. endmacro()
  1693. macro(ocv_copyfiles_append_file list_var src dst)
  1694. list(LENGTH ${list_var} __id)
  1695. list(APPEND ${list_var} ${__id})
  1696. set(${list_var}_SRC_${__id} "${src}")
  1697. set(${list_var}_DST_${__id} "${dst}")
  1698. endmacro()
  1699. macro(ocv_copyfiles_append_dir list_var src dst)
  1700. set(__glob ${ARGN})
  1701. list(LENGTH ${list_var} __id)
  1702. list(APPEND ${list_var} ${__id})
  1703. set(${list_var}_SRC_${__id} "${src}")
  1704. set(${list_var}_DST_${__id} "${dst}")
  1705. set(${list_var}_MODE_${__id} "COPYDIR")
  1706. if(__glob)
  1707. set(${list_var}_GLOB_${__id} ${__glob})
  1708. endif()
  1709. endmacro()
  1710. macro(ocv_copyfiles_make_config_string content_var list_var)
  1711. set(var_name "${list_var}")
  1712. set(${content_var} "${${content_var}}
  1713. set(${var_name} \"${${var_name}}\")
  1714. ")
  1715. foreach(__id ${${list_var}})
  1716. set(${content_var} "${${content_var}}
  1717. set(${list_var}_SRC_${__id} \"${${list_var}_SRC_${__id}}\")
  1718. set(${list_var}_DST_${__id} \"${${list_var}_DST_${__id}}\")
  1719. ")
  1720. if(DEFINED ${list_var}_MODE_${__id})
  1721. set(${content_var} "${${content_var}}set(${list_var}_MODE_${__id} \"${${list_var}_MODE_${__id}}\")\n")
  1722. endif()
  1723. if(DEFINED ${list_var}_GLOB_${__id})
  1724. set(${content_var} "${${content_var}}set(${list_var}_GLOB_${__id} \"${${list_var}_GLOB_${__id}}\")\n")
  1725. endif()
  1726. endforeach()
  1727. endmacro()
  1728. macro(ocv_copyfiles_make_config_file filename_var list_var)
  1729. ocv_copyfiles_make_config_string(${list_var}_CONFIG ${list_var})
  1730. set(${filename_var} "${CMAKE_CURRENT_BINARY_DIR}/copyfiles-${list_var}.cmake")
  1731. file(WRITE "${${filename_var}}" "${${list_var}_CONFIG}")
  1732. endmacro()
  1733. macro(ocv_copyfiles_add_forced_target target list_var comment_str)
  1734. ocv_copyfiles_make_config_file(CONFIG_FILE ${list_var})
  1735. ocv_cmake_byproducts(__byproducts BYPRODUCTS "${OPENCV_DEPHELPER}/${target}")
  1736. add_custom_target(${target}
  1737. ${__byproducts} # required for add_custom_target() by ninja
  1738. COMMAND ${CMAKE_COMMAND}
  1739. "-DCONFIG_FILE:PATH=${CONFIG_FILE}"
  1740. "-DCOPYLIST_VAR:STRING=${list_var}"
  1741. "-DDEPHELPER=${OPENCV_DEPHELPER}/${target}"
  1742. -P "${OpenCV_SOURCE_DIR}/cmake/copy_files.cmake"
  1743. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  1744. COMMENT "${comment_str}"
  1745. DEPENDS "${OpenCV_SOURCE_DIR}/cmake/copy_files.cmake"
  1746. # ninja warn about file(WRITE): "${SRC_COPY_CONFIG_FILE}"
  1747. )
  1748. endmacro()
  1749. macro(ocv_copyfiles_add_target target list_var comment_str)
  1750. set(deps ${ARGN})
  1751. ocv_copyfiles_make_config_file(CONFIG_FILE ${list_var})
  1752. add_custom_command(OUTPUT "${OPENCV_DEPHELPER}/${target}"
  1753. COMMAND ${CMAKE_COMMAND}
  1754. "-DCONFIG_FILE:PATH=${CONFIG_FILE}"
  1755. "-DCOPYLIST_VAR:STRING=${list_var}"
  1756. "-DDEPHELPER=${OPENCV_DEPHELPER}/${target}"
  1757. -P "${OpenCV_SOURCE_DIR}/cmake/copy_files.cmake"
  1758. WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
  1759. COMMENT "${comment_str}"
  1760. DEPENDS "${OpenCV_SOURCE_DIR}/cmake/copy_files.cmake" ${deps}
  1761. # ninja warn about file(WRITE): "${SRC_COPY_CONFIG_FILE}"
  1762. )
  1763. add_custom_target(${target} DEPENDS "${OPENCV_DEPHELPER}/${target}")
  1764. endmacro()
  1765. macro(ocv_get_smart_file_name output_var fpath)
  1766. ocv_is_subdir(__subir "${OpenCV_BINARY_DIR}" "${fpath}")
  1767. if(__subir)
  1768. file(RELATIVE_PATH ${output_var} "${OpenCV_BINARY_DIR}" "${fpath}")
  1769. set(${output_var} "<BUILD>/${${output_var}}")
  1770. else()
  1771. ocv_is_subdir(__subir "${OpenCV_SOURCE_DIR}" "${fpath}")
  1772. if(__subir)
  1773. file(RELATIVE_PATH ${output_var} "${OpenCV_SOURCE_DIR}" "${fpath}")
  1774. else()
  1775. set(${output_var} "${fpath}")
  1776. endif()
  1777. endif()
  1778. unset(__subir)
  1779. endmacro()
  1780. # Needed by install(DIRECTORY ...)
  1781. if(NOT CMAKE_VERSION VERSION_LESS 3.1)
  1782. set(compatible_MESSAGE_NEVER MESSAGE_NEVER)
  1783. else()
  1784. set(compatible_MESSAGE_NEVER "")
  1785. endif()
  1786. macro(ocv_git_describe var_name path)
  1787. if(GIT_FOUND)
  1788. execute_process(COMMAND "${GIT_EXECUTABLE}" describe --tags --exact-match --dirty
  1789. WORKING_DIRECTORY "${path}"
  1790. OUTPUT_VARIABLE ${var_name}
  1791. RESULT_VARIABLE GIT_RESULT
  1792. ERROR_QUIET
  1793. OUTPUT_STRIP_TRAILING_WHITESPACE
  1794. )
  1795. if(NOT GIT_RESULT EQUAL 0)
  1796. execute_process(COMMAND "${GIT_EXECUTABLE}" describe --tags --always --dirty --match "[0-9].[0-9].[0-9]*" --exclude "[^-]*-cvsdk"
  1797. WORKING_DIRECTORY "${path}"
  1798. OUTPUT_VARIABLE ${var_name}
  1799. RESULT_VARIABLE GIT_RESULT
  1800. ERROR_QUIET
  1801. OUTPUT_STRIP_TRAILING_WHITESPACE
  1802. )
  1803. if(NOT GIT_RESULT EQUAL 0) # --exclude is not supported by 'git'
  1804. # match only tags with complete OpenCV versions (ignores -alpha/-beta/-rc suffixes)
  1805. execute_process(COMMAND "${GIT_EXECUTABLE}" describe --tags --always --dirty --match "[0-9].[0-9]*[0-9]"
  1806. WORKING_DIRECTORY "${path}"
  1807. OUTPUT_VARIABLE ${var_name}
  1808. RESULT_VARIABLE GIT_RESULT
  1809. ERROR_QUIET
  1810. OUTPUT_STRIP_TRAILING_WHITESPACE
  1811. )
  1812. if(NOT GIT_RESULT EQUAL 0)
  1813. set(${var_name} "unknown")
  1814. endif()
  1815. endif()
  1816. endif()
  1817. else()
  1818. set(${var_name} "unknown")
  1819. endif()
  1820. endmacro()
  1821. # ocv_update_file(filepath content [VERBOSE])
  1822. # - write content to file
  1823. # - will not change modification time in case when file already exists and content has not changed
  1824. function(ocv_update_file filepath content)
  1825. if(EXISTS "${filepath}")
  1826. file(READ "${filepath}" actual_content)
  1827. else()
  1828. set(actual_content "")
  1829. endif()
  1830. if("${actual_content}" STREQUAL "${content}")
  1831. if(";${ARGN};" MATCHES ";VERBOSE;")
  1832. message(STATUS "${filepath} contains the same content")
  1833. endif()
  1834. else()
  1835. file(WRITE "${filepath}" "${content}")
  1836. endif()
  1837. endfunction()
  1838. if(NOT BUILD_SHARED_LIBS AND (CMAKE_VERSION VERSION_LESS "3.14.0"))
  1839. ocv_update(OPENCV_3RDPARTY_EXCLUDE_FROM_ALL "") # avoid CMake warnings: https://gitlab.kitware.com/cmake/cmake/-/issues/18938
  1840. else()
  1841. ocv_update(OPENCV_3RDPARTY_EXCLUDE_FROM_ALL "EXCLUDE_FROM_ALL")
  1842. endif()
  1843. #
  1844. # Include configuration override settings
  1845. #
  1846. include("${CMAKE_CURRENT_LIST_DIR}/vars/EnableModeVars.cmake")