Merge pull request #3063 from akarsakov:ocl_warps_check
[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   if (exclude_cuda EQUAL -1)
589     file(GLOB lib_cuda_srcs
590          "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
591     )
592     set(cuda_objs "")
593     set(lib_cuda_hdrs "")
594     if(HAVE_CUDA)
595       ocv_include_directories(${CUDA_INCLUDE_DIRS})
596       file(GLOB lib_cuda_hdrs
597            "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
598       )
599
600       ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
601       source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
602     endif()
603   else()
604     set(cuda_objs "")
605     set(lib_cuda_srcs "")
606     set(lib_cuda_hdrs "")
607   endif()
608
609   file(GLOB cl_kernels
610        "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
611   )
612   if(cl_kernels)
613     set(OCL_NAME opencl_kernels_${name})
614     ocv_include_directories(${OPENCL_INCLUDE_DIRS})
615     add_custom_command(
616       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
617       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"
618       DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
619     ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
620     ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
621     list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
622   endif()
623
624   ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
625                          SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
626 endmacro()
627
628 # creates OpenCV module in current folder
629 # creates new target, configures standard dependencies, compilers flags, install rules
630 # Usage:
631 #   ocv_create_module(<extra link dependencies>)
632 #   ocv_create_module()
633 macro(ocv_create_module)
634   ocv_debug_message("ocv_create_module(" ${ARGN} ")")
635   set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
636   if(${BUILD_opencv_world} AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
637     # nothing
638     set(the_module_target opencv_world)
639   else()
640     _ocv_create_module(${ARGN})
641     set(the_module_target ${the_module})
642   endif()
643 endmacro()
644
645 macro(_ocv_create_module)
646   # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
647   # be called at some point in the future. We can't look into the future, though,
648   # so this will have to do.
649   if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
650     get_native_precompiled_header(${the_module} precomp.hpp)
651   endif()
652
653   ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
654     "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
655     ${${the_module}_pch})
656   if(NOT the_module STREQUAL opencv_ts)
657     set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
658   endif()
659
660   ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
661   ocv_target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
662   ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
663   if (HAVE_CUDA)
664     ocv_target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
665   endif()
666
667   add_dependencies(opencv_modules ${the_module})
668
669   if(ENABLE_SOLUTION_FOLDERS)
670     set_target_properties(${the_module} PROPERTIES FOLDER "modules")
671   endif()
672
673   set_target_properties(${the_module} PROPERTIES
674     OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
675     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
676     ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
677     LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
678     RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
679     INSTALL_NAME_DIR lib
680   )
681
682   # For dynamic link numbering convenions
683   if(NOT ANDROID)
684     # Android SDK build scripts can include only .so files into final .apk
685     # As result we should not set version properties for Android
686     set_target_properties(${the_module} PROPERTIES
687       VERSION ${OPENCV_LIBVERSION}
688       SOVERSION ${OPENCV_SOVERSION}
689     )
690   endif()
691
692   if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
693       OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
694     set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
695   endif()
696
697   if(MSVC)
698     if(CMAKE_CROSSCOMPILING)
699       set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
700     endif()
701     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
702   endif()
703
704   ocv_install_target(${the_module} EXPORT OpenCVModules
705     RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
706     LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
707     ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
708     )
709
710   # only "public" headers need to be installed
711   if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
712     foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
713       string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
714       if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
715         install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
716       endif()
717     endforeach()
718   endif()
719   _ocv_add_precompiled_headers(${the_module})
720 endmacro()
721
722 # opencv precompiled headers macro (can add pch to modules and tests)
723 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
724 # Usage:
725 # ocv_add_precompiled_headers(${the_module})
726 macro(_ocv_add_precompiled_headers the_target)
727   ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
728
729   if("${the_target}" MATCHES "^opencv_test_.*$")
730     SET(pch_path "test/test_")
731   elseif("${the_target}" MATCHES "^opencv_perf_.*$")
732     SET(pch_path "perf/perf_")
733   else()
734     SET(pch_path "src/")
735   endif()
736   ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
737   unset(pch_path)
738 endmacro()
739
740 # short command for adding simple OpenCV module
741 # see ocv_add_module for argument details
742 # Usage:
743 # ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
744 macro(ocv_define_module module_name)
745   ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
746   set(_argn ${ARGN})
747   set(exclude_cuda "")
748   foreach(arg ${_argn})
749     if("${arg}" STREQUAL "EXCLUDE_CUDA")
750       set(exclude_cuda "${arg}")
751       list(REMOVE_ITEM _argn ${arg})
752     endif()
753   endforeach()
754
755   ocv_add_module(${module_name} ${_argn})
756   ocv_glob_module_sources(${exclude_cuda})
757   ocv_module_include_directories()
758   ocv_create_module()
759
760   ocv_add_accuracy_tests()
761   ocv_add_perf_tests()
762   ocv_add_samples()
763 endmacro()
764
765 # ensures that all passed modules are available
766 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
767 macro(ocv_check_dependencies)
768   set(OCV_DEPENDENCIES_FOUND TRUE)
769   foreach(d ${ARGN})
770     if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
771       set(OCV_DEPENDENCIES_FOUND FALSE)
772       break()
773     endif()
774   endforeach()
775 endmacro()
776
777 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
778 macro(__ocv_parse_test_sources tests_type)
779   set(OPENCV_${tests_type}_${the_module}_SOURCES "")
780   set(OPENCV_${tests_type}_${the_module}_DEPS "")
781   set(__file_group_name "")
782   set(__file_group_sources "")
783   foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
784     if(arg STREQUAL "FILES")
785       set(__currentvar "__file_group_sources")
786       if(__file_group_name AND __file_group_sources)
787         source_group("${__file_group_name}" FILES ${__file_group_sources})
788         list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
789       endif()
790       set(__file_group_name "")
791       set(__file_group_sources "")
792     elseif(arg STREQUAL "DEPENDS_ON")
793       set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
794     elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
795       set(__file_group_name "${arg}")
796     else()
797       list(APPEND ${__currentvar} "${arg}")
798     endif()
799   endforeach()
800   unset(__file_group_name)
801   unset(__file_group_sources)
802   unset(__currentvar)
803 endmacro()
804
805 # this is a command for adding OpenCV performance tests to the module
806 # ocv_add_perf_tests(<extra_dependencies>)
807 function(ocv_add_perf_tests)
808   ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
809
810   set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
811   if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
812     __ocv_parse_test_sources(PERF ${ARGN})
813
814     # opencv_imgcodecs is required for imread/imwrite
815     set(perf_deps ${the_module} opencv_ts opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
816     ocv_check_dependencies(${perf_deps})
817
818     if(OCV_DEPENDENCIES_FOUND)
819       set(the_target "opencv_perf_${name}")
820       # project(${the_target})
821
822       if(NOT OPENCV_PERF_${the_module}_SOURCES)
823         file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
824         file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
825         ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
826         ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
827         set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
828       endif()
829
830       if(NOT BUILD_opencv_world)
831         get_native_precompiled_header(${the_target} perf_precomp.hpp)
832       endif()
833
834       ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
835       ocv_target_include_modules(${the_target} ${perf_deps} "${perf_path}")
836       ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
837       add_dependencies(opencv_perf_tests ${the_target})
838
839       # Additional target properties
840       set_target_properties(${the_target} PROPERTIES
841         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
842         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
843       )
844
845       if(ENABLE_SOLUTION_FOLDERS)
846         set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
847       endif()
848
849       if(NOT BUILD_opencv_world)
850         _ocv_add_precompiled_headers(${the_target})
851       endif()
852     else(OCV_DEPENDENCIES_FOUND)
853       # TODO: warn about unsatisfied dependencies
854     endif(OCV_DEPENDENCIES_FOUND)
855     if(INSTALL_TESTS)
856       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
857     endif()
858   endif()
859 endfunction()
860
861 # this is a command for adding OpenCV accuracy/regression tests to the module
862 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
863 function(ocv_add_accuracy_tests)
864   ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
865
866   set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
867   if(BUILD_TESTS AND EXISTS "${test_path}")
868     __ocv_parse_test_sources(TEST ${ARGN})
869
870     # opencv_imgcodecs is required for imread/imwrite
871     set(test_deps ${the_module} opencv_ts opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
872     ocv_check_dependencies(${test_deps})
873     if(OCV_DEPENDENCIES_FOUND)
874       set(the_target "opencv_test_${name}")
875       # project(${the_target})
876
877       if(NOT OPENCV_TEST_${the_module}_SOURCES)
878         file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
879         file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
880         ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
881         ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
882         set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
883       endif()
884
885       if(NOT BUILD_opencv_world)
886         get_native_precompiled_header(${the_target} test_precomp.hpp)
887       endif()
888
889       ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
890       ocv_target_include_modules(${the_target} ${test_deps} "${test_path}")
891       ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
892       add_dependencies(opencv_tests ${the_target})
893
894       # Additional target properties
895       set_target_properties(${the_target} PROPERTIES
896         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
897         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
898       )
899
900       if(ENABLE_SOLUTION_FOLDERS)
901         set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
902       endif()
903
904       enable_testing()
905       get_target_property(LOC ${the_target} LOCATION)
906       add_test(${the_target} "${LOC}")
907
908       if(NOT BUILD_opencv_world)
909         _ocv_add_precompiled_headers(${the_target})
910       endif()
911     else(OCV_DEPENDENCIES_FOUND)
912       # TODO: warn about unsatisfied dependencies
913     endif(OCV_DEPENDENCIES_FOUND)
914
915     if(INSTALL_TESTS)
916       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
917     endif()
918   endif()
919 endfunction()
920
921 function(ocv_add_samples)
922   ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
923
924   set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
925   string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
926
927   if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
928     set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
929     ocv_check_dependencies(${samples_deps})
930
931     if(OCV_DEPENDENCIES_FOUND)
932       file(GLOB sample_sources "${samples_path}/*.cpp")
933
934       foreach(source ${sample_sources})
935         get_filename_component(name "${source}" NAME_WE)
936         set(the_target "example_${module_id}_${name}")
937
938         ocv_add_executable(${the_target} "${source}")
939         ocv_target_include_modules(${the_target} ${samples_deps})
940         ocv_target_link_libraries(${the_target} ${samples_deps})
941         set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
942
943         if(ENABLE_SOLUTION_FOLDERS)
944           set_target_properties(${the_target} PROPERTIES
945             OUTPUT_NAME "${module_id}-example-${name}"
946             FOLDER "samples/${module_id}")
947         endif()
948
949         if(WIN32)
950           install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
951         endif()
952       endforeach()
953     endif()
954   endif()
955
956   if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
957     file(GLOB sample_files "${samples_path}/*")
958     install(FILES ${sample_files}
959             DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
960             PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
961   endif()
962 endfunction()