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
6# Global variables:
7#
8# OPENCV_MODULE_${the_module}_LOCATION
9# OPENCV_MODULE_${the_module}_BINARY_DIR
10# OPENCV_MODULE_${the_module}_DESCRIPTION
11# OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
12# OPENCV_MODULE_${the_module}_HEADERS
13# OPENCV_MODULE_${the_module}_SOURCES
14# OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
15# OPENCV_MODULE_${the_module}_DEPS_TO_LINK - differs from above for world build only
16# OPENCV_MODULE_${the_module}_DEPS_EXT - non-module dependencies
17# OPENCV_MODULE_${the_module}_REQ_DEPS
18# OPENCV_MODULE_${the_module}_OPT_DEPS
19# OPENCV_MODULE_${the_module}_PRIVATE_REQ_DEPS
20# OPENCV_MODULE_${the_module}_PRIVATE_OPT_DEPS
21# OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD
22# OPENCV_MODULE_${the_module}_CUDA_OBJECTS - compiled CUDA objects list
23# OPENCV_MODULE_${the_module}_CHILDREN - list of submodules for compound modules (cmake >= 2.8.8)
24# OPENCV_MODULE_${the_module}_WRAPPERS - list of wrappers supporting this module
25# HAVE_${the_module} - for fast check of module availability
26
27# To control the setup of the module you could also set:
28# the_description - text to be used as current module description
29# OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
30# OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
31# BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
32# OPENCV_MODULE_CHILDREN - list of submodules
33
34# The verbose template for OpenCV module:
35#
36#   ocv_add_module(modname <dependencies>)
37#   ocv_glob_module_sources(([EXCLUDE_CUDA] <extra sources&headers>)
38#                          or glob them manually and ocv_set_module_sources(...)
39#   ocv_module_include_directories(<extra include directories>)
40#   ocv_create_module()
41#   <add extra link dependencies, compiler options, etc>
42#   ocv_add_precompiled_headers(${the_module})
43#   <add extra installation rules>
44#   ocv_add_accuracy_tests(<extra dependencies>)
45#   ocv_add_perf_tests(<extra dependencies>)
46#   ocv_add_samples(<extra dependencies>)
47#
48#
49# If module have no "extra" then you can define it in one line:
50#
51#   ocv_define_module(modname <dependencies>)
52
53# clean flags for modules enabled on previous cmake run
54# this is necessary to correctly handle modules removal
55foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
56  if(HAVE_${mod})
57    unset(HAVE_${mod} CACHE)
58  endif()
59  unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
60  unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
61  unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
62  unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
63  unset(OPENCV_MODULE_${mod}_LINK_DEPS CACHE)
64  unset(OPENCV_MODULE_${mod}_WRAPPERS CACHE)
65endforeach()
66
67# clean modules info which needs to be recalculated
68set(OPENCV_MODULES_PUBLIC         "" CACHE INTERNAL "List of OpenCV modules marked for export")
69set(OPENCV_MODULES_BUILD          "" CACHE INTERNAL "List of OpenCV modules included into the build")
70set(OPENCV_MODULES_DISABLED_USER  "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
71set(OPENCV_MODULES_DISABLED_AUTO  "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
72set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
73unset(OPENCV_WORLD_MODULES CACHE)
74
75# adds dependencies to OpenCV module
76# Usage:
77#   add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>] [WRAP <list of wrappers>])
78# Notes:
79# * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
80macro(ocv_add_dependencies full_modname)
81  ocv_debug_message("ocv_add_dependencies(" ${full_modname} ${ARGN} ")")
82  #we don't clean the dependencies here to allow this macro several times for every module
83  foreach(d "REQUIRED" ${ARGN})
84    if(d STREQUAL "REQUIRED")
85      set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
86    elseif(d STREQUAL "OPTIONAL")
87      set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
88    elseif(d STREQUAL "PRIVATE_REQUIRED")
89      set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
90    elseif(d STREQUAL "PRIVATE_OPTIONAL")
91      set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
92    elseif(d STREQUAL "WRAP")
93      set(__depsvar OPENCV_MODULE_${full_modname}_WRAPPERS)
94    else()
95      list(APPEND ${__depsvar} "${d}")
96    endif()
97  endforeach()
98  unset(__depsvar)
99
100  # hack for python
101  set(__python_idx)
102  list(FIND OPENCV_MODULE_${full_modname}_WRAPPERS "python" __python_idx)
103  if (NOT __python_idx EQUAL -1)
104    list(REMOVE_ITEM OPENCV_MODULE_${full_modname}_WRAPPERS "python")
105    list(APPEND OPENCV_MODULE_${full_modname}_WRAPPERS "python2" "python3")
106  endif()
107  unset(__python_idx)
108
109  ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
110  ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
111  ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
112  ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
113  ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
114
115  set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
116    CACHE INTERNAL "Required dependencies of ${full_modname} module")
117  set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
118    CACHE INTERNAL "Optional dependencies of ${full_modname} module")
119  set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
120    CACHE INTERNAL "Required private dependencies of ${full_modname} module")
121  set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
122    CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
123  set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
124    CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
125endmacro()
126
127# declare new OpenCV module in current folder
128# Usage:
129#   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
130# Example:
131#   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
132macro(ocv_add_module _name)
133  ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
134  string(TOLOWER "${_name}" name)
135  set(the_module opencv_${name})
136
137  # the first pass - collect modules info, the second pass - create targets
138  if(OPENCV_INITIAL_PASS)
139    #guard agains redefinition
140    if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
141      message(FATAL_ERROR "Redefinition of the ${the_module} module.
142  at:                    ${CMAKE_CURRENT_SOURCE_DIR}
143  previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
144")
145    endif()
146
147    if(NOT DEFINED the_description)
148      set(the_description "The ${name} OpenCV module")
149    endif()
150
151    if(NOT DEFINED BUILD_${the_module}_INIT)
152      set(BUILD_${the_module}_INIT ON)
153    endif()
154
155    # create option to enable/disable this module
156    option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
157
158    # remember the module details
159    set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
160    set(OPENCV_MODULE_${the_module}_LOCATION    "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
161
162    set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
163
164    # parse list of dependencies
165    if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
166      set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
167      set(__ocv_argn__ ${ARGN})
168      list(REMOVE_AT __ocv_argn__ 0)
169      ocv_add_dependencies(${the_module} ${__ocv_argn__})
170      unset(__ocv_argn__)
171    else()
172      set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
173      ocv_add_dependencies(${the_module} ${ARGN})
174      if(BUILD_${the_module})
175        set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
176      endif()
177    endif()
178
179    # add HAL as dependency
180    if(NOT "${the_module}" STREQUAL "opencv_hal")
181      ocv_add_dependencies(${the_module} opencv_hal)
182    endif()
183
184    # add self to the world dependencies
185    if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD
186        AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS"
187        AND NOT OPENCV_PROCESSING_EXTRA_MODULES)
188        OR OPENCV_MODULE_IS_PART_OF_WORLD
189        )
190      set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
191      ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
192    else()
193      set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
194    endif()
195
196    if(BUILD_${the_module})
197      set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
198    else()
199      set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
200    endif()
201
202    # add submodules if any
203    set(OPENCV_MODULE_${the_module}_CHILDREN "${OPENCV_MODULE_CHILDREN}" CACHE INTERNAL "List of ${the_module} submodules")
204
205    # add reverse wrapper dependencies
206    foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
207      ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
208    endforeach()
209
210    # stop processing of current file
211    return()
212  else()
213    set(OPENCV_MODULE_${the_module}_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
214    if(NOT BUILD_${the_module})
215      return() # extra protection from redefinition
216    endif()
217    if((NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD AND NOT ${the_module} STREQUAL opencv_world) OR NOT ${BUILD_opencv_world})
218      project(${the_module})
219    endif()
220  endif()
221endmacro()
222
223# excludes module from current configuration
224macro(ocv_module_disable module)
225  set(__modname ${module})
226  if(NOT __modname MATCHES "^opencv_")
227    set(__modname opencv_${module})
228  endif()
229  list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
230  set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
231  set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
232  set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
233  if(BUILD_${__modname})
234    # touch variable controlling build of the module to suppress "unused variable" CMake warning
235  endif()
236  unset(__modname)
237  return() # leave the current folder
238endmacro()
239
240
241# collect modules from specified directories
242# NB: must be called only once!
243macro(ocv_glob_modules)
244  if(DEFINED OPENCV_INITIAL_PASS)
245    message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
246  endif()
247  set(__directories_observed "")
248
249  # collect modules
250  set(OPENCV_INITIAL_PASS ON)
251  set(OPENCV_PROCESSING_EXTRA_MODULES 0)
252  foreach(__path ${ARGN})
253    if("${__path}" STREQUAL "EXTRA")
254      set(OPENCV_PROCESSING_EXTRA_MODULES 1)
255    endif()
256    get_filename_component(__path "${__path}" ABSOLUTE)
257
258    list(FIND __directories_observed "${__path}" __pathIdx)
259    if(__pathIdx GREATER -1)
260      message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
261    endif()
262    list(APPEND __directories_observed "${__path}")
263
264    file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
265    if(__ocvmodules)
266      list(SORT __ocvmodules)
267      foreach(mod ${__ocvmodules})
268        get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
269        if(EXISTS "${__modpath}/CMakeLists.txt")
270
271          list(FIND __directories_observed "${__modpath}" __pathIdx)
272          if(__pathIdx GREATER -1)
273            message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
274          endif()
275          list(APPEND __directories_observed "${__modpath}")
276
277          add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
278        endif()
279      endforeach()
280    endif()
281  endforeach()
282  ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
283
284  # resolve dependencies
285  __ocv_resolve_dependencies()
286
287  # create modules
288  set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
289  set(OPENCV_INITIAL_PASS OFF)
290  if(${BUILD_opencv_world})
291    add_subdirectory("${OPENCV_MODULE_opencv_world_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/world")
292    foreach(m ${OPENCV_MODULES_BUILD})
293      if(NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD AND NOT ${m} STREQUAL opencv_world)
294        message(STATUS "Processing module ${m}...")
295        if(m MATCHES "^opencv_")
296          string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
297          add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
298        else()
299          message(WARNING "Check module name: ${m}")
300          add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
301        endif()
302      endif()
303    endforeach()
304  else()
305    foreach(m ${OPENCV_MODULES_BUILD})
306      if(m MATCHES "^opencv_")
307        string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
308        add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
309      else()
310        message(WARNING "Check module name: ${m}")
311        add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
312      endif()
313    endforeach()
314  endif()
315  unset(__shortname)
316endmacro()
317
318
319# disables OpenCV module with missing dependencies
320function(__ocv_module_turn_off the_module)
321  list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
322  list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
323  list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
324  list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
325  set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
326
327  set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
328  set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
329  set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
330endfunction()
331
332# sort modules by dependencies
333function(__ocv_sort_modules_by_deps __lst)
334  ocv_list_sort(${__lst})
335  set(input ${${__lst}})
336  set(result "")
337  while(input)
338    list(LENGTH input length_before)
339    foreach (m ${input})
340      # check if module is in the result already
341      if (NOT ";${result};" MATCHES ";${m};")
342        # scan through module dependencies...
343        set(unresolved_deps_found FALSE)
344        foreach (d ${OPENCV_MODULE_${m}_CHILDREN} ${OPENCV_MODULE_${m}_DEPS})
345          # ... which are not already in the result and are enabled
346          if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
347            set(unresolved_deps_found TRUE)
348            break()
349          endif()
350        endforeach()
351        # chek if all dependencies for this module has been resolved
352        if (NOT unresolved_deps_found)
353          list(APPEND result ${m})
354          list(REMOVE_ITEM input ${m})
355        endif()
356      endif()
357    endforeach()
358    list(LENGTH input length_after)
359    # check for infinite loop or unresolved dependencies
360    if (NOT length_after LESS length_before)
361      message(WARNING "Unresolved dependencies or loop in dependency graph (${length_after})\n"
362        "Processed ${__lst}: ${${__lst}}\n"
363        "Good modules: ${result}\n"
364        "Bad modules: ${input}"
365      )
366      list(APPEND result ${input})
367      break()
368    endif()
369  endwhile()
370  set(${__lst} "${result}" PARENT_SCOPE)
371endfunction()
372
373# resolve dependensies
374function(__ocv_resolve_dependencies)
375  foreach(m ${OPENCV_MODULES_DISABLED_USER})
376    set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
377  endforeach()
378  foreach(m ${OPENCV_MODULES_BUILD})
379    set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
380  endforeach()
381
382  # disable MODULES with unresolved dependencies
383  set(has_changes ON)
384  while(has_changes)
385    set(has_changes OFF)
386    foreach(m ${OPENCV_MODULES_BUILD})
387      set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
388      while(__deps)
389        ocv_list_pop_front(__deps d)
390        string(TOLOWER "${d}" upper_d)
391        if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
392          if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
393            message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
394            __ocv_module_turn_off(${m})
395            set(has_changes ON)
396            break()
397          else()
398            message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
399          endif()
400        endif()
401      endwhile()
402    endforeach()
403  endwhile()
404
405#  message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
406
407  foreach(m ${OPENCV_MODULES_BUILD})
408    set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
409    foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
410      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
411        if(HAVE_${d} OR TARGET ${d})
412          list(APPEND deps_${m} ${d})
413        endif()
414      endif()
415    endforeach()
416#    message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
417  endforeach()
418
419  # propagate dependencies
420  set(has_changes ON)
421  while(has_changes)
422    set(has_changes OFF)
423    foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
424      foreach(m ${OPENCV_MODULES_BUILD})
425        if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
426          foreach(d ${deps_${m2}})
427            if(NOT (";${deps_${m}};" MATCHES ";${d};"))
428#              message(STATUS "  Transfer dependency ${d} from ${m2} to ${m}")
429              list(APPEND deps_${m} ${d})
430              set(has_changes ON)
431            endif()
432          endforeach()
433        endif()
434      endforeach()
435    endforeach()
436  endwhile()
437
438  # process private deps
439  foreach(m ${OPENCV_MODULES_BUILD})
440    foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
441      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
442        list(APPEND deps_${m} ${d})
443      endif()
444    endforeach()
445    foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
446      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
447        if(HAVE_${d} OR TARGET ${d})
448          list(APPEND deps_${m} ${d})
449        endif()
450      endif()
451    endforeach()
452  endforeach()
453
454  ocv_list_sort(OPENCV_MODULES_BUILD)
455
456  foreach(m ${OPENCV_MODULES_BUILD})
457#    message(STATUS "FULL deps of ${m}: ${deps_${m}}")
458    set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
459    set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
460    ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
461    if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
462      list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
463    endif()
464  endforeach()
465
466  # reorder dependencies
467  foreach(m ${OPENCV_MODULES_BUILD})
468    __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
469    ocv_list_sort(OPENCV_MODULE_${m}_DEPS_EXT)
470
471    set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
472
473    # process world
474    if(BUILD_opencv_world)
475      if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
476        list(APPEND OPENCV_WORLD_MODULES ${m})
477      endif()
478      foreach(m2 ${OPENCV_MODULES_BUILD})
479        if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
480          if(";${LINK_DEPS};" MATCHES ";${m2};")
481            list(REMOVE_ITEM LINK_DEPS ${m2})
482            if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
483              list(APPEND LINK_DEPS opencv_world)
484            endif()
485          endif()
486          if(${m} STREQUAL opencv_world)
487            list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
488          endif()
489        endif()
490      endforeach()
491    endif()
492
493    set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
494    set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
495    set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
496
497#    message(STATUS "  module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
498#    message(STATUS "  module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
499#    message(STATUS "  extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
500#    message(STATUS "")
501  endforeach()
502
503  __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
504
505  set(OPENCV_MODULES_PUBLIC        ${OPENCV_MODULES_PUBLIC}        CACHE INTERNAL "List of OpenCV modules marked for export")
506  set(OPENCV_MODULES_BUILD         ${OPENCV_MODULES_BUILD}         CACHE INTERNAL "List of OpenCV modules included into the build")
507  set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
508  set(OPENCV_WORLD_MODULES         ${OPENCV_WORLD_MODULES}         CACHE INTERNAL "List of OpenCV modules included into the world")
509endfunction()
510
511
512# setup include paths for the list of passed modules
513macro(ocv_include_modules)
514  foreach(d ${ARGN})
515    if(d MATCHES "^opencv_" AND HAVE_${d})
516      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
517        ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
518      endif()
519    elseif(EXISTS "${d}")
520      ocv_include_directories("${d}")
521    endif()
522  endforeach()
523endmacro()
524
525# same as previous but with dependencies
526macro(ocv_include_modules_recurse)
527  ocv_include_modules(${ARGN})
528  foreach(d ${ARGN})
529    if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
530      foreach (sub ${OPENCV_MODULE_${d}_DEPS})
531        ocv_include_modules(${sub})
532      endforeach()
533    endif()
534  endforeach()
535endmacro()
536
537# setup include paths for the list of passed modules
538macro(ocv_target_include_modules target)
539  foreach(d ${ARGN})
540    if(d MATCHES "^opencv_" AND HAVE_${d})
541      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
542        ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
543      endif()
544    elseif(EXISTS "${d}")
545      ocv_target_include_directories(${target} "${d}")
546    endif()
547  endforeach()
548endmacro()
549
550# setup include paths for the list of passed modules and recursively add dependent modules
551macro(ocv_target_include_modules_recurse target)
552  foreach(d ${ARGN})
553    if(d MATCHES "^opencv_" AND HAVE_${d})
554      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
555        ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
556      endif()
557      if(OPENCV_MODULE_${d}_DEPS)
558        ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
559      endif()
560    elseif(EXISTS "${d}")
561      ocv_target_include_directories(${target} "${d}")
562    endif()
563  endforeach()
564endmacro()
565
566# setup include path for OpenCV headers for specified module
567# ocv_module_include_directories(<extra include directories/extra include modules>)
568macro(ocv_module_include_directories)
569  ocv_target_include_directories(${the_module}
570      "${OPENCV_MODULE_${the_module}_LOCATION}/include"
571      "${OPENCV_MODULE_${the_module}_LOCATION}/src"
572      "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
573      )
574  ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
575endmacro()
576
577
578# sets header and source files for the current module
579# NB: all files specified as headers will be installed
580# Usage:
581# ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
582macro(ocv_set_module_sources)
583  ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
584
585  set(OPENCV_MODULE_${the_module}_HEADERS "")
586  set(OPENCV_MODULE_${the_module}_SOURCES "")
587
588  foreach(f "HEADERS" ${ARGN})
589    if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
590      set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
591    else()
592      list(APPEND ${__filesvar} "${f}")
593    endif()
594  endforeach()
595
596  # the hacky way to embeed any files into the OpenCV without modification of its build system
597  if(COMMAND ocv_get_module_external_sources)
598    ocv_get_module_external_sources()
599  endif()
600
601  # use full paths for module to be independent from the module location
602  ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
603
604  set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
605  set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
606endmacro()
607
608# finds and sets headers and sources for the standard OpenCV module
609# Usage:
610# ocv_glob_module_sources([EXCLUDE_CUDA] <extra sources&headers in the same format as used in ocv_set_module_sources>)
611macro(ocv_glob_module_sources)
612  ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
613  set(_argn ${ARGN})
614  list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
615  if(NOT exclude_cuda EQUAL -1)
616    list(REMOVE_AT _argn ${exclude_cuda})
617  endif()
618
619  file(GLOB_RECURSE lib_srcs
620       "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
621  )
622  file(GLOB_RECURSE lib_int_hdrs
623       "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
624       "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
625  )
626  file(GLOB lib_hdrs
627       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
628       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
629       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
630  )
631  file(GLOB lib_hdrs_detail
632       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
633       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
634  )
635  if (APPLE)
636    file(GLOB_RECURSE lib_srcs_apple
637         "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
638    )
639    list(APPEND lib_srcs ${lib_srcs_apple})
640  endif()
641
642  ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
643  ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
644
645  set(lib_cuda_srcs "")
646  set(lib_cuda_hdrs "")
647  if(HAVE_CUDA AND exclude_cuda EQUAL -1)
648    file(GLOB lib_cuda_srcs
649         "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
650    )
651    file(GLOB lib_cuda_hdrs
652         "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
653    )
654    source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
655  endif()
656
657  file(GLOB cl_kernels
658       "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
659  )
660  if(cl_kernels)
661    set(OCL_NAME opencl_kernels_${name})
662    ocv_include_directories(${OPENCL_INCLUDE_DIRS})
663    add_custom_command(
664      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
665      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"
666      DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
667    ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
668    ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
669    list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
670  endif()
671
672  ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
673                         SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
674endmacro()
675
676# creates OpenCV module in current folder
677# creates new target, configures standard dependencies, compilers flags, install rules
678# Usage:
679#   ocv_create_module(<extra link dependencies>)
680#   ocv_create_module()
681macro(ocv_create_module)
682  ocv_debug_message("ocv_create_module(" ${ARGN} ")")
683  set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
684  if(${BUILD_opencv_world} AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
685    # nothing
686    set(the_module_target opencv_world)
687  else()
688    _ocv_create_module(${ARGN})
689    set(the_module_target ${the_module})
690  endif()
691endmacro()
692
693macro(_ocv_create_module)
694  # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
695  # be called at some point in the future. We can't look into the future, though,
696  # so this will have to do.
697  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
698    get_native_precompiled_header(${the_module} precomp.hpp)
699  endif()
700
701  set(sub_objs "")
702  set(sub_links "")
703  set(cuda_objs "")
704  if (OPENCV_MODULE_${the_module}_CHILDREN)
705    status("Complex module ${the_module}")
706    foreach (m ${OPENCV_MODULE_${the_module}_CHILDREN})
707      if (BUILD_${m} AND TARGET ${m}_object)
708        get_target_property(_sub_links ${m} LINK_LIBRARIES)
709        list(APPEND sub_objs $<TARGET_OBJECTS:${m}_object>)
710        list(APPEND sub_links ${_sub_links})
711        status("    + ${m}")
712      else()
713        status("    - ${m}")
714      endif()
715      list(APPEND cuda_objs ${OPENCV_MODULE_${m}_CUDA_OBJECTS})
716    endforeach()
717  endif()
718
719  ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
720    "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
721    ${${the_module}_pch} ${sub_objs})
722
723  if (cuda_objs)
724    target_link_libraries(${the_module} ${cuda_objs})
725  endif()
726
727  # TODO: is it needed?
728  if (sub_links)
729    ocv_list_filterout(sub_links "^opencv_")
730    ocv_list_unique(sub_links)
731    target_link_libraries(${the_module} ${sub_links})
732  endif()
733
734  unset(sub_objs)
735  unset(sub_links)
736  unset(cuda_objs)
737
738  ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
739  ocv_target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
740  ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
741  if (HAVE_CUDA)
742    ocv_target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
743  endif()
744
745  add_dependencies(opencv_modules ${the_module})
746
747  if(ENABLE_SOLUTION_FOLDERS)
748    set_target_properties(${the_module} PROPERTIES FOLDER "modules")
749  endif()
750
751  set_target_properties(${the_module} PROPERTIES
752    OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
753    DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
754    ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
755    LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
756    RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
757    INSTALL_NAME_DIR lib
758  )
759
760  # For dynamic link numbering convenions
761  if(NOT ANDROID)
762    # Android SDK build scripts can include only .so files into final .apk
763    # As result we should not set version properties for Android
764    set_target_properties(${the_module} PROPERTIES
765      VERSION ${OPENCV_LIBVERSION}
766      SOVERSION ${OPENCV_SOVERSION}
767    )
768  endif()
769
770  if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
771      OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
772    set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS CVAPI_EXPORTS)
773    set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
774  endif()
775
776  if(MSVC)
777    if(CMAKE_CROSSCOMPILING)
778      set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
779    endif()
780    set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
781  endif()
782
783  ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
784    RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
785    LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
786    ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
787    )
788  get_target_property(_target_type ${the_module} TYPE)
789  if("${_target_type}" STREQUAL "SHARED_LIBRARY")
790    install(TARGETS ${the_module}
791      LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
792  endif()
793
794  foreach(m ${OPENCV_MODULE_${the_module}_CHILDREN} ${the_module})
795    # only "public" headers need to be installed
796    if(OPENCV_MODULE_${m}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${m};")
797      foreach(hdr ${OPENCV_MODULE_${m}_HEADERS})
798        string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
799        if(NOT hdr2 MATCHES "opencv2/${m}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
800          install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
801        endif()
802      endforeach()
803    endif()
804  endforeach()
805
806  _ocv_add_precompiled_headers(${the_module})
807
808  if (TARGET ${the_module}_object)
809    # copy COMPILE_DEFINITIONS
810    get_target_property(main_defs ${the_module} COMPILE_DEFINITIONS)
811    if (main_defs)
812      set_target_properties(${the_module}_object PROPERTIES COMPILE_DEFINITIONS ${main_defs})
813    endif()
814    # use same PCH
815    if (TARGET pch_Generate_${the_module})
816      add_dependencies(${the_module}_object pch_Generate_${the_module} )
817    endif()
818  endif()
819endmacro()
820
821# opencv precompiled headers macro (can add pch to modules and tests)
822# this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
823# Usage:
824# ocv_add_precompiled_headers(${the_module})
825macro(_ocv_add_precompiled_headers the_target)
826  ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
827
828  if("${the_target}" MATCHES "^opencv_test_.*$")
829    SET(pch_path "test/test_")
830  elseif("${the_target}" MATCHES "^opencv_perf_.*$")
831    SET(pch_path "perf/perf_")
832  else()
833    SET(pch_path "src/")
834  endif()
835  ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
836  unset(pch_path)
837endmacro()
838
839# short command for adding simple OpenCV module
840# see ocv_add_module for argument details
841# Usage:
842# ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
843macro(ocv_define_module module_name)
844  ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
845  set(_argn ${ARGN})
846  set(exclude_cuda "")
847  foreach(arg ${_argn})
848    if("${arg}" STREQUAL "EXCLUDE_CUDA")
849      set(exclude_cuda "${arg}")
850      list(REMOVE_ITEM _argn ${arg})
851    endif()
852  endforeach()
853
854  ocv_add_module(${module_name} ${_argn})
855  ocv_glob_module_sources(${exclude_cuda})
856  ocv_module_include_directories()
857  ocv_create_module()
858
859  ocv_add_accuracy_tests()
860  ocv_add_perf_tests()
861  ocv_add_samples()
862endmacro()
863
864# ensures that all passed modules are available
865# sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
866macro(ocv_check_dependencies)
867  set(OCV_DEPENDENCIES_FOUND TRUE)
868  foreach(d ${ARGN})
869    if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
870      set(OCV_DEPENDENCIES_FOUND FALSE)
871      break()
872    endif()
873  endforeach()
874endmacro()
875
876# auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
877macro(__ocv_parse_test_sources tests_type)
878  set(OPENCV_${tests_type}_${the_module}_SOURCES "")
879  set(OPENCV_${tests_type}_${the_module}_DEPS "")
880  set(__file_group_name "")
881  set(__file_group_sources "")
882  foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
883    if(arg STREQUAL "FILES")
884      set(__currentvar "__file_group_sources")
885      if(__file_group_name AND __file_group_sources)
886        source_group("${__file_group_name}" FILES ${__file_group_sources})
887        list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
888      endif()
889      set(__file_group_name "")
890      set(__file_group_sources "")
891    elseif(arg STREQUAL "DEPENDS_ON")
892      set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
893    elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
894      set(__file_group_name "${arg}")
895    else()
896      list(APPEND ${__currentvar} "${arg}")
897    endif()
898  endforeach()
899  unset(__file_group_name)
900  unset(__file_group_sources)
901  unset(__currentvar)
902endmacro()
903
904# this is a command for adding OpenCV performance tests to the module
905# ocv_add_perf_tests(<extra_dependencies>)
906function(ocv_add_perf_tests)
907  ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
908
909  set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
910  if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
911    __ocv_parse_test_sources(PERF ${ARGN})
912
913    # opencv_imgcodecs is required for imread/imwrite
914    set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
915    ocv_check_dependencies(${perf_deps})
916
917    if(OCV_DEPENDENCIES_FOUND)
918      set(the_target "opencv_perf_${name}")
919      # project(${the_target})
920
921      if(NOT OPENCV_PERF_${the_module}_SOURCES)
922        file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
923        file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
924        ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
925        ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
926        set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
927      endif()
928
929      if(NOT BUILD_opencv_world)
930        get_native_precompiled_header(${the_target} perf_precomp.hpp)
931      endif()
932
933      ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
934      ocv_target_include_modules(${the_target} ${perf_deps} "${perf_path}")
935      ocv_target_link_libraries(${the_target} ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS})
936      add_dependencies(opencv_perf_tests ${the_target})
937
938      # Additional target properties
939      set_target_properties(${the_target} PROPERTIES
940        DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
941        RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
942      )
943
944      if(ENABLE_SOLUTION_FOLDERS)
945        set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
946      endif()
947
948      if(NOT BUILD_opencv_world)
949        _ocv_add_precompiled_headers(${the_target})
950      endif()
951    else(OCV_DEPENDENCIES_FOUND)
952      # TODO: warn about unsatisfied dependencies
953    endif(OCV_DEPENDENCIES_FOUND)
954    if(INSTALL_TESTS)
955      install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
956    endif()
957  endif()
958endfunction()
959
960# this is a command for adding OpenCV accuracy/regression tests to the module
961# ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
962function(ocv_add_accuracy_tests)
963  ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
964
965  set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
966  if(BUILD_TESTS AND EXISTS "${test_path}")
967    __ocv_parse_test_sources(TEST ${ARGN})
968
969    # opencv_imgcodecs is required for imread/imwrite
970    set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
971    ocv_check_dependencies(${test_deps})
972    if(OCV_DEPENDENCIES_FOUND)
973      set(the_target "opencv_test_${name}")
974      # project(${the_target})
975
976      if(NOT OPENCV_TEST_${the_module}_SOURCES)
977        file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
978        file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
979        ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
980        ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
981        set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
982      endif()
983
984      if(NOT BUILD_opencv_world)
985        get_native_precompiled_header(${the_target} test_precomp.hpp)
986      endif()
987
988      ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
989      ocv_target_include_modules(${the_target} ${test_deps} "${test_path}")
990      ocv_target_link_libraries(${the_target} ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS})
991      add_dependencies(opencv_tests ${the_target})
992
993      # Additional target properties
994      set_target_properties(${the_target} PROPERTIES
995        DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
996        RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
997      )
998
999      if(ENABLE_SOLUTION_FOLDERS)
1000        set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
1001      endif()
1002
1003      enable_testing()
1004      get_target_property(LOC ${the_target} LOCATION)
1005      add_test(${the_target} "${LOC}")
1006
1007      if(NOT BUILD_opencv_world)
1008        _ocv_add_precompiled_headers(${the_target})
1009      endif()
1010    else(OCV_DEPENDENCIES_FOUND)
1011      # TODO: warn about unsatisfied dependencies
1012    endif(OCV_DEPENDENCIES_FOUND)
1013
1014    if(INSTALL_TESTS)
1015      install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1016    endif()
1017  endif()
1018endfunction()
1019
1020function(ocv_add_samples)
1021  ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
1022
1023  set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1024  string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
1025
1026  if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
1027    set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1028    ocv_check_dependencies(${samples_deps})
1029
1030    if(OCV_DEPENDENCIES_FOUND)
1031      file(GLOB sample_sources "${samples_path}/*.cpp")
1032
1033      foreach(source ${sample_sources})
1034        get_filename_component(name "${source}" NAME_WE)
1035        set(the_target "example_${module_id}_${name}")
1036
1037        ocv_add_executable(${the_target} "${source}")
1038        ocv_target_include_modules(${the_target} ${samples_deps})
1039        ocv_target_link_libraries(${the_target} ${samples_deps})
1040        set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
1041
1042        if(ENABLE_SOLUTION_FOLDERS)
1043          set_target_properties(${the_target} PROPERTIES
1044            OUTPUT_NAME "${module_id}-example-${name}"
1045            FOLDER "samples/${module_id}")
1046        endif()
1047
1048        if(WIN32)
1049          install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
1050        endif()
1051      endforeach()
1052    endif()
1053  endif()
1054
1055  if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
1056  file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1057    foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
1058        IF( IS_DIRECTORY "${ITEM}" )
1059            LIST( APPEND sample_dirs "${ITEM}" )
1060        ELSE()
1061            LIST( APPEND sample_files "${ITEM}" )
1062        ENDIF()
1063    endforeach()
1064    install(FILES ${sample_files}
1065            DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
1066            PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
1067    install(DIRECTORY ${sample_dirs}
1068            DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
1069            USE_SOURCE_PERMISSIONS COMPONENT samples)
1070  endif()
1071endfunction()
1072