OpenCVModule.cmake 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  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. # optionally configure delay load
  341. if(MSVC AND BUILD_SHARED_LIBS AND ENABLE_DELAYLOAD AND NOT BUILD_opencv_world)
  342. if(${CMAKE_SHARED_LINKER_FLAGS} MATCHES "delayimp.lib")
  343. set(DELAYFLAGS "")
  344. else()
  345. set(DELAYFLAGS "delayimp.lib")
  346. endif()
  347. foreach(mod ${OPENCV_MODULES_BUILD})
  348. if(NOT ${mod} STREQUAL "opencv_core" AND NOT ${mod} MATCHES "bindings_generator|python")
  349. set(DELAYFLAGS "${DELAYFLAGS} /DELAYLOAD:${mod}${OPENCV_VERSION_MAJOR}${OPENCV_VERSION_MINOR}${OPENCV_VERSION_PATCH}.dll")
  350. endif()
  351. endforeach()
  352. if(NOT ${CMAKE_SHARED_LINKER_FLAGS} MATCHES "/IGNORE:4199")
  353. set(DELAYFLAGS "${DELAYFLAGS} /IGNORE:4199")
  354. endif()
  355. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${DELAYFLAGS}")
  356. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${DELAYFLAGS}")
  357. set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${DELAYFLAGS}")
  358. endif()
  359. # create modules
  360. set(OPENCV_INITIAL_PASS OFF)
  361. ocv_cmake_hook(PRE_MODULES_CREATE)
  362. _add_modules_2(${OPENCV_MODULES_BUILD})
  363. ocv_cmake_hook(POST_MODULES_CREATE)
  364. endmacro()
  365. # called by root CMakeLists.txt
  366. macro(ocv_register_modules)
  367. if(NOT OPENCV_MODULES_PATH)
  368. set(OPENCV_MODULES_PATH "${OpenCV_SOURCE_DIR}/modules")
  369. endif()
  370. ocv_glob_modules(${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH})
  371. # build lists of modules to be documented
  372. set(OPENCV_MODULES_MAIN "")
  373. set(OPENCV_MODULES_EXTRA "")
  374. foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
  375. string(REGEX REPLACE "^opencv_" "" mod "${mod}")
  376. if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${OpenCV_SOURCE_DIR}/modules/${mod}")
  377. list(APPEND OPENCV_MODULES_MAIN ${mod})
  378. else()
  379. list(APPEND OPENCV_MODULES_EXTRA ${mod})
  380. endif()
  381. endforeach()
  382. ocv_list_sort(OPENCV_MODULES_MAIN)
  383. ocv_list_sort(OPENCV_MODULES_EXTRA)
  384. set(FIXED_ORDER_MODULES core imgproc imgcodecs videoio highgui video calib3d features2d objdetect dnn ml flann photo stitching)
  385. list(REMOVE_ITEM OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES})
  386. set(OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES} ${OPENCV_MODULES_MAIN})
  387. set(OPENCV_MODULES_MAIN ${OPENCV_MODULES_MAIN} CACHE INTERNAL "List of main modules" FORCE)
  388. set(OPENCV_MODULES_EXTRA ${OPENCV_MODULES_EXTRA} CACHE INTERNAL "List of extra modules" FORCE)
  389. endmacro()
  390. # disables OpenCV module with missing dependencies
  391. function(__ocv_module_turn_off the_module)
  392. list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
  393. list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
  394. list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
  395. list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
  396. set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
  397. set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
  398. set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
  399. set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
  400. endfunction()
  401. # sort modules by dependencies
  402. function(__ocv_sort_modules_by_deps __lst)
  403. ocv_list_sort(${__lst})
  404. set(input ${${__lst}})
  405. set(result "")
  406. set(result_extra "")
  407. while(input)
  408. list(LENGTH input length_before)
  409. foreach (m ${input})
  410. # check if module is in the result already
  411. if (NOT ";${result};" MATCHES ";${m};")
  412. # scan through module dependencies...
  413. set(unresolved_deps_found FALSE)
  414. foreach (d ${OPENCV_MODULE_${m}_DEPS})
  415. # ... which are not already in the result and are enabled
  416. if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
  417. set(unresolved_deps_found TRUE)
  418. break()
  419. endif()
  420. endforeach()
  421. # check if all dependencies for this module has been resolved
  422. if (NOT unresolved_deps_found)
  423. list(APPEND result ${m})
  424. list(REMOVE_ITEM input ${m})
  425. endif()
  426. endif()
  427. endforeach()
  428. list(LENGTH input length_after)
  429. # check for infinite loop or unresolved dependencies
  430. if (NOT length_after LESS length_before)
  431. if(NOT BUILD_SHARED_LIBS)
  432. if (";${input};" MATCHES ";opencv_world;")
  433. list(REMOVE_ITEM input "opencv_world")
  434. list(APPEND result_extra "opencv_world")
  435. else()
  436. # We can't do here something
  437. list(APPEND result ${input})
  438. break()
  439. endif()
  440. else()
  441. message(FATAL_ERROR "FATAL: Unresolved dependencies or loop in dependency graph (${length_after})\n"
  442. "Processed ${__lst}: ${${__lst}}\n"
  443. "Good modules: ${result}\n"
  444. "Bad modules: ${input}"
  445. )
  446. list(APPEND result ${input})
  447. break()
  448. endif()
  449. endif()
  450. endwhile()
  451. set(${__lst} "${result};${result_extra}" PARENT_SCOPE)
  452. endfunction()
  453. # resolve dependencies
  454. function(__ocv_resolve_dependencies)
  455. foreach(m ${OPENCV_MODULES_DISABLED_USER})
  456. set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
  457. endforeach()
  458. foreach(m ${OPENCV_MODULES_BUILD})
  459. set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
  460. endforeach()
  461. # Whitelist feature
  462. if(BUILD_LIST)
  463. # Prepare the list
  464. string(REGEX REPLACE "[ ,:]+" ";" whitelist "${BUILD_LIST}" )
  465. if(BUILD_opencv_world)
  466. list(APPEND whitelist world)
  467. endif()
  468. ocv_list_add_prefix(whitelist "opencv_")
  469. ocv_list_sort(whitelist)
  470. ocv_list_unique(whitelist)
  471. message(STATUS "Using whitelist: ${whitelist}")
  472. # Expand the list
  473. foreach(depth RANGE 10)
  474. set(new_whitelist ${whitelist})
  475. foreach(m ${whitelist})
  476. list(APPEND new_whitelist ${OPENCV_MODULE_${m}_REQ_DEPS})
  477. list(APPEND new_whitelist ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
  478. endforeach()
  479. ocv_list_sort(new_whitelist)
  480. ocv_list_unique(new_whitelist)
  481. if("${whitelist}" STREQUAL "${new_whitelist}")
  482. break()
  483. endif()
  484. set(whitelist "${new_whitelist}")
  485. endforeach()
  486. # Disable modules not in whitelist
  487. foreach(m ${OPENCV_MODULES_BUILD})
  488. list(FIND whitelist ${m} idx)
  489. if(idx EQUAL -1)
  490. message(STATUS "Module ${m} disabled by whitelist")
  491. __ocv_module_turn_off(${m})
  492. endif()
  493. endforeach()
  494. endif()
  495. # add reverse wrapper dependencies (BINDINDS)
  496. foreach(the_module ${OPENCV_MODULES_BUILD})
  497. foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
  498. if(wrapper STREQUAL "python") # hack for python (BINDINDS)
  499. ocv_add_dependencies(opencv_python2 OPTIONAL ${the_module})
  500. ocv_add_dependencies(opencv_python3 OPTIONAL ${the_module})
  501. else()
  502. ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
  503. endif()
  504. if(DEFINED OPENCV_MODULE_opencv_${wrapper}_bindings_generator_CLASS)
  505. ocv_add_dependencies(opencv_${wrapper}_bindings_generator OPTIONAL ${the_module})
  506. endif()
  507. endforeach()
  508. endforeach()
  509. # disable MODULES with unresolved dependencies
  510. set(has_changes ON)
  511. while(has_changes)
  512. set(has_changes OFF)
  513. foreach(m ${OPENCV_MODULES_BUILD})
  514. set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
  515. while(__deps)
  516. ocv_list_pop_front(__deps d)
  517. string(TOLOWER "${d}" upper_d)
  518. if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
  519. if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
  520. message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
  521. __ocv_module_turn_off(${m})
  522. set(has_changes ON)
  523. break()
  524. else()
  525. message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
  526. endif()
  527. endif()
  528. endwhile()
  529. endforeach()
  530. endwhile()
  531. # message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
  532. foreach(m ${OPENCV_MODULES_BUILD})
  533. set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
  534. foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
  535. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  536. if(HAVE_${d} OR TARGET ${d})
  537. list(APPEND deps_${m} ${d})
  538. endif()
  539. endif()
  540. endforeach()
  541. # message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
  542. endforeach()
  543. # propagate dependencies
  544. set(has_changes ON)
  545. while(has_changes)
  546. set(has_changes OFF)
  547. foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
  548. foreach(m ${OPENCV_MODULES_BUILD})
  549. if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
  550. foreach(d ${deps_${m2}})
  551. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  552. # message(STATUS " Transfer dependency ${d} from ${m2} to ${m}")
  553. list(APPEND deps_${m} ${d})
  554. set(has_changes ON)
  555. endif()
  556. if(BUILD_opencv_world
  557. AND NOT "${m}" STREQUAL "opencv_world"
  558. AND NOT "${m2}" STREQUAL "opencv_world"
  559. AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD
  560. AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
  561. if(NOT (";${deps_${m}};" MATCHES ";opencv_world;"))
  562. # message(STATUS " Transfer dependency opencv_world alias ${m2} to ${m}")
  563. list(APPEND deps_${m} opencv_world)
  564. set(has_changes ON)
  565. endif()
  566. endif()
  567. endforeach()
  568. endif()
  569. endforeach()
  570. endforeach()
  571. endwhile()
  572. # process private deps
  573. foreach(m ${OPENCV_MODULES_BUILD})
  574. foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
  575. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  576. list(APPEND deps_${m} ${d})
  577. endif()
  578. endforeach()
  579. foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
  580. if(NOT (";${deps_${m}};" MATCHES ";${d};"))
  581. if(HAVE_${d} OR TARGET ${d})
  582. list(APPEND deps_${m} ${d})
  583. endif()
  584. endif()
  585. endforeach()
  586. endforeach()
  587. ocv_list_sort(OPENCV_MODULES_BUILD)
  588. foreach(m ${OPENCV_MODULES_BUILD})
  589. # message(STATUS "FULL deps of ${m}: ${deps_${m}}")
  590. set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
  591. set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
  592. ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
  593. if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
  594. list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
  595. endif()
  596. endforeach()
  597. # reorder dependencies
  598. foreach(m ${OPENCV_MODULES_BUILD})
  599. __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
  600. set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
  601. # process world
  602. if(BUILD_opencv_world)
  603. if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
  604. list(APPEND OPENCV_WORLD_MODULES ${m})
  605. endif()
  606. foreach(m2 ${OPENCV_MODULES_BUILD})
  607. if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
  608. if(";${LINK_DEPS};" MATCHES ";${m2};")
  609. list(REMOVE_ITEM LINK_DEPS ${m2})
  610. if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
  611. list(APPEND LINK_DEPS opencv_world)
  612. endif()
  613. endif()
  614. if("${m}" STREQUAL opencv_world)
  615. list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
  616. endif()
  617. endif()
  618. endforeach()
  619. endif()
  620. set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
  621. set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
  622. set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
  623. # message(STATUS " module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
  624. # message(STATUS " module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
  625. # message(STATUS " extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
  626. # message(STATUS "")
  627. endforeach()
  628. __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
  629. set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} CACHE INTERNAL "List of OpenCV modules marked for export")
  630. set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} CACHE INTERNAL "List of OpenCV modules included into the build")
  631. set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
  632. set(OPENCV_WORLD_MODULES ${OPENCV_WORLD_MODULES} CACHE INTERNAL "List of OpenCV modules included into the world")
  633. endfunction()
  634. # setup include paths for the list of passed modules
  635. macro(ocv_include_modules)
  636. foreach(d ${ARGN})
  637. if(d MATCHES "^opencv_" AND HAVE_${d})
  638. if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  639. ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
  640. endif()
  641. elseif(EXISTS "${d}")
  642. ocv_include_directories("${d}")
  643. endif()
  644. endforeach()
  645. endmacro()
  646. # same as previous but with dependencies
  647. macro(ocv_include_modules_recurse)
  648. ocv_include_modules(${ARGN})
  649. foreach(d ${ARGN})
  650. if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
  651. foreach (sub ${OPENCV_MODULE_${d}_DEPS})
  652. ocv_include_modules(${sub})
  653. endforeach()
  654. endif()
  655. endforeach()
  656. endmacro()
  657. # setup include paths for the list of passed modules
  658. macro(ocv_target_include_modules target)
  659. foreach(d ${ARGN})
  660. if(d MATCHES "^opencv_")
  661. if(HAVE_${d} AND EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  662. ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
  663. endif()
  664. elseif(EXISTS "${d}")
  665. ocv_target_include_directories(${target} "${d}")
  666. else()
  667. message(WARNING "Unexpected include: ${d} (module=${the_module})")
  668. endif()
  669. endforeach()
  670. endmacro()
  671. # setup include paths for the list of passed modules and recursively add dependent modules
  672. macro(ocv_target_include_modules_recurse target)
  673. foreach(d ${ARGN})
  674. if(d MATCHES "^opencv_" AND HAVE_${d})
  675. if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  676. ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
  677. endif()
  678. if(OPENCV_MODULE_${d}_DEPS)
  679. ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
  680. endif()
  681. elseif(EXISTS "${d}")
  682. ocv_target_include_directories(${target} "${d}")
  683. endif()
  684. endforeach()
  685. endmacro()
  686. # setup include path for OpenCV headers for specified module
  687. # ocv_module_include_directories(<extra include directories/extra include modules>)
  688. macro(ocv_module_include_directories)
  689. if(ENABLE_PRECOMPILED_HEADERS OR OPENCV_INCLUDE_DIR_APPEND_MODULE_SRC)
  690. ocv_target_include_directories(${the_module} "${OPENCV_MODULE_${the_module}_LOCATION}/src")
  691. endif()
  692. ocv_target_include_directories(${the_module}
  693. "${OPENCV_MODULE_${the_module}_LOCATION}/include"
  694. "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
  695. )
  696. ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
  697. endmacro()
  698. # sets header and source files for the current module
  699. # NB: all files specified as headers will be installed
  700. # Usage:
  701. # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
  702. macro(ocv_set_module_sources)
  703. ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
  704. set(OPENCV_MODULE_${the_module}_HEADERS "")
  705. set(OPENCV_MODULE_${the_module}_SOURCES "")
  706. foreach(f "HEADERS" ${ARGN})
  707. if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
  708. set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
  709. else()
  710. list(APPEND ${__filesvar} "${f}")
  711. endif()
  712. endforeach()
  713. # the hacky way to embed any files into the OpenCV without modification of its build system
  714. if(COMMAND ocv_get_module_external_sources)
  715. ocv_get_module_external_sources()
  716. endif()
  717. if(OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED)
  718. list(APPEND OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED})
  719. endif()
  720. # TODO Update hooks above
  721. ocv_cmake_hook(INIT_MODULE_SOURCES)
  722. ocv_cmake_hook(INIT_MODULE_SOURCES_${the_module})
  723. # use full paths for module to be independent from the module location
  724. ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
  725. set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
  726. set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
  727. endmacro()
  728. # finds and sets headers and sources for the standard OpenCV module
  729. # Usage:
  730. # ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
  731. macro(ocv_glob_module_sources)
  732. ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
  733. set(_argn ${ARGN})
  734. list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
  735. if(NOT exclude_cuda EQUAL -1)
  736. list(REMOVE_AT _argn ${exclude_cuda})
  737. endif()
  738. list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
  739. if(NOT exclude_opencl EQUAL -1)
  740. list(REMOVE_AT _argn ${exclude_opencl})
  741. endif()
  742. file(GLOB_RECURSE lib_srcs
  743. "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
  744. )
  745. file(GLOB_RECURSE lib_int_hdrs
  746. "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
  747. "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
  748. )
  749. file(GLOB lib_hdrs
  750. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
  751. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
  752. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
  753. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp"
  754. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h"
  755. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.hpp"
  756. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.h"
  757. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/legacy/*.h"
  758. )
  759. file(GLOB lib_hdrs_detail
  760. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
  761. "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
  762. )
  763. if (APPLE)
  764. file(GLOB_RECURSE lib_srcs_apple
  765. "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
  766. "${CMAKE_CURRENT_LIST_DIR}/src/*.swift"
  767. )
  768. list(APPEND lib_srcs ${lib_srcs_apple})
  769. endif()
  770. ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
  771. ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
  772. set(lib_cuda_srcs "")
  773. set(lib_cuda_hdrs "")
  774. if(HAVE_CUDA AND exclude_cuda EQUAL -1)
  775. file(GLOB lib_cuda_srcs
  776. "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
  777. )
  778. file(GLOB lib_cuda_hdrs
  779. "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
  780. )
  781. source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
  782. endif()
  783. file(GLOB cl_kernels
  784. "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
  785. )
  786. if(cl_kernels AND exclude_opencl EQUAL -1)
  787. set(OCL_NAME opencl_kernels_${name})
  788. add_custom_command(
  789. OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" # don't add .hpp file here to optimize build process
  790. 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"
  791. DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
  792. COMMENT "Processing OpenCL kernels (${name})"
  793. )
  794. ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
  795. ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
  796. set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
  797. PROPERTIES GENERATED TRUE
  798. )
  799. list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
  800. endif()
  801. ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
  802. SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
  803. endmacro()
  804. # creates OpenCV module in current folder
  805. # creates new target, configures standard dependencies, compilers flags, install rules
  806. # Usage:
  807. # ocv_create_module(<extra link dependencies>)
  808. # ocv_create_module()
  809. macro(ocv_create_module)
  810. ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
  811. if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
  812. message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
  813. endif()
  814. if(NOT " ${ARGN}" STREQUAL " ")
  815. set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
  816. endif()
  817. if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
  818. # nothing
  819. set(the_module_target opencv_world)
  820. else()
  821. _ocv_create_module(${ARGN})
  822. set(the_module_target ${the_module})
  823. endif()
  824. if(WINRT AND BUILD_TESTS)
  825. # removing APPCONTAINER from modules to run from console
  826. # in case of usual starting of WinRT test apps output is missing
  827. # so starting of console version w/o APPCONTAINER is required to get test results
  828. # also this allows to use opencv_extra test data for these tests
  829. if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
  830. add_custom_command(TARGET ${the_module}
  831. POST_BUILD
  832. COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
  833. endif()
  834. endif()
  835. endmacro()
  836. macro(_ocv_create_module)
  837. add_definitions(-D__OPENCV_BUILD=1)
  838. ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
  839. set(__module_headers ${OPENCV_MODULE_${the_module}_HEADERS})
  840. if(__module_headers)
  841. list(SORT __module_headers) # fix headers order, useful for bindings
  842. endif()
  843. set(OPENCV_MODULE_${the_module}_HEADERS ${__module_headers} CACHE INTERNAL "List of header files for ${the_module}")
  844. set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
  845. # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
  846. # be called at some point in the future. We can't look into the future, though,
  847. # so this will have to do.
  848. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
  849. get_native_precompiled_header(${the_module} precomp.hpp)
  850. endif()
  851. if(WIN32
  852. AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
  853. AND NOT OPENCV_VS_VERSIONINFO_SKIP)
  854. if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
  855. set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
  856. elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
  857. set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
  858. elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
  859. set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
  860. ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
  861. NAME "${the_module}"
  862. FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
  863. INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
  864. ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
  865. )
  866. endif()
  867. if(_VS_VERSION_FILE)
  868. if(NOT EXISTS "${_VS_VERSION_FILE}")
  869. message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
  870. endif()
  871. source_group("Src" FILES "${_VS_VERSION_FILE}")
  872. endif()
  873. endif()
  874. if(WIN32 AND NOT (
  875. "${the_module}" STREQUAL "opencv_core" OR
  876. "${the_module}" STREQUAL "opencv_world" OR
  877. "${the_module}" STREQUAL "opencv_cudev"
  878. )
  879. AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
  880. AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
  881. )
  882. set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
  883. configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
  884. endif()
  885. source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
  886. source_group("Src" FILES "${${the_module}_pch}")
  887. ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
  888. ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
  889. ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
  890. "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
  891. ${${the_module}_pch}
  892. ${_VS_VERSION_FILE}
  893. ${_DLLMAIN_FILE}
  894. )
  895. set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  896. set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
  897. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  898. ocv_target_link_libraries(${the_module} PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
  899. INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
  900. )
  901. ocv_target_link_libraries(${the_module} PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_EXT}
  902. INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_EXT}
  903. )
  904. ocv_target_link_libraries(${the_module} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
  905. if (NOT ENABLE_CUDA_FIRST_CLASS_LANGUAGE AND HAVE_CUDA)
  906. ocv_target_link_libraries(${the_module} PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
  907. endif()
  908. if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
  909. target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
  910. unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
  911. endif()
  912. add_dependencies(opencv_modules ${the_module})
  913. if(ENABLE_SOLUTION_FOLDERS)
  914. set_target_properties(${the_module} PROPERTIES FOLDER "modules")
  915. endif()
  916. set_target_properties(${the_module} PROPERTIES
  917. OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
  918. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  919. COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
  920. COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
  921. ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  922. COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  923. LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  924. RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  925. DEFINE_SYMBOL CVAPI_EXPORTS
  926. )
  927. if(BUILD_FAT_JAVA_LIB) # force exports from static modules too
  928. if(BUILD_SHARED_LIBS)
  929. message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
  930. endif()
  931. target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
  932. endif()
  933. # For dynamic link numbering conventions
  934. if(NOT ANDROID)
  935. # Android SDK build scripts can include only .so files into final .apk
  936. # As result we should not set version properties for Android
  937. set_target_properties(${the_module} PROPERTIES
  938. VERSION ${OPENCV_LIBVERSION}
  939. SOVERSION ${OPENCV_SOVERSION}
  940. )
  941. endif()
  942. if (ENABLE_GNU_STL_DEBUG)
  943. target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
  944. endif()
  945. if(MSVC)
  946. if(CMAKE_CROSSCOMPILING)
  947. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
  948. endif()
  949. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
  950. endif()
  951. get_target_property(_target_type ${the_module} TYPE)
  952. if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
  953. ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
  954. ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
  955. RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
  956. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
  957. ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
  958. )
  959. endif()
  960. if("${_target_type}" STREQUAL "SHARED_LIBRARY")
  961. install(TARGETS ${the_module}
  962. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
  963. endif()
  964. # only "public" headers need to be installed
  965. ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
  966. ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
  967. if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
  968. foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
  969. string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
  970. if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
  971. install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
  972. else()
  973. #message("Header file will be NOT installed: ${hdr}")
  974. endif()
  975. endforeach()
  976. endif()
  977. _ocv_add_precompiled_headers(${the_module})
  978. ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
  979. ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
  980. endmacro()
  981. # opencv precompiled headers macro (can add pch to modules and tests)
  982. # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
  983. # Usage:
  984. # ocv_add_precompiled_headers(${the_module})
  985. macro(_ocv_add_precompiled_headers the_target)
  986. ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
  987. if("${the_target}" MATCHES "^opencv_test_.*$")
  988. SET(pch_path "test/test_")
  989. elseif("${the_target}" MATCHES "^opencv_perf_.*$")
  990. SET(pch_path "perf/perf_")
  991. else()
  992. SET(pch_path "src/")
  993. endif()
  994. ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
  995. unset(pch_path)
  996. endmacro()
  997. # short command for adding simple OpenCV module
  998. # see ocv_add_module for argument details
  999. # Usage:
  1000. # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
  1001. macro(ocv_define_module module_name)
  1002. ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
  1003. set(_argn ${ARGN})
  1004. set(exclude_cuda "")
  1005. foreach(arg ${_argn})
  1006. if("${arg}" STREQUAL "EXCLUDE_CUDA")
  1007. set(exclude_cuda "${arg}")
  1008. list(REMOVE_ITEM _argn ${arg})
  1009. endif()
  1010. endforeach()
  1011. ocv_add_module(${module_name} ${_argn})
  1012. ocv_glob_module_sources(${exclude_cuda})
  1013. ocv_module_include_directories()
  1014. ocv_create_module()
  1015. ocv_add_accuracy_tests()
  1016. ocv_add_perf_tests()
  1017. ocv_add_samples()
  1018. endmacro()
  1019. # ensures that all passed modules are available
  1020. # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
  1021. macro(ocv_check_dependencies)
  1022. set(OCV_DEPENDENCIES_FOUND TRUE)
  1023. foreach(d ${ARGN})
  1024. if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
  1025. set(OCV_DEPENDENCIES_FOUND FALSE)
  1026. break()
  1027. endif()
  1028. endforeach()
  1029. endmacro()
  1030. ################################################################################
  1031. # OpenCV tests
  1032. ################################################################################
  1033. if(DEFINED OPENCV_BUILD_TEST_MODULES_LIST)
  1034. string(REPLACE "," ";" OPENCV_BUILD_TEST_MODULES_LIST "${OPENCV_BUILD_TEST_MODULES_LIST}") # support comma-separated list (,) too
  1035. endif()
  1036. if(DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST)
  1037. string(REPLACE "," ";" OPENCV_BUILD_PERF_TEST_MODULES_LIST "${OPENCV_BUILD_PERF_TEST_MODULES_LIST}") # support comma-separated list (,) too
  1038. endif()
  1039. # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
  1040. macro(__ocv_parse_test_sources tests_type)
  1041. set(OPENCV_${tests_type}_${the_module}_SOURCES "")
  1042. set(OPENCV_${tests_type}_${the_module}_DEPS "")
  1043. set(__file_group_name "")
  1044. set(__file_group_sources "")
  1045. foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
  1046. if(arg STREQUAL "FILES")
  1047. set(__currentvar "__file_group_sources")
  1048. if(__file_group_name AND __file_group_sources)
  1049. source_group("${__file_group_name}" FILES ${__file_group_sources})
  1050. list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
  1051. endif()
  1052. set(__file_group_name "")
  1053. set(__file_group_sources "")
  1054. elseif(arg STREQUAL "DEPENDS_ON")
  1055. set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
  1056. elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
  1057. set(__file_group_name "${arg}")
  1058. else()
  1059. list(APPEND ${__currentvar} "${arg}")
  1060. endif()
  1061. endforeach()
  1062. unset(__file_group_name)
  1063. unset(__file_group_sources)
  1064. unset(__currentvar)
  1065. endmacro()
  1066. ocv_check_environment_variables(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
  1067. # this is a command for adding OpenCV performance tests to the module
  1068. # ocv_add_perf_tests(<extra_dependencies>)
  1069. function(ocv_add_perf_tests)
  1070. ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
  1071. if(WINRT)
  1072. set(OPENCV_DEBUG_POSTFIX "")
  1073. endif()
  1074. set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
  1075. if(BUILD_PERF_TESTS AND EXISTS "${perf_path}"
  1076. AND (NOT DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST
  1077. OR OPENCV_BUILD_PERF_TEST_MODULES_LIST STREQUAL "all"
  1078. OR ";${OPENCV_BUILD_PERF_TEST_MODULES_LIST};" MATCHES ";${name};"
  1079. )
  1080. )
  1081. __ocv_parse_test_sources(PERF ${ARGN})
  1082. # opencv_imgcodecs is required for imread/imwrite
  1083. set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
  1084. ocv_check_dependencies(${perf_deps})
  1085. if(OCV_DEPENDENCIES_FOUND)
  1086. set(the_target "opencv_perf_${name}")
  1087. # project(${the_target})
  1088. if(NOT OPENCV_PERF_${the_module}_SOURCES)
  1089. file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
  1090. file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
  1091. ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
  1092. ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
  1093. set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
  1094. endif()
  1095. ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
  1096. if(NOT BUILD_opencv_world)
  1097. get_native_precompiled_header(${the_target} perf_precomp.hpp)
  1098. endif()
  1099. source_group("Src" FILES "${${the_target}_pch}")
  1100. ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
  1101. ocv_target_include_modules(${the_target} ${perf_deps})
  1102. ocv_target_link_libraries(${the_target} PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
  1103. add_dependencies(opencv_perf_tests ${the_target})
  1104. if(TARGET opencv_videoio_plugins)
  1105. add_dependencies(${the_target} opencv_videoio_plugins)
  1106. endif()
  1107. if(TARGET opencv_highgui_plugins)
  1108. add_dependencies(${the_target} opencv_highgui_plugins)
  1109. endif()
  1110. if(HAVE_HPX)
  1111. message("Linking HPX to Perf test of module ${name}")
  1112. ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
  1113. endif()
  1114. set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
  1115. set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
  1116. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
  1117. # Additional target properties
  1118. set_target_properties(${the_target} PROPERTIES
  1119. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  1120. RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
  1121. )
  1122. if(ENABLE_SOLUTION_FOLDERS)
  1123. set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
  1124. endif()
  1125. if(WINRT)
  1126. # removing APPCONTAINER from tests to run from console
  1127. # look for detailed description inside of ocv_create_module macro above
  1128. add_custom_command(TARGET "opencv_perf_${name}"
  1129. POST_BUILD
  1130. COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
  1131. endif()
  1132. if(NOT BUILD_opencv_world)
  1133. _ocv_add_precompiled_headers(${the_target})
  1134. endif()
  1135. ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
  1136. ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
  1137. "--perf_min_samples=1"
  1138. "--perf_force_samples=1"
  1139. "--perf_verify_sanity")
  1140. else(OCV_DEPENDENCIES_FOUND)
  1141. # TODO: warn about unsatisfied dependencies
  1142. endif(OCV_DEPENDENCIES_FOUND)
  1143. if(INSTALL_TESTS)
  1144. install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
  1145. endif()
  1146. endif()
  1147. endfunction()
  1148. # this is a command for adding OpenCV accuracy/regression tests to the module
  1149. # ocv_add_accuracy_tests(<list of extra dependencies>)
  1150. function(ocv_add_accuracy_tests)
  1151. ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
  1152. set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
  1153. if(BUILD_TESTS AND EXISTS "${test_path}"
  1154. AND (NOT DEFINED OPENCV_BUILD_TEST_MODULES_LIST
  1155. OR OPENCV_BUILD_TEST_MODULES_LIST STREQUAL "all"
  1156. OR ";${OPENCV_BUILD_TEST_MODULES_LIST};" MATCHES ";${name};"
  1157. )
  1158. )
  1159. __ocv_parse_test_sources(TEST ${ARGN})
  1160. # opencv_imgcodecs is required for imread/imwrite
  1161. set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
  1162. ocv_check_dependencies(${test_deps})
  1163. if(OCV_DEPENDENCIES_FOUND)
  1164. set(the_target "opencv_test_${name}")
  1165. # project(${the_target})
  1166. if(NOT OPENCV_TEST_${the_module}_SOURCES)
  1167. file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
  1168. file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
  1169. ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
  1170. ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
  1171. set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
  1172. endif()
  1173. if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
  1174. list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
  1175. endif()
  1176. ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
  1177. if(NOT BUILD_opencv_world)
  1178. get_native_precompiled_header(${the_target} test_precomp.hpp)
  1179. endif()
  1180. source_group("Src" FILES "${${the_target}_pch}")
  1181. ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
  1182. ocv_target_include_modules(${the_target} ${test_deps})
  1183. if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
  1184. ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
  1185. endif()
  1186. ocv_target_link_libraries(${the_target} PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
  1187. add_dependencies(opencv_tests ${the_target})
  1188. if(TARGET opencv_videoio_plugins)
  1189. add_dependencies(${the_target} opencv_videoio_plugins)
  1190. endif()
  1191. if(TARGET opencv_highgui_plugins)
  1192. add_dependencies(${the_target} opencv_highgui_plugins)
  1193. endif()
  1194. if(HAVE_HPX)
  1195. message("Linking HPX to Perf test of module ${name}")
  1196. ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
  1197. endif()
  1198. set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
  1199. set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
  1200. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
  1201. # Additional target properties
  1202. set_target_properties(${the_target} PROPERTIES
  1203. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  1204. RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
  1205. )
  1206. ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
  1207. if(ENABLE_SOLUTION_FOLDERS)
  1208. set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
  1209. endif()
  1210. if(OPENCV_TEST_BIGDATA)
  1211. ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
  1212. endif()
  1213. if(NOT BUILD_opencv_world)
  1214. _ocv_add_precompiled_headers(${the_target})
  1215. endif()
  1216. if(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
  1217. target_compile_options(${the_target} PRIVATE "$<$<CONFIG:Release>:${OPENCV_TEST_EXTRA_CXX_FLAGS_Release}>")
  1218. endif()
  1219. ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
  1220. else(OCV_DEPENDENCIES_FOUND)
  1221. # TODO: warn about unsatisfied dependencies
  1222. endif(OCV_DEPENDENCIES_FOUND)
  1223. if(INSTALL_TESTS)
  1224. install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
  1225. endif()
  1226. endif()
  1227. endfunction()
  1228. function(ocv_add_samples)
  1229. ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
  1230. set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
  1231. if(NOT EXISTS "${samples_path}")
  1232. return()
  1233. endif()
  1234. string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
  1235. if(BUILD_EXAMPLES)
  1236. set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
  1237. ocv_check_dependencies(${samples_deps})
  1238. if(OCV_DEPENDENCIES_FOUND)
  1239. file(GLOB sample_sources "${samples_path}/*.cpp")
  1240. foreach(source ${sample_sources})
  1241. get_filename_component(name "${source}" NAME_WE)
  1242. set(the_target "example_${module_id}_${name}")
  1243. ocv_add_executable(${the_target} "${source}")
  1244. ocv_target_include_modules(${the_target} ${samples_deps})
  1245. ocv_target_link_libraries(${the_target} PRIVATE ${samples_deps})
  1246. set_target_properties(${the_target} PROPERTIES
  1247. PROJECT_LABEL "(sample) ${name}"
  1248. LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
  1249. set_source_files_properties("${source}" PROPERTIES
  1250. LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
  1251. if(ENABLE_SOLUTION_FOLDERS)
  1252. set_target_properties(${the_target} PROPERTIES
  1253. FOLDER "samples/${module_id}")
  1254. endif()
  1255. # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
  1256. set(parent_target opencv_samples_${module_id})
  1257. if(NOT TARGET ${parent_target})
  1258. add_custom_target(${parent_target})
  1259. add_dependencies(opencv_samples ${parent_target})
  1260. endif()
  1261. add_dependencies(${parent_target} ${the_target})
  1262. if(TARGET opencv_videoio_plugins)
  1263. add_dependencies(${the_target} opencv_videoio_plugins)
  1264. endif()
  1265. if(TARGET opencv_highgui_plugins)
  1266. add_dependencies(${the_target} opencv_highgui_plugins)
  1267. endif()
  1268. if(INSTALL_BIN_EXAMPLES)
  1269. install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${module_id}" COMPONENT samples)
  1270. endif()
  1271. endforeach()
  1272. endif()
  1273. endif()
  1274. if(INSTALL_C_EXAMPLES)
  1275. file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
  1276. foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
  1277. IF( IS_DIRECTORY "${ITEM}" )
  1278. LIST( APPEND sample_dirs "${ITEM}" )
  1279. ELSE()
  1280. LIST( APPEND sample_files "${ITEM}" )
  1281. ENDIF()
  1282. endforeach()
  1283. install(FILES ${sample_files}
  1284. DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
  1285. COMPONENT samples)
  1286. install(DIRECTORY ${sample_dirs}
  1287. DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
  1288. COMPONENT samples)
  1289. endif()
  1290. endfunction()