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 if (exclude_cuda EQUAL -1)
589 file(GLOB lib_cuda_srcs
590 "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
593 set(lib_cuda_hdrs "")
595 ocv_include_directories(${CUDA_INCLUDE_DIRS})
596 file(GLOB lib_cuda_hdrs
597 "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
600 ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
601 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
605 set(lib_cuda_srcs "")
606 set(lib_cuda_hdrs "")
610 "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
613 set(OCL_NAME opencl_kernels_${name})
614 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
616 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
617 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"
618 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
619 ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
620 ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
621 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
624 ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
625 SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
628 # creates OpenCV module in current folder
629 # creates new target, configures standard dependencies, compilers flags, install rules
631 # ocv_create_module(<extra link dependencies>)
632 # ocv_create_module()
633 macro(ocv_create_module)
634 ocv_debug_message("ocv_create_module(" ${ARGN} ")")
635 set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
636 if(${BUILD_opencv_world} AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
638 set(the_module_target opencv_world)
640 _ocv_create_module(${ARGN})
641 set(the_module_target ${the_module})
645 macro(_ocv_create_module)
646 # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
647 # be called at some point in the future. We can't look into the future, though,
648 # so this will have to do.
649 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
650 get_native_precompiled_header(${the_module} precomp.hpp)
653 ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
654 "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
655 ${${the_module}_pch})
656 if(NOT the_module STREQUAL opencv_ts)
657 set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
660 ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
661 ocv_target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
662 ocv_target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
664 ocv_target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
667 add_dependencies(opencv_modules ${the_module})
669 if(ENABLE_SOLUTION_FOLDERS)
670 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
673 set_target_properties(${the_module} PROPERTIES
674 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
675 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
676 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
677 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
678 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
682 # For dynamic link numbering convenions
684 # Android SDK build scripts can include only .so files into final .apk
685 # As result we should not set version properties for Android
686 set_target_properties(${the_module} PROPERTIES
687 VERSION ${OPENCV_LIBVERSION}
688 SOVERSION ${OPENCV_SOVERSION}
692 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
693 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
694 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
698 if(CMAKE_CROSSCOMPILING)
699 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
701 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
704 ocv_install_target(${the_module} EXPORT OpenCVModules
705 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
706 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
707 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
710 # only "public" headers need to be installed
711 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
712 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
713 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
714 if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
715 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
719 _ocv_add_precompiled_headers(${the_module})
722 # opencv precompiled headers macro (can add pch to modules and tests)
723 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
725 # ocv_add_precompiled_headers(${the_module})
726 macro(_ocv_add_precompiled_headers the_target)
727 ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
729 if("${the_target}" MATCHES "^opencv_test_.*$")
730 SET(pch_path "test/test_")
731 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
732 SET(pch_path "perf/perf_")
736 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
740 # short command for adding simple OpenCV module
741 # see ocv_add_module for argument details
743 # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
744 macro(ocv_define_module module_name)
745 ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
748 foreach(arg ${_argn})
749 if("${arg}" STREQUAL "EXCLUDE_CUDA")
750 set(exclude_cuda "${arg}")
751 list(REMOVE_ITEM _argn ${arg})
755 ocv_add_module(${module_name} ${_argn})
756 ocv_glob_module_sources(${exclude_cuda})
757 ocv_module_include_directories()
760 ocv_add_accuracy_tests()
765 # ensures that all passed modules are available
766 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
767 macro(ocv_check_dependencies)
768 set(OCV_DEPENDENCIES_FOUND TRUE)
770 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
771 set(OCV_DEPENDENCIES_FOUND FALSE)
777 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
778 macro(__ocv_parse_test_sources tests_type)
779 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
780 set(OPENCV_${tests_type}_${the_module}_DEPS "")
781 set(__file_group_name "")
782 set(__file_group_sources "")
783 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
784 if(arg STREQUAL "FILES")
785 set(__currentvar "__file_group_sources")
786 if(__file_group_name AND __file_group_sources)
787 source_group("${__file_group_name}" FILES ${__file_group_sources})
788 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
790 set(__file_group_name "")
791 set(__file_group_sources "")
792 elseif(arg STREQUAL "DEPENDS_ON")
793 set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
794 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
795 set(__file_group_name "${arg}")
797 list(APPEND ${__currentvar} "${arg}")
800 unset(__file_group_name)
801 unset(__file_group_sources)
805 # this is a command for adding OpenCV performance tests to the module
806 # ocv_add_perf_tests(<extra_dependencies>)
807 function(ocv_add_perf_tests)
808 ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
810 set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
811 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
812 __ocv_parse_test_sources(PERF ${ARGN})
814 # opencv_imgcodecs is required for imread/imwrite
815 set(perf_deps ${the_module} opencv_ts opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
816 ocv_check_dependencies(${perf_deps})
818 if(OCV_DEPENDENCIES_FOUND)
819 set(the_target "opencv_perf_${name}")
820 # project(${the_target})
822 if(NOT OPENCV_PERF_${the_module}_SOURCES)
823 file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
824 file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
825 ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
826 ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
827 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
830 if(NOT BUILD_opencv_world)
831 get_native_precompiled_header(${the_target} perf_precomp.hpp)
834 ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
835 ocv_target_include_modules(${the_target} ${perf_deps} "${perf_path}")
836 ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
837 add_dependencies(opencv_perf_tests ${the_target})
839 # Additional target properties
840 set_target_properties(${the_target} PROPERTIES
841 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
842 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
845 if(ENABLE_SOLUTION_FOLDERS)
846 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
849 if(NOT BUILD_opencv_world)
850 _ocv_add_precompiled_headers(${the_target})
852 else(OCV_DEPENDENCIES_FOUND)
853 # TODO: warn about unsatisfied dependencies
854 endif(OCV_DEPENDENCIES_FOUND)
856 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
861 # this is a command for adding OpenCV accuracy/regression tests to the module
862 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
863 function(ocv_add_accuracy_tests)
864 ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
866 set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
867 if(BUILD_TESTS AND EXISTS "${test_path}")
868 __ocv_parse_test_sources(TEST ${ARGN})
870 # opencv_imgcodecs is required for imread/imwrite
871 set(test_deps ${the_module} opencv_ts opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
872 ocv_check_dependencies(${test_deps})
873 if(OCV_DEPENDENCIES_FOUND)
874 set(the_target "opencv_test_${name}")
875 # project(${the_target})
877 if(NOT OPENCV_TEST_${the_module}_SOURCES)
878 file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
879 file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
880 ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
881 ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
882 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
885 if(NOT BUILD_opencv_world)
886 get_native_precompiled_header(${the_target} test_precomp.hpp)
889 ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
890 ocv_target_include_modules(${the_target} ${test_deps} "${test_path}")
891 ocv_target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
892 add_dependencies(opencv_tests ${the_target})
894 # Additional target properties
895 set_target_properties(${the_target} PROPERTIES
896 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
897 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
900 if(ENABLE_SOLUTION_FOLDERS)
901 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
905 get_target_property(LOC ${the_target} LOCATION)
906 add_test(${the_target} "${LOC}")
908 if(NOT BUILD_opencv_world)
909 _ocv_add_precompiled_headers(${the_target})
911 else(OCV_DEPENDENCIES_FOUND)
912 # TODO: warn about unsatisfied dependencies
913 endif(OCV_DEPENDENCIES_FOUND)
916 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
921 function(ocv_add_samples)
922 ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
924 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
925 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
927 if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
928 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
929 ocv_check_dependencies(${samples_deps})
931 if(OCV_DEPENDENCIES_FOUND)
932 file(GLOB sample_sources "${samples_path}/*.cpp")
934 foreach(source ${sample_sources})
935 get_filename_component(name "${source}" NAME_WE)
936 set(the_target "example_${module_id}_${name}")
938 ocv_add_executable(${the_target} "${source}")
939 ocv_target_include_modules(${the_target} ${samples_deps})
940 ocv_target_link_libraries(${the_target} ${samples_deps})
941 set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
943 if(ENABLE_SOLUTION_FOLDERS)
944 set_target_properties(${the_target} PROPERTIES
945 OUTPUT_NAME "${module_id}-example-${name}"
946 FOLDER "samples/${module_id}")
950 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
956 if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
957 file(GLOB sample_files "${samples_path}/*")
958 install(FILES ${sample_files}
959 DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
960 PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)