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