Merge pull request #16100 from sajarindider:brief
[platform/upstream/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 # OPENCV_MODULE_${the_module}_CUDA_OBJECTS - compiled CUDA objects list
23 # OPENCV_MODULE_${the_module}_WRAPPERS - list of wrappers supporting this module
24 # HAVE_${the_module} - for fast check of module availability
25
26 # To control the setup of the module you could also set:
27 # the_description - text to be used as current module description
28 # the_label - label for current module
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
33 # The verbose template for OpenCV module:
34 #
35 #   ocv_add_module(modname <dependencies>)
36 #   ocv_glob_module_sources(([EXCLUDE_CUDA] <extra sources&headers>)
37 #                          or glob them manually and ocv_set_module_sources(...)
38 #   ocv_module_include_directories(<extra include directories>)
39 #   ocv_create_module()
40 #   <add extra link dependencies, compiler options, etc>
41 #   ocv_add_precompiled_headers(${the_module})
42 #   <add extra installation rules>
43 #   ocv_add_accuracy_tests(<extra dependencies>)
44 #   ocv_add_perf_tests(<extra dependencies>)
45 #   ocv_add_samples(<extra dependencies>)
46 #
47 #
48 # If module have no "extra" then you can define it in one line:
49 #
50 #   ocv_define_module(modname <dependencies>)
51
52 # clean flags for modules enabled on previous cmake run
53 # this is necessary to correctly handle modules removal
54 foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
55   if(HAVE_${mod})
56     unset(HAVE_${mod} CACHE)
57   endif()
58   unset(OPENCV_MODULE_${mod}_DEPS CACHE)
59   unset(OPENCV_MODULE_${mod}_DEPS_EXT CACHE)
60   unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
61   unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
62   unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
63   unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
64   unset(OPENCV_MODULE_${mod}_LINK_DEPS CACHE)
65   unset(OPENCV_MODULE_${mod}_WRAPPERS CACHE)
66 endforeach()
67
68 # clean modules info which needs to be recalculated
69 set(OPENCV_MODULES_PUBLIC         "" CACHE INTERNAL "List of OpenCV modules marked for export")
70 set(OPENCV_MODULES_BUILD          "" CACHE INTERNAL "List of OpenCV modules included into the build")
71 set(OPENCV_MODULES_DISABLED_USER  "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
72 set(OPENCV_MODULES_DISABLED_AUTO  "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
73 set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
74 unset(OPENCV_WORLD_MODULES CACHE)
75
76 # adds dependencies to OpenCV module
77 # Usage:
78 #   add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>] [WRAP <list of wrappers>])
79 # Notes:
80 # * <list of dependencies> - can include full names of modules or full paths to shared/static libraries or cmake targets
81 macro(ocv_add_dependencies full_modname)
82   ocv_debug_message("ocv_add_dependencies(" ${full_modname} ${ARGN} ")")
83   #we don't clean the dependencies here to allow this macro several times for every module
84   foreach(d "REQUIRED" ${ARGN})
85     if(d STREQUAL "REQUIRED")
86       set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
87     elseif(d STREQUAL "OPTIONAL")
88       set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
89     elseif(d STREQUAL "PRIVATE_REQUIRED")
90       set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
91     elseif(d STREQUAL "PRIVATE_OPTIONAL")
92       set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
93     elseif(d STREQUAL "WRAP")
94       set(__depsvar OPENCV_MODULE_${full_modname}_WRAPPERS)
95     else()
96       list(APPEND ${__depsvar} "${d}")
97     endif()
98   endforeach()
99   unset(__depsvar)
100
101   # hack for python
102   set(__python_idx)
103   list(FIND OPENCV_MODULE_${full_modname}_WRAPPERS "python" __python_idx)
104   if (NOT __python_idx EQUAL -1)
105     list(REMOVE_ITEM OPENCV_MODULE_${full_modname}_WRAPPERS "python")
106     list(APPEND OPENCV_MODULE_${full_modname}_WRAPPERS "python_bindings_generator" "python2" "python3")
107   endif()
108   unset(__python_idx)
109
110   ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
111   ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
112   ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
113   ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
114   ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
115
116   set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
117     CACHE INTERNAL "Required dependencies of ${full_modname} module")
118   set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
119     CACHE INTERNAL "Optional dependencies of ${full_modname} module")
120   set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
121     CACHE INTERNAL "Required private dependencies of ${full_modname} module")
122   set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
123     CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
124   set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
125     CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
126 endmacro()
127
128 # declare new OpenCV module in current folder
129 # Usage:
130 #   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
131 # Example:
132 #   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
133 macro(ocv_add_module _name)
134   ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
135   string(TOLOWER "${_name}" name)
136   set(the_module opencv_${name})
137
138   # the first pass - collect modules info, the second pass - create targets
139   if(OPENCV_INITIAL_PASS)
140     #guard against redefinition
141     if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
142       message(FATAL_ERROR "Redefinition of the ${the_module} module.
143   at:                    ${CMAKE_CURRENT_SOURCE_DIR}
144   previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
145 ")
146     endif()
147
148     if(NOT DEFINED the_description)
149       set(the_description "The ${name} OpenCV module")
150     endif()
151
152     if(NOT DEFINED BUILD_${the_module}_INIT)
153       set(BUILD_${the_module}_INIT ON)
154     endif()
155
156     # create option to enable/disable this module
157     option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
158
159     # remember the module details
160     set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
161     set(OPENCV_MODULE_${the_module}_LOCATION    "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
162
163     set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
164
165     set(ADD_MODULE_ARGN ${ARGN})
166     ocv_cmake_hook(PRE_ADD_MODULE)
167     ocv_cmake_hook(PRE_ADD_MODULE_${the_module})
168
169     # parse list of dependencies
170     if(" ${ARGV1}" STREQUAL " INTERNAL" OR " ${ARGV1}" STREQUAL " BINDINGS")
171       set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
172       set(__ocv_argn__ ${ADD_MODULE_ARGN})
173       list(REMOVE_AT __ocv_argn__ 0)
174       ocv_add_dependencies(${the_module} ${__ocv_argn__})
175       unset(__ocv_argn__)
176     else()
177       set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
178       ocv_add_dependencies(${the_module} ${ADD_MODULE_ARGN})
179       if(BUILD_${the_module})
180         set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
181       endif()
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 OR NOT OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
188         AND (NOT BUILD_SHARED_LIBS OR NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC"))
189         OR OPENCV_MODULE_IS_PART_OF_WORLD
190         )
191       set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
192       ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
193     else()
194       set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
195     endif()
196
197     if(NOT DEFINED the_label)
198       if(OPENCV_PROCESSING_EXTRA_MODULES)
199         set(the_label "Extra")
200       else()
201         set(the_label "Main")
202       endif()
203     endif()
204     set(OPENCV_MODULE_${the_module}_LABEL "${the_label};${the_module}" CACHE INTERNAL "")
205
206     if(BUILD_${the_module})
207       set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
208     else()
209       set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
210     endif()
211
212     # add reverse wrapper dependencies
213     foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
214       ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
215     endforeach()
216
217     # stop processing of current file
218     ocv_cmake_hook(POST_ADD_MODULE)
219     ocv_cmake_hook(POST_ADD_MODULE_${the_module})
220     return()
221   else()
222     set(OPENCV_MODULE_${the_module}_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
223     if(NOT BUILD_${the_module})
224       return() # extra protection from redefinition
225     endif()
226     if(NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR NOT ${BUILD_opencv_world})
227       if (NOT ${the_module} STREQUAL opencv_world)
228         project(${the_module})
229       endif()
230       add_definitions(
231         -D_USE_MATH_DEFINES  # M_PI constant in MSVS
232         -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_FORMAT_MACROS  # to use C libraries from C++ code (ffmpeg)
233       )
234     endif()
235   endif()
236 endmacro()
237
238 # excludes module from current configuration
239 macro(ocv_module_disable_ module)
240   set(__modname ${module})
241   if(NOT __modname MATCHES "^opencv_")
242     set(__modname opencv_${module})
243   endif()
244   list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
245   set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
246   set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
247   set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
248   if(BUILD_${__modname})
249     # touch variable controlling build of the module to suppress "unused variable" CMake warning
250   endif()
251   unset(__modname)
252 endmacro()
253
254 macro(ocv_module_disable module)
255   ocv_module_disable_(${module})
256   return() # leave the current folder
257 endmacro()
258
259 # gather acceptable locations and generate names for them
260 # if folder contains CMakeLists.txt - it is accepted,
261 # otherwise all first-level subfolders containing CMakeLists.txt are accepted.
262 # Usage: _glob_locations(<output paths list> <output names list> <folder> [<folder> ...])
263 function(_glob_locations out_paths out_names)
264   set(PATHS ${ARGN})
265   foreach(path ${PATHS})
266     #message(STATUS "Inspect: ${path}")
267     list(LENGTH paths before)
268     get_filename_component(path "${path}" ABSOLUTE)
269     # Either module itself
270     if(NOT path STREQUAL CMAKE_CURRENT_SOURCE_DIR AND EXISTS "${path}/CMakeLists.txt")
271       get_filename_component(name "${path}" NAME)
272       list(APPEND paths "${path}")
273       list(APPEND names "${name}")
274     else()
275       # Either flat collection of modules
276       file(GLOB subdirs RELATIVE "${path}" "${path}/*")
277       foreach(subdir ${subdirs})
278         #message(STATUS "Inspect: ${path}/${subdir}")
279         if(EXISTS "${path}/${subdir}/CMakeLists.txt")
280           list(APPEND paths "${path}/${subdir}")
281           list(APPEND names "${subdir}")
282         endif()
283       endforeach()
284     endif()
285     list(LENGTH paths after)
286     if(before EQUAL after)
287       message(SEND_ERROR "No modules has been found: ${path}")
288     endif()
289   endforeach()
290   # Return
291   set(${out_paths} ${paths} PARENT_SCOPE)
292   set(${out_names} ${names} PARENT_SCOPE)
293 endfunction()
294
295 # Calls 'add_subdirectory' for each location.
296 # Note: both input lists should have same length.
297 # Usage: _add_modules_1(<list with paths> <list with names>)
298 macro(_add_modules_1 paths names)
299   ocv_debug_message("_add_modules_1(paths=${paths}, names=${names}, ... " ${ARGN} ")")
300   list(LENGTH ${paths} __len)
301   if(NOT __len EQUAL 0)
302     list(LENGTH ${names} __len_verify)
303     if(NOT __len EQUAL __len_verify)
304       message(FATAL_ERROR "Bad configuration! ${__len} != ${__len_verify}")
305     endif()
306     math(EXPR __len "${__len} - 1")
307     foreach(i RANGE ${__len})
308       list(GET ${paths} ${i} __path)
309       list(GET ${names} ${i} __name)
310       #message(STATUS "First pass: ${__name} => ${__path}")
311       include("${__path}/cmake/init.cmake" OPTIONAL)
312       add_subdirectory("${__path}" "${CMAKE_CURRENT_BINARY_DIR}/.firstpass/${__name}")
313     endforeach()
314   endif()
315 endmacro()
316
317 # Calls 'add_subdirectory' for each module name.
318 # Usage: _add_modules_2([<module> ...])
319 macro(_add_modules_2)
320   ocv_debug_message("_add_modules_2(" ${ARGN} ")")
321   foreach(m ${ARGN})
322     set(the_module "${m}")
323     ocv_cmake_hook(PRE_MODULES_CREATE_${the_module})
324     if(BUILD_opencv_world AND m STREQUAL "opencv_world"
325         OR NOT BUILD_opencv_world
326         OR NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
327       if(NOT m MATCHES "^opencv_")
328         message(WARNING "Incorrect module name: ${m}")
329       endif()
330       string(REGEX REPLACE "^opencv_" "" name "${m}")
331       #message(STATUS "Second pass: ${name} => ${OPENCV_MODULE_${m}_LOCATION}")
332       add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${name}")
333     endif()
334     ocv_cmake_hook(POST_MODULES_CREATE_${the_module})
335   endforeach()
336   unset(the_module)
337 endmacro()
338
339 # Check if list of input items is unique.
340 # Usage: _assert_uniqueness(<failure message> <element> [<element> ...])
341 function(_assert_uniqueness msg)
342   ocv_get_duplicates(dups ${ARGN})
343   if(dups)
344     foreach(e ${ARGN})
345       list(FIND dups "${e}" idx)
346       if(NOT idx EQUAL -1)
347         set(prefix " > ")
348       else()
349         set(prefix "   ")
350       endif()
351       message("${prefix}${e}")
352     endforeach()
353     message(FATAL_ERROR "${msg}")
354   endif()
355 endfunction()
356
357 # collect modules from specified directories
358 # NB: must be called only once!
359 # Usage: ocv_glob_modules(<main location> [<extra location> ...])
360 macro(ocv_glob_modules main_root)
361   ocv_cmake_hook(INIT_MODULES_GLOB)
362   if(DEFINED OPENCV_INITIAL_PASS)
363     message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
364   endif()
365
366   # collect modules
367   set(OPENCV_INITIAL_PASS ON)
368   _glob_locations(__main_paths __main_names ${main_root})
369   _glob_locations(__extra_paths __extra_names ${ARGN})
370   _assert_uniqueness("Duplicated modules LOCATIONS has been found" ${__main_paths} ${__extra_paths})
371   _assert_uniqueness("Duplicated modules NAMES has been found" ${__main_names} ${__extra_names})
372   set(OPENCV_PROCESSING_EXTRA_MODULES 0)
373   ocv_cmake_hook(PRE_MODULES_SCAN)
374   _add_modules_1(__main_paths __main_names)
375   set(OPENCV_PROCESSING_EXTRA_MODULES 1)
376   ocv_cmake_hook(PRE_MODULES_SCAN_EXTRA)
377   _add_modules_1(__extra_paths __extra_names)
378   ocv_clear_vars(__main_names __extra_names __main_paths __extra_paths)
379   ocv_cmake_hook(POST_MODULES_SCAN)
380
381   # resolve dependencies
382   __ocv_resolve_dependencies()
383
384   # create modules
385   set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
386   set(OPENCV_INITIAL_PASS OFF)
387   ocv_cmake_hook(PRE_MODULES_CREATE)
388   _add_modules_2(${OPENCV_MODULES_BUILD})
389   ocv_cmake_hook(POST_MODULES_CREATE)
390 endmacro()
391
392
393 # disables OpenCV module with missing dependencies
394 function(__ocv_module_turn_off the_module)
395   list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
396   list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
397   list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
398   list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
399   set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
400
401   set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
402   set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
403   set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
404 endfunction()
405
406 # sort modules by dependencies
407 function(__ocv_sort_modules_by_deps __lst)
408   ocv_list_sort(${__lst})
409   set(input ${${__lst}})
410   set(result "")
411   set(result_extra "")
412   while(input)
413     list(LENGTH input length_before)
414     foreach (m ${input})
415       # check if module is in the result already
416       if (NOT ";${result};" MATCHES ";${m};")
417         # scan through module dependencies...
418         set(unresolved_deps_found FALSE)
419         foreach (d ${OPENCV_MODULE_${m}_DEPS})
420           # ... which are not already in the result and are enabled
421           if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
422             set(unresolved_deps_found TRUE)
423             break()
424           endif()
425         endforeach()
426         # check if all dependencies for this module has been resolved
427         if (NOT unresolved_deps_found)
428           list(APPEND result ${m})
429           list(REMOVE_ITEM input ${m})
430         endif()
431       endif()
432     endforeach()
433     list(LENGTH input length_after)
434     # check for infinite loop or unresolved dependencies
435     if (NOT length_after LESS length_before)
436       if(NOT BUILD_SHARED_LIBS)
437         if (";${input};" MATCHES ";opencv_world;")
438           list(REMOVE_ITEM input "opencv_world")
439           list(APPEND result_extra "opencv_world")
440         else()
441           # We can't do here something
442           list(APPEND result ${input})
443           break()
444         endif()
445       else()
446         message(FATAL_ERROR "FATAL: Unresolved dependencies or loop in dependency graph (${length_after})\n"
447           "Processed ${__lst}: ${${__lst}}\n"
448           "Good modules: ${result}\n"
449           "Bad modules: ${input}"
450         )
451         list(APPEND result ${input})
452         break()
453       endif()
454     endif()
455   endwhile()
456   set(${__lst} "${result};${result_extra}" PARENT_SCOPE)
457 endfunction()
458
459 # resolve dependencies
460 function(__ocv_resolve_dependencies)
461   foreach(m ${OPENCV_MODULES_DISABLED_USER})
462     set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
463   endforeach()
464   foreach(m ${OPENCV_MODULES_BUILD})
465     set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
466   endforeach()
467
468   # Whitelist feature
469   if(BUILD_LIST)
470     # Prepare the list
471     string(REGEX REPLACE "[ ,:]+" ";" whitelist "${BUILD_LIST}" )
472     if(BUILD_opencv_world)
473       list(APPEND whitelist world)
474     endif()
475     ocv_list_add_prefix(whitelist "opencv_")
476     ocv_list_sort(whitelist)
477     ocv_list_unique(whitelist)
478     message(STATUS "Using whitelist: ${whitelist}")
479     # Expand the list
480     foreach(depth RANGE 10)
481       set(new_whitelist ${whitelist})
482       foreach(m ${whitelist})
483         list(APPEND new_whitelist ${OPENCV_MODULE_${m}_REQ_DEPS})
484         list(APPEND new_whitelist ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
485       endforeach()
486       ocv_list_sort(new_whitelist)
487       ocv_list_unique(new_whitelist)
488       if("${whitelist}" STREQUAL "${new_whitelist}")
489         break()
490       endif()
491       set(whitelist "${new_whitelist}")
492     endforeach()
493     # Disable modules not in whitelist
494     foreach(m ${OPENCV_MODULES_BUILD})
495       list(FIND whitelist ${m} idx)
496       if(idx EQUAL -1)
497         message(STATUS "Module ${m} disabled by whitelist")
498         __ocv_module_turn_off(${m})
499       endif()
500     endforeach()
501   endif()
502
503   # disable MODULES with unresolved dependencies
504   set(has_changes ON)
505   while(has_changes)
506     set(has_changes OFF)
507     foreach(m ${OPENCV_MODULES_BUILD})
508       set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
509       while(__deps)
510         ocv_list_pop_front(__deps d)
511         string(TOLOWER "${d}" upper_d)
512         if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
513           if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
514             message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
515             __ocv_module_turn_off(${m})
516             set(has_changes ON)
517             break()
518           else()
519             message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
520           endif()
521         endif()
522       endwhile()
523     endforeach()
524   endwhile()
525
526 #  message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
527
528   foreach(m ${OPENCV_MODULES_BUILD})
529     set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
530     foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
531       if(NOT (";${deps_${m}};" MATCHES ";${d};"))
532         if(HAVE_${d} OR TARGET ${d})
533           list(APPEND deps_${m} ${d})
534         endif()
535       endif()
536     endforeach()
537 #    message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
538   endforeach()
539
540   # propagate dependencies
541   set(has_changes ON)
542   while(has_changes)
543     set(has_changes OFF)
544     foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
545       foreach(m ${OPENCV_MODULES_BUILD})
546         if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
547           foreach(d ${deps_${m2}})
548             if(NOT (";${deps_${m}};" MATCHES ";${d};"))
549 #              message(STATUS "  Transfer dependency ${d} from ${m2} to ${m}")
550               list(APPEND deps_${m} ${d})
551               set(has_changes ON)
552             endif()
553             if(BUILD_opencv_world
554                 AND NOT "${m}" STREQUAL "opencv_world"
555                 AND NOT "${m2}" STREQUAL "opencv_world"
556                 AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD
557                 AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
558               if(NOT (";${deps_${m}};" MATCHES ";opencv_world;"))
559 #                message(STATUS "  Transfer dependency opencv_world alias ${m2} to ${m}")
560                 list(APPEND deps_${m} opencv_world)
561                 set(has_changes ON)
562               endif()
563             endif()
564           endforeach()
565         endif()
566       endforeach()
567     endforeach()
568   endwhile()
569
570   # process private deps
571   foreach(m ${OPENCV_MODULES_BUILD})
572     foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
573       if(NOT (";${deps_${m}};" MATCHES ";${d};"))
574         list(APPEND deps_${m} ${d})
575       endif()
576     endforeach()
577     foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
578       if(NOT (";${deps_${m}};" MATCHES ";${d};"))
579         if(HAVE_${d} OR TARGET ${d})
580           list(APPEND deps_${m} ${d})
581         endif()
582       endif()
583     endforeach()
584   endforeach()
585
586   ocv_list_sort(OPENCV_MODULES_BUILD)
587
588   foreach(m ${OPENCV_MODULES_BUILD})
589 #    message(STATUS "FULL deps of ${m}: ${deps_${m}}")
590     set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
591     set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
592     ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
593     if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
594       list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
595     endif()
596   endforeach()
597
598   # reorder dependencies
599   foreach(m ${OPENCV_MODULES_BUILD})
600     __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
601
602     set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
603
604     # process world
605     if(BUILD_opencv_world)
606       if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
607         list(APPEND OPENCV_WORLD_MODULES ${m})
608       endif()
609       foreach(m2 ${OPENCV_MODULES_BUILD})
610         if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
611           if(";${LINK_DEPS};" MATCHES ";${m2};")
612             list(REMOVE_ITEM LINK_DEPS ${m2})
613             if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
614               list(APPEND LINK_DEPS opencv_world)
615             endif()
616           endif()
617           if("${m}" STREQUAL opencv_world)
618             list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
619           endif()
620         endif()
621       endforeach()
622     endif()
623
624     set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
625     set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
626     set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
627
628 #    message(STATUS "  module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
629 #    message(STATUS "  module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
630 #    message(STATUS "  extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
631 #    message(STATUS "")
632   endforeach()
633
634   __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
635
636   set(OPENCV_MODULES_PUBLIC        ${OPENCV_MODULES_PUBLIC}        CACHE INTERNAL "List of OpenCV modules marked for export")
637   set(OPENCV_MODULES_BUILD         ${OPENCV_MODULES_BUILD}         CACHE INTERNAL "List of OpenCV modules included into the build")
638   set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
639   set(OPENCV_WORLD_MODULES         ${OPENCV_WORLD_MODULES}         CACHE INTERNAL "List of OpenCV modules included into the world")
640 endfunction()
641
642
643 # setup include paths for the list of passed modules
644 macro(ocv_include_modules)
645   foreach(d ${ARGN})
646     if(d MATCHES "^opencv_" AND HAVE_${d})
647       if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
648         ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
649       endif()
650     elseif(EXISTS "${d}")
651       ocv_include_directories("${d}")
652     endif()
653   endforeach()
654 endmacro()
655
656 # same as previous but with dependencies
657 macro(ocv_include_modules_recurse)
658   ocv_include_modules(${ARGN})
659   foreach(d ${ARGN})
660     if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
661       foreach (sub ${OPENCV_MODULE_${d}_DEPS})
662         ocv_include_modules(${sub})
663       endforeach()
664     endif()
665   endforeach()
666 endmacro()
667
668 # setup include paths for the list of passed modules
669 macro(ocv_target_include_modules target)
670   foreach(d ${ARGN})
671     if(d MATCHES "^opencv_")
672       if(HAVE_${d} AND EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
673         ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
674       endif()
675     elseif(EXISTS "${d}")
676       ocv_target_include_directories(${target} "${d}")
677     else()
678       message(WARNING "Unexpected include: ${d} (module=${the_module})")
679     endif()
680   endforeach()
681 endmacro()
682
683 # setup include paths for the list of passed modules and recursively add dependent modules
684 macro(ocv_target_include_modules_recurse target)
685   foreach(d ${ARGN})
686     if(d MATCHES "^opencv_" AND HAVE_${d})
687       if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
688         ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
689       endif()
690       if(OPENCV_MODULE_${d}_DEPS)
691         ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
692       endif()
693     elseif(EXISTS "${d}")
694       ocv_target_include_directories(${target} "${d}")
695     endif()
696   endforeach()
697 endmacro()
698
699 # setup include path for OpenCV headers for specified module
700 # ocv_module_include_directories(<extra include directories/extra include modules>)
701 macro(ocv_module_include_directories)
702   if(ENABLE_PRECOMPILED_HEADERS OR OPENCV_INCLUDE_DIR_APPEND_MODULE_SRC)
703     ocv_target_include_directories(${the_module} "${OPENCV_MODULE_${the_module}_LOCATION}/src")
704   endif()
705   ocv_target_include_directories(${the_module}
706       "${OPENCV_MODULE_${the_module}_LOCATION}/include"
707       "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
708       )
709   ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
710 endmacro()
711
712
713 # sets header and source files for the current module
714 # NB: all files specified as headers will be installed
715 # Usage:
716 # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
717 macro(ocv_set_module_sources)
718   ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
719
720   set(OPENCV_MODULE_${the_module}_HEADERS "")
721   set(OPENCV_MODULE_${the_module}_SOURCES "")
722
723   foreach(f "HEADERS" ${ARGN})
724     if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
725       set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
726     else()
727       list(APPEND ${__filesvar} "${f}")
728     endif()
729   endforeach()
730
731   # the hacky way to embed any files into the OpenCV without modification of its build system
732   if(COMMAND ocv_get_module_external_sources)
733     ocv_get_module_external_sources()
734   endif()
735
736   if(OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED)
737     list(APPEND OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED})
738   endif()
739
740   # TODO Update hooks above
741   ocv_cmake_hook(INIT_MODULE_SOURCES)
742   ocv_cmake_hook(INIT_MODULE_SOURCES_${the_module})
743
744   # use full paths for module to be independent from the module location
745   ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
746
747   set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
748   set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
749 endmacro()
750
751 # finds and sets headers and sources for the standard OpenCV module
752 # Usage:
753 # ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
754 macro(ocv_glob_module_sources)
755   ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
756   set(_argn ${ARGN})
757   list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
758   if(NOT exclude_cuda EQUAL -1)
759     list(REMOVE_AT _argn ${exclude_cuda})
760   endif()
761   list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
762   if(NOT exclude_opencl EQUAL -1)
763     list(REMOVE_AT _argn ${exclude_opencl})
764   endif()
765
766   file(GLOB_RECURSE lib_srcs
767        "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
768   )
769   file(GLOB_RECURSE lib_int_hdrs
770        "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
771        "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
772   )
773   file(GLOB lib_hdrs
774        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
775        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
776        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
777        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp"
778        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h"
779        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.hpp"
780        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.h"
781   )
782   file(GLOB lib_hdrs_detail
783        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
784        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
785   )
786   if (APPLE)
787     file(GLOB_RECURSE lib_srcs_apple
788          "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
789     )
790     list(APPEND lib_srcs ${lib_srcs_apple})
791   endif()
792
793   ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
794   ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
795
796   set(lib_cuda_srcs "")
797   set(lib_cuda_hdrs "")
798   if(HAVE_CUDA AND exclude_cuda EQUAL -1)
799     file(GLOB lib_cuda_srcs
800          "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
801     )
802     file(GLOB lib_cuda_hdrs
803          "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
804     )
805     source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
806   endif()
807
808   file(GLOB cl_kernels
809        "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
810   )
811   if(cl_kernels AND exclude_opencl EQUAL -1)
812     set(OCL_NAME opencl_kernels_${name})
813     add_custom_command(
814       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp"  # don't add .hpp file here to optimize build process
815       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"
816       DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
817       COMMENT "Processing OpenCL kernels (${name})"
818     )
819     ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
820     ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
821     set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
822         PROPERTIES GENERATED TRUE
823     )
824     list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
825   endif()
826
827   ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
828                          SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
829 endmacro()
830
831 # creates OpenCV module in current folder
832 # creates new target, configures standard dependencies, compilers flags, install rules
833 # Usage:
834 #   ocv_create_module(<extra link dependencies>)
835 #   ocv_create_module()
836 macro(ocv_create_module)
837   ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
838   if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
839     message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
840   endif()
841   if(NOT " ${ARGN}" STREQUAL " ")
842     set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
843   endif()
844   if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
845     # nothing
846     set(the_module_target opencv_world)
847   else()
848     _ocv_create_module(${ARGN})
849     set(the_module_target ${the_module})
850   endif()
851
852   if(WINRT AND BUILD_TESTS)
853     # removing APPCONTAINER from modules to run from console
854     # in case of usual starting of WinRT test apps output is missing
855     # so starting of console version w/o APPCONTAINER is required to get test results
856     # also this allows to use opencv_extra test data for these tests
857     if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
858       add_custom_command(TARGET ${the_module}
859                          POST_BUILD
860                          COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
861     endif()
862
863     if("${the_module}" STREQUAL "opencv_ts")
864       # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
865       # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
866       add_custom_command(TARGET ${the_module}
867         POST_BUILD
868         COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
869         COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
870         COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
871     endif()
872   endif()
873 endmacro()
874
875 macro(_ocv_create_module)
876
877   ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
878   set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
879   set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
880
881   # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
882   # be called at some point in the future. We can't look into the future, though,
883   # so this will have to do.
884   if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
885     get_native_precompiled_header(${the_module} precomp.hpp)
886   endif()
887
888   if(WIN32
889       AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
890       AND NOT OPENCV_VS_VERSIONINFO_SKIP)
891     if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
892       set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
893     elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
894       set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
895     elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
896       set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
897       ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
898         NAME "${the_module}"
899         FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
900         INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
901         ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
902       )
903     endif()
904     if(_VS_VERSION_FILE)
905       if(NOT EXISTS "${_VS_VERSION_FILE}")
906         message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
907       endif()
908       source_group("Src" FILES "${_VS_VERSION_FILE}")
909     endif()
910   endif()
911   if(WIN32 AND NOT (
912           "${the_module}" STREQUAL "opencv_core" OR
913           "${the_module}" STREQUAL "opencv_world" OR
914           "${the_module}" STREQUAL "opencv_cudev"
915       )
916       AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
917       AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
918   )
919       set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
920       configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
921   endif()
922
923   source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
924   source_group("Src" FILES "${${the_module}_pch}")
925   ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
926   ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
927   ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
928     "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
929     ${${the_module}_pch}
930     ${_VS_VERSION_FILE}
931     ${_DLLMAIN_FILE}
932   )
933   set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
934   set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
935     PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
936
937   ocv_target_link_libraries(${the_module} PUBLIC    ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
938                                           INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
939   )
940   ocv_target_link_libraries(${the_module} PUBLIC    ${OPENCV_MODULE_${the_module}_DEPS_EXT}
941                                           INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_EXT}
942   )
943   ocv_target_link_libraries(${the_module} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
944   if (HAVE_CUDA)
945     ocv_target_link_libraries(${the_module} PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
946   endif()
947
948   if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
949     target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
950     unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
951   endif()
952
953   add_dependencies(opencv_modules ${the_module})
954
955   if(ENABLE_SOLUTION_FOLDERS)
956     set_target_properties(${the_module} PROPERTIES FOLDER "modules")
957   endif()
958
959   set_target_properties(${the_module} PROPERTIES
960     OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
961     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
962     COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
963     COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
964     ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
965     COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
966     LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
967     RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
968     DEFINE_SYMBOL CVAPI_EXPORTS
969   )
970
971   if(BUILD_FAT_JAVA_LIB)  # force exports from static modules too
972     if(BUILD_SHARED_LIBS)
973       message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
974     endif()
975     target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
976   endif()
977
978   # For dynamic link numbering conventions
979   if(NOT ANDROID)
980     # Android SDK build scripts can include only .so files into final .apk
981     # As result we should not set version properties for Android
982     set_target_properties(${the_module} PROPERTIES
983       VERSION ${OPENCV_LIBVERSION}
984       SOVERSION ${OPENCV_SOVERSION}
985     )
986   endif()
987
988   if (ENABLE_GNU_STL_DEBUG)
989     target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
990   endif()
991
992   if(MSVC)
993     if(CMAKE_CROSSCOMPILING)
994       set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
995     endif()
996     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
997   endif()
998
999   get_target_property(_target_type ${the_module} TYPE)
1000   if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
1001       ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
1002     ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
1003       RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
1004       LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
1005       ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
1006       )
1007   endif()
1008   if("${_target_type}" STREQUAL "SHARED_LIBRARY")
1009     install(TARGETS ${the_module}
1010       LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
1011   endif()
1012
1013   # only "public" headers need to be installed
1014   ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
1015   ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
1016   if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
1017     foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
1018       string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
1019       if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
1020         install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
1021       endif()
1022     endforeach()
1023   endif()
1024
1025   _ocv_add_precompiled_headers(${the_module})
1026
1027   ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
1028   ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
1029 endmacro()
1030
1031 # opencv precompiled headers macro (can add pch to modules and tests)
1032 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
1033 # Usage:
1034 # ocv_add_precompiled_headers(${the_module})
1035 macro(_ocv_add_precompiled_headers the_target)
1036   ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
1037
1038   if("${the_target}" MATCHES "^opencv_test_.*$")
1039     SET(pch_path "test/test_")
1040   elseif("${the_target}" MATCHES "^opencv_perf_.*$")
1041     SET(pch_path "perf/perf_")
1042   else()
1043     SET(pch_path "src/")
1044   endif()
1045   ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
1046   unset(pch_path)
1047 endmacro()
1048
1049 # short command for adding simple OpenCV module
1050 # see ocv_add_module for argument details
1051 # Usage:
1052 # ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
1053 macro(ocv_define_module module_name)
1054   ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
1055   set(_argn ${ARGN})
1056   set(exclude_cuda "")
1057   foreach(arg ${_argn})
1058     if("${arg}" STREQUAL "EXCLUDE_CUDA")
1059       set(exclude_cuda "${arg}")
1060       list(REMOVE_ITEM _argn ${arg})
1061     endif()
1062   endforeach()
1063
1064   ocv_add_module(${module_name} ${_argn})
1065   ocv_glob_module_sources(${exclude_cuda})
1066   ocv_module_include_directories()
1067   ocv_create_module()
1068
1069   ocv_add_accuracy_tests()
1070   ocv_add_perf_tests()
1071   ocv_add_samples()
1072 endmacro()
1073
1074 # ensures that all passed modules are available
1075 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
1076 macro(ocv_check_dependencies)
1077   set(OCV_DEPENDENCIES_FOUND TRUE)
1078   foreach(d ${ARGN})
1079     if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
1080       set(OCV_DEPENDENCIES_FOUND FALSE)
1081       break()
1082     endif()
1083   endforeach()
1084 endmacro()
1085
1086 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
1087 macro(__ocv_parse_test_sources tests_type)
1088   set(OPENCV_${tests_type}_${the_module}_SOURCES "")
1089   set(OPENCV_${tests_type}_${the_module}_DEPS "")
1090   set(__file_group_name "")
1091   set(__file_group_sources "")
1092   foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
1093     if(arg STREQUAL "FILES")
1094       set(__currentvar "__file_group_sources")
1095       if(__file_group_name AND __file_group_sources)
1096         source_group("${__file_group_name}" FILES ${__file_group_sources})
1097         list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
1098       endif()
1099       set(__file_group_name "")
1100       set(__file_group_sources "")
1101     elseif(arg STREQUAL "DEPENDS_ON")
1102       set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
1103     elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
1104       set(__file_group_name "${arg}")
1105     else()
1106       list(APPEND ${__currentvar} "${arg}")
1107     endif()
1108   endforeach()
1109   unset(__file_group_name)
1110   unset(__file_group_sources)
1111   unset(__currentvar)
1112 endmacro()
1113
1114 # this is a command for adding OpenCV performance tests to the module
1115 # ocv_add_perf_tests(<extra_dependencies>)
1116 function(ocv_add_perf_tests)
1117   ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
1118
1119   if(WINRT)
1120     set(OPENCV_DEBUG_POSTFIX "")
1121   endif()
1122
1123   set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
1124   if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
1125     __ocv_parse_test_sources(PERF ${ARGN})
1126
1127     # opencv_imgcodecs is required for imread/imwrite
1128     set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1129     ocv_check_dependencies(${perf_deps})
1130
1131     if(OCV_DEPENDENCIES_FOUND)
1132       set(the_target "opencv_perf_${name}")
1133       # project(${the_target})
1134
1135       if(NOT OPENCV_PERF_${the_module}_SOURCES)
1136         file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
1137         file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
1138         ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
1139         ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
1140         set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
1141       endif()
1142
1143       ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
1144
1145       if(NOT BUILD_opencv_world)
1146         get_native_precompiled_header(${the_target} perf_precomp.hpp)
1147       endif()
1148
1149       source_group("Src" FILES "${${the_target}_pch}")
1150       ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
1151       ocv_target_include_modules(${the_target} ${perf_deps})
1152       ocv_target_link_libraries(${the_target} PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
1153       add_dependencies(opencv_perf_tests ${the_target})
1154
1155       set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1156       set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
1157         PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1158
1159       # Additional target properties
1160       set_target_properties(${the_target} PROPERTIES
1161         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1162         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1163       )
1164       if(ENABLE_SOLUTION_FOLDERS)
1165         set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
1166       endif()
1167
1168       if(WINRT)
1169         # removing APPCONTAINER from tests to run from console
1170         # look for detailed description inside of ocv_create_module macro above
1171         add_custom_command(TARGET "opencv_perf_${name}"
1172                            POST_BUILD
1173                            COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
1174       endif()
1175
1176       if(NOT BUILD_opencv_world)
1177         _ocv_add_precompiled_headers(${the_target})
1178       endif()
1179
1180       ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
1181       ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
1182                                "--perf_min_samples=1"
1183                                "--perf_force_samples=1"
1184                                "--perf_verify_sanity")
1185     else(OCV_DEPENDENCIES_FOUND)
1186       # TODO: warn about unsatisfied dependencies
1187     endif(OCV_DEPENDENCIES_FOUND)
1188     if(INSTALL_TESTS)
1189       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1190     endif()
1191   endif()
1192 endfunction()
1193
1194 # this is a command for adding OpenCV accuracy/regression tests to the module
1195 # ocv_add_accuracy_tests(<list of extra dependencies>)
1196 function(ocv_add_accuracy_tests)
1197   ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
1198
1199   set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
1200   if(BUILD_TESTS AND EXISTS "${test_path}")
1201     __ocv_parse_test_sources(TEST ${ARGN})
1202
1203     # opencv_imgcodecs is required for imread/imwrite
1204     set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1205     ocv_check_dependencies(${test_deps})
1206     if(OCV_DEPENDENCIES_FOUND)
1207       set(the_target "opencv_test_${name}")
1208       # project(${the_target})
1209
1210       if(NOT OPENCV_TEST_${the_module}_SOURCES)
1211         file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
1212         file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
1213         ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
1214         ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
1215         set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
1216       endif()
1217
1218       if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
1219         list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
1220       endif()
1221       ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
1222
1223       if(NOT BUILD_opencv_world)
1224         get_native_precompiled_header(${the_target} test_precomp.hpp)
1225       endif()
1226
1227       source_group("Src" FILES "${${the_target}_pch}")
1228       ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
1229       ocv_target_include_modules(${the_target} ${test_deps})
1230       if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
1231         ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
1232       endif()
1233       ocv_target_link_libraries(${the_target} PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
1234       add_dependencies(opencv_tests ${the_target})
1235
1236       set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1237       set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
1238         PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1239
1240       # Additional target properties
1241       set_target_properties(${the_target} PROPERTIES
1242         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1243         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1244       )
1245
1246       ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
1247
1248       if(ENABLE_SOLUTION_FOLDERS)
1249         set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
1250       endif()
1251
1252       if(OPENCV_TEST_BIGDATA)
1253         ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
1254       endif()
1255
1256       if(NOT BUILD_opencv_world)
1257         _ocv_add_precompiled_headers(${the_target})
1258       endif()
1259
1260       ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
1261     else(OCV_DEPENDENCIES_FOUND)
1262       # TODO: warn about unsatisfied dependencies
1263     endif(OCV_DEPENDENCIES_FOUND)
1264
1265     if(INSTALL_TESTS)
1266       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1267     endif()
1268   endif()
1269 endfunction()
1270
1271 function(ocv_add_samples)
1272   ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
1273
1274   set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1275   if(NOT EXISTS "${samples_path}")
1276     return()
1277   endif()
1278
1279   string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
1280
1281   if(BUILD_EXAMPLES)
1282     set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1283     ocv_check_dependencies(${samples_deps})
1284
1285     if(OCV_DEPENDENCIES_FOUND)
1286       file(GLOB sample_sources "${samples_path}/*.cpp")
1287
1288       foreach(source ${sample_sources})
1289         get_filename_component(name "${source}" NAME_WE)
1290         set(the_target "example_${module_id}_${name}")
1291
1292         ocv_add_executable(${the_target} "${source}")
1293         ocv_target_include_modules(${the_target} ${samples_deps})
1294         ocv_target_link_libraries(${the_target} PRIVATE ${samples_deps})
1295
1296         set_target_properties(${the_target} PROPERTIES
1297           PROJECT_LABEL "(sample) ${name}"
1298           LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1299         set_source_files_properties("${source}" PROPERTIES
1300           LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1301         if(ENABLE_SOLUTION_FOLDERS)
1302           set_target_properties(${the_target} PROPERTIES
1303             FOLDER "samples/${module_id}")
1304         endif()
1305         # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
1306         set(parent_target opencv_samples_${module_id})
1307         if(NOT TARGET ${parent_target})
1308           add_custom_target(${parent_target})
1309           add_dependencies(opencv_samples ${parent_target})
1310         endif()
1311         add_dependencies(${parent_target} ${the_target})
1312
1313         if(WIN32)
1314           install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
1315         endif()
1316       endforeach()
1317     endif()
1318   endif()
1319
1320   if(INSTALL_C_EXAMPLES)
1321     file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1322     foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
1323         IF( IS_DIRECTORY "${ITEM}" )
1324             LIST( APPEND sample_dirs "${ITEM}" )
1325         ELSE()
1326             LIST( APPEND sample_files "${ITEM}" )
1327         ENDIF()
1328     endforeach()
1329     install(FILES ${sample_files}
1330             DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1331             COMPONENT samples)
1332     install(DIRECTORY ${sample_dirs}
1333             DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1334             COMPONENT samples)
1335   endif()
1336 endfunction()