Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[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_OPENCL] [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   ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
102   ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
103   ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
104   ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
105   ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
106
107   set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
108     CACHE INTERNAL "Required dependencies of ${full_modname} module")
109   set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
110     CACHE INTERNAL "Optional dependencies of ${full_modname} module")
111   set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
112     CACHE INTERNAL "Required private dependencies of ${full_modname} module")
113   set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
114     CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
115   set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
116     CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
117 endmacro()
118
119 # declare new OpenCV module in current folder
120 # Usage:
121 #   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
122 # Example:
123 #   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
124 macro(ocv_add_module _name)
125   ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
126   string(TOLOWER "${_name}" name)
127   set(the_module opencv_${name})
128
129   # the first pass - collect modules info, the second pass - create targets
130   if(OPENCV_INITIAL_PASS)
131     #guard against redefinition
132     if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
133       message(FATAL_ERROR "Redefinition of the ${the_module} module.
134   at:                    ${CMAKE_CURRENT_SOURCE_DIR}
135   previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
136 ")
137     endif()
138
139     if(NOT DEFINED the_description)
140       set(the_description "The ${name} OpenCV module")
141     endif()
142
143     if(NOT DEFINED BUILD_${the_module}_INIT)
144       set(BUILD_${the_module}_INIT ON)
145     endif()
146
147     # create option to enable/disable this module
148     option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
149
150     # remember the module details
151     set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
152     set(OPENCV_MODULE_${the_module}_LOCATION    "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
153
154     set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
155
156     set(ADD_MODULE_ARGN ${ARGN})
157     ocv_cmake_hook(PRE_ADD_MODULE)
158     ocv_cmake_hook(PRE_ADD_MODULE_${the_module})
159
160     # parse list of dependencies
161     if(" ${ARGV1}" STREQUAL " INTERNAL" OR " ${ARGV1}" STREQUAL " BINDINGS")
162       set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
163       set(__ocv_argn__ ${ADD_MODULE_ARGN})
164       list(REMOVE_AT __ocv_argn__ 0)
165       ocv_add_dependencies(${the_module} ${__ocv_argn__})
166       unset(__ocv_argn__)
167     else()
168       set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
169       ocv_add_dependencies(${the_module} ${ADD_MODULE_ARGN})
170       if(BUILD_${the_module})
171         set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
172       endif()
173     endif()
174
175     # add self to the world dependencies
176     if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD
177         AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS"
178         AND (NOT DEFINED OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
179         AND (NOT OPENCV_PROCESSING_EXTRA_MODULES OR NOT OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
180         AND (NOT BUILD_SHARED_LIBS OR NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC"))
181         OR OPENCV_MODULE_IS_PART_OF_WORLD
182         )
183       set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
184       ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
185     else()
186       set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
187     endif()
188
189     if(NOT DEFINED the_label)
190       if(OPENCV_PROCESSING_EXTRA_MODULES)
191         set(the_label "Extra")
192       else()
193         set(the_label "Main")
194       endif()
195     endif()
196     set(OPENCV_MODULE_${the_module}_LABEL "${the_label};${the_module}" CACHE INTERNAL "")
197
198     if(BUILD_${the_module})
199       set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
200     else()
201       set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
202     endif()
203
204     # add reverse wrapper dependencies (BINDINDS)
205     foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
206       if(wrapper STREQUAL "python")  # hack for python (BINDINDS)
207         ocv_add_dependencies(opencv_python2 OPTIONAL ${the_module})
208         ocv_add_dependencies(opencv_python3 OPTIONAL ${the_module})
209       else()
210         ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
211       endif()
212       if(DEFINED OPENCV_MODULE_opencv_${wrapper}_bindings_generator_CLASS)
213         ocv_add_dependencies(opencv_${wrapper}_bindings_generator OPTIONAL ${the_module})
214       endif()
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        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/legacy/*.h"
782   )
783   file(GLOB lib_hdrs_detail
784        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
785        "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
786   )
787   if (APPLE)
788     file(GLOB_RECURSE lib_srcs_apple
789          "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
790          "${CMAKE_CURRENT_LIST_DIR}/src/*.swift"
791     )
792     list(APPEND lib_srcs ${lib_srcs_apple})
793   endif()
794
795   ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
796   ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
797
798   set(lib_cuda_srcs "")
799   set(lib_cuda_hdrs "")
800   if(HAVE_CUDA AND exclude_cuda EQUAL -1)
801     file(GLOB lib_cuda_srcs
802          "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
803     )
804     file(GLOB lib_cuda_hdrs
805          "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
806     )
807     source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
808   endif()
809
810   file(GLOB cl_kernels
811        "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
812   )
813   if(cl_kernels AND exclude_opencl EQUAL -1)
814     set(OCL_NAME opencl_kernels_${name})
815     add_custom_command(
816       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp"  # don't add .hpp file here to optimize build process
817       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"
818       DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
819       COMMENT "Processing OpenCL kernels (${name})"
820     )
821     ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
822     ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
823     set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
824         PROPERTIES GENERATED TRUE
825     )
826     list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
827   endif()
828
829   ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
830                          SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
831 endmacro()
832
833 # creates OpenCV module in current folder
834 # creates new target, configures standard dependencies, compilers flags, install rules
835 # Usage:
836 #   ocv_create_module(<extra link dependencies>)
837 #   ocv_create_module()
838 macro(ocv_create_module)
839   ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
840   if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
841     message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
842   endif()
843   if(NOT " ${ARGN}" STREQUAL " ")
844     set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
845   endif()
846   if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
847     # nothing
848     set(the_module_target opencv_world)
849   else()
850     _ocv_create_module(${ARGN})
851     set(the_module_target ${the_module})
852   endif()
853
854   if(WINRT AND BUILD_TESTS)
855     # removing APPCONTAINER from modules to run from console
856     # in case of usual starting of WinRT test apps output is missing
857     # so starting of console version w/o APPCONTAINER is required to get test results
858     # also this allows to use opencv_extra test data for these tests
859     if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
860       add_custom_command(TARGET ${the_module}
861                          POST_BUILD
862                          COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
863     endif()
864
865     if("${the_module}" STREQUAL "opencv_ts")
866       # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
867       # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
868       add_custom_command(TARGET ${the_module}
869         POST_BUILD
870         COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
871         COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
872         COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
873     endif()
874   endif()
875 endmacro()
876
877 macro(_ocv_create_module)
878
879   ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
880   set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
881   set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
882
883   # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
884   # be called at some point in the future. We can't look into the future, though,
885   # so this will have to do.
886   if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
887     get_native_precompiled_header(${the_module} precomp.hpp)
888   endif()
889
890   if(WIN32
891       AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
892       AND NOT OPENCV_VS_VERSIONINFO_SKIP)
893     if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
894       set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
895     elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
896       set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
897     elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
898       set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
899       ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
900         NAME "${the_module}"
901         FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
902         INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
903         ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
904       )
905     endif()
906     if(_VS_VERSION_FILE)
907       if(NOT EXISTS "${_VS_VERSION_FILE}")
908         message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
909       endif()
910       source_group("Src" FILES "${_VS_VERSION_FILE}")
911     endif()
912   endif()
913   if(WIN32 AND NOT (
914           "${the_module}" STREQUAL "opencv_core" OR
915           "${the_module}" STREQUAL "opencv_world" OR
916           "${the_module}" STREQUAL "opencv_cudev"
917       )
918       AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
919       AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
920   )
921       set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
922       configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
923   endif()
924
925   source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
926   source_group("Src" FILES "${${the_module}_pch}")
927   ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
928   ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
929   ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
930     "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
931     ${${the_module}_pch}
932     ${_VS_VERSION_FILE}
933     ${_DLLMAIN_FILE}
934   )
935   set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
936   set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
937     PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
938
939   ocv_target_link_libraries(${the_module} PUBLIC    ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
940                                           INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
941   )
942   ocv_target_link_libraries(${the_module} PUBLIC    ${OPENCV_MODULE_${the_module}_DEPS_EXT}
943                                           INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_EXT}
944   )
945   ocv_target_link_libraries(${the_module} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
946   if (HAVE_CUDA)
947     ocv_target_link_libraries(${the_module} PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
948   endif()
949
950   if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
951     target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
952     unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
953   endif()
954
955   add_dependencies(opencv_modules ${the_module})
956
957   if(ENABLE_SOLUTION_FOLDERS)
958     set_target_properties(${the_module} PROPERTIES FOLDER "modules")
959   endif()
960
961   set_target_properties(${the_module} PROPERTIES
962     OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
963     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
964     COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
965     COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
966     ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
967     COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
968     LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
969     RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
970     DEFINE_SYMBOL CVAPI_EXPORTS
971   )
972
973   if(BUILD_FAT_JAVA_LIB)  # force exports from static modules too
974     if(BUILD_SHARED_LIBS)
975       message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
976     endif()
977     target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
978   endif()
979
980   # For dynamic link numbering conventions
981   if(NOT ANDROID)
982     # Android SDK build scripts can include only .so files into final .apk
983     # As result we should not set version properties for Android
984     set_target_properties(${the_module} PROPERTIES
985       VERSION ${OPENCV_LIBVERSION}
986       SOVERSION ${OPENCV_SOVERSION}
987     )
988   endif()
989
990   if (ENABLE_GNU_STL_DEBUG)
991     target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
992   endif()
993
994   if(MSVC)
995     if(CMAKE_CROSSCOMPILING)
996       set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
997     endif()
998     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
999   endif()
1000
1001   get_target_property(_target_type ${the_module} TYPE)
1002   if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
1003       ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
1004     ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
1005       RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
1006       LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
1007       ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
1008       )
1009   endif()
1010   if("${_target_type}" STREQUAL "SHARED_LIBRARY")
1011     install(TARGETS ${the_module}
1012       LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
1013   endif()
1014
1015   # only "public" headers need to be installed
1016   ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
1017   ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
1018   if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
1019     foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
1020       string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
1021       if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
1022         install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
1023       else()
1024         #message("Header file will be NOT installed: ${hdr}")
1025       endif()
1026     endforeach()
1027   endif()
1028
1029   _ocv_add_precompiled_headers(${the_module})
1030
1031   ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
1032   ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
1033 endmacro()
1034
1035 # opencv precompiled headers macro (can add pch to modules and tests)
1036 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
1037 # Usage:
1038 # ocv_add_precompiled_headers(${the_module})
1039 macro(_ocv_add_precompiled_headers the_target)
1040   ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
1041
1042   if("${the_target}" MATCHES "^opencv_test_.*$")
1043     SET(pch_path "test/test_")
1044   elseif("${the_target}" MATCHES "^opencv_perf_.*$")
1045     SET(pch_path "perf/perf_")
1046   else()
1047     SET(pch_path "src/")
1048   endif()
1049   ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
1050   unset(pch_path)
1051 endmacro()
1052
1053 # short command for adding simple OpenCV module
1054 # see ocv_add_module for argument details
1055 # Usage:
1056 # ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
1057 macro(ocv_define_module module_name)
1058   ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
1059   set(_argn ${ARGN})
1060   set(exclude_cuda "")
1061   foreach(arg ${_argn})
1062     if("${arg}" STREQUAL "EXCLUDE_CUDA")
1063       set(exclude_cuda "${arg}")
1064       list(REMOVE_ITEM _argn ${arg})
1065     endif()
1066   endforeach()
1067
1068   ocv_add_module(${module_name} ${_argn})
1069   ocv_glob_module_sources(${exclude_cuda})
1070   ocv_module_include_directories()
1071   ocv_create_module()
1072
1073   ocv_add_accuracy_tests()
1074   ocv_add_perf_tests()
1075   ocv_add_samples()
1076 endmacro()
1077
1078 # ensures that all passed modules are available
1079 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
1080 macro(ocv_check_dependencies)
1081   set(OCV_DEPENDENCIES_FOUND TRUE)
1082   foreach(d ${ARGN})
1083     if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
1084       set(OCV_DEPENDENCIES_FOUND FALSE)
1085       break()
1086     endif()
1087   endforeach()
1088 endmacro()
1089
1090 ################################################################################
1091 # OpenCV tests
1092 ################################################################################
1093
1094 if(DEFINED OPENCV_BUILD_TEST_MODULES_LIST)
1095   string(REPLACE "," ";" OPENCV_BUILD_TEST_MODULES_LIST "${OPENCV_BUILD_TEST_MODULES_LIST}")  # support comma-separated list (,) too
1096 endif()
1097 if(DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST)
1098   string(REPLACE "," ";" OPENCV_BUILD_PERF_TEST_MODULES_LIST "${OPENCV_BUILD_PERF_TEST_MODULES_LIST}")  # support comma-separated list (,) too
1099 endif()
1100
1101 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
1102 macro(__ocv_parse_test_sources tests_type)
1103   set(OPENCV_${tests_type}_${the_module}_SOURCES "")
1104   set(OPENCV_${tests_type}_${the_module}_DEPS "")
1105   set(__file_group_name "")
1106   set(__file_group_sources "")
1107   foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
1108     if(arg STREQUAL "FILES")
1109       set(__currentvar "__file_group_sources")
1110       if(__file_group_name AND __file_group_sources)
1111         source_group("${__file_group_name}" FILES ${__file_group_sources})
1112         list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
1113       endif()
1114       set(__file_group_name "")
1115       set(__file_group_sources "")
1116     elseif(arg STREQUAL "DEPENDS_ON")
1117       set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
1118     elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
1119       set(__file_group_name "${arg}")
1120     else()
1121       list(APPEND ${__currentvar} "${arg}")
1122     endif()
1123   endforeach()
1124   unset(__file_group_name)
1125   unset(__file_group_sources)
1126   unset(__currentvar)
1127 endmacro()
1128
1129 ocv_check_environment_variables(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
1130
1131 # this is a command for adding OpenCV performance tests to the module
1132 # ocv_add_perf_tests(<extra_dependencies>)
1133 function(ocv_add_perf_tests)
1134   ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
1135
1136   if(WINRT)
1137     set(OPENCV_DEBUG_POSTFIX "")
1138   endif()
1139
1140   set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
1141   if(BUILD_PERF_TESTS AND EXISTS "${perf_path}"
1142       AND (NOT DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST
1143           OR OPENCV_BUILD_PERF_TEST_MODULES_LIST STREQUAL "all"
1144           OR ";${OPENCV_BUILD_PERF_TEST_MODULES_LIST};" MATCHES ";${name};"
1145       )
1146   )
1147     __ocv_parse_test_sources(PERF ${ARGN})
1148
1149     # opencv_imgcodecs is required for imread/imwrite
1150     set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1151     ocv_check_dependencies(${perf_deps})
1152
1153     if(OCV_DEPENDENCIES_FOUND)
1154       set(the_target "opencv_perf_${name}")
1155       # project(${the_target})
1156
1157       if(NOT OPENCV_PERF_${the_module}_SOURCES)
1158         file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
1159         file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
1160         ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
1161         ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
1162         set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
1163       endif()
1164
1165       ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
1166
1167       if(NOT BUILD_opencv_world)
1168         get_native_precompiled_header(${the_target} perf_precomp.hpp)
1169       endif()
1170
1171       source_group("Src" FILES "${${the_target}_pch}")
1172       ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
1173       ocv_target_include_modules(${the_target} ${perf_deps})
1174       ocv_target_link_libraries(${the_target} PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
1175       add_dependencies(opencv_perf_tests ${the_target})
1176
1177       if(TARGET opencv_videoio_plugins)
1178         add_dependencies(${the_target} opencv_videoio_plugins)
1179       endif()
1180
1181       if(HAVE_HPX)
1182         message("Linking HPX to Perf test of module ${name}")
1183         ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
1184       endif()
1185
1186       set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1187       set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
1188         PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1189
1190       # Additional target properties
1191       set_target_properties(${the_target} PROPERTIES
1192         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1193         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1194       )
1195       if(ENABLE_SOLUTION_FOLDERS)
1196         set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
1197       endif()
1198
1199       if(WINRT)
1200         # removing APPCONTAINER from tests to run from console
1201         # look for detailed description inside of ocv_create_module macro above
1202         add_custom_command(TARGET "opencv_perf_${name}"
1203                            POST_BUILD
1204                            COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
1205       endif()
1206
1207       if(NOT BUILD_opencv_world)
1208         _ocv_add_precompiled_headers(${the_target})
1209       endif()
1210
1211       ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
1212       ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
1213                                "--perf_min_samples=1"
1214                                "--perf_force_samples=1"
1215                                "--perf_verify_sanity")
1216     else(OCV_DEPENDENCIES_FOUND)
1217       # TODO: warn about unsatisfied dependencies
1218     endif(OCV_DEPENDENCIES_FOUND)
1219     if(INSTALL_TESTS)
1220       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1221     endif()
1222   endif()
1223 endfunction()
1224
1225 # this is a command for adding OpenCV accuracy/regression tests to the module
1226 # ocv_add_accuracy_tests(<list of extra dependencies>)
1227 function(ocv_add_accuracy_tests)
1228   ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
1229
1230   set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
1231   if(BUILD_TESTS AND EXISTS "${test_path}"
1232       AND (NOT DEFINED OPENCV_BUILD_TEST_MODULES_LIST
1233           OR OPENCV_BUILD_TEST_MODULES_LIST STREQUAL "all"
1234           OR ";${OPENCV_BUILD_TEST_MODULES_LIST};" MATCHES ";${name};"
1235       )
1236   )
1237     __ocv_parse_test_sources(TEST ${ARGN})
1238
1239     # opencv_imgcodecs is required for imread/imwrite
1240     set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1241     ocv_check_dependencies(${test_deps})
1242     if(OCV_DEPENDENCIES_FOUND)
1243       set(the_target "opencv_test_${name}")
1244       # project(${the_target})
1245
1246       if(NOT OPENCV_TEST_${the_module}_SOURCES)
1247         file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
1248         file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
1249         ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
1250         ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
1251         set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
1252       endif()
1253
1254       if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
1255         list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
1256       endif()
1257       ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
1258
1259       if(NOT BUILD_opencv_world)
1260         get_native_precompiled_header(${the_target} test_precomp.hpp)
1261       endif()
1262
1263       source_group("Src" FILES "${${the_target}_pch}")
1264       ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
1265       ocv_target_include_modules(${the_target} ${test_deps})
1266       if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
1267         ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
1268       endif()
1269       ocv_target_link_libraries(${the_target} PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
1270       add_dependencies(opencv_tests ${the_target})
1271
1272       if(TARGET opencv_videoio_plugins)
1273         add_dependencies(${the_target} opencv_videoio_plugins)
1274       endif()
1275
1276       if(HAVE_HPX)
1277         message("Linking HPX to Perf test of module ${name}")
1278         ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
1279       endif()
1280
1281       set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1282       set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
1283         PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1284
1285       # Additional target properties
1286       set_target_properties(${the_target} PROPERTIES
1287         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1288         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1289       )
1290
1291       ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
1292
1293       if(ENABLE_SOLUTION_FOLDERS)
1294         set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
1295       endif()
1296
1297       if(OPENCV_TEST_BIGDATA)
1298         ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
1299       endif()
1300
1301       if(NOT BUILD_opencv_world)
1302         _ocv_add_precompiled_headers(${the_target})
1303       endif()
1304
1305       if(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
1306         target_compile_options(${the_target} PRIVATE "$<$<CONFIG:Release>:${OPENCV_TEST_EXTRA_CXX_FLAGS_Release}>")
1307       endif()
1308
1309       ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
1310     else(OCV_DEPENDENCIES_FOUND)
1311       # TODO: warn about unsatisfied dependencies
1312     endif(OCV_DEPENDENCIES_FOUND)
1313
1314     if(INSTALL_TESTS)
1315       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1316     endif()
1317   endif()
1318 endfunction()
1319
1320 function(ocv_add_samples)
1321   ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
1322
1323   set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1324   if(NOT EXISTS "${samples_path}")
1325     return()
1326   endif()
1327
1328   string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
1329
1330   if(BUILD_EXAMPLES)
1331     set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1332     ocv_check_dependencies(${samples_deps})
1333
1334     if(OCV_DEPENDENCIES_FOUND)
1335       file(GLOB sample_sources "${samples_path}/*.cpp")
1336
1337       foreach(source ${sample_sources})
1338         get_filename_component(name "${source}" NAME_WE)
1339         set(the_target "example_${module_id}_${name}")
1340
1341         ocv_add_executable(${the_target} "${source}")
1342         ocv_target_include_modules(${the_target} ${samples_deps})
1343         ocv_target_link_libraries(${the_target} PRIVATE ${samples_deps})
1344
1345         set_target_properties(${the_target} PROPERTIES
1346           PROJECT_LABEL "(sample) ${name}"
1347           LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1348         set_source_files_properties("${source}" PROPERTIES
1349           LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1350         if(ENABLE_SOLUTION_FOLDERS)
1351           set_target_properties(${the_target} PROPERTIES
1352             FOLDER "samples/${module_id}")
1353         endif()
1354         # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
1355         set(parent_target opencv_samples_${module_id})
1356         if(NOT TARGET ${parent_target})
1357           add_custom_target(${parent_target})
1358           add_dependencies(opencv_samples ${parent_target})
1359         endif()
1360         add_dependencies(${parent_target} ${the_target})
1361
1362         if(TARGET opencv_videoio_plugins)
1363           add_dependencies(${the_target} opencv_videoio_plugins)
1364         endif()
1365
1366         if(INSTALL_BIN_EXAMPLES)
1367           install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${module_id}" COMPONENT samples)
1368         endif()
1369       endforeach()
1370     endif()
1371   endif()
1372
1373   if(INSTALL_C_EXAMPLES)
1374     file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1375     foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
1376         IF( IS_DIRECTORY "${ITEM}" )
1377             LIST( APPEND sample_dirs "${ITEM}" )
1378         ELSE()
1379             LIST( APPEND sample_files "${ITEM}" )
1380         ENDIF()
1381     endforeach()
1382     install(FILES ${sample_files}
1383             DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1384             COMPONENT samples)
1385     install(DIRECTORY ${sample_dirs}
1386             DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1387             COMPONENT samples)
1388   endif()
1389 endfunction()