1 # Local variables (set for each module):
3 # name - short name in lower case i.e. core
4 # the_module - full name in lower case i.e. opencv_core
8 # OPENCV_MODULE_${the_module}_LOCATION
9 # OPENCV_MODULE_${the_module}_BINARY_DIR
10 # OPENCV_MODULE_${the_module}_DESCRIPTION
11 # OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
12 # OPENCV_MODULE_${the_module}_HEADERS
13 # OPENCV_MODULE_${the_module}_SOURCES
14 # OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
15 # OPENCV_MODULE_${the_module}_DEPS_TO_LINK - differs from above for world build only
16 # OPENCV_MODULE_${the_module}_DEPS_EXT - non-module dependencies
17 # OPENCV_MODULE_${the_module}_REQ_DEPS
18 # OPENCV_MODULE_${the_module}_OPT_DEPS
19 # OPENCV_MODULE_${the_module}_PRIVATE_REQ_DEPS
20 # OPENCV_MODULE_${the_module}_PRIVATE_OPT_DEPS
21 # OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD
22 # HAVE_${the_module} - for fast check of module availability
24 # To control the setup of the module you could also set:
25 # the_description - text to be used as current module description
26 # OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
27 # OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
28 # BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
30 # The verbose template for OpenCV module:
32 # ocv_add_module(modname <dependencies>)
33 # ocv_glob_module_sources(([EXCLUDE_CUDA] <extra sources&headers>)
34 # or glob them manually and ocv_set_module_sources(...)
35 # ocv_module_include_directories(<extra include directories>)
37 # <add extra link dependencies, compiler options, etc>
38 # ocv_add_precompiled_headers(${the_module})
39 # <add extra installation rules>
40 # ocv_add_accuracy_tests(<extra dependencies>)
41 # ocv_add_perf_tests(<extra dependencies>)
42 # ocv_add_samples(<extra dependencies>)
45 # If module have no "extra" then you can define it in one line:
47 # ocv_define_module(modname <dependencies>)
49 # clean flags for modules enabled on previous cmake run
50 # this is necessary to correctly handle modules removal
51 foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
53 unset(HAVE_${mod} CACHE)
55 unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
56 unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
57 unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
58 unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
59 unset(OPENCV_MODULE_${mod}_LINK_DEPS CACHE)
62 # clean modules info which needs to be recalculated
63 set(OPENCV_MODULES_PUBLIC "" CACHE INTERNAL "List of OpenCV modules marked for export")
64 set(OPENCV_MODULES_BUILD "" CACHE INTERNAL "List of OpenCV modules included into the build")
65 set(OPENCV_MODULES_DISABLED_USER "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
66 set(OPENCV_MODULES_DISABLED_AUTO "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
67 set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
68 unset(OPENCV_WORLD_MODULES CACHE)
70 # adds dependencies to OpenCV module
72 # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
74 # * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
75 macro(ocv_add_dependencies full_modname)
76 ocv_debug_message("ocv_add_dependencies(" ${full_modname} ${ARGN} ")")
77 #we don't clean the dependencies here to allow this macro several times for every module
78 foreach(d "REQUIRED" ${ARGN})
79 if(d STREQUAL "REQUIRED")
80 set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
81 elseif(d STREQUAL "OPTIONAL")
82 set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
83 elseif(d STREQUAL "PRIVATE_REQUIRED")
84 set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
85 elseif(d STREQUAL "PRIVATE_OPTIONAL")
86 set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
88 list(APPEND ${__depsvar} "${d}")
93 ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
94 ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
95 ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
96 ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
98 set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
99 CACHE INTERNAL "Required dependencies of ${full_modname} module")
100 set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
101 CACHE INTERNAL "Optional dependencies of ${full_modname} module")
102 set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
103 CACHE INTERNAL "Required private dependencies of ${full_modname} module")
104 set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
105 CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
108 # declare new OpenCV module in current folder
110 # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
112 # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cuda)
113 macro(ocv_add_module _name)
114 ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
115 string(TOLOWER "${_name}" name)
116 string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
117 set(the_module opencv_${name})
119 # the first pass - collect modules info, the second pass - create targets
120 if(OPENCV_INITIAL_PASS)
121 #guard agains redefinition
122 if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
123 message(FATAL_ERROR "Redefinition of the ${the_module} module.
124 at: ${CMAKE_CURRENT_SOURCE_DIR}
125 previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
129 if(NOT DEFINED the_description)
130 set(the_description "The ${name} OpenCV module")
133 if(NOT DEFINED BUILD_${the_module}_INIT)
134 set(BUILD_${the_module}_INIT ON)
137 # create option to enable/disable this module
138 option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
140 # remember the module details
141 set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
142 set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
144 set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
146 # parse list of dependencies
147 if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
148 set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
149 set(__ocv_argn__ ${ARGN})
150 list(REMOVE_AT __ocv_argn__ 0)
151 ocv_add_dependencies(${the_module} ${__ocv_argn__})
154 set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
155 ocv_add_dependencies(${the_module} ${ARGN})
156 if(BUILD_${the_module})
157 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
161 # add self to the world dependencies
162 if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS"
163 AND NOT OPENCV_PROCESSING_EXTRA_MODULES)
164 OR OPENCV_MODULE_IS_PART_OF_WORLD
166 set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
167 ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
169 set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
172 if(BUILD_${the_module})
173 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
175 set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
178 # TODO: add submodules if any
180 # stop processing of current file
183 set(OPENCV_MODULE_${the_module}_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
184 if(NOT BUILD_${the_module})
185 return() # extra protection from redefinition
187 if((NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD AND NOT ${the_module} STREQUAL opencv_world) OR NOT ${BUILD_opencv_world})
188 project(${the_module})
193 # excludes module from current configuration
194 macro(ocv_module_disable module)
195 set(__modname ${module})
196 if(NOT __modname MATCHES "^opencv_")
197 set(__modname opencv_${module})
199 list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
200 set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
201 set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
202 set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
203 if(BUILD_${__modname})
204 # touch variable controlling build of the module to suppress "unused variable" CMake warning
207 return() # leave the current folder
211 # collect modules from specified directories
212 # NB: must be called only once!
213 macro(ocv_glob_modules)
214 if(DEFINED OPENCV_INITIAL_PASS)
215 message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
217 set(__directories_observed "")
220 set(OPENCV_INITIAL_PASS ON)
221 set(OPENCV_PROCESSING_EXTRA_MODULES 0)
222 foreach(__path ${ARGN})
223 if("${__path}" STREQUAL "EXTRA")
224 set(OPENCV_PROCESSING_EXTRA_MODULES 1)
226 get_filename_component(__path "${__path}" ABSOLUTE)
228 list(FIND __directories_observed "${__path}" __pathIdx)
229 if(__pathIdx GREATER -1)
230 message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
232 list(APPEND __directories_observed "${__path}")
234 file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
236 list(SORT __ocvmodules)
237 foreach(mod ${__ocvmodules})
238 get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
239 if(EXISTS "${__modpath}/CMakeLists.txt")
241 list(FIND __directories_observed "${__modpath}" __pathIdx)
242 if(__pathIdx GREATER -1)
243 message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
245 list(APPEND __directories_observed "${__modpath}")
247 add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
252 ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
254 # resolve dependencies
255 __ocv_resolve_dependencies()
258 set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
259 set(OPENCV_INITIAL_PASS OFF)
260 if(${BUILD_opencv_world})
261 add_subdirectory("${OPENCV_MODULE_opencv_world_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/world")
262 foreach(m ${OPENCV_MODULES_BUILD})
263 if(NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD AND NOT ${m} STREQUAL opencv_world)
264 message(STATUS "Processing module ${m}...")
265 if(m MATCHES "^opencv_")
266 string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
267 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
269 message(WARNING "Check module name: ${m}")
270 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
275 foreach(m ${OPENCV_MODULES_BUILD})
276 if(m MATCHES "^opencv_")
277 string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
278 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
280 message(WARNING "Check module name: ${m}")
281 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
289 # disables OpenCV module with missing dependencies
290 function(__ocv_module_turn_off the_module)
291 list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
292 list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
293 list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
294 list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
295 set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
297 set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
298 set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
299 set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
302 # sort modules by dependencies
303 function(__ocv_sort_modules_by_deps __lst)
304 ocv_list_sort(${__lst})
305 set(${__lst}_ORDERED ${${__lst}} CACHE INTERNAL "")
307 foreach (m ${${__lst}})
308 list(LENGTH __result __lastindex)
309 set(__index ${__lastindex})
310 foreach (__d ${__result})
311 set(__deps "${OPENCV_MODULE_${__d}_DEPS}")
312 if(";${__deps};" MATCHES ";${m};")
313 list(FIND __result "${__d}" __i)
314 if(__i LESS "${__index}")
315 set(__index "${__i}")
319 if(__index STREQUAL __lastindex)
320 list(APPEND __result "${m}")
322 list(INSERT __result ${__index} "${m}")
325 set(${__lst} "${__result}" PARENT_SCOPE)
328 # resolve dependensies
329 function(__ocv_resolve_dependencies)
330 foreach(m ${OPENCV_MODULES_DISABLED_USER})
331 set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
333 foreach(m ${OPENCV_MODULES_BUILD})
334 set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
337 # disable MODULES with unresolved dependencies
341 foreach(m ${OPENCV_MODULES_BUILD})
342 set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
344 ocv_list_pop_front(__deps d)
345 string(TOLOWER "${d}" upper_d)
346 if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
347 if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
348 message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
349 __ocv_module_turn_off(${m})
353 message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
360 # message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
362 foreach(m ${OPENCV_MODULES_BUILD})
363 set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
364 foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
365 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
366 if(HAVE_${d} OR TARGET ${d})
367 list(APPEND deps_${m} ${d})
371 # message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
374 # propagate dependencies
378 foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
379 foreach(m ${OPENCV_MODULES_BUILD})
380 if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
381 foreach(d ${deps_${m2}})
382 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
383 # message(STATUS " Transfer dependency ${d} from ${m2} to ${m}")
384 list(APPEND deps_${m} ${d})
393 # process private deps
394 foreach(m ${OPENCV_MODULES_BUILD})
395 foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
396 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
397 list(APPEND deps_${m} ${d})
400 foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
401 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
402 if(HAVE_${d} OR TARGET ${d})
403 list(APPEND deps_${m} ${d})
409 ocv_list_sort(OPENCV_MODULES_BUILD)
411 foreach(m ${OPENCV_MODULES_BUILD})
412 # message(STATUS "FULL deps of ${m}: ${deps_${m}}")
413 set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
414 set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
415 ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
416 if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
417 list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
421 # reorder dependencies
422 foreach(m ${OPENCV_MODULES_BUILD})
423 __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
424 ocv_list_sort(OPENCV_MODULE_${m}_DEPS_EXT)
426 set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
429 if(BUILD_opencv_world)
430 if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
431 list(APPEND OPENCV_WORLD_MODULES ${m})
433 foreach(m2 ${OPENCV_MODULES_BUILD})
434 if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
435 if(";${LINK_DEPS};" MATCHES ";${m2};")
436 list(REMOVE_ITEM LINK_DEPS ${m2})
437 if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
438 list(APPEND LINK_DEPS opencv_world)
441 if(${m} STREQUAL opencv_world)
442 list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
448 set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
449 set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
450 set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
452 # message(STATUS " module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
453 # message(STATUS " module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
454 # message(STATUS " extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
458 __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
460 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} CACHE INTERNAL "List of OpenCV modules marked for export")
461 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} CACHE INTERNAL "List of OpenCV modules included into the build")
462 set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
463 set(OPENCV_WORLD_MODULES ${OPENCV_WORLD_MODULES} CACHE INTERNAL "List of OpenCV modules included into the world")
467 # setup include paths for the list of passed modules
468 macro(ocv_include_modules)
470 if(d MATCHES "^opencv_" AND HAVE_${d})
471 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
472 ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
474 elseif(EXISTS "${d}")
475 ocv_include_directories("${d}")
480 # setup include paths for the list of passed modules
481 macro(ocv_target_include_modules target)
483 if(d MATCHES "^opencv_" AND HAVE_${d})
484 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
485 ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
487 elseif(EXISTS "${d}")
488 ocv_target_include_directories(${target} "${d}")
493 # setup include paths for the list of passed modules and recursively add dependent modules
494 macro(ocv_target_include_modules_recurse target)
496 if(d MATCHES "^opencv_" AND HAVE_${d})
497 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
498 ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
500 if(OPENCV_MODULE_${d}_DEPS)
501 ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
503 elseif(EXISTS "${d}")
504 ocv_target_include_directories(${target} "${d}")
509 # setup include path for OpenCV headers for specified module
510 # ocv_module_include_directories(<extra include directories/extra include modules>)
511 macro(ocv_module_include_directories)
512 ocv_target_include_directories(${the_module}
513 "${OPENCV_MODULE_${the_module}_LOCATION}/include"
514 "${OPENCV_MODULE_${the_module}_LOCATION}/src"
515 "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
517 ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
521 # sets header and source files for the current module
522 # NB: all files specified as headers will be installed
524 # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
525 macro(ocv_set_module_sources)
526 ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
528 set(OPENCV_MODULE_${the_module}_HEADERS "")
529 set(OPENCV_MODULE_${the_module}_SOURCES "")
531 foreach(f "HEADERS" ${ARGN})
532 if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
533 set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
535 list(APPEND ${__filesvar} "${f}")
539 # the hacky way to embeed any files into the OpenCV without modification of its build system
540 if(COMMAND ocv_get_module_external_sources)
541 ocv_get_module_external_sources()
544 # use full paths for module to be independent from the module location
545 ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
547 set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
548 set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
551 # finds and sets headers and sources for the standard OpenCV module
553 # ocv_glob_module_sources([EXCLUDE_CUDA] <extra sources&headers in the same format as used in ocv_set_module_sources>)
554 macro(ocv_glob_module_sources)
555 ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
557 list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
558 if(NOT exclude_cuda EQUAL -1)
559 list(REMOVE_AT _argn ${exclude_cuda})
562 file(GLOB_RECURSE lib_srcs
563 "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
565 file(GLOB_RECURSE lib_int_hdrs
566 "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
567 "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
570 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
571 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
572 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
574 file(GLOB lib_hdrs_detail
575 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
576 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
579 file(GLOB_RECURSE lib_srcs_apple
580 "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
582 list(APPEND lib_srcs ${lib_srcs_apple})
585 ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
586 ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
588 set(lib_cuda_srcs "")
589 set(lib_cuda_hdrs "")
590 if(HAVE_CUDA AND exclude_cuda EQUAL -1)
591 file(GLOB lib_cuda_srcs
592 "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
594 file(GLOB lib_cuda_hdrs
595 "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
597 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
601 "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
604 set(OCL_NAME opencl_kernels_${name})
605 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
607 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
608 COMMAND ${CMAKE_COMMAND} -DMODULE_NAME="${name}" -DCL_DIR="${CMAKE_CURRENT_LIST_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
609 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
610 ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
611 ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
612 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
615 ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
616 SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
619 # creates OpenCV module in current folder
620 # creates new target, configures standard dependencies, compilers flags, install rules
622 # ocv_create_module(<extra link dependencies>)
623 # ocv_create_module()
624 macro(ocv_create_module)
625 ocv_debug_message("ocv_create_module(" ${ARGN} ")")
626 set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
627 if(${BUILD_opencv_world} AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
629 set(the_module_target opencv_world)
631 _ocv_create_module(${ARGN})
632 set(the_module_target ${the_module})
636 macro(_ocv_create_module)
637 # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
638 # be called at some point in the future. We can't look into the future, though,
639 # so this will have to do.
640 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
641 get_native_precompiled_header(${the_module} precomp.hpp)
644 ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
645 "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
646 ${${the_module}_pch})
647 if(NOT the_module STREQUAL opencv_ts)
648 set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
651 ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
652 ocv_target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
653 ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
655 ocv_target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
658 add_dependencies(opencv_modules ${the_module})
660 if(ENABLE_SOLUTION_FOLDERS)
661 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
664 set_target_properties(${the_module} PROPERTIES
665 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
666 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
667 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
668 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
669 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
673 # For dynamic link numbering convenions
675 # Android SDK build scripts can include only .so files into final .apk
676 # As result we should not set version properties for Android
677 set_target_properties(${the_module} PROPERTIES
678 VERSION ${OPENCV_LIBVERSION}
679 SOVERSION ${OPENCV_SOVERSION}
683 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
684 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
685 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
689 if(CMAKE_CROSSCOMPILING)
690 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
692 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
695 ocv_install_target(${the_module} EXPORT OpenCVModules
696 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
697 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
698 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
701 # only "public" headers need to be installed
702 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
703 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
704 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
705 if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
706 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
710 _ocv_add_precompiled_headers(${the_module})
713 # opencv precompiled headers macro (can add pch to modules and tests)
714 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
716 # ocv_add_precompiled_headers(${the_module})
717 macro(_ocv_add_precompiled_headers the_target)
718 ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
720 if("${the_target}" MATCHES "^opencv_test_.*$")
721 SET(pch_path "test/test_")
722 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
723 SET(pch_path "perf/perf_")
727 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
731 # short command for adding simple OpenCV module
732 # see ocv_add_module for argument details
734 # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
735 macro(ocv_define_module module_name)
736 ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
739 foreach(arg ${_argn})
740 if("${arg}" STREQUAL "EXCLUDE_CUDA")
741 set(exclude_cuda "${arg}")
742 list(REMOVE_ITEM _argn ${arg})
746 ocv_add_module(${module_name} ${_argn})
747 ocv_glob_module_sources(${exclude_cuda})
748 ocv_module_include_directories()
751 ocv_add_accuracy_tests()
756 # ensures that all passed modules are available
757 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
758 macro(ocv_check_dependencies)
759 set(OCV_DEPENDENCIES_FOUND TRUE)
761 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
762 set(OCV_DEPENDENCIES_FOUND FALSE)
768 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
769 macro(__ocv_parse_test_sources tests_type)
770 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
771 set(OPENCV_${tests_type}_${the_module}_DEPS "")
772 set(__file_group_name "")
773 set(__file_group_sources "")
774 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
775 if(arg STREQUAL "FILES")
776 set(__currentvar "__file_group_sources")
777 if(__file_group_name AND __file_group_sources)
778 source_group("${__file_group_name}" FILES ${__file_group_sources})
779 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
781 set(__file_group_name "")
782 set(__file_group_sources "")
783 elseif(arg STREQUAL "DEPENDS_ON")
784 set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
785 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
786 set(__file_group_name "${arg}")
788 list(APPEND ${__currentvar} "${arg}")
791 unset(__file_group_name)
792 unset(__file_group_sources)
796 # this is a command for adding OpenCV performance tests to the module
797 # ocv_add_perf_tests(<extra_dependencies>)
798 function(ocv_add_perf_tests)
799 ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
801 set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
802 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
803 __ocv_parse_test_sources(PERF ${ARGN})
805 # opencv_imgcodecs is required for imread/imwrite
806 set(perf_deps ${the_module} opencv_ts opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
807 ocv_check_dependencies(${perf_deps})
809 if(OCV_DEPENDENCIES_FOUND)
810 set(the_target "opencv_perf_${name}")
811 # project(${the_target})
813 if(NOT OPENCV_PERF_${the_module}_SOURCES)
814 file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
815 file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
816 ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
817 ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
818 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
821 if(NOT BUILD_opencv_world)
822 get_native_precompiled_header(${the_target} perf_precomp.hpp)
825 ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
826 ocv_target_include_modules(${the_target} ${perf_deps} "${perf_path}")
827 ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
828 add_dependencies(opencv_perf_tests ${the_target})
830 # Additional target properties
831 set_target_properties(${the_target} PROPERTIES
832 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
833 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
836 if(ENABLE_SOLUTION_FOLDERS)
837 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
840 if(NOT BUILD_opencv_world)
841 _ocv_add_precompiled_headers(${the_target})
843 else(OCV_DEPENDENCIES_FOUND)
844 # TODO: warn about unsatisfied dependencies
845 endif(OCV_DEPENDENCIES_FOUND)
847 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
852 # this is a command for adding OpenCV accuracy/regression tests to the module
853 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
854 function(ocv_add_accuracy_tests)
855 ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
857 set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
858 if(BUILD_TESTS AND EXISTS "${test_path}")
859 __ocv_parse_test_sources(TEST ${ARGN})
861 # opencv_imgcodecs is required for imread/imwrite
862 set(test_deps ${the_module} opencv_ts opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
863 ocv_check_dependencies(${test_deps})
864 if(OCV_DEPENDENCIES_FOUND)
865 set(the_target "opencv_test_${name}")
866 # project(${the_target})
868 if(NOT OPENCV_TEST_${the_module}_SOURCES)
869 file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
870 file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
871 ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
872 ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
873 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
876 if(NOT BUILD_opencv_world)
877 get_native_precompiled_header(${the_target} test_precomp.hpp)
880 ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
881 ocv_target_include_modules(${the_target} ${test_deps} "${test_path}")
882 ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
883 add_dependencies(opencv_tests ${the_target})
885 # Additional target properties
886 set_target_properties(${the_target} PROPERTIES
887 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
888 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
891 if(ENABLE_SOLUTION_FOLDERS)
892 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
896 get_target_property(LOC ${the_target} LOCATION)
897 add_test(${the_target} "${LOC}")
899 if(NOT BUILD_opencv_world)
900 _ocv_add_precompiled_headers(${the_target})
902 else(OCV_DEPENDENCIES_FOUND)
903 # TODO: warn about unsatisfied dependencies
904 endif(OCV_DEPENDENCIES_FOUND)
907 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
912 function(ocv_add_samples)
913 ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
915 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
916 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
918 if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
919 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
920 ocv_check_dependencies(${samples_deps})
922 if(OCV_DEPENDENCIES_FOUND)
923 file(GLOB sample_sources "${samples_path}/*.cpp")
925 foreach(source ${sample_sources})
926 get_filename_component(name "${source}" NAME_WE)
927 set(the_target "example_${module_id}_${name}")
929 ocv_add_executable(${the_target} "${source}")
930 ocv_target_include_modules(${the_target} ${samples_deps})
931 ocv_target_link_libraries(${the_target} ${samples_deps})
932 set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
934 if(ENABLE_SOLUTION_FOLDERS)
935 set_target_properties(${the_target} PROPERTIES
936 OUTPUT_NAME "${module_id}-example-${name}"
937 FOLDER "samples/${module_id}")
941 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
947 if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
948 file(GLOB sample_files "${samples_path}/*")
949 install(FILES ${sample_files}
950 DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
951 PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)