OpenCVModule.cmake 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458
  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. if("${the_module}" STREQUAL "opencv_ts")
  835. # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
  836. # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
  837. add_custom_command(TARGET ${the_module}
  838. POST_BUILD
  839. COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
  840. COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
  841. COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
  842. endif()
  843. endif()
  844. endmacro()
  845. macro(_ocv_create_module)
  846. add_definitions(-D__OPENCV_BUILD=1)
  847. ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
  848. set(__module_headers ${OPENCV_MODULE_${the_module}_HEADERS})
  849. if(__module_headers)
  850. list(SORT __module_headers) # fix headers order, useful for bindings
  851. endif()
  852. set(OPENCV_MODULE_${the_module}_HEADERS ${__module_headers} CACHE INTERNAL "List of header files for ${the_module}")
  853. set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
  854. # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
  855. # be called at some point in the future. We can't look into the future, though,
  856. # so this will have to do.
  857. if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
  858. get_native_precompiled_header(${the_module} precomp.hpp)
  859. endif()
  860. if(WIN32
  861. AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
  862. AND NOT OPENCV_VS_VERSIONINFO_SKIP)
  863. if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
  864. set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
  865. elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
  866. set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
  867. elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
  868. set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
  869. ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
  870. NAME "${the_module}"
  871. FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
  872. INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
  873. ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
  874. )
  875. endif()
  876. if(_VS_VERSION_FILE)
  877. if(NOT EXISTS "${_VS_VERSION_FILE}")
  878. message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
  879. endif()
  880. source_group("Src" FILES "${_VS_VERSION_FILE}")
  881. endif()
  882. endif()
  883. if(WIN32 AND NOT (
  884. "${the_module}" STREQUAL "opencv_core" OR
  885. "${the_module}" STREQUAL "opencv_world" OR
  886. "${the_module}" STREQUAL "opencv_cudev"
  887. )
  888. AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
  889. AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
  890. )
  891. set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
  892. configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
  893. endif()
  894. source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
  895. source_group("Src" FILES "${${the_module}_pch}")
  896. ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
  897. ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
  898. ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
  899. "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
  900. ${${the_module}_pch}
  901. ${_VS_VERSION_FILE}
  902. ${_DLLMAIN_FILE}
  903. )
  904. set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  905. set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
  906. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  907. ocv_target_link_libraries(${the_module} PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
  908. INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
  909. )
  910. ocv_target_link_libraries(${the_module} PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_EXT}
  911. INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_EXT}
  912. )
  913. ocv_target_link_libraries(${the_module} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
  914. if (HAVE_CUDA)
  915. ocv_target_link_libraries(${the_module} PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
  916. endif()
  917. if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
  918. target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
  919. unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
  920. endif()
  921. add_dependencies(opencv_modules ${the_module})
  922. if(ENABLE_SOLUTION_FOLDERS)
  923. set_target_properties(${the_module} PROPERTIES FOLDER "modules")
  924. endif()
  925. set_target_properties(${the_module} PROPERTIES
  926. OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
  927. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  928. COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
  929. COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
  930. ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  931. COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  932. LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  933. RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  934. DEFINE_SYMBOL CVAPI_EXPORTS
  935. )
  936. if(BUILD_FAT_JAVA_LIB) # force exports from static modules too
  937. if(BUILD_SHARED_LIBS)
  938. message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
  939. endif()
  940. target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
  941. endif()
  942. # For dynamic link numbering conventions
  943. if(NOT ANDROID)
  944. # Android SDK build scripts can include only .so files into final .apk
  945. # As result we should not set version properties for Android
  946. set_target_properties(${the_module} PROPERTIES
  947. VERSION ${OPENCV_LIBVERSION}
  948. SOVERSION ${OPENCV_SOVERSION}
  949. )
  950. endif()
  951. if (ENABLE_GNU_STL_DEBUG)
  952. target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
  953. endif()
  954. if(MSVC)
  955. if(CMAKE_CROSSCOMPILING)
  956. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
  957. endif()
  958. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
  959. endif()
  960. get_target_property(_target_type ${the_module} TYPE)
  961. if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
  962. ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
  963. ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
  964. RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
  965. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
  966. ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
  967. )
  968. endif()
  969. if("${_target_type}" STREQUAL "SHARED_LIBRARY")
  970. install(TARGETS ${the_module}
  971. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
  972. endif()
  973. # only "public" headers need to be installed
  974. ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
  975. ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
  976. if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
  977. foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
  978. string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
  979. if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
  980. install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
  981. else()
  982. #message("Header file will be NOT installed: ${hdr}")
  983. endif()
  984. endforeach()
  985. endif()
  986. _ocv_add_precompiled_headers(${the_module})
  987. ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
  988. ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
  989. endmacro()
  990. # opencv precompiled headers macro (can add pch to modules and tests)
  991. # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
  992. # Usage:
  993. # ocv_add_precompiled_headers(${the_module})
  994. macro(_ocv_add_precompiled_headers the_target)
  995. ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
  996. if("${the_target}" MATCHES "^opencv_test_.*$")
  997. SET(pch_path "test/test_")
  998. elseif("${the_target}" MATCHES "^opencv_perf_.*$")
  999. SET(pch_path "perf/perf_")
  1000. else()
  1001. SET(pch_path "src/")
  1002. endif()
  1003. ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
  1004. unset(pch_path)
  1005. endmacro()
  1006. # short command for adding simple OpenCV module
  1007. # see ocv_add_module for argument details
  1008. # Usage:
  1009. # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
  1010. macro(ocv_define_module module_name)
  1011. ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
  1012. set(_argn ${ARGN})
  1013. set(exclude_cuda "")
  1014. foreach(arg ${_argn})
  1015. if("${arg}" STREQUAL "EXCLUDE_CUDA")
  1016. set(exclude_cuda "${arg}")
  1017. list(REMOVE_ITEM _argn ${arg})
  1018. endif()
  1019. endforeach()
  1020. ocv_add_module(${module_name} ${_argn})
  1021. ocv_glob_module_sources(${exclude_cuda})
  1022. ocv_module_include_directories()
  1023. ocv_create_module()
  1024. ocv_add_accuracy_tests()
  1025. ocv_add_perf_tests()
  1026. ocv_add_samples()
  1027. endmacro()
  1028. # ensures that all passed modules are available
  1029. # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
  1030. macro(ocv_check_dependencies)
  1031. set(OCV_DEPENDENCIES_FOUND TRUE)
  1032. foreach(d ${ARGN})
  1033. if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
  1034. set(OCV_DEPENDENCIES_FOUND FALSE)
  1035. break()
  1036. endif()
  1037. endforeach()
  1038. endmacro()
  1039. ################################################################################
  1040. # OpenCV tests
  1041. ################################################################################
  1042. if(DEFINED OPENCV_BUILD_TEST_MODULES_LIST)
  1043. string(REPLACE "," ";" OPENCV_BUILD_TEST_MODULES_LIST "${OPENCV_BUILD_TEST_MODULES_LIST}") # support comma-separated list (,) too
  1044. endif()
  1045. if(DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST)
  1046. string(REPLACE "," ";" OPENCV_BUILD_PERF_TEST_MODULES_LIST "${OPENCV_BUILD_PERF_TEST_MODULES_LIST}") # support comma-separated list (,) too
  1047. endif()
  1048. # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
  1049. macro(__ocv_parse_test_sources tests_type)
  1050. set(OPENCV_${tests_type}_${the_module}_SOURCES "")
  1051. set(OPENCV_${tests_type}_${the_module}_DEPS "")
  1052. set(__file_group_name "")
  1053. set(__file_group_sources "")
  1054. foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
  1055. if(arg STREQUAL "FILES")
  1056. set(__currentvar "__file_group_sources")
  1057. if(__file_group_name AND __file_group_sources)
  1058. source_group("${__file_group_name}" FILES ${__file_group_sources})
  1059. list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
  1060. endif()
  1061. set(__file_group_name "")
  1062. set(__file_group_sources "")
  1063. elseif(arg STREQUAL "DEPENDS_ON")
  1064. set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
  1065. elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
  1066. set(__file_group_name "${arg}")
  1067. else()
  1068. list(APPEND ${__currentvar} "${arg}")
  1069. endif()
  1070. endforeach()
  1071. unset(__file_group_name)
  1072. unset(__file_group_sources)
  1073. unset(__currentvar)
  1074. endmacro()
  1075. ocv_check_environment_variables(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
  1076. # this is a command for adding OpenCV performance tests to the module
  1077. # ocv_add_perf_tests(<extra_dependencies>)
  1078. function(ocv_add_perf_tests)
  1079. ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
  1080. if(WINRT)
  1081. set(OPENCV_DEBUG_POSTFIX "")
  1082. endif()
  1083. set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
  1084. if(BUILD_PERF_TESTS AND EXISTS "${perf_path}"
  1085. AND (NOT DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST
  1086. OR OPENCV_BUILD_PERF_TEST_MODULES_LIST STREQUAL "all"
  1087. OR ";${OPENCV_BUILD_PERF_TEST_MODULES_LIST};" MATCHES ";${name};"
  1088. )
  1089. )
  1090. __ocv_parse_test_sources(PERF ${ARGN})
  1091. # opencv_imgcodecs is required for imread/imwrite
  1092. set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
  1093. ocv_check_dependencies(${perf_deps})
  1094. if(OCV_DEPENDENCIES_FOUND)
  1095. set(the_target "opencv_perf_${name}")
  1096. # project(${the_target})
  1097. if(NOT OPENCV_PERF_${the_module}_SOURCES)
  1098. file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
  1099. file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
  1100. ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
  1101. ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
  1102. set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
  1103. endif()
  1104. ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
  1105. if(NOT BUILD_opencv_world)
  1106. get_native_precompiled_header(${the_target} perf_precomp.hpp)
  1107. endif()
  1108. source_group("Src" FILES "${${the_target}_pch}")
  1109. ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
  1110. ocv_target_include_modules(${the_target} ${perf_deps})
  1111. ocv_target_link_libraries(${the_target} PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
  1112. add_dependencies(opencv_perf_tests ${the_target})
  1113. if(TARGET opencv_videoio_plugins)
  1114. add_dependencies(${the_target} opencv_videoio_plugins)
  1115. endif()
  1116. if(TARGET opencv_highgui_plugins)
  1117. add_dependencies(${the_target} opencv_highgui_plugins)
  1118. endif()
  1119. if(HAVE_HPX)
  1120. message("Linking HPX to Perf test of module ${name}")
  1121. ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
  1122. endif()
  1123. set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
  1124. set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
  1125. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
  1126. # Additional target properties
  1127. set_target_properties(${the_target} PROPERTIES
  1128. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  1129. RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
  1130. )
  1131. if(ENABLE_SOLUTION_FOLDERS)
  1132. set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
  1133. endif()
  1134. if(WINRT)
  1135. # removing APPCONTAINER from tests to run from console
  1136. # look for detailed description inside of ocv_create_module macro above
  1137. add_custom_command(TARGET "opencv_perf_${name}"
  1138. POST_BUILD
  1139. COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
  1140. endif()
  1141. if(NOT BUILD_opencv_world)
  1142. _ocv_add_precompiled_headers(${the_target})
  1143. endif()
  1144. ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
  1145. ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
  1146. "--perf_min_samples=1"
  1147. "--perf_force_samples=1"
  1148. "--perf_verify_sanity")
  1149. else(OCV_DEPENDENCIES_FOUND)
  1150. # TODO: warn about unsatisfied dependencies
  1151. endif(OCV_DEPENDENCIES_FOUND)
  1152. if(INSTALL_TESTS)
  1153. install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
  1154. endif()
  1155. endif()
  1156. endfunction()
  1157. # this is a command for adding OpenCV accuracy/regression tests to the module
  1158. # ocv_add_accuracy_tests(<list of extra dependencies>)
  1159. function(ocv_add_accuracy_tests)
  1160. ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
  1161. set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
  1162. if(BUILD_TESTS AND EXISTS "${test_path}"
  1163. AND (NOT DEFINED OPENCV_BUILD_TEST_MODULES_LIST
  1164. OR OPENCV_BUILD_TEST_MODULES_LIST STREQUAL "all"
  1165. OR ";${OPENCV_BUILD_TEST_MODULES_LIST};" MATCHES ";${name};"
  1166. )
  1167. )
  1168. __ocv_parse_test_sources(TEST ${ARGN})
  1169. # opencv_imgcodecs is required for imread/imwrite
  1170. set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
  1171. ocv_check_dependencies(${test_deps})
  1172. if(OCV_DEPENDENCIES_FOUND)
  1173. set(the_target "opencv_test_${name}")
  1174. # project(${the_target})
  1175. if(NOT OPENCV_TEST_${the_module}_SOURCES)
  1176. file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
  1177. file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
  1178. ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
  1179. ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
  1180. set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
  1181. endif()
  1182. if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
  1183. list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
  1184. endif()
  1185. ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
  1186. if(NOT BUILD_opencv_world)
  1187. get_native_precompiled_header(${the_target} test_precomp.hpp)
  1188. endif()
  1189. source_group("Src" FILES "${${the_target}_pch}")
  1190. ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
  1191. ocv_target_include_modules(${the_target} ${test_deps})
  1192. if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
  1193. ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
  1194. endif()
  1195. ocv_target_link_libraries(${the_target} PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
  1196. add_dependencies(opencv_tests ${the_target})
  1197. if(TARGET opencv_videoio_plugins)
  1198. add_dependencies(${the_target} opencv_videoio_plugins)
  1199. endif()
  1200. if(TARGET opencv_highgui_plugins)
  1201. add_dependencies(${the_target} opencv_highgui_plugins)
  1202. endif()
  1203. if(HAVE_HPX)
  1204. message("Linking HPX to Perf test of module ${name}")
  1205. ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
  1206. endif()
  1207. set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
  1208. set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
  1209. PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
  1210. # Additional target properties
  1211. set_target_properties(${the_target} PROPERTIES
  1212. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  1213. RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
  1214. )
  1215. ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
  1216. if(ENABLE_SOLUTION_FOLDERS)
  1217. set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
  1218. endif()
  1219. if(OPENCV_TEST_BIGDATA)
  1220. ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
  1221. endif()
  1222. if(NOT BUILD_opencv_world)
  1223. _ocv_add_precompiled_headers(${the_target})
  1224. endif()
  1225. if(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
  1226. target_compile_options(${the_target} PRIVATE "$<$<CONFIG:Release>:${OPENCV_TEST_EXTRA_CXX_FLAGS_Release}>")
  1227. endif()
  1228. ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
  1229. else(OCV_DEPENDENCIES_FOUND)
  1230. # TODO: warn about unsatisfied dependencies
  1231. endif(OCV_DEPENDENCIES_FOUND)
  1232. if(INSTALL_TESTS)
  1233. install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
  1234. endif()
  1235. endif()
  1236. endfunction()
  1237. function(ocv_add_samples)
  1238. ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
  1239. set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
  1240. if(NOT EXISTS "${samples_path}")
  1241. return()
  1242. endif()
  1243. string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
  1244. if(BUILD_EXAMPLES)
  1245. set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
  1246. ocv_check_dependencies(${samples_deps})
  1247. if(OCV_DEPENDENCIES_FOUND)
  1248. file(GLOB sample_sources "${samples_path}/*.cpp")
  1249. foreach(source ${sample_sources})
  1250. get_filename_component(name "${source}" NAME_WE)
  1251. set(the_target "example_${module_id}_${name}")
  1252. ocv_add_executable(${the_target} "${source}")
  1253. ocv_target_include_modules(${the_target} ${samples_deps})
  1254. ocv_target_link_libraries(${the_target} PRIVATE ${samples_deps})
  1255. set_target_properties(${the_target} PROPERTIES
  1256. PROJECT_LABEL "(sample) ${name}"
  1257. LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
  1258. set_source_files_properties("${source}" PROPERTIES
  1259. LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
  1260. if(ENABLE_SOLUTION_FOLDERS)
  1261. set_target_properties(${the_target} PROPERTIES
  1262. FOLDER "samples/${module_id}")
  1263. endif()
  1264. # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
  1265. set(parent_target opencv_samples_${module_id})
  1266. if(NOT TARGET ${parent_target})
  1267. add_custom_target(${parent_target})
  1268. add_dependencies(opencv_samples ${parent_target})
  1269. endif()
  1270. add_dependencies(${parent_target} ${the_target})
  1271. if(TARGET opencv_videoio_plugins)
  1272. add_dependencies(${the_target} opencv_videoio_plugins)
  1273. endif()
  1274. if(TARGET opencv_highgui_plugins)
  1275. add_dependencies(${the_target} opencv_highgui_plugins)
  1276. endif()
  1277. if(INSTALL_BIN_EXAMPLES)
  1278. install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${module_id}" COMPONENT samples)
  1279. endif()
  1280. endforeach()
  1281. endif()
  1282. endif()
  1283. if(INSTALL_C_EXAMPLES)
  1284. file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
  1285. foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
  1286. IF( IS_DIRECTORY "${ITEM}" )
  1287. LIST( APPEND sample_dirs "${ITEM}" )
  1288. ELSE()
  1289. LIST( APPEND sample_files "${ITEM}" )
  1290. ENDIF()
  1291. endforeach()
  1292. install(FILES ${sample_files}
  1293. DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
  1294. COMPONENT samples)
  1295. install(DIRECTORY ${sample_dirs}
  1296. DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
  1297. COMPONENT samples)
  1298. endif()
  1299. endfunction()