Merge pull request #2523 from ilya-lavrenov:tapi_filters
[profile/ivi/opencv.git] / cmake / OpenCVModule.cmake
1 # Local variables (set for each module):
2 #
3 # name       - short name in lower case i.e. core
4 # the_module - full name in lower case i.e. opencv_core
5
6 # Global variables:
7 #
8 # OPENCV_MODULE_${the_module}_LOCATION
9 # OPENCV_MODULE_${the_module}_DESCRIPTION
10 # OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
11 # OPENCV_MODULE_${the_module}_HEADERS
12 # OPENCV_MODULE_${the_module}_SOURCES
13 # OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
14 # OPENCV_MODULE_${the_module}_DEPS_EXT - non-module dependencies
15 # OPENCV_MODULE_${the_module}_REQ_DEPS
16 # OPENCV_MODULE_${the_module}_OPT_DEPS
17 # OPENCV_MODULE_${the_module}_PRIVATE_REQ_DEPS
18 # OPENCV_MODULE_${the_module}_PRIVATE_OPT_DEPS
19 # HAVE_${the_module} - for fast check of module availability
20
21 # To control the setup of the module you could also set:
22 # the_description - text to be used as current module description
23 # OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
24 # OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
25 # BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
26
27 # The verbose template for OpenCV module:
28 #
29 #   ocv_add_module(modname <dependencies>)
30 #   ocv_glob_module_sources(([EXCLUDE_CUDA] <extra sources&headers>)
31 #                          or glob them manually and ocv_set_module_sources(...)
32 #   ocv_module_include_directories(<extra include directories>)
33 #   ocv_create_module()
34 #   <add extra link dependencies, compiler options, etc>
35 #   ocv_add_precompiled_headers(${the_module})
36 #   <add extra installation rules>
37 #   ocv_add_accuracy_tests(<extra dependencies>)
38 #   ocv_add_perf_tests(<extra dependencies>)
39 #   ocv_add_samples(<extra dependencies>)
40 #
41 #
42 # If module have no "extra" then you can define it in one line:
43 #
44 #   ocv_define_module(modname <dependencies>)
45
46 # clean flags for modules enabled on previous cmake run
47 # this is necessary to correctly handle modules removal
48 foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
49   if(HAVE_${mod})
50     unset(HAVE_${mod} CACHE)
51   endif()
52   unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
53   unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
54   unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
55   unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
56 endforeach()
57
58 # clean modules info which needs to be recalculated
59 set(OPENCV_MODULES_PUBLIC         "" CACHE INTERNAL "List of OpenCV modules marked for export")
60 set(OPENCV_MODULES_BUILD          "" CACHE INTERNAL "List of OpenCV modules included into the build")
61 set(OPENCV_MODULES_DISABLED_USER  "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
62 set(OPENCV_MODULES_DISABLED_AUTO  "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
63 set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
64
65 # adds dependencies to OpenCV module
66 # Usage:
67 #   add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
68 # Notes:
69 # * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
70 macro(ocv_add_dependencies full_modname)
71   #we don't clean the dependencies here to allow this macro several times for every module
72   foreach(d "REQUIRED" ${ARGN})
73     if(d STREQUAL "REQUIRED")
74       set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
75     elseif(d STREQUAL "OPTIONAL")
76       set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
77     elseif(d STREQUAL "PRIVATE_REQUIRED")
78       set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
79     elseif(d STREQUAL "PRIVATE_OPTIONAL")
80       set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
81     else()
82       list(APPEND ${__depsvar} "${d}")
83     endif()
84   endforeach()
85   unset(__depsvar)
86
87   ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
88   ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
89   ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
90   ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
91
92   set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
93     CACHE INTERNAL "Required dependencies of ${full_modname} module")
94   set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
95     CACHE INTERNAL "Optional dependencies of ${full_modname} module")
96   set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
97     CACHE INTERNAL "Required private dependencies of ${full_modname} module")
98   set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
99     CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
100 endmacro()
101
102 # declare new OpenCV module in current folder
103 # Usage:
104 #   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
105 # Example:
106 #   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cuda)
107 macro(ocv_add_module _name)
108   string(TOLOWER "${_name}" name)
109   string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
110   set(the_module opencv_${name})
111
112   # the first pass - collect modules info, the second pass - create targets
113   if(OPENCV_INITIAL_PASS)
114     #guard agains redefinition
115     if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
116       message(FATAL_ERROR "Redefinition of the ${the_module} module.
117   at:                    ${CMAKE_CURRENT_SOURCE_DIR}
118   previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
119 ")
120     endif()
121
122     if(NOT DEFINED the_description)
123       set(the_description "The ${name} OpenCV module")
124     endif()
125
126     if(NOT DEFINED BUILD_${the_module}_INIT)
127       set(BUILD_${the_module}_INIT ON)
128     endif()
129
130     # create option to enable/disable this module
131     option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
132
133     # remember the module details
134     set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
135     set(OPENCV_MODULE_${the_module}_LOCATION    "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
136
137     # parse list of dependencies
138     if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
139       set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
140       set(__ocv_argn__ ${ARGN})
141       list(REMOVE_AT __ocv_argn__ 0)
142       ocv_add_dependencies(${the_module} ${__ocv_argn__})
143       unset(__ocv_argn__)
144     else()
145       set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
146       ocv_add_dependencies(${the_module} ${ARGN})
147       if(BUILD_${the_module})
148         set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
149       endif()
150     endif()
151
152     # add self to the world dependencies
153     if(NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS" OR OPENCV_MODULE_IS_PART_OF_WORLD)
154       ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
155     endif()
156
157     if(BUILD_${the_module})
158       set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
159     else()
160       set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
161     endif()
162
163     # TODO: add submodules if any
164
165     # stop processing of current file
166     return()
167   else(OPENCV_INITIAL_PASS)
168     if(NOT BUILD_${the_module})
169       return() # extra protection from redefinition
170     endif()
171     project(${the_module})
172   endif(OPENCV_INITIAL_PASS)
173 endmacro()
174
175 # excludes module from current configuration
176 macro(ocv_module_disable module)
177   set(__modname ${module})
178   if(NOT __modname MATCHES "^opencv_")
179     set(__modname opencv_${module})
180   endif()
181   list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
182   set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
183   set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
184   set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
185   if(BUILD_${__modname})
186     # touch variable controlling build of the module to suppress "unused variable" CMake warning
187   endif()
188   unset(__modname)
189   return() # leave the current folder
190 endmacro()
191
192
193 # collect modules from specified directories
194 # NB: must be called only once!
195 macro(ocv_glob_modules)
196   if(DEFINED OPENCV_INITIAL_PASS)
197     message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
198   endif()
199   set(__directories_observed "")
200
201   # collect modules
202   set(OPENCV_INITIAL_PASS ON)
203   foreach(__path ${ARGN})
204     get_filename_component(__path "${__path}" ABSOLUTE)
205
206     list(FIND __directories_observed "${__path}" __pathIdx)
207     if(__pathIdx GREATER -1)
208       message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
209     endif()
210     list(APPEND __directories_observed "${__path}")
211
212     file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
213     if(__ocvmodules)
214       list(SORT __ocvmodules)
215       foreach(mod ${__ocvmodules})
216         get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
217         if(EXISTS "${__modpath}/CMakeLists.txt")
218
219           list(FIND __directories_observed "${__modpath}" __pathIdx)
220           if(__pathIdx GREATER -1)
221             message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
222           endif()
223           list(APPEND __directories_observed "${__modpath}")
224
225           if(OCV_MODULE_RELOCATE_ON_INITIAL_PASS)
226             file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
227             file(COPY "${__modpath}/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
228             add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
229             if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
230               set(OPENCV_MODULE_opencv_${mod}_LOCATION "${__modpath}" CACHE PATH "" FORCE)
231             endif()
232           else()
233             add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
234           endif()
235         endif()
236       endforeach()
237     endif()
238   endforeach()
239   ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
240
241   # resolve dependencies
242   __ocv_resolve_dependencies()
243
244   # create modules
245   set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
246   set(OPENCV_INITIAL_PASS OFF)
247   foreach(m ${OPENCV_MODULES_BUILD})
248     if(m MATCHES "^opencv_")
249       string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
250       add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
251     else()
252       message(WARNING "Check module name: ${m}")
253       add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
254     endif()
255   endforeach()
256   unset(__shortname)
257 endmacro()
258
259
260 # disables OpenCV module with missing dependencies
261 function(__ocv_module_turn_off the_module)
262   list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
263   list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
264   list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
265   list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
266   set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
267
268   set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
269   set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
270   set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
271 endfunction()
272
273 # sort modules by dependencies
274 function(__ocv_sort_modules_by_deps __lst)
275   ocv_list_sort(${__lst})
276   set(${__lst}_ORDERED ${${__lst}} CACHE INTERNAL "")
277   set(__result "")
278   foreach (m ${${__lst}})
279     list(LENGTH __result __lastindex)
280     set(__index ${__lastindex})
281     foreach (__d ${__result})
282       set(__deps "${OPENCV_MODULE_${__d}_DEPS}")
283       if(";${__deps};" MATCHES ";${m};")
284         list(FIND __result "${__d}" __i)
285         if(__i LESS "${__index}")
286           set(__index "${__i}")
287         endif()
288       endif()
289     endforeach()
290     if(__index STREQUAL __lastindex)
291       list(APPEND __result "${m}")
292     else()
293       list(INSERT __result ${__index} "${m}")
294     endif()
295   endforeach()
296   set(${__lst} "${__result}" PARENT_SCOPE)
297 endfunction()
298
299 # resolve dependensies
300 function(__ocv_resolve_dependencies)
301   foreach(m ${OPENCV_MODULES_DISABLED_USER})
302     set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
303   endforeach()
304   foreach(m ${OPENCV_MODULES_BUILD})
305     set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
306   endforeach()
307
308   # disable MODULES with unresolved dependencies
309   set(has_changes ON)
310   while(has_changes)
311     set(has_changes OFF)
312     foreach(m ${OPENCV_MODULES_BUILD})
313       set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
314       while(__deps)
315         ocv_list_pop_front(__deps d)
316         string(TOLOWER "${d}" upper_d)
317         if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
318           if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
319             message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
320             __ocv_module_turn_off(${m})
321             set(has_changes ON)
322             break()
323           else()
324             message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
325           endif()
326         endif()
327       endwhile()
328     endforeach()
329   endwhile()
330
331 #  message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
332
333   foreach(m ${OPENCV_MODULES_BUILD})
334     set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
335     foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
336       if(NOT (";${deps_${m}};" MATCHES ";${d};"))
337         if(HAVE_${d} OR TARGET ${d})
338           list(APPEND deps_${m} ${d})
339         endif()
340       endif()
341     endforeach()
342 #    message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
343   endforeach()
344
345   # propagate dependencies
346   set(has_changes ON)
347   while(has_changes)
348     set(has_changes OFF)
349     foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
350       foreach(m ${OPENCV_MODULES_BUILD})
351         if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
352           foreach(d ${deps_${m2}})
353             if(NOT (";${deps_${m}};" MATCHES ";${d};"))
354 #              message(STATUS "  Transfer dependency ${d} from ${m2} to ${m}")
355               list(APPEND deps_${m} ${d})
356               set(has_changes ON)
357             endif()
358           endforeach()
359         endif()
360       endforeach()
361     endforeach()
362   endwhile()
363
364   # process private deps
365   foreach(m ${OPENCV_MODULES_BUILD})
366     foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
367       if(NOT (";${deps_${m}};" MATCHES ";${d};"))
368         list(APPEND deps_${m} ${d})
369       endif()
370     endforeach()
371     foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
372       if(NOT (";${deps_${m}};" MATCHES ";${d};"))
373         if(HAVE_${d} OR TARGET ${d})
374           list(APPEND deps_${m} ${d})
375         endif()
376       endif()
377     endforeach()
378   endforeach()
379
380   ocv_list_sort(OPENCV_MODULES_BUILD)
381
382   foreach(m ${OPENCV_MODULES_BUILD})
383 #    message(STATUS "FULL deps of ${m}: ${deps_${m}}")
384     set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
385     set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
386     ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
387     if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
388       list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
389     endif()
390   endforeach()
391
392   # reorder dependencies
393   foreach(m ${OPENCV_MODULES_BUILD})
394     __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
395     ocv_list_sort(OPENCV_MODULE_${m}_DEPS_EXT)
396
397     set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
398     set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
399
400 #    message(STATUS "  module deps: ${OPENCV_MODULE_${m}_DEPS}")
401 #    message(STATUS "  extra deps: ${OPENCV_MODULE_${m}_DEPS_EXT}")
402   endforeach()
403
404   __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
405
406   set(OPENCV_MODULES_PUBLIC        ${OPENCV_MODULES_PUBLIC}        CACHE INTERNAL "List of OpenCV modules marked for export")
407   set(OPENCV_MODULES_BUILD         ${OPENCV_MODULES_BUILD}         CACHE INTERNAL "List of OpenCV modules included into the build")
408   set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
409 endfunction()
410
411
412 # setup include paths for the list of passed modules
413 macro(ocv_include_modules)
414   foreach(d ${ARGN})
415     if(d MATCHES "^opencv_" AND HAVE_${d})
416       if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
417         ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
418       endif()
419     elseif(EXISTS "${d}")
420       ocv_include_directories("${d}")
421     endif()
422   endforeach()
423 endmacro()
424
425 # setup include paths for the list of passed modules and recursively add dependent modules
426 macro(ocv_include_modules_recurse)
427   foreach(d ${ARGN})
428     if(d MATCHES "^opencv_" AND HAVE_${d})
429       if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
430         ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
431       endif()
432       if(OPENCV_MODULE_${d}_DEPS)
433         ocv_include_modules(${OPENCV_MODULE_${d}_DEPS})
434       endif()
435     elseif(EXISTS "${d}")
436       ocv_include_directories("${d}")
437     endif()
438   endforeach()
439 endmacro()
440
441 # setup include path for OpenCV headers for specified module
442 # ocv_module_include_directories(<extra include directories/extra include modules>)
443 macro(ocv_module_include_directories)
444   ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/include"
445                           "${OPENCV_MODULE_${the_module}_LOCATION}/src"
446                           "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
447                           )
448   ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
449 endmacro()
450
451
452 # sets header and source files for the current module
453 # NB: all files specified as headers will be installed
454 # Usage:
455 # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
456 macro(ocv_set_module_sources)
457   set(OPENCV_MODULE_${the_module}_HEADERS "")
458   set(OPENCV_MODULE_${the_module}_SOURCES "")
459
460   foreach(f "HEADERS" ${ARGN})
461     if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
462       set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
463     else()
464       list(APPEND ${__filesvar} "${f}")
465     endif()
466   endforeach()
467
468   # the hacky way to embeed any files into the OpenCV without modification of its build system
469   if(COMMAND ocv_get_module_external_sources)
470     ocv_get_module_external_sources()
471   endif()
472
473   # use full paths for module to be independent from the module location
474   ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
475
476   set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
477   set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
478 endmacro()
479
480 # finds and sets headers and sources for the standard OpenCV module
481 # Usage:
482 # ocv_glob_module_sources([EXCLUDE_CUDA] <extra sources&headers in the same format as used in ocv_set_module_sources>)
483 macro(ocv_glob_module_sources)
484   set(_argn ${ARGN})
485   list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
486   if(NOT exclude_cuda EQUAL -1)
487     list(REMOVE_AT _argn ${exclude_cuda})
488   endif()
489
490   file(GLOB_RECURSE lib_srcs "src/*.cpp")
491   file(GLOB_RECURSE lib_int_hdrs "src/*.hpp" "src/*.h")
492   file(GLOB lib_hdrs     "include/opencv2/*.hpp" "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h")
493   file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h")
494   file(GLOB_RECURSE lib_srcs_apple "src/*.mm")
495   if (APPLE)
496     list(APPEND lib_srcs ${lib_srcs_apple})
497   endif()
498
499   ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
500   ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
501
502   if (exclude_cuda EQUAL -1)
503     file(GLOB lib_cuda_srcs "src/cuda/*.cu")
504     set(cuda_objs "")
505     set(lib_cuda_hdrs "")
506     if(HAVE_CUDA)
507       ocv_include_directories(${CUDA_INCLUDE_DIRS})
508       file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
509
510       ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
511       source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
512     endif()
513   else()
514     set(cuda_objs "")
515     set(lib_cuda_srcs "")
516     set(lib_cuda_hdrs "")
517   endif()
518
519   file(GLOB cl_kernels "src/opencl/*.cl")
520   if(cl_kernels)
521     ocv_include_directories(${OPENCL_INCLUDE_DIRS})
522     string(REGEX REPLACE "opencv_" "" the_module_barename "${the_module}")
523     add_custom_command(
524       OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp"
525       COMMAND ${CMAKE_COMMAND} -DMODULE_NAME="${the_module_barename}" -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
526       DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
527     ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
528     ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
529     list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
530   endif()
531
532   ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
533                          SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
534 endmacro()
535
536 # creates OpenCV module in current folder
537 # creates new target, configures standard dependencies, compilers flags, install rules
538 # Usage:
539 #   ocv_create_module(<extra link dependencies>)
540 #   ocv_create_module(SKIP_LINK)
541 macro(ocv_create_module)
542   # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
543   # be called at some point in the future. We can't look into the future, though,
544   # so this will have to do.
545   if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp")
546     get_native_precompiled_header(${the_module} precomp.hpp)
547   endif()
548
549   add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
550     "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
551     ${${the_module}_pch})
552   if(NOT the_module STREQUAL opencv_ts)
553     set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
554   endif()
555
556   if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
557     target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
558     target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS})
559     target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
560     if (HAVE_CUDA)
561       target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
562     endif()
563   endif()
564
565   add_dependencies(opencv_modules ${the_module})
566
567   if(ENABLE_SOLUTION_FOLDERS)
568     set_target_properties(${the_module} PROPERTIES FOLDER "modules")
569   endif()
570
571   set_target_properties(${the_module} PROPERTIES
572     OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
573     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
574     ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
575     LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
576     RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
577     INSTALL_NAME_DIR lib
578   )
579
580   # For dynamic link numbering convenions
581   if(NOT ANDROID)
582     # Android SDK build scripts can include only .so files into final .apk
583     # As result we should not set version properties for Android
584     set_target_properties(${the_module} PROPERTIES
585       VERSION ${OPENCV_LIBVERSION}
586       SOVERSION ${OPENCV_SOVERSION}
587     )
588   endif()
589
590   if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
591       OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
592     set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
593   endif()
594
595   if(MSVC)
596     if(CMAKE_CROSSCOMPILING)
597       set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
598     endif()
599     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
600   endif()
601
602   ocv_install_target(${the_module} EXPORT OpenCVModules
603     RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
604     LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
605     ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
606     )
607
608   # only "public" headers need to be installed
609   if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
610     foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
611       string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
612       if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
613         install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
614       endif()
615     endforeach()
616   endif()
617 endmacro()
618
619 # opencv precompiled headers macro (can add pch to modules and tests)
620 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
621 # Usage:
622 # ocv_add_precompiled_headers(${the_module})
623 macro(ocv_add_precompiled_headers the_target)
624   if("${the_target}" MATCHES "^opencv_test_.*$")
625     SET(pch_path "test/test_")
626   elseif("${the_target}" MATCHES "^opencv_perf_.*$")
627     SET(pch_path "perf/perf_")
628   else()
629     SET(pch_path "src/")
630   endif()
631   ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
632   unset(pch_path)
633 endmacro()
634
635 # short command for adding simple OpenCV module
636 # see ocv_add_module for argument details
637 # Usage:
638 # ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
639 macro(ocv_define_module module_name)
640   set(_argn ${ARGN})
641   set(exclude_cuda "")
642   foreach(arg ${_argn})
643     if("${arg}" STREQUAL "EXCLUDE_CUDA")
644       set(exclude_cuda "${arg}")
645       list(REMOVE_ITEM _argn ${arg})
646     endif()
647   endforeach()
648
649   ocv_add_module(${module_name} ${_argn})
650   ocv_module_include_directories()
651   ocv_glob_module_sources(${exclude_cuda})
652   ocv_create_module()
653   ocv_add_precompiled_headers(${the_module})
654
655   ocv_add_accuracy_tests()
656   ocv_add_perf_tests()
657   ocv_add_samples()
658 endmacro()
659
660 # ensures that all passed modules are available
661 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
662 macro(ocv_check_dependencies)
663   set(OCV_DEPENDENCIES_FOUND TRUE)
664   foreach(d ${ARGN})
665     if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
666       set(OCV_DEPENDENCIES_FOUND FALSE)
667       break()
668     endif()
669   endforeach()
670 endmacro()
671
672 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
673 macro(__ocv_parse_test_sources tests_type)
674   set(OPENCV_${tests_type}_${the_module}_SOURCES "")
675   set(OPENCV_${tests_type}_${the_module}_DEPS "")
676   set(__file_group_name "")
677   set(__file_group_sources "")
678   foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
679     if(arg STREQUAL "FILES")
680       set(__currentvar "__file_group_sources")
681       if(__file_group_name AND __file_group_sources)
682         source_group("${__file_group_name}" FILES ${__file_group_sources})
683         list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
684       endif()
685       set(__file_group_name "")
686       set(__file_group_sources "")
687     elseif(arg STREQUAL "DEPENDS_ON")
688       set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
689     elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
690       set(__file_group_name "${arg}")
691     else()
692       list(APPEND ${__currentvar} "${arg}")
693     endif()
694   endforeach()
695   unset(__file_group_name)
696   unset(__file_group_sources)
697   unset(__currentvar)
698 endmacro()
699
700 # this is a command for adding OpenCV performance tests to the module
701 # ocv_add_perf_tests(<extra_dependencies>)
702 function(ocv_add_perf_tests)
703   set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
704   if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
705     __ocv_parse_test_sources(PERF ${ARGN})
706
707     # opencv_highgui is required for imread/imwrite
708     set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
709     ocv_check_dependencies(${perf_deps})
710
711     if(OCV_DEPENDENCIES_FOUND)
712       set(the_target "opencv_perf_${name}")
713       # project(${the_target})
714
715       ocv_module_include_directories(${perf_deps} "${perf_path}")
716
717       if(NOT OPENCV_PERF_${the_module}_SOURCES)
718         file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
719         file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
720         ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
721         ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
722         set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
723       endif()
724
725       get_native_precompiled_header(${the_target} perf_precomp.hpp)
726
727       add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
728       target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
729       add_dependencies(opencv_perf_tests ${the_target})
730
731       # Additional target properties
732       set_target_properties(${the_target} PROPERTIES
733         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
734         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
735       )
736
737       if(ENABLE_SOLUTION_FOLDERS)
738         set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
739       endif()
740
741       ocv_add_precompiled_headers(${the_target})
742
743     else(OCV_DEPENDENCIES_FOUND)
744       # TODO: warn about unsatisfied dependencies
745     endif(OCV_DEPENDENCIES_FOUND)
746     if(INSTALL_TESTS)
747       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
748     endif()
749   endif()
750 endfunction()
751
752 # this is a command for adding OpenCV accuracy/regression tests to the module
753 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
754 function(ocv_add_accuracy_tests)
755   set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
756   ocv_check_dependencies(${test_deps})
757   if(BUILD_TESTS AND EXISTS "${test_path}")
758     __ocv_parse_test_sources(TEST ${ARGN})
759
760     # opencv_highgui is required for imread/imwrite
761     set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
762     ocv_check_dependencies(${test_deps})
763
764     if(OCV_DEPENDENCIES_FOUND)
765       set(the_target "opencv_test_${name}")
766       # project(${the_target})
767
768       ocv_module_include_directories(${test_deps} "${test_path}")
769
770       if(NOT OPENCV_TEST_${the_module}_SOURCES)
771         file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
772         file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
773         ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
774         ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
775         set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
776       endif()
777
778       get_native_precompiled_header(${the_target} test_precomp.hpp)
779       add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
780
781       target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
782       add_dependencies(opencv_tests ${the_target})
783
784       # Additional target properties
785       set_target_properties(${the_target} PROPERTIES
786         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
787         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
788       )
789
790       if(ENABLE_SOLUTION_FOLDERS)
791         set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
792       endif()
793
794       enable_testing()
795       get_target_property(LOC ${the_target} LOCATION)
796       add_test(${the_target} "${LOC}")
797
798       ocv_add_precompiled_headers(${the_target})
799     else(OCV_DEPENDENCIES_FOUND)
800       # TODO: warn about unsatisfied dependencies
801     endif(OCV_DEPENDENCIES_FOUND)
802
803     if(INSTALL_TESTS)
804       install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
805     endif()
806   endif()
807 endfunction()
808
809 function(ocv_add_samples)
810   set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
811   string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
812
813   if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
814     set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_highgui ${ARGN})
815     ocv_check_dependencies(${samples_deps})
816
817     if(OCV_DEPENDENCIES_FOUND)
818       file(GLOB sample_sources "${samples_path}/*.cpp")
819       ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS})
820
821       foreach(source ${sample_sources})
822         get_filename_component(name "${source}" NAME_WE)
823         set(the_target "example_${module_id}_${name}")
824
825         add_executable(${the_target} "${source}")
826         target_link_libraries(${the_target} ${samples_deps})
827
828         set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
829
830         if(ENABLE_SOLUTION_FOLDERS)
831           set_target_properties(${the_target} PROPERTIES
832             OUTPUT_NAME "${module_id}-example-${name}"
833             FOLDER "samples/${module_id}")
834         endif()
835
836         if(WIN32)
837           install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
838         endif()
839       endforeach()
840     endif()
841   endif()
842
843   if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
844     file(GLOB sample_files "${samples_path}/*")
845     install(FILES ${sample_files}
846             DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
847             PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
848   endif()
849 endfunction()
850
851 # internal macro; finds all link dependencies of the module
852 # should be used at the end of CMake processing
853 macro(__ocv_track_module_link_dependencies the_module optkind)
854   set(${the_module}_MODULE_DEPS_${optkind}   "")
855   set(${the_module}_EXTRA_DEPS_${optkind}    "")
856
857   get_target_property(__module_type ${the_module} TYPE)
858   if(__module_type STREQUAL "STATIC_LIBRARY")
859     #in case of static library we have to inherit its dependencies (in right order!!!)
860     if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
861       ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
862     endif()
863
864     set(__resolved_deps "")
865     set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
866     set(__has_cycle FALSE)
867
868     while(__mod_depends)
869       list(GET __mod_depends 0 __dep)
870       list(REMOVE_AT __mod_depends 0)
871       if(__dep STREQUAL the_module)
872         set(__has_cycle TRUE)
873       else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
874         ocv_regex_escape(__rdep "${__dep}")
875         if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
876           #all dependencies of this module are already resolved
877           list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
878         else()
879           get_target_property(__module_type ${__dep} TYPE)
880           if(__module_type STREQUAL "STATIC_LIBRARY")
881             if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
882               ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
883             endif()
884             list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
885             list(APPEND __resolved_deps "${__dep}")
886           elseif(NOT __module_type)
887             list(APPEND  ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
888           endif()
889         endif()
890       #else()
891        # get_target_property(__dep_location "${__dep}" LOCATION)
892       endif()
893     endwhile()
894
895     ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
896     #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
897     ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
898     #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
899
900     if(__has_cycle)
901       # not sure if it can work
902       list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
903     endif()
904
905     unset(__dep_location)
906     unset(__mod_depends)
907     unset(__resolved_deps)
908     unset(__has_cycle)
909     unset(__rdep)
910   endif()#STATIC_LIBRARY
911   unset(__module_type)
912
913   #message("${the_module}_MODULE_DEPS_${optkind}")
914   #message("       ${${the_module}_MODULE_DEPS_${optkind}}")
915   #message("       ${OPENCV_MODULE_${the_module}_DEPS}")
916   #message("")
917   #message("${the_module}_EXTRA_DEPS_${optkind}")
918   #message("       ${${the_module}_EXTRA_DEPS_${optkind}}")
919   #message("")
920 endmacro()
921
922 # creates lists of build dependencies needed for external projects
923 macro(ocv_track_build_dependencies)
924   foreach(m ${OPENCV_MODULES_BUILD})
925     __ocv_track_module_link_dependencies("${m}" OPT)
926     __ocv_track_module_link_dependencies("${m}" DBG)
927   endforeach()
928 endmacro()