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}_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
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}
27 # The verbose template for OpenCV module:
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>)
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>)
41 # If module have no "extra" then you can define it in one line:
43 # ocv_define_module(modname <dependencies>)
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})
49 unset(HAVE_${mod} CACHE)
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)
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")
64 # adds dependencies to OpenCV module
66 # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
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)
81 list(APPEND ${__depsvar} "${d}")
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)
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")
101 # declare new OpenCV module in current folder
103 # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
105 # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cuda)
106 macro(ocv_add_module _name)
107 string(TOLOWER "${_name}" name)
108 string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
109 set(the_module opencv_${name})
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}
121 if(NOT DEFINED the_description)
122 set(the_description "The ${name} OpenCV module")
125 if(NOT DEFINED BUILD_${the_module}_INIT)
126 set(BUILD_${the_module}_INIT ON)
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})
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")
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__})
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")
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})
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")
159 set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
162 # TODO: add submodules if any
164 # stop processing of current file
166 else(OPENCV_INITIAL_PASS)
167 if(NOT BUILD_${the_module})
168 return() # extra protection from redefinition
170 project(${the_module})
171 endif(OPENCV_INITIAL_PASS)
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})
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
188 return() # leave the current folder
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.")
198 set(__directories_observed "")
201 set(OPENCV_INITIAL_PASS ON)
202 foreach(__path ${ARGN})
203 get_filename_component(__path "${__path}" ABSOLUTE)
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.")
209 list(APPEND __directories_observed "${__path}")
211 file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
213 list(SORT __ocvmodules)
214 foreach(mod ${__ocvmodules})
215 get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
216 if(EXISTS "${__modpath}/CMakeLists.txt")
218 list(FIND __directories_observed "${__modpath}" __pathIdx)
219 if(__pathIdx GREATER -1)
220 message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
222 list(APPEND __directories_observed "${__modpath}")
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)
232 add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
238 ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
240 # resolve dependencies
241 __ocv_resolve_dependencies()
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}")
251 message(WARNING "Check module name: ${m}")
252 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
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")
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 "")
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 "")
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}")
289 if(__index STREQUAL __lastindex)
290 list(APPEND __result "${m}")
292 list(INSERT __result ${__index} "${m}")
295 set(${__lst} "${__result}" PARENT_SCOPE)
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")
303 foreach(m ${OPENCV_MODULES_BUILD})
304 set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
307 # disable MODULES with unresolved dependencies
311 foreach(m ${OPENCV_MODULES_BUILD})
312 set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_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})
323 message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
330 # message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
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})
341 # message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
344 # propagate dependencies
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})
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})
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})
379 ocv_list_sort(OPENCV_MODULES_BUILD)
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})
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)
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")
399 # message(STATUS " module deps: ${OPENCV_MODULE_${m}_DEPS}")
400 # message(STATUS " extra deps: ${OPENCV_MODULE_${m}_DEPS_EXT}")
403 __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
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")
411 # setup include paths for the list of passed modules
412 macro(ocv_include_modules)
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")
418 elseif(EXISTS "${d}")
419 ocv_include_directories("${d}")
424 # setup include paths for the list of passed modules and recursively add dependent modules
425 macro(ocv_include_modules_recurse)
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")
431 if(OPENCV_MODULE_${d}_DEPS)
432 ocv_include_modules(${OPENCV_MODULE_${d}_DEPS})
434 elseif(EXISTS "${d}")
435 ocv_include_directories("${d}")
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
447 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
451 # sets header and source files for the current module
452 # NB: all files specified as headers will be installed
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 "")
459 foreach(f "HEADERS" ${ARGN})
460 if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
461 set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
463 list(APPEND ${__filesvar} "${f}")
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()
472 # use full paths for module to be independent from the module location
473 ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
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}")
479 # finds and sets headers and sources for the standard OpenCV module
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/*.hpp" "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h")
486 file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h")
488 ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
489 ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
491 file(GLOB lib_cuda_srcs "src/cuda/*.cu")
493 set(lib_cuda_hdrs "")
494 if(HAVE_CUDA AND lib_cuda_srcs)
495 ocv_include_directories(${CUDA_INCLUDE_DIRS})
496 file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
498 ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
499 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
502 file(GLOB cl_kernels "src/opencl/*.cl")
504 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
505 string(REGEX REPLACE "opencv_" "" the_module_barename "${the_module}")
507 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp"
508 COMMAND ${CMAKE_COMMAND} -DMODULE_NAME="${the_module_barename}" -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
509 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
510 ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
511 ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
512 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
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})
519 # creates OpenCV module in current folder
520 # creates new target, configures standard dependencies, compilers flags, install rules
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)
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 if(NOT the_module STREQUAL opencv_ts)
536 set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
539 if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
540 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
541 target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS})
542 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
544 target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
548 add_dependencies(opencv_modules ${the_module})
550 if(ENABLE_SOLUTION_FOLDERS)
551 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
554 set_target_properties(${the_module} PROPERTIES
555 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
556 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
557 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
558 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
559 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
563 # For dynamic link numbering convenions
565 # Android SDK build scripts can include only .so files into final .apk
566 # As result we should not set version properties for Android
567 set_target_properties(${the_module} PROPERTIES
568 VERSION ${OPENCV_LIBVERSION}
569 SOVERSION ${OPENCV_SOVERSION}
573 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
574 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
575 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
579 if(CMAKE_CROSSCOMPILING)
580 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
582 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
585 ocv_install_target(${the_module} EXPORT OpenCVModules
586 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
587 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
588 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
591 # only "public" headers need to be installed
592 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
593 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
594 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
595 if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
596 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
602 # opencv precompiled headers macro (can add pch to modules and tests)
603 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
605 # ocv_add_precompiled_headers(${the_module})
606 macro(ocv_add_precompiled_headers the_target)
607 if("${the_target}" MATCHES "^opencv_test_.*$")
608 SET(pch_path "test/test_")
609 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
610 SET(pch_path "perf/perf_")
614 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
618 # short command for adding simple OpenCV module
619 # see ocv_add_module for argument details
621 # ocv_define_module(module_name [INTERNAL] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
622 macro(ocv_define_module module_name)
623 ocv_add_module(${module_name} ${ARGN})
624 ocv_module_include_directories()
625 ocv_glob_module_sources()
627 ocv_add_precompiled_headers(${the_module})
629 ocv_add_accuracy_tests()
634 # ensures that all passed modules are available
635 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
636 macro(ocv_check_dependencies)
637 set(OCV_DEPENDENCIES_FOUND TRUE)
639 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
640 set(OCV_DEPENDENCIES_FOUND FALSE)
646 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
647 macro(__ocv_parse_test_sources tests_type)
648 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
649 set(OPENCV_${tests_type}_${the_module}_DEPS "")
650 set(__file_group_name "")
651 set(__file_group_sources "")
652 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
653 if(arg STREQUAL "FILES")
654 set(__currentvar "__file_group_sources")
655 if(__file_group_name AND __file_group_sources)
656 source_group("${__file_group_name}" FILES ${__file_group_sources})
657 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
659 set(__file_group_name "")
660 set(__file_group_sources "")
661 elseif(arg STREQUAL "DEPENDS_ON")
662 set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
663 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
664 set(__file_group_name "${arg}")
666 list(APPEND ${__currentvar} "${arg}")
669 unset(__file_group_name)
670 unset(__file_group_sources)
674 # this is a command for adding OpenCV performance tests to the module
675 # ocv_add_perf_tests(<extra_dependencies>)
676 function(ocv_add_perf_tests)
677 set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
678 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
679 __ocv_parse_test_sources(PERF ${ARGN})
681 # opencv_highgui is required for imread/imwrite
682 set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
683 ocv_check_dependencies(${perf_deps})
685 if(OCV_DEPENDENCIES_FOUND)
686 set(the_target "opencv_perf_${name}")
687 # project(${the_target})
689 ocv_module_include_directories(${perf_deps} "${perf_path}")
691 if(NOT OPENCV_PERF_${the_module}_SOURCES)
692 file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
693 file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
694 ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
695 ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
696 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
699 get_native_precompiled_header(${the_target} perf_precomp.hpp)
701 add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
702 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
703 add_dependencies(opencv_perf_tests ${the_target})
705 # Additional target properties
706 set_target_properties(${the_target} PROPERTIES
707 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
708 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
711 if(ENABLE_SOLUTION_FOLDERS)
712 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
715 ocv_add_precompiled_headers(${the_target})
717 else(OCV_DEPENDENCIES_FOUND)
718 # TODO: warn about unsatisfied dependencies
719 endif(OCV_DEPENDENCIES_FOUND)
721 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
726 # this is a command for adding OpenCV accuracy/regression tests to the module
727 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
728 function(ocv_add_accuracy_tests)
729 set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
730 ocv_check_dependencies(${test_deps})
731 if(BUILD_TESTS AND EXISTS "${test_path}")
732 __ocv_parse_test_sources(TEST ${ARGN})
734 # opencv_highgui is required for imread/imwrite
735 set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
736 ocv_check_dependencies(${test_deps})
738 if(OCV_DEPENDENCIES_FOUND)
739 set(the_target "opencv_test_${name}")
740 # project(${the_target})
742 ocv_module_include_directories(${test_deps} "${test_path}")
744 if(NOT OPENCV_TEST_${the_module}_SOURCES)
745 file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
746 file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
747 ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
748 ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
749 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
752 get_native_precompiled_header(${the_target} test_precomp.hpp)
754 add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
755 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
756 add_dependencies(opencv_tests ${the_target})
758 # Additional target properties
759 set_target_properties(${the_target} PROPERTIES
760 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
761 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
764 if(ENABLE_SOLUTION_FOLDERS)
765 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
769 get_target_property(LOC ${the_target} LOCATION)
770 add_test(${the_target} "${LOC}")
772 ocv_add_precompiled_headers(${the_target})
773 else(OCV_DEPENDENCIES_FOUND)
774 # TODO: warn about unsatisfied dependencies
775 endif(OCV_DEPENDENCIES_FOUND)
778 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
783 function(ocv_add_samples)
784 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
785 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
787 if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
788 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_highgui ${ARGN})
789 ocv_check_dependencies(${samples_deps})
791 if(OCV_DEPENDENCIES_FOUND)
792 file(GLOB sample_sources "${samples_path}/*.cpp")
793 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS})
795 foreach(source ${sample_sources})
796 get_filename_component(name "${source}" NAME_WE)
797 set(the_target "example_${module_id}_${name}")
799 add_executable(${the_target} "${source}")
800 target_link_libraries(${the_target} ${samples_deps})
802 set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
804 if(ENABLE_SOLUTION_FOLDERS)
805 set_target_properties(${the_target} PROPERTIES
806 OUTPUT_NAME "${module_id}-example-${name}"
807 FOLDER "samples/${module_id}")
811 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
817 if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
818 file(GLOB sample_files "${samples_path}/*")
819 install(FILES ${sample_files}
820 DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
821 PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
825 # internal macro; finds all link dependencies of the module
826 # should be used at the end of CMake processing
827 macro(__ocv_track_module_link_dependencies the_module optkind)
828 set(${the_module}_MODULE_DEPS_${optkind} "")
829 set(${the_module}_EXTRA_DEPS_${optkind} "")
831 get_target_property(__module_type ${the_module} TYPE)
832 if(__module_type STREQUAL "STATIC_LIBRARY")
833 #in case of static library we have to inherit its dependencies (in right order!!!)
834 if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
835 ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
838 set(__resolved_deps "")
839 set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
840 set(__has_cycle FALSE)
843 list(GET __mod_depends 0 __dep)
844 list(REMOVE_AT __mod_depends 0)
845 if(__dep STREQUAL the_module)
846 set(__has_cycle TRUE)
847 else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
848 ocv_regex_escape(__rdep "${__dep}")
849 if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
850 #all dependencies of this module are already resolved
851 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
853 get_target_property(__module_type ${__dep} TYPE)
854 if(__module_type STREQUAL "STATIC_LIBRARY")
855 if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
856 ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
858 list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
859 list(APPEND __resolved_deps "${__dep}")
860 elseif(NOT __module_type)
861 list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
865 # get_target_property(__dep_location "${__dep}" LOCATION)
869 ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
870 #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
871 ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
872 #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
875 # not sure if it can work
876 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
879 unset(__dep_location)
881 unset(__resolved_deps)
884 endif()#STATIC_LIBRARY
887 #message("${the_module}_MODULE_DEPS_${optkind}")
888 #message(" ${${the_module}_MODULE_DEPS_${optkind}}")
889 #message(" ${OPENCV_MODULE_${the_module}_DEPS}")
891 #message("${the_module}_EXTRA_DEPS_${optkind}")
892 #message(" ${${the_module}_EXTRA_DEPS_${optkind}}")
896 # creates lists of build dependencies needed for external projects
897 macro(ocv_track_build_dependencies)
898 foreach(m ${OPENCV_MODULES_BUILD})
899 __ocv_track_module_link_dependencies("${m}" OPT)
900 __ocv_track_module_link_dependencies("${m}" DBG)