CLAHE Python bindings
[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   add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
474     "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
475
476   if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
477     target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
478     if (HAVE_CUDA)
479       target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
480     endif()
481     if(HAVE_OPENCL AND OPENCL_LIBRARIES)
482       target_link_libraries(${the_module} ${OPENCL_LIBRARIES})
483     endif()
484   endif()
485
486   add_dependencies(opencv_modules ${the_module})
487
488   if(ENABLE_SOLUTION_FOLDERS)
489     set_target_properties(${the_module} PROPERTIES FOLDER "modules")
490   endif()
491
492   set_target_properties(${the_module} PROPERTIES
493     OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
494     DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
495     ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
496     LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
497     RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
498     INSTALL_NAME_DIR lib
499   )
500
501   # For dynamic link numbering convenions
502   if(NOT ANDROID)
503     # Android SDK build scripts can include only .so files into final .apk
504     # As result we should not set version properties for Android
505     set_target_properties(${the_module} PROPERTIES
506       VERSION ${OPENCV_LIBVERSION}
507       SOVERSION ${OPENCV_SOVERSION}
508     )
509   endif()
510
511   if(BUILD_SHARED_LIBS)
512     if(MSVC)
513       set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
514     else()
515       add_definitions(-DCVAPI_EXPORTS)
516     endif()
517   endif()
518
519   if(MSVC)
520     if(CMAKE_CROSSCOMPILING)
521       set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
522     endif()
523     set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
524   endif()
525
526   install(TARGETS ${the_module}
527     RUNTIME DESTINATION bin COMPONENT main
528     LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
529     ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
530     )
531
532   # only "public" headers need to be installed
533   if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
534     foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
535       string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
536       if(hdr2 MATCHES "^(opencv2/.*)/[^/]+.h(..)?$")
537         install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT main)
538       endif()
539     endforeach()
540   endif()
541 endmacro()
542
543 # opencv precompiled headers macro (can add pch to modules and tests)
544 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
545 # Usage:
546 # ocv_add_precompiled_headers(${the_module})
547 macro(ocv_add_precompiled_headers the_target)
548   if("${the_target}" MATCHES "^opencv_test_.*$")
549     SET(pch_path "test/test_")
550   elseif("${the_target}" MATCHES "^opencv_perf_.*$")
551     SET(pch_path "perf/perf_")
552   else()
553     SET(pch_path "src/")
554   endif()
555   ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
556   unset(pch_path)
557 endmacro()
558
559 # short command for adding simple OpenCV module
560 # see ocv_add_module for argument details
561 # Usage:
562 # ocv_define_module(module_name  [INTERNAL] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
563 macro(ocv_define_module module_name)
564   ocv_add_module(${module_name} ${ARGN})
565   ocv_module_include_directories()
566   ocv_glob_module_sources()
567   ocv_create_module()
568   ocv_add_precompiled_headers(${the_module})
569
570   ocv_add_accuracy_tests()
571   ocv_add_perf_tests()
572 endmacro()
573
574 # ensures that all passed modules are available
575 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
576 macro(ocv_check_dependencies)
577   set(OCV_DEPENDENCIES_FOUND TRUE)
578   foreach(d ${ARGN})
579     if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
580       set(OCV_DEPENDENCIES_FOUND FALSE)
581       break()
582     endif()
583   endforeach()
584 endmacro()
585
586 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
587 macro(__ocv_parse_test_sources tests_type)
588   set(OPENCV_${tests_type}_${the_module}_SOURCES "")
589   set(OPENCV_${tests_type}_${the_module}_DEPS "")
590   set(__file_group_name "")
591   set(__file_group_sources "")
592   foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
593     if(arg STREQUAL "FILES")
594       set(__currentvar "__file_group_sources")
595       if(__file_group_name AND __file_group_sources)
596         source_group("${__file_group_name}" FILES ${__file_group_sources})
597         list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
598       endif()
599       set(__file_group_name "")
600       set(__file_group_sources "")
601     elseif(arg STREQUAL "DEPENDS_ON")
602       set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
603     elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
604       set(__file_group_name "${arg}")
605     else()
606       list(APPEND ${__currentvar} "${arg}")
607     endif()
608   endforeach()
609   unset(__file_group_name)
610   unset(__file_group_sources)
611   unset(__currentvar)
612 endmacro()
613
614 # this is a command for adding OpenCV performance tests to the module
615 # ocv_add_perf_tests(<extra_dependencies>)
616 function(ocv_add_perf_tests)
617   set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
618   if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
619     __ocv_parse_test_sources(PERF ${ARGN})
620
621     # opencv_highgui is required for imread/imwrite
622     set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
623     ocv_check_dependencies(${perf_deps})
624
625     if(OCV_DEPENDENCIES_FOUND)
626       set(the_target "opencv_perf_${name}")
627       # project(${the_target})
628
629       ocv_module_include_directories(${perf_deps} "${perf_path}")
630
631       if(NOT OPENCV_PERF_${the_module}_SOURCES)
632         file(GLOB perf_srcs "${perf_path}/*.cpp")
633         file(GLOB perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
634         source_group("Src" FILES ${perf_srcs})
635         source_group("Include" FILES ${perf_hdrs})
636         set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
637       endif()
638
639       add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES})
640       target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
641       add_dependencies(opencv_perf_tests ${the_target})
642
643       # Additional target properties
644       set_target_properties(${the_target} PROPERTIES
645         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
646         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
647       )
648
649       if(ENABLE_SOLUTION_FOLDERS)
650         set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
651       endif()
652
653       ocv_add_precompiled_headers(${the_target})
654
655     else(OCV_DEPENDENCIES_FOUND)
656       # TODO: warn about unsatisfied dependencies
657     endif(OCV_DEPENDENCIES_FOUND)
658   endif()
659 endfunction()
660
661 # this is a command for adding OpenCV accuracy/regression tests to the module
662 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
663 function(ocv_add_accuracy_tests)
664   set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
665   ocv_check_dependencies(${test_deps})
666   if(BUILD_TESTS AND EXISTS "${test_path}")
667     __ocv_parse_test_sources(TEST ${ARGN})
668
669     # opencv_highgui is required for imread/imwrite
670     set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
671     ocv_check_dependencies(${test_deps})
672
673     if(OCV_DEPENDENCIES_FOUND)
674       set(the_target "opencv_test_${name}")
675       # project(${the_target})
676
677       ocv_module_include_directories(${test_deps} "${test_path}")
678
679       if(NOT OPENCV_TEST_${the_module}_SOURCES)
680         file(GLOB test_srcs "${test_path}/*.cpp")
681         file(GLOB test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
682         source_group("Src" FILES ${test_srcs})
683         source_group("Include" FILES ${test_hdrs})
684         set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
685       endif()
686
687       add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES})
688       target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
689       add_dependencies(opencv_tests ${the_target})
690
691       # Additional target properties
692       set_target_properties(${the_target} PROPERTIES
693         DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
694         RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
695       )
696
697       if(ENABLE_SOLUTION_FOLDERS)
698         set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
699       endif()
700
701       enable_testing()
702       get_target_property(LOC ${the_target} LOCATION)
703       add_test(${the_target} "${LOC}")
704
705       ocv_add_precompiled_headers(${the_target})
706     else(OCV_DEPENDENCIES_FOUND)
707       # TODO: warn about unsatisfied dependencies
708     endif(OCV_DEPENDENCIES_FOUND)
709   endif()
710 endfunction()
711
712 # internal macro; finds all link dependencies of the module
713 # should be used at the end of CMake processing
714 macro(__ocv_track_module_link_dependencies the_module optkind)
715   set(${the_module}_MODULE_DEPS_${optkind}   "")
716   set(${the_module}_EXTRA_DEPS_${optkind}    "")
717
718   get_target_property(__module_type ${the_module} TYPE)
719   if(__module_type STREQUAL "STATIC_LIBRARY")
720     #in case of static library we have to inherit its dependencies (in right order!!!)
721     if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
722       ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
723     endif()
724
725     set(__resolved_deps "")
726     set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
727     set(__has_cycle FALSE)
728
729     while(__mod_depends)
730       list(GET __mod_depends 0 __dep)
731       list(REMOVE_AT __mod_depends 0)
732       if(__dep STREQUAL the_module)
733         set(__has_cycle TRUE)
734       else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
735         ocv_regex_escape(__rdep "${__dep}")
736         if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
737           #all dependencies of this module are already resolved
738           list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
739         else()
740           get_target_property(__module_type ${__dep} TYPE)
741           if(__module_type STREQUAL "STATIC_LIBRARY")
742             if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
743               ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
744             endif()
745             list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
746             list(APPEND __resolved_deps "${__dep}")
747           elseif(NOT __module_type)
748             list(APPEND  ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
749           endif()
750         endif()
751       #else()
752        # get_target_property(__dep_location "${__dep}" LOCATION)
753       endif()
754     endwhile()
755
756     ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
757     #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
758     ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
759     #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
760
761     if(__has_cycle)
762       # not sure if it can work
763       list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
764     endif()
765
766     unset(__dep_location)
767     unset(__mod_depends)
768     unset(__resolved_deps)
769     unset(__has_cycle)
770     unset(__rdep)
771   endif()#STATIC_LIBRARY
772   unset(__module_type)
773
774   #message("${the_module}_MODULE_DEPS_${optkind}")
775   #message("       ${${the_module}_MODULE_DEPS_${optkind}}")
776   #message("       ${OPENCV_MODULE_${the_module}_DEPS}")
777   #message("")
778   #message("${the_module}_EXTRA_DEPS_${optkind}")
779   #message("       ${${the_module}_EXTRA_DEPS_${optkind}}")
780   #message("")
781 endmacro()
782
783 # creates lists of build dependencies needed for external projects
784 macro(ocv_track_build_dependencies)
785   foreach(m ${OPENCV_MODULES_BUILD})
786     __ocv_track_module_link_dependencies("${m}" OPT)
787     __ocv_track_module_link_dependencies("${m}" DBG)
788   endforeach()
789 endmacro()