Merge pull request #16122 from alalek:cmake_update_cpu_compiler_detection
[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} LINK_PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
938   ocv_target_link_libraries(${the_module} LINK_PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_EXT})
939   ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
940   if (HAVE_CUDA)
941     ocv_target_link_libraries(${the_module} LINK_PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
942   endif()
943
944   if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
945     target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
946     unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
947   endif()
948
949   add_dependencies(opencv_modules ${the_module})
950
951   if(ENABLE_SOLUTION_FOLDERS)
952     set_target_properties(${the_module} PROPERTIES FOLDER "modules")
953   endif()
954
955   set_target_properties(${the_module} PROPERTIES
956     OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
957     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
958     COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
959     COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
960     ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
961     COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
962     LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
963     RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
964     DEFINE_SYMBOL CVAPI_EXPORTS
965   )
966
967   if(BUILD_FAT_JAVA_LIB)  # force exports from static modules too
968     if(BUILD_SHARED_LIBS)
969       message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
970     endif()
971     target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
972   endif()
973
974   # For dynamic link numbering conventions
975   if(NOT ANDROID)
976     # Android SDK build scripts can include only .so files into final .apk
977     # As result we should not set version properties for Android
978     set_target_properties(${the_module} PROPERTIES
979       VERSION ${OPENCV_LIBVERSION}
980       SOVERSION ${OPENCV_SOVERSION}
981     )
982   endif()
983
984   if (ENABLE_GNU_STL_DEBUG)
985     target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
986   endif()
987
988   if(MSVC)
989     if(CMAKE_CROSSCOMPILING)
990       set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
991     endif()
992     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
993   endif()
994
995   get_target_property(_target_type ${the_module} TYPE)
996   if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
997       ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
998     ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
999       RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
1000       LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
1001       ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
1002       )
1003   endif()
1004   if("${_target_type}" STREQUAL "SHARED_LIBRARY")
1005     install(TARGETS ${the_module}
1006       LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
1007   endif()
1008
1009   # only "public" headers need to be installed
1010   ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
1011   ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
1012   if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
1013     foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
1014       string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
1015       if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
1016         install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
1017       endif()
1018     endforeach()
1019   endif()
1020
1021   _ocv_add_precompiled_headers(${the_module})
1022
1023   ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
1024   ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
1025 endmacro()
1026
1027 # opencv precompiled headers macro (can add pch to modules and tests)
1028 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
1029 # Usage:
1030 # ocv_add_precompiled_headers(${the_module})
1031 macro(_ocv_add_precompiled_headers the_target)
1032   ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
1033
1034   if("${the_target}" MATCHES "^opencv_test_.*$")
1035     SET(pch_path "test/test_")
1036   elseif("${the_target}" MATCHES "^opencv_perf_.*$")
1037     SET(pch_path "perf/perf_")
1038   else()
1039     SET(pch_path "src/")
1040   endif()
1041   ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
1042   unset(pch_path)
1043 endmacro()
1044
1045 # short command for adding simple OpenCV module
1046 # see ocv_add_module for argument details
1047 # Usage:
1048 # ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
1049 macro(ocv_define_module module_name)
1050   ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
1051   set(_argn ${ARGN})
1052   set(exclude_cuda "")
1053   foreach(arg ${_argn})
1054     if("${arg}" STREQUAL "EXCLUDE_CUDA")
1055       set(exclude_cuda "${arg}")
1056       list(REMOVE_ITEM _argn ${arg})
1057     endif()
1058   endforeach()
1059
1060   ocv_add_module(${module_name} ${_argn})
1061   ocv_glob_module_sources(${exclude_cuda})
1062   ocv_module_include_directories()
1063   ocv_create_module()
1064
1065   ocv_add_accuracy_tests()
1066   ocv_add_perf_tests()
1067   ocv_add_samples()
1068 endmacro()
1069
1070 # ensures that all passed modules are available
1071 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
1072 macro(ocv_check_dependencies)
1073   set(OCV_DEPENDENCIES_FOUND TRUE)
1074   foreach(d ${ARGN})
1075     if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
1076       set(OCV_DEPENDENCIES_FOUND FALSE)
1077       break()
1078     endif()
1079   endforeach()
1080 endmacro()
1081
1082 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
1083 macro(__ocv_parse_test_sources tests_type)
1084   set(OPENCV_${tests_type}_${the_module}_SOURCES "")
1085   set(OPENCV_${tests_type}_${the_module}_DEPS "")
1086   set(__file_group_name "")
1087   set(__file_group_sources "")
1088   foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
1089     if(arg STREQUAL "FILES")
1090       set(__currentvar "__file_group_sources")
1091       if(__file_group_name AND __file_group_sources)
1092         source_group("${__file_group_name}" FILES ${__file_group_sources})
1093         list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
1094       endif()
1095       set(__file_group_name "")
1096       set(__file_group_sources "")
1097     elseif(arg STREQUAL "DEPENDS_ON")
1098       set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
1099     elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
1100       set(__file_group_name "${arg}")
1101     else()
1102       list(APPEND ${__currentvar} "${arg}")
1103     endif()
1104   endforeach()
1105   unset(__file_group_name)
1106   unset(__file_group_sources)
1107   unset(__currentvar)
1108 endmacro()
1109
1110 # this is a command for adding OpenCV performance tests to the module
1111 # ocv_add_perf_tests(<extra_dependencies>)
1112 function(ocv_add_perf_tests)
1113   ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
1114
1115   if(WINRT)
1116     set(OPENCV_DEBUG_POSTFIX "")
1117   endif()
1118
1119   set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
1120   if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
1121     __ocv_parse_test_sources(PERF ${ARGN})
1122
1123     # opencv_imgcodecs is required for imread/imwrite
1124     set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1125     ocv_check_dependencies(${perf_deps})
1126
1127     if(OCV_DEPENDENCIES_FOUND)
1128       set(the_target "opencv_perf_${name}")
1129       # project(${the_target})
1130
1131       if(NOT OPENCV_PERF_${the_module}_SOURCES)
1132         file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
1133         file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
1134         ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
1135         ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
1136         set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
1137       endif()
1138
1139       ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
1140
1141       if(NOT BUILD_opencv_world)
1142         get_native_precompiled_header(${the_target} perf_precomp.hpp)
1143       endif()
1144
1145       source_group("Src" FILES "${${the_target}_pch}")
1146       ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
1147       ocv_target_include_modules(${the_target} ${perf_deps})
1148       ocv_target_link_libraries(${the_target} LINK_PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
1149       add_dependencies(opencv_perf_tests ${the_target})
1150
1151       set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1152       set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
1153         PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1154
1155       # Additional target properties
1156       set_target_properties(${the_target} PROPERTIES
1157         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1158         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1159       )
1160       if(ENABLE_SOLUTION_FOLDERS)
1161         set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
1162       endif()
1163
1164       if(WINRT)
1165         # removing APPCONTAINER from tests to run from console
1166         # look for detailed description inside of ocv_create_module macro above
1167         add_custom_command(TARGET "opencv_perf_${name}"
1168                            POST_BUILD
1169                            COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
1170       endif()
1171
1172       if(NOT BUILD_opencv_world)
1173         _ocv_add_precompiled_headers(${the_target})
1174       endif()
1175
1176       ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
1177       ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
1178                                "--perf_min_samples=1"
1179                                "--perf_force_samples=1"
1180                                "--perf_verify_sanity")
1181     else(OCV_DEPENDENCIES_FOUND)
1182       # TODO: warn about unsatisfied dependencies
1183     endif(OCV_DEPENDENCIES_FOUND)
1184     if(INSTALL_TESTS)
1185       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1186     endif()
1187   endif()
1188 endfunction()
1189
1190 # this is a command for adding OpenCV accuracy/regression tests to the module
1191 # ocv_add_accuracy_tests(<list of extra dependencies>)
1192 function(ocv_add_accuracy_tests)
1193   ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
1194
1195   set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
1196   if(BUILD_TESTS AND EXISTS "${test_path}")
1197     __ocv_parse_test_sources(TEST ${ARGN})
1198
1199     # opencv_imgcodecs is required for imread/imwrite
1200     set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1201     ocv_check_dependencies(${test_deps})
1202     if(OCV_DEPENDENCIES_FOUND)
1203       set(the_target "opencv_test_${name}")
1204       # project(${the_target})
1205
1206       if(NOT OPENCV_TEST_${the_module}_SOURCES)
1207         file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
1208         file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
1209         ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
1210         ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
1211         set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
1212       endif()
1213
1214       if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
1215         list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
1216       endif()
1217       ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
1218
1219       if(NOT BUILD_opencv_world)
1220         get_native_precompiled_header(${the_target} test_precomp.hpp)
1221       endif()
1222
1223       source_group("Src" FILES "${${the_target}_pch}")
1224       ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
1225       ocv_target_include_modules(${the_target} ${test_deps})
1226       if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
1227         ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
1228       endif()
1229       ocv_target_link_libraries(${the_target} LINK_PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
1230       add_dependencies(opencv_tests ${the_target})
1231
1232       set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1233       set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
1234         PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1235
1236       # Additional target properties
1237       set_target_properties(${the_target} PROPERTIES
1238         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1239         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1240       )
1241
1242       ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
1243
1244       if(ENABLE_SOLUTION_FOLDERS)
1245         set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
1246       endif()
1247
1248       if(OPENCV_TEST_BIGDATA)
1249         ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
1250       endif()
1251
1252       if(NOT BUILD_opencv_world)
1253         _ocv_add_precompiled_headers(${the_target})
1254       endif()
1255
1256       ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
1257     else(OCV_DEPENDENCIES_FOUND)
1258       # TODO: warn about unsatisfied dependencies
1259     endif(OCV_DEPENDENCIES_FOUND)
1260
1261     if(INSTALL_TESTS)
1262       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1263     endif()
1264   endif()
1265 endfunction()
1266
1267 function(ocv_add_samples)
1268   ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
1269
1270   set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1271   if(NOT EXISTS "${samples_path}")
1272     return()
1273   endif()
1274
1275   string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
1276
1277   if(BUILD_EXAMPLES)
1278     set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1279     ocv_check_dependencies(${samples_deps})
1280
1281     if(OCV_DEPENDENCIES_FOUND)
1282       file(GLOB sample_sources "${samples_path}/*.cpp")
1283
1284       foreach(source ${sample_sources})
1285         get_filename_component(name "${source}" NAME_WE)
1286         set(the_target "example_${module_id}_${name}")
1287
1288         ocv_add_executable(${the_target} "${source}")
1289         ocv_target_include_modules(${the_target} ${samples_deps})
1290         ocv_target_link_libraries(${the_target} LINK_PRIVATE ${samples_deps})
1291
1292         set_target_properties(${the_target} PROPERTIES
1293           PROJECT_LABEL "(sample) ${name}"
1294           LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1295         set_source_files_properties("${source}" PROPERTIES
1296           LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1297         if(ENABLE_SOLUTION_FOLDERS)
1298           set_target_properties(${the_target} PROPERTIES
1299             FOLDER "samples/${module_id}")
1300         endif()
1301         # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
1302         set(parent_target opencv_samples_${module_id})
1303         if(NOT TARGET ${parent_target})
1304           add_custom_target(${parent_target})
1305           add_dependencies(opencv_samples ${parent_target})
1306         endif()
1307         add_dependencies(${parent_target} ${the_target})
1308
1309         if(WIN32)
1310           install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
1311         endif()
1312       endforeach()
1313     endif()
1314   endif()
1315
1316   if(INSTALL_C_EXAMPLES)
1317     file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1318     foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
1319         IF( IS_DIRECTORY "${ITEM}" )
1320             LIST( APPEND sample_dirs "${ITEM}" )
1321         ELSE()
1322             LIST( APPEND sample_files "${ITEM}" )
1323         ENDIF()
1324     endforeach()
1325     install(FILES ${sample_files}
1326             DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1327             COMPONENT samples)
1328     install(DIRECTORY ${sample_dirs}
1329             DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1330             COMPONENT samples)
1331   endif()
1332 endfunction()