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