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