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