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