OpenCVModule.cmake 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. # Local variables (set for each module):
  2. #
  3. # name - short name in lower case i.e. core
  4. # the_module - full name in lower case i.e. opencv_core
  5. # Global variables:
  6. #
  7. # OPENCV_MODULE_${the_module}_LOCATION
  8. # OPENCV_MODULE_${the_module}_BINARY_DIR
  9. # OPENCV_MODULE_${the_module}_DESCRIPTION
  10. # OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
  11. # OPENCV_MODULE_${the_module}_HEADERS
  12. # OPENCV_MODULE_${the_module}_SOURCES
  13. # OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
  14. # OPENCV_MODULE_${the_module}_DEPS_TO_LINK - differs from above for world build only
  15. # OPENCV_MODULE_${the_module}_DEPS_EXT - non-module dependencies
  16. # OPENCV_MODULE_${the_module}_REQ_DEPS
  17. # OPENCV_MODULE_${the_module}_OPT_DEPS
  18. # OPENCV_MODULE_${the_module}_PRIVATE_REQ_DEPS
  19. # OPENCV_MODULE_${the_module}_PRIVATE_OPT_DEPS
  20. # OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD
  21. # OPENCV_MODULE_${the_module}_CUDA_OBJECTS - compiled CUDA objects list
  22. # OPENCV_MODULE_${the_module}_WRAPPERS - list of wrappers supporting this module
  23. # HAVE_${the_module} - for fast check of module availability
  24. # To control the setup of the module you could also set:
  25. # the_description - text to be used as current module description
  26. # the_label - label for current module
  27. # OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
  28. # OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
  29. # BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
  30. # The verbose template for OpenCV module:
  31. #
  32. # ocv_add_module(modname <dependencies>)
  33. # ocv_glob_module_sources(([EXCLUDE_OPENCL] [EXCLUDE_CUDA] <extra sources&headers>)
  34. # or glob them manually and ocv_set_module_sources(...)
  35. # ocv_module_include_directories(<extra include directories>)
  36. # ocv_create_module()
  37. # <add extra link dependencies, compiler options, etc>
  38. # ocv_add_precompiled_headers(${the_module})
  39. # <add extra installation rules>
  40. # ocv_add_accuracy_tests(<extra dependencies>)
  41. # ocv_add_perf_tests(<extra dependencies>)
  42. # ocv_add_samples(<extra dependencies>)
  43. #
  44. #
  45. # If module have no "extra" then you can define it in one line:
  46. #
  47. # ocv_define_module(modname <dependencies>)
  48. # clean flags for modules enabled on previous cmake run
  49. # this is necessary to correctly handle modules removal
  50. foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
  51. if(HAVE_${mod})
  52. unset(HAVE_${mod} CACHE)
  53. endif()
  54. unset(OPENCV_MODULE_${mod}_DEPS CACHE)
  55. unset(OPENCV_MODULE_${mod}_DEPS_EXT CACHE)
  56. unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
  57. unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
  58. unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
  59. unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
  60. unset(OPENCV_MODULE_${mod}_LINK_DEPS CACHE)
  61. unset(OPENCV_MODULE_${mod}_WRAPPERS CACHE)
  62. endforeach()
  63. # clean modules info which needs to be recalculated
  64. set(OPENCV_MODULES_PUBLIC "" CACHE INTERNAL "List of OpenCV modules marked for export")
  65. set(OPENCV_MODULES_BUILD "" CACHE INTERNAL "List of OpenCV modules included into the build")
  66. set(OPENCV_MODULES_DISABLED_USER "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
  67. set(OPENCV_MODULES_DISABLED_AUTO "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
  68. set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
  69. unset(OPENCV_WORLD_MODULES CACHE)
  70. # adds dependencies to OpenCV module
  71. # Usage:
  72. # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>] [WRAP <list of wrappers>])
  73. # Notes:
  74. # * <list of dependencies> - can include full names of modules or full paths to shared/static libraries or cmake targets
  75. macro(ocv_add_dependencies full_modname)
  76. ocv_debug_message("ocv_add_dependencies(" ${full_modname} ${ARGN} ")")
  77. #we don't clean the dependencies here to allow this macro several times for every module
  78. foreach(d "REQUIRED" ${ARGN})
  79. if(d STREQUAL "REQUIRED")
  80. set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
  81. elseif(d STREQUAL "OPTIONAL")
  82. set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
  83. elseif(d STREQUAL "PRIVATE_REQUIRED")
  84. set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
  85. elseif(d STREQUAL "PRIVATE_OPTIONAL")
  86. set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
  87. elseif(d STREQUAL "WRAP")
  88. set(__depsvar OPENCV_MODULE_${full_modname}_WRAPPERS)
  89. else()
  90. list(APPEND ${__depsvar} "${d}")
  91. endif()
  92. endforeach()
  93. unset(__depsvar)
  94. ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
  95. ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
  96. ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
  97. ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
  98. ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
  99. set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
  100. CACHE INTERNAL "Required dependencies of ${full_modname} module")
  101. set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
  102. CACHE INTERNAL "Optional dependencies of ${full_modname} module")
  103. set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
  104. CACHE INTERNAL "Required private dependencies of ${full_modname} module")
  105. set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
  106. CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
  107. set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
  108. CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
  109. endmacro()
  110. # declare new OpenCV module in current folder
  111. # Usage:
  112. # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
  113. # Example:
  114. # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
  115. macro(ocv_add_module _name)
  116. ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
  117. string(TOLOWER "${_name}" name)
  118. set(the_module opencv_${name})
  119. # the first pass - collect modules info, the second pass - create targets
  120. if(OPENCV_INITIAL_PASS)
  121. #guard against redefinition
  122. if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
  123. message(FATAL_ERROR "Redefinition of the ${the_module} module.
  124. at: ${CMAKE_CURRENT_SOURCE_DIR}
  125. previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
  126. ")
  127. endif()
  128. if(NOT DEFINED the_description)
  129. set(the_description "The ${name} OpenCV module")
  130. endif()
  131. if(NOT DEFINED BUILD_${the_module}_INIT)
  132. set(BUILD_${the_module}_INIT ON)
  133. endif()
  134. # create option to enable/disable this module
  135. option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
  136. # remember the module details
  137. set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
  138. set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
  139. set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
  140. set(ADD_MODULE_ARGN ${ARGN})
  141. ocv_cmake_hook(PRE_ADD_MODULE)
  142. ocv_cmake_hook(PRE_ADD_MODULE_${the_module})
  143. # parse list of dependencies
  144. if(" ${ARGV1}" STREQUAL " INTERNAL" OR " ${ARGV1}" STREQUAL " BINDINGS")
  145. set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
  146. set(__ocv_argn__ ${ADD_MODULE_ARGN})
  147. list(REMOVE_AT __ocv_argn__ 0)
  148. ocv_add_dependencies(${the_module} ${__ocv_argn__})
  149. unset(__ocv_argn__)
  150. else()
  151. set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
  152. ocv_add_dependencies(${the_module} ${ADD_MODULE_ARGN})
  153. if(BUILD_${the_module})
  154. set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
  155. endif()
  156. endif()
  157. # add self to the world dependencies
  158. if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD
  159. AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS"
  160. AND (NOT DEFINED OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
  161. AND (NOT OPENCV_PROCESSING_EXTRA_MODULES OR NOT OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
  162. AND (NOT BUILD_SHARED_LIBS OR NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC"))
  163. OR OPENCV_MODULE_IS_PART_OF_WORLD
  164. )
  165. set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
  166. ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
  167. else()
  168. set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
  169. endif()
  170. if(NOT DEFINED the_label)
  171. if(OPENCV_PROCESSING_EXTRA_MODULES)
  172. set(the_label "Extra")
  173. else()
  174. set(the_label "Main")
  175. endif()
  176. endif()
  177. set(OPENCV_MODULE_${the_module}_LABEL "${the_label};${the_module}" CACHE INTERNAL "")
  178. if(BUILD_${the_module})
  179. set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
  180. else()
  181. set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
  182. endif()
  183. # stop processing of current file
  184. ocv_cmake_hook(POST_ADD_MODULE)
  185. ocv_cmake_hook(POST_ADD_MODULE_${the_module})
  186. return()
  187. else()
  188. set(OPENCV_MODULE_${the_module}_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
  189. if(NOT BUILD_${the_module})
  190. return() # extra protection from redefinition
  191. endif()
  192. if(NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR NOT ${BUILD_opencv_world})
  193. if (NOT ${the_module} STREQUAL opencv_world)
  194. project(${the_module})
  195. endif()
  196. add_definitions(
  197. -D_USE_MATH_DEFINES # M_PI constant in MSVS
  198. -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_FORMAT_MACROS # to use C libraries from C++ code (ffmpeg)
  199. )
  200. endif()
  201. endif()
  202. endmacro()
  203. # excludes module from current configuration
  204. macro(ocv_module_disable_ module)
  205. set(__modname ${module})
  206. if(NOT __modname MATCHES "^opencv_")
  207. set(__modname opencv_${module})
  208. endif()
  209. list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
  210. set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
  211. set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
  212. set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
  213. if(BUILD_${__modname})
  214. # touch variable controlling build of the module to suppress "unused variable" CMake warning
  215. endif()
  216. unset(__modname)
  217. endmacro()
  218. macro(ocv_module_disable module)
  219. ocv_module_disable_(${module})
  220. return() # leave the current folder
  221. endmacro()
  222. # gather acceptable locations and generate names for them
  223. # if folder contains CMakeLists.txt - it is accepted,
  224. # otherwise all first-level subfolders containing CMakeLists.txt are accepted.
  225. # Usage: _glob_locations(<output paths list> <output names list> <folder> [<folder> ...])
  226. function(_glob_locations out_paths out_names)
  227. set(PATHS ${ARGN})
  228. foreach(path ${PATHS})
  229. #message(STATUS "Inspect: ${path}")
  230. list(LENGTH paths before)
  231. get_filename_component(path "${path}" ABSOLUTE)
  232. # Either module itself
  233. if(NOT path STREQUAL "${OpenCV_SOURCE_DIR}/modules" AND EXISTS "${path}/CMakeLists.txt")
  234. get_filename_component(name "${path}" NAME)
  235. list(APPEND paths "${path}")
  236. list(APPEND names "${name}")
  237. else()
  238. # Either flat collection of modules
  239. file(GLOB subdirs RELATIVE "${path}" "${path}/*")
  240. foreach(subdir ${subdirs})
  241. #message(STATUS "Inspect: ${path}/${subdir}")
  242. if(EXISTS "${path}/${subdir}/CMakeLists.txt")
  243. list(APPEND paths "${path}/${subdir}")
  244. list(APPEND names "${subdir}")
  245. endif()
  246. endforeach()
  247. endif()
  248. list(LENGTH paths after)
  249. if(before EQUAL after)
  250. message(SEND_ERROR "No modules has been found: ${path}")
  251. endif()
  252. endforeach()
  253. # Return
  254. set(${out_paths} ${paths} PARENT_SCOPE)
  255. set(${out_names} ${names} PARENT_SCOPE)
  256. endfunction()
  257. # Calls 'add_subdirectory' for each location.
  258. # Note: both input lists should have same length.
  259. # Usage: _add_modules_1(<list with paths> <list with names>)
  260. macro(_add_modules_1 paths names)
  261. ocv_debug_message("_add_modules_1(paths=${paths}, names=${names}, ... " ${ARGN} ")")
  262. list(LENGTH ${paths} __len)
  263. if(NOT __len EQUAL 0)
  264. list(LENGTH ${names} __len_verify)
  265. if(NOT __len EQUAL __len_verify)
  266. message(FATAL_ERROR "Bad configuration! ${__len} != ${__len_verify}")
  267. endif()
  268. math(EXPR __len "${__len} - 1")
  269. foreach(i RANGE ${__len})
  270. list(GET ${paths} ${i} __path)
  271. list(GET ${names} ${i} __name)
  272. #message(STATUS "First pass: ${__name} => ${__path}")
  273. include("${__path}/cmake/init.cmake" OPTIONAL)
  274. add_subdirectory("${__path}" "${OpenCV_BINARY_DIR}/modules/.firstpass/${__name}")
  275. endforeach()
  276. endif()
  277. endmacro()
  278. # Calls 'add_subdirectory' for each module name.
  279. # Usage: _add_modules_2([<module> ...])
  280. macro(_add_modules_2)
  281. ocv_debug_message("_add_modules_2(" ${ARGN} ")")
  282. foreach(m ${ARGN})
  283. set(the_module "${m}")
  284. ocv_cmake_hook(PRE_MODULES_CREATE_${the_module})
  285. if(BUILD_opencv_world AND m STREQUAL "opencv_world"
  286. OR NOT BUILD_opencv_world
  287. OR NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
  288. if(NOT m MATCHES "^opencv_")
  289. message(WARNING "Incorrect module name: ${m}")
  290. endif()
  291. string(REGEX REPLACE "^opencv_" "" name "${m}")
  292. #message(STATUS "Second pass: ${name} => ${OPENCV_MODULE_${m}_LOCATION}")
  293. add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${OpenCV_BINARY_DIR}/modules/${name}")
  294. endif()
  295. ocv_cmake_hook(POST_MODULES_CREATE_${the_module})
  296. endforeach()
  297. unset(the_module)
  298. endmacro()
  299. # Check if list of input items is unique.
  300. # Usage: _assert_uniqueness(<failure message> <element> [<element> ...])
  301. function(_assert_uniqueness msg)
  302. ocv_get_duplicates(dups ${ARGN})
  303. if(dups)
  304. foreach(e ${ARGN})
  305. list(FIND dups "${e}" idx)
  306. if(NOT idx EQUAL -1)
  307. set(prefix " > ")
  308. else()
  309. set(prefix " ")
  310. endif()
  311. message("${prefix}${e}")
  312. endforeach()
  313. message(FATAL_ERROR "${msg}")
  314. endif()
  315. endfunction()
  316. # collect modules from specified directories
  317. # NB: must be called only once!
  318. # Usage: ocv_glob_modules(<main location> [<extra location> ...])
  319. macro(ocv_glob_modules main_root)
  320. ocv_cmake_hook(INIT_MODULES_GLOB)
  321. if(DEFINED OPENCV_INITIAL_PASS)
  322. message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
  323. endif()
  324. # collect modules
  325. set(OPENCV_INITIAL_PASS ON)
  326. _glob_locations(__main_paths __main_names ${main_root})
  327. _glob_locations(__extra_paths __extra_names ${ARGN})
  328. _assert_uniqueness("Duplicated modules LOCATIONS has been found" ${__main_paths} ${__extra_paths})
  329. _assert_uniqueness("Duplicated modules NAMES has been found" ${__main_names} ${__extra_names})
  330. set(OPENCV_PROCESSING_EXTRA_MODULES 0)
  331. ocv_cmake_hook(PRE_MODULES_SCAN)
  332. _add_modules_1(__main_paths __main_names)
  333. set(OPENCV_PROCESSING_EXTRA_MODULES 1)
  334. ocv_cmake_hook(PRE_MODULES_SCAN_EXTRA)
  335. _add_modules_1(__extra_paths __extra_names)
  336. ocv_clear_vars(__main_names __extra_names __main_paths __extra_paths)
  337. ocv_cmake_hook(POST_MODULES_SCAN)
  338. # resolve dependencies
  339. __ocv_resolve_dependencies()
  340. # create modules
  341. set(OPENCV_INITIAL_PASS OFF)
  342. ocv_cmake_hook(PRE_MODULES_CREATE)
  343. _add_modules_2(${OPENCV_MODULES_BUILD})
  344. ocv_cmake_hook(POST_MODULES_CREATE)
  345. endmacro()
  346. # called by root CMakeLists.txt
  347. macro(ocv_register_modules)
  348. if(NOT OPENCV_MODULES_PATH)
  349. set(OPENCV_MODULES_PATH "${OpenCV_SOURCE_DIR}/modules")
  350. endif()
  351. ocv_glob_modules(${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH})
  352. # build lists of modules to be documented
  353. set(OPENCV_MODULES_MAIN "")
  354. set(OPENCV_MODULES_EXTRA "")
  355. foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
  356. string(REGEX REPLACE "^opencv_" "" mod "${mod}")
  357. if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${OpenCV_SOURCE_DIR}/modules/${mod}")
  358. list(APPEND OPENCV_MODULES_MAIN ${mod})
  359. else()
  360. list(APPEND OPENCV_MODULES_EXTRA ${mod})
  361. endif()
  362. endforeach()
  363. ocv_list_sort(OPENCV_MODULES_MAIN)
  364. ocv_list_sort(OPENCV_MODULES_EXTRA)
  365. set(FIXED_ORDER_MODULES core imgproc imgcodecs videoio highgui video calib3d features2d objdetect dnn ml flann photo stitching)
  366. list(REMOVE_ITEM OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES})
  367. set(OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES} ${OPENCV_MODULES_MAIN})
  368. set(OPENCV_MODULES_MAIN ${OPENCV_MODULES_MAIN} CACHE INTERNAL "List of main modules" FORCE)
  369. set(OPENCV_MODULES_EXTRA ${OPENCV_MODULES_EXTRA} CACHE INTERNAL "List of extra modules" FORCE)
  370. endmacro()
  371. # disables OpenCV module with missing dependencies
  372. function(__ocv_module_turn_off the_module)
  373. list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
  374. list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
  375. list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
  376. list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
  377. set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
  378. set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
  379. set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
  380. set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
  381. endfunction()
  382. # sort modules by dependencies
  383. function(__ocv_sort_modules_by_deps __lst)
  384. ocv_list_sort(${__lst})
  385. set(input ${${__lst}})
  386. set(result "")
  387. set(result_extra "")
  388. while(input)
  389. list(LENGTH input length_before)
  390. foreach (m ${input})
  391. # check if module is in the result already
  392. if (NOT ";${result};" MATCHES ";${m};")
  393. # scan through module dependencies...
  394. set(unresolved_deps_found FALSE)
  395. foreach (d ${OPENCV_MODULE_${m}_DEPS})
  396. # ... which are not already in the result and are enabled
  397. if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
  398. set(unresolved_deps_found TRUE)
  399. break()
  400. endif()
  401. endforeach()
  402. # check if all dependencies for this module has been resolved
  403. if (NOT unresolved_deps_found)
  404. list(APPEND result ${m})
  405. list(REMOVE_ITEM input ${m})
  406. endif()
  407. endif()
  408. endforeach()
  409. list(LENGTH input length_after)
  410. # check for infinite loop or unresolved dependencies
  411. if (NOT length_after LESS length_before)
  412. if(NOT BUILD_SHARED_LIBS)
  413. if (";${input};" MATCHES ";opencv_world;")
  414. list(REMOVE_ITEM input "opencv_world")
  415. list(APPEND result_extra "opencv_world")
  416. else()
  417. # We can't do here something
  418. list(APPEND result ${input})
  419. break()
  420. endif()
  421. else()
  422. message(FATAL_ERROR "FATAL: Unresolved dependencies or loop in dependency graph (${length_after})\n"
  423. "Processed ${__lst}: ${${__lst}}\n"
  424. "Good modules: ${result}\n"
  425. "Bad modules: ${input}"
  426. )
  427. list(APPEND result ${input})
  428. break()
  429. endif()
  430. endif()
  431. endwhile()
  432. set(${__lst} "${result};${result_extra}" PARENT_SCOPE)
  433. endfunction()
  434. # resolve dependencies
  435. function(__ocv_resolve_dependencies)
  436. foreach(m ${OPENCV_MODULES_DISABLED_USER})
  437. set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
  438. endforeach()
  439. foreach(m ${OPENCV_MODULES_BUILD})
  440. set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
  441. endforeach()
  442. # Whitelist feature
  443. if(BUILD_LIST)
  444. # Prepare the list
  445. string(REGEX REPLACE "[ ,:]+" ";" whitelist "${BUILD_LIST}" )
  446. if(BUILD_opencv_world)
  447. list(APPEND whitelist world)
  448. endif()
  449. ocv_list_add_prefix(whitelist "opencv_")
  450. ocv_list_sort(whitelist)
  451. ocv_list_unique(whitelist)
  452. message(STATUS "Using whitelist: ${whitelist}")
  453. # Expand the list
  454. foreach(depth RANGE 10)
  455. set(new_whitelist ${whitelist})
  456. foreach(m ${whitelist})
  457. list(APPEND new_whitelist ${OPENCV_MODULE_${m}_REQ_DEPS})
  458. list(APPEND new_whitelist ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
  459. endforeach()
  460. ocv_list_sort(new_whitelist)
  461. ocv_list_unique(new_whitelist)
  462. if("${whitelist}" STREQUAL "${new_whitelist}")
  463. break()
  464. endif()
  465. set(whitelist "${new_whitelist}")
  466. endforeach()
  467. # Disable modules not in whitelist
  468. foreach(m ${OPENCV_MODULES_BUILD})
  469. list(FIND whitelist ${m} idx)
  470. if(idx EQUAL -1)
  471. message(STATUS "Module ${m} disabled by whitelist")
  472. __ocv_module_turn_off(${m})
  473. endif()
  474. endforeach()
  475. endif()
  476. # add reverse wrapper dependencies (BINDINDS)
  477. foreach(the_module ${OPENCV_MODULES_BUILD})
  478. foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
  479. if(wrapper STREQUAL "python") # hack for python (BINDINDS)
  480. ocv_add_dependencies(opencv_python2 OPTIONAL ${the_module})
  481. ocv_add_dependencies(opencv_python3 OPTIONAL ${the_module})
  482. else()
  483. ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
  484. endif()
  485. if(DEFINED OPENCV_MODULE_opencv_${wrapper}_bindings_generator_CLASS)
  486. ocv_add_dependencies(opencv_${wrapper}_bindings_generator OPTIONAL ${the_module})
  487. endif()
  488. endforeach()
  489. endforeach()
  490. # disable MODULES with unresolved dependencies
  491. set(has_changes ON)
  492. while(has_changes)
  493. set(has_changes OFF)
  494. foreach(m ${OPENCV_MODULES_BUILD})
  495. set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
  496. while(__deps)
  497. ocv_list_pop_front(__deps d)
  498. string(TOLOWER "${d}" upper_d)
  499. if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
  500. if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
  501. message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
  502. __ocv_module_turn_off(${m})
  503. set(has_changes ON)
  504. break()
  505. else()
  506. message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
  507. endif()
  508. endif()
  509. endwhile()
  510. endforeach()
  511. endwhile()
  512. # message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
  513. foreach(m ${OPENCV_MODULES_BUILD})
  514. set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
  515. foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
  516. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  517. if(HAVE_${d} OR TARGET ${d})
  518. list(APPEND deps_${m} ${d})
  519. endif()
  520. endif()
  521. endforeach()
  522. # message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
  523. endforeach()
  524. # propagate dependencies
  525. set(has_changes ON)
  526. while(has_changes)
  527. set(has_changes OFF)
  528. foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
  529. foreach(m ${OPENCV_MODULES_BUILD})
  530. if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
  531. foreach(d ${deps_${m2}})
  532. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  533. # message(STATUS " Transfer dependency ${d} from ${m2} to ${m}")
  534. list(APPEND deps_${m} ${d})
  535. set(has_changes ON)
  536. endif()
  537. if(BUILD_opencv_world
  538. AND NOT "${m}" STREQUAL "opencv_world"
  539. AND NOT "${m2}" STREQUAL "opencv_world"
  540. AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD
  541. AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
  542. if(NOT (";${deps_${m}};" MATCHES ";opencv_world;"))
  543. # message(STATUS " Transfer dependency opencv_world alias ${m2} to ${m}")
  544. list(APPEND deps_${m} opencv_world)
  545. set(has_changes ON)
  546. endif()
  547. endif()
  548. endforeach()
  549. endif()
  550. endforeach()
  551. endforeach()
  552. endwhile()
  553. # process private deps
  554. foreach(m ${OPENCV_MODULES_BUILD})
  555. foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
  556. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  557. list(APPEND deps_${m} ${d})
  558. endif()
  559. endforeach()
  560. foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
  561. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  562. if(HAVE_${d} OR TARGET ${d})
  563. list(APPEND deps_${m} ${d})
  564. endif()
  565. endif()
  566. endforeach()
  567. endforeach()
  568. ocv_list_sort(OPENCV_MODULES_BUILD)
  569. foreach(m ${OPENCV_MODULES_BUILD})
  570. # message(STATUS "FULL deps of ${m}: ${deps_${m}}")
  571. set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
  572. set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
  573. ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
  574. if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
  575. list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
  576. endif()
  577. endforeach()
  578. # reorder dependencies
  579. foreach(m ${OPENCV_MODULES_BUILD})
  580. __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
  581. set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
  582. # process world
  583. if(BUILD_opencv_world)
  584. if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
  585. list(APPEND OPENCV_WORLD_MODULES ${m})
  586. endif()
  587. foreach(m2 ${OPENCV_MODULES_BUILD})
  588. if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
  589. if(";${LINK_DEPS};" MATCHES ";${m2};")
  590. list(REMOVE_ITEM LINK_DEPS ${m2})
  591. if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
  592. list(APPEND LINK_DEPS opencv_world)
  593. endif()
  594. endif()
  595. if("${m}" STREQUAL opencv_world)
  596. list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
  597. endif()
  598. endif()
  599. endforeach()
  600. endif()
  601. set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
  602. set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
  603. set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
  604. # message(STATUS " module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
  605. # message(STATUS " module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
  606. # message(STATUS " extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
  607. # message(STATUS "")
  608. endforeach()
  609. __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
  610. set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} CACHE INTERNAL "List of OpenCV modules marked for export")
  611. set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} CACHE INTERNAL "List of OpenCV modules included into the build")
  612. set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
  613. set(OPENCV_WORLD_MODULES ${OPENCV_WORLD_MODULES} CACHE INTERNAL "List of OpenCV modules included into the world")
  614. endfunction()
  615. # setup include paths for the list of passed modules
  616. macro(ocv_include_modules)
  617. foreach(d ${ARGN})
  618. if(d MATCHES "^opencv_" AND HAVE_${d})
  619. if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  620. ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
  621. endif()
  622. elseif(EXISTS "${d}")
  623. ocv_include_directories("${d}")
  624. endif()
  625. endforeach()
  626. endmacro()
  627. # same as previous but with dependencies
  628. macro(ocv_include_modules_recurse)
  629. ocv_include_modules(${ARGN})
  630. foreach(d ${ARGN})
  631. if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
  632. foreach (sub ${OPENCV_MODULE_${d}_DEPS})
  633. ocv_include_modules(${sub})
  634. endforeach()
  635. endif()
  636. endforeach()
  637. endmacro()
  638. # setup include paths for the list of passed modules
  639. macro(ocv_target_include_modules target)
  640. foreach(d ${ARGN})
  641. if(d MATCHES "^opencv_")
  642. if(HAVE_${d} AND EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  643. ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
  644. endif()
  645. elseif(EXISTS "${d}")
  646. ocv_target_include_directories(${target} "${d}")
  647. else()
  648. message(WARNING "Unexpected include: ${d} (module=${the_module})")
  649. endif()
  650. endforeach()
  651. endmacro()
  652. # setup include paths for the list of passed modules and recursively add dependent modules
  653. macro(ocv_target_include_modules_recurse target)
  654. foreach(d ${ARGN})
  655. if(d MATCHES "^opencv_" AND HAVE_${d})
  656. if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  657. ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
  658. endif()
  659. if(OPENCV_MODULE_${d}_DEPS)
  660. ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
  661. endif()
  662. elseif(EXISTS "${d}")
  663. ocv_target_include_directories(${target} "${d}")
  664. endif()
  665. endforeach()
  666. endmacro()
  667. # setup include path for OpenCV headers for specified module
  668. # ocv_module_include_directories(<extra include directories/extra include modules>)
  669. macro(ocv_module_include_directories)
  670. if(ENABLE_PRECOMPILED_HEADERS OR OPENCV_INCLUDE_DIR_APPEND_MODULE_SRC)
  671. ocv_target_include_directories(${the_module} "${OPENCV_MODULE_${the_module}_LOCATION}/src")
  672. endif()
  673. ocv_target_include_directories(${the_module}
  674. "${OPENCV_MODULE_${the_module}_LOCATION}/include"
  675. "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
  676. )
  677. ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
  678. endmacro()
  679. # sets header and source files for the current module
  680. # NB: all files specified as headers will be installed
  681. # Usage:
  682. # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
  683. macro(ocv_set_module_sources)
  684. ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
  685. set(OPENCV_MODULE_${the_module}_HEADERS "")
  686. set(OPENCV_MODULE_${the_module}_SOURCES "")
  687. foreach(f "HEADERS" ${ARGN})
  688. if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
  689. set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
  690. else()
  691. list(APPEND ${__filesvar} "${f}")
  692. endif()
  693. endforeach()
  694. # the hacky way to embed any files into the OpenCV without modification of its build system
  695. if(COMMAND ocv_get_module_external_sources)
  696. ocv_get_module_external_sources()
  697. endif()
  698. if(OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED)
  699. list(APPEND OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED})
  700. endif()
  701. # TODO Update hooks above
  702. ocv_cmake_hook(INIT_MODULE_SOURCES)
  703. ocv_cmake_hook(INIT_MODULE_SOURCES_${the_module})
  704. # use full paths for module to be independent from the module location
  705. ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
  706. set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
  707. set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
  708. endmacro()
  709. # finds and sets headers and sources for the standard OpenCV module
  710. # Usage:
  711. # ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
  712. macro(ocv_glob_module_sources)
  713. ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
  714. set(_argn ${ARGN})
  715. list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
  716. if(NOT exclude_cuda EQUAL -1)
  717. list(REMOVE_AT _argn ${exclude_cuda})
  718. endif()
  719. list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
  720. if(NOT exclude_opencl EQUAL -1)
  721. list(REMOVE_AT _argn ${exclude_opencl})
  722. endif()
  723. file(GLOB_RECURSE lib_srcs
  724. "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
  725. )
  726. file(GLOB_RECURSE lib_int_hdrs
  727. "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
  728. "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
  729. )
  730. file(GLOB lib_hdrs
  731. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
  732. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
  733. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
  734. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp"
  735. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h"
  736. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.hpp"
  737. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.h"
  738. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/legacy/*.h"
  739. )
  740. file(GLOB lib_hdrs_detail
  741. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
  742. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
  743. )
  744. if (APPLE)
  745. file(GLOB_RECURSE lib_srcs_apple
  746. "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
  747. "${CMAKE_CURRENT_LIST_DIR}/src/*.swift"
  748. )
  749. list(APPEND lib_srcs ${lib_srcs_apple})
  750. endif()
  751. ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
  752. ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
  753. set(lib_cuda_srcs "")
  754. set(lib_cuda_hdrs "")
  755. if(HAVE_CUDA AND exclude_cuda EQUAL -1)
  756. file(GLOB lib_cuda_srcs
  757. "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
  758. )
  759. file(GLOB lib_cuda_hdrs
  760. "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
  761. )
  762. source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
  763. endif()
  764. file(GLOB cl_kernels
  765. "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
  766. )
  767. if(cl_kernels AND exclude_opencl EQUAL -1)
  768. set(OCL_NAME opencl_kernels_${name})
  769. add_custom_command(
  770. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" # don't add .hpp file here to optimize build process
  771. COMMAND ${CMAKE_COMMAND} "-DMODULE_NAME=${name}" "-DCL_DIR=${CMAKE_CURRENT_LIST_DIR}/src/opencl" "-DOUTPUT=${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
  772. DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
  773. COMMENT "Processing OpenCL kernels (${name})"
  774. )
  775. ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
  776. ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
  777. set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
  778. PROPERTIES GENERATED TRUE
  779. )
  780. list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
  781. endif()
  782. ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
  783. SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
  784. endmacro()
  785. # creates OpenCV module in current folder
  786. # creates new target, configures standard dependencies, compilers flags, install rules
  787. # Usage:
  788. # ocv_create_module(<extra link dependencies>)
  789. # ocv_create_module()
  790. macro(ocv_create_module)
  791. ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
  792. if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
  793. message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
  794. endif()
  795. if(NOT " ${ARGN}" STREQUAL " ")
  796. set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
  797. endif()
  798. if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
  799. # nothing
  800. set(the_module_target opencv_world)
  801. else()
  802. _ocv_create_module(${ARGN})
  803. set(the_module_target ${the_module})
  804. endif()
  805. if(WINRT AND BUILD_TESTS)
  806. # removing APPCONTAINER from modules to run from console
  807. # in case of usual starting of WinRT test apps output is missing
  808. # so starting of console version w/o APPCONTAINER is required to get test results
  809. # also this allows to use opencv_extra test data for these tests
  810. if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
  811. add_custom_command(TARGET ${the_module}
  812. POST_BUILD
  813. COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
  814. endif()
  815. if("${the_module}" STREQUAL "opencv_ts")
  816. # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
  817. # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
  818. add_custom_command(TARGET ${the_module}
  819. POST_BUILD
  820. COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
  821. COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
  822. COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
  823. endif()
  824. endif()
  825. endmacro()
  826. macro(_ocv_create_module)
  827. add_definitions(-D__OPENCV_BUILD=1)
  828. ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
  829. set(__module_headers ${OPENCV_MODULE_${the_module}_HEADERS})
  830. if(__module_headers)
  831. list(SORT __module_headers) # fix headers order, useful for bindings
  832. endif()
  833. set(OPENCV_MODULE_${the_module}_HEADERS ${__module_headers} CACHE INTERNAL "List of header files for ${the_module}")
  834. set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
  835. # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
  836. # be called at some point in the future. We can't look into the future, though,
  837. # so this will have to do.
  838. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
  839. get_native_precompiled_header(${the_module} precomp.hpp)
  840. endif()
  841. if(WIN32
  842. AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
  843. AND NOT OPENCV_VS_VERSIONINFO_SKIP)
  844. if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
  845. set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
  846. elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
  847. set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
  848. elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
  849. set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
  850. ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
  851. NAME "${the_module}"
  852. FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
  853. INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
  854. ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
  855. )
  856. endif()
  857. if(_VS_VERSION_FILE)
  858. if(NOT EXISTS "${_VS_VERSION_FILE}")
  859. message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
  860. endif()
  861. source_group("Src" FILES "${_VS_VERSION_FILE}")
  862. endif()
  863. endif()
  864. if(WIN32 AND NOT (
  865. "${the_module}" STREQUAL "opencv_core" OR
  866. "${the_module}" STREQUAL "opencv_world" OR
  867. "${the_module}" STREQUAL "opencv_cudev"
  868. )
  869. AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
  870. AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
  871. )
  872. set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
  873. configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
  874. endif()
  875. source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
  876. source_group("Src" FILES "${${the_module}_pch}")
  877. ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
  878. ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
  879. ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
  880. "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
  881. ${${the_module}_pch}
  882. ${_VS_VERSION_FILE}
  883. ${_DLLMAIN_FILE}
  884. )
  885. set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  886. set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
  887. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  888. ocv_target_link_libraries(${the_module} PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
  889. INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
  890. )
  891. ocv_target_link_libraries(${the_module} PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_EXT}
  892. INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_EXT}
  893. )
  894. ocv_target_link_libraries(${the_module} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
  895. if (HAVE_CUDA)
  896. ocv_target_link_libraries(${the_module} PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
  897. endif()
  898. if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
  899. target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
  900. unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
  901. endif()
  902. add_dependencies(opencv_modules ${the_module})
  903. if(ENABLE_SOLUTION_FOLDERS)
  904. set_target_properties(${the_module} PROPERTIES FOLDER "modules")
  905. endif()
  906. set_target_properties(${the_module} PROPERTIES
  907. OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
  908. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  909. COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
  910. COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
  911. ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  912. COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  913. LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  914. RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  915. DEFINE_SYMBOL CVAPI_EXPORTS
  916. )
  917. if(BUILD_FAT_JAVA_LIB) # force exports from static modules too
  918. if(BUILD_SHARED_LIBS)
  919. message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
  920. endif()
  921. target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
  922. endif()
  923. # For dynamic link numbering conventions
  924. if(NOT ANDROID)
  925. # Android SDK build scripts can include only .so files into final .apk
  926. # As result we should not set version properties for Android
  927. set_target_properties(${the_module} PROPERTIES
  928. VERSION ${OPENCV_LIBVERSION}
  929. SOVERSION ${OPENCV_SOVERSION}
  930. )
  931. endif()
  932. if (ENABLE_GNU_STL_DEBUG)
  933. target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
  934. endif()
  935. if(MSVC)
  936. if(CMAKE_CROSSCOMPILING)
  937. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
  938. endif()
  939. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
  940. endif()
  941. get_target_property(_target_type ${the_module} TYPE)
  942. if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
  943. ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
  944. ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
  945. RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
  946. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
  947. ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
  948. )
  949. endif()
  950. if("${_target_type}" STREQUAL "SHARED_LIBRARY")
  951. install(TARGETS ${the_module}
  952. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
  953. endif()
  954. # only "public" headers need to be installed
  955. ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
  956. ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
  957. if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
  958. foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
  959. string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
  960. if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
  961. install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
  962. else()
  963. #message("Header file will be NOT installed: ${hdr}")
  964. endif()
  965. endforeach()
  966. endif()
  967. _ocv_add_precompiled_headers(${the_module})
  968. ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
  969. ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
  970. endmacro()
  971. # opencv precompiled headers macro (can add pch to modules and tests)
  972. # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
  973. # Usage:
  974. # ocv_add_precompiled_headers(${the_module})
  975. macro(_ocv_add_precompiled_headers the_target)
  976. ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
  977. if("${the_target}" MATCHES "^opencv_test_.*$")
  978. SET(pch_path "test/test_")
  979. elseif("${the_target}" MATCHES "^opencv_perf_.*$")
  980. SET(pch_path "perf/perf_")
  981. else()
  982. SET(pch_path "src/")
  983. endif()
  984. ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
  985. unset(pch_path)
  986. endmacro()
  987. # short command for adding simple OpenCV module
  988. # see ocv_add_module for argument details
  989. # Usage:
  990. # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
  991. macro(ocv_define_module module_name)
  992. ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
  993. set(_argn ${ARGN})
  994. set(exclude_cuda "")
  995. foreach(arg ${_argn})
  996. if("${arg}" STREQUAL "EXCLUDE_CUDA")
  997. set(exclude_cuda "${arg}")
  998. list(REMOVE_ITEM _argn ${arg})
  999. endif()
  1000. endforeach()
  1001. ocv_add_module(${module_name} ${_argn})
  1002. ocv_glob_module_sources(${exclude_cuda})
  1003. ocv_module_include_directories()
  1004. ocv_create_module()
  1005. ocv_add_accuracy_tests()
  1006. ocv_add_perf_tests()
  1007. ocv_add_samples()
  1008. endmacro()
  1009. # ensures that all passed modules are available
  1010. # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
  1011. macro(ocv_check_dependencies)
  1012. set(OCV_DEPENDENCIES_FOUND TRUE)
  1013. foreach(d ${ARGN})
  1014. if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
  1015. set(OCV_DEPENDENCIES_FOUND FALSE)
  1016. break()
  1017. endif()
  1018. endforeach()
  1019. endmacro()
  1020. ################################################################################
  1021. # OpenCV tests
  1022. ################################################################################
  1023. if(DEFINED OPENCV_BUILD_TEST_MODULES_LIST)
  1024. string(REPLACE "," ";" OPENCV_BUILD_TEST_MODULES_LIST "${OPENCV_BUILD_TEST_MODULES_LIST}") # support comma-separated list (,) too
  1025. endif()
  1026. if(DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST)
  1027. string(REPLACE "," ";" OPENCV_BUILD_PERF_TEST_MODULES_LIST "${OPENCV_BUILD_PERF_TEST_MODULES_LIST}") # support comma-separated list (,) too
  1028. endif()
  1029. # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
  1030. macro(__ocv_parse_test_sources tests_type)
  1031. set(OPENCV_${tests_type}_${the_module}_SOURCES "")
  1032. set(OPENCV_${tests_type}_${the_module}_DEPS "")
  1033. set(__file_group_name "")
  1034. set(__file_group_sources "")
  1035. foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
  1036. if(arg STREQUAL "FILES")
  1037. set(__currentvar "__file_group_sources")
  1038. if(__file_group_name AND __file_group_sources)
  1039. source_group("${__file_group_name}" FILES ${__file_group_sources})
  1040. list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
  1041. endif()
  1042. set(__file_group_name "")
  1043. set(__file_group_sources "")
  1044. elseif(arg STREQUAL "DEPENDS_ON")
  1045. set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
  1046. elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
  1047. set(__file_group_name "${arg}")
  1048. else()
  1049. list(APPEND ${__currentvar} "${arg}")
  1050. endif()
  1051. endforeach()
  1052. unset(__file_group_name)
  1053. unset(__file_group_sources)
  1054. unset(__currentvar)
  1055. endmacro()
  1056. ocv_check_environment_variables(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
  1057. # this is a command for adding OpenCV performance tests to the module
  1058. # ocv_add_perf_tests(<extra_dependencies>)
  1059. function(ocv_add_perf_tests)
  1060. ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
  1061. if(WINRT)
  1062. set(OPENCV_DEBUG_POSTFIX "")
  1063. endif()
  1064. set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
  1065. if(BUILD_PERF_TESTS AND EXISTS "${perf_path}"
  1066. AND (NOT DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST
  1067. OR OPENCV_BUILD_PERF_TEST_MODULES_LIST STREQUAL "all"
  1068. OR ";${OPENCV_BUILD_PERF_TEST_MODULES_LIST};" MATCHES ";${name};"
  1069. )
  1070. )
  1071. __ocv_parse_test_sources(PERF ${ARGN})
  1072. # opencv_imgcodecs is required for imread/imwrite
  1073. set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
  1074. ocv_check_dependencies(${perf_deps})
  1075. if(OCV_DEPENDENCIES_FOUND)
  1076. set(the_target "opencv_perf_${name}")
  1077. # project(${the_target})
  1078. if(NOT OPENCV_PERF_${the_module}_SOURCES)
  1079. file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
  1080. file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
  1081. ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
  1082. ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
  1083. set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
  1084. endif()
  1085. ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
  1086. if(NOT BUILD_opencv_world)
  1087. get_native_precompiled_header(${the_target} perf_precomp.hpp)
  1088. endif()
  1089. source_group("Src" FILES "${${the_target}_pch}")
  1090. ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
  1091. ocv_target_include_modules(${the_target} ${perf_deps})
  1092. ocv_target_link_libraries(${the_target} PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
  1093. add_dependencies(opencv_perf_tests ${the_target})
  1094. if(TARGET opencv_videoio_plugins)
  1095. add_dependencies(${the_target} opencv_videoio_plugins)
  1096. endif()
  1097. if(TARGET opencv_highgui_plugins)
  1098. add_dependencies(${the_target} opencv_highgui_plugins)
  1099. endif()
  1100. if(HAVE_HPX)
  1101. message("Linking HPX to Perf test of module ${name}")
  1102. ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
  1103. endif()
  1104. set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
  1105. set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
  1106. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
  1107. # Additional target properties
  1108. set_target_properties(${the_target} PROPERTIES
  1109. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  1110. RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
  1111. )
  1112. if(ENABLE_SOLUTION_FOLDERS)
  1113. set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
  1114. endif()
  1115. if(WINRT)
  1116. # removing APPCONTAINER from tests to run from console
  1117. # look for detailed description inside of ocv_create_module macro above
  1118. add_custom_command(TARGET "opencv_perf_${name}"
  1119. POST_BUILD
  1120. COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
  1121. endif()
  1122. if(NOT BUILD_opencv_world)
  1123. _ocv_add_precompiled_headers(${the_target})
  1124. endif()
  1125. ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
  1126. ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
  1127. "--perf_min_samples=1"
  1128. "--perf_force_samples=1"
  1129. "--perf_verify_sanity")
  1130. else(OCV_DEPENDENCIES_FOUND)
  1131. # TODO: warn about unsatisfied dependencies
  1132. endif(OCV_DEPENDENCIES_FOUND)
  1133. if(INSTALL_TESTS)
  1134. install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
  1135. endif()
  1136. endif()
  1137. endfunction()
  1138. # this is a command for adding OpenCV accuracy/regression tests to the module
  1139. # ocv_add_accuracy_tests(<list of extra dependencies>)
  1140. function(ocv_add_accuracy_tests)
  1141. ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
  1142. set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
  1143. if(BUILD_TESTS AND EXISTS "${test_path}"
  1144. AND (NOT DEFINED OPENCV_BUILD_TEST_MODULES_LIST
  1145. OR OPENCV_BUILD_TEST_MODULES_LIST STREQUAL "all"
  1146. OR ";${OPENCV_BUILD_TEST_MODULES_LIST};" MATCHES ";${name};"
  1147. )
  1148. )
  1149. __ocv_parse_test_sources(TEST ${ARGN})
  1150. # opencv_imgcodecs is required for imread/imwrite
  1151. set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
  1152. ocv_check_dependencies(${test_deps})
  1153. if(OCV_DEPENDENCIES_FOUND)
  1154. set(the_target "opencv_test_${name}")
  1155. # project(${the_target})
  1156. if(NOT OPENCV_TEST_${the_module}_SOURCES)
  1157. file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
  1158. file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
  1159. ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
  1160. ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
  1161. set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
  1162. endif()
  1163. if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
  1164. list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
  1165. endif()
  1166. ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
  1167. if(NOT BUILD_opencv_world)
  1168. get_native_precompiled_header(${the_target} test_precomp.hpp)
  1169. endif()
  1170. source_group("Src" FILES "${${the_target}_pch}")
  1171. ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
  1172. ocv_target_include_modules(${the_target} ${test_deps})
  1173. if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
  1174. ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
  1175. endif()
  1176. ocv_target_link_libraries(${the_target} PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
  1177. add_dependencies(opencv_tests ${the_target})
  1178. if(TARGET opencv_videoio_plugins)
  1179. add_dependencies(${the_target} opencv_videoio_plugins)
  1180. endif()
  1181. if(TARGET opencv_highgui_plugins)
  1182. add_dependencies(${the_target} opencv_highgui_plugins)
  1183. endif()
  1184. if(HAVE_HPX)
  1185. message("Linking HPX to Perf test of module ${name}")
  1186. ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
  1187. endif()
  1188. set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
  1189. set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
  1190. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
  1191. # Additional target properties
  1192. set_target_properties(${the_target} PROPERTIES
  1193. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  1194. RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
  1195. )
  1196. ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
  1197. if(ENABLE_SOLUTION_FOLDERS)
  1198. set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
  1199. endif()
  1200. if(OPENCV_TEST_BIGDATA)
  1201. ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
  1202. endif()
  1203. if(NOT BUILD_opencv_world)
  1204. _ocv_add_precompiled_headers(${the_target})
  1205. endif()
  1206. if(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
  1207. target_compile_options(${the_target} PRIVATE "$<$<CONFIG:Release>:${OPENCV_TEST_EXTRA_CXX_FLAGS_Release}>")
  1208. endif()
  1209. ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
  1210. else(OCV_DEPENDENCIES_FOUND)
  1211. # TODO: warn about unsatisfied dependencies
  1212. endif(OCV_DEPENDENCIES_FOUND)
  1213. if(INSTALL_TESTS)
  1214. install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
  1215. endif()
  1216. endif()
  1217. endfunction()
  1218. function(ocv_add_samples)
  1219. ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
  1220. set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
  1221. if(NOT EXISTS "${samples_path}")
  1222. return()
  1223. endif()
  1224. string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
  1225. if(BUILD_EXAMPLES)
  1226. set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
  1227. ocv_check_dependencies(${samples_deps})
  1228. if(OCV_DEPENDENCIES_FOUND)
  1229. file(GLOB sample_sources "${samples_path}/*.cpp")
  1230. foreach(source ${sample_sources})
  1231. get_filename_component(name "${source}" NAME_WE)
  1232. set(the_target "example_${module_id}_${name}")
  1233. ocv_add_executable(${the_target} "${source}")
  1234. ocv_target_include_modules(${the_target} ${samples_deps})
  1235. ocv_target_link_libraries(${the_target} PRIVATE ${samples_deps})
  1236. set_target_properties(${the_target} PROPERTIES
  1237. PROJECT_LABEL "(sample) ${name}"
  1238. LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
  1239. set_source_files_properties("${source}" PROPERTIES
  1240. LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
  1241. if(ENABLE_SOLUTION_FOLDERS)
  1242. set_target_properties(${the_target} PROPERTIES
  1243. FOLDER "samples/${module_id}")
  1244. endif()
  1245. # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
  1246. set(parent_target opencv_samples_${module_id})
  1247. if(NOT TARGET ${parent_target})
  1248. add_custom_target(${parent_target})
  1249. add_dependencies(opencv_samples ${parent_target})
  1250. endif()
  1251. add_dependencies(${parent_target} ${the_target})
  1252. if(TARGET opencv_videoio_plugins)
  1253. add_dependencies(${the_target} opencv_videoio_plugins)
  1254. endif()
  1255. if(TARGET opencv_highgui_plugins)
  1256. add_dependencies(${the_target} opencv_highgui_plugins)
  1257. endif()
  1258. if(INSTALL_BIN_EXAMPLES)
  1259. install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${module_id}" COMPONENT samples)
  1260. endif()
  1261. endforeach()
  1262. endif()
  1263. endif()
  1264. if(INSTALL_C_EXAMPLES)
  1265. file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
  1266. foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
  1267. IF( IS_DIRECTORY "${ITEM}" )
  1268. LIST( APPEND sample_dirs "${ITEM}" )
  1269. ELSE()
  1270. LIST( APPEND sample_files "${ITEM}" )
  1271. ENDIF()
  1272. endforeach()
  1273. install(FILES ${sample_files}
  1274. DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
  1275. COMPONENT samples)
  1276. install(DIRECTORY ${sample_dirs}
  1277. DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
  1278. COMPONENT samples)
  1279. endif()
  1280. endfunction()