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 category 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 category 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")
487 file(GLOB_RECURSE lib_srcs_apple "src/*.mm")
489 list(APPEND lib_srcs ${lib_srcs_apple})
492 ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
493 ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
495 file(GLOB lib_cuda_srcs "src/cuda/*.cu")
497 set(lib_cuda_hdrs "")
498 if(HAVE_CUDA AND lib_cuda_srcs)
499 ocv_include_directories(${CUDA_INCLUDE_DIRS})
500 file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
502 ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
503 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
506 file(GLOB cl_kernels "src/opencl/*.cl")
508 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
509 string(REGEX REPLACE "opencv_" "" the_module_barename "${the_module}")
511 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp"
512 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"
513 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
514 ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
515 ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
516 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
519 ocv_set_module_sources(${ARGN} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
520 SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
523 # creates OpenCV module in current folder
524 # creates new target, configures standard dependencies, compilers flags, install rules
526 # ocv_create_module(<extra link dependencies>)
527 # ocv_create_module(SKIP_LINK)
528 macro(ocv_create_module)
529 # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
530 # be called at some point in the future. We can't look into the future, though,
531 # so this will have to do.
532 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp")
533 get_native_precompiled_header(${the_module} precomp.hpp)
536 add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
537 "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
538 ${${the_module}_pch})
539 if(NOT the_module STREQUAL opencv_ts)
540 set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
543 if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
544 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
545 target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS})
546 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
548 target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
552 add_dependencies(opencv_modules ${the_module})
554 if(ENABLE_SOLUTION_FOLDERS)
555 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
558 set_target_properties(${the_module} PROPERTIES
559 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
560 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
561 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
562 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
563 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
567 # For dynamic link numbering convenions
569 # Android SDK build scripts can include only .so files into final .apk
570 # As result we should not set version properties for Android
571 set_target_properties(${the_module} PROPERTIES
572 VERSION ${OPENCV_LIBVERSION}
573 SOVERSION ${OPENCV_SOVERSION}
577 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
578 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
579 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
583 if(CMAKE_CROSSCOMPILING)
584 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
586 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
589 ocv_install_target(${the_module} EXPORT OpenCVModules
590 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
591 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
592 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
595 # only "public" headers need to be installed
596 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
597 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
598 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
599 if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
600 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
606 # opencv precompiled headers macro (can add pch to modules and tests)
607 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
609 # ocv_add_precompiled_headers(${the_module})
610 macro(ocv_add_precompiled_headers the_target)
611 if("${the_target}" MATCHES "^opencv_test_.*$")
612 SET(pch_path "test/test_")
613 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
614 SET(pch_path "perf/perf_")
618 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
622 # short command for adding simple OpenCV module
623 # see ocv_add_module for argument details
625 # ocv_define_module(module_name [INTERNAL] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
626 macro(ocv_define_module module_name)
627 ocv_add_module(${module_name} ${ARGN})
628 ocv_module_include_directories()
629 ocv_glob_module_sources()
631 ocv_add_precompiled_headers(${the_module})
633 ocv_add_accuracy_tests()
638 # ensures that all passed modules are available
639 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
640 macro(ocv_check_dependencies)
641 set(OCV_DEPENDENCIES_FOUND TRUE)
643 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
644 set(OCV_DEPENDENCIES_FOUND FALSE)
650 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
651 macro(__ocv_parse_test_sources tests_type)
652 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
653 set(OPENCV_${tests_type}_${the_module}_DEPS "")
654 set(__file_group_name "")
655 set(__file_group_sources "")
656 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
657 if(arg STREQUAL "FILES")
658 set(__currentvar "__file_group_sources")
659 if(__file_group_name AND __file_group_sources)
660 source_group("${__file_group_name}" FILES ${__file_group_sources})
661 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
663 set(__file_group_name "")
664 set(__file_group_sources "")
665 elseif(arg STREQUAL "DEPENDS_ON")
666 set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
667 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
668 set(__file_group_name "${arg}")
670 list(APPEND ${__currentvar} "${arg}")
673 unset(__file_group_name)
674 unset(__file_group_sources)
678 # this is a command for adding OpenCV performance tests to the module
679 # ocv_add_perf_tests(<extra_dependencies>)
680 function(ocv_add_perf_tests)
681 set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
682 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
683 __ocv_parse_test_sources(PERF ${ARGN})
685 # opencv_highgui is required for imread/imwrite
686 set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
687 ocv_check_dependencies(${perf_deps})
689 if(OCV_DEPENDENCIES_FOUND)
690 set(the_target "opencv_perf_${name}")
691 # project(${the_target})
693 ocv_module_include_directories(${perf_deps} "${perf_path}")
695 if(NOT OPENCV_PERF_${the_module}_SOURCES)
696 file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
697 file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
698 ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
699 ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
700 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
703 get_native_precompiled_header(${the_target} perf_precomp.hpp)
705 add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
706 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
707 add_dependencies(opencv_perf_tests ${the_target})
709 # Additional target properties
710 set_target_properties(${the_target} PROPERTIES
711 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
712 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
715 if(ENABLE_SOLUTION_FOLDERS)
716 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
719 ocv_add_precompiled_headers(${the_target})
721 else(OCV_DEPENDENCIES_FOUND)
722 # TODO: warn about unsatisfied dependencies
723 endif(OCV_DEPENDENCIES_FOUND)
725 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
730 # this is a command for adding OpenCV accuracy/regression tests to the module
731 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
732 function(ocv_add_accuracy_tests)
733 set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
734 ocv_check_dependencies(${test_deps})
735 if(BUILD_TESTS AND EXISTS "${test_path}")
736 __ocv_parse_test_sources(TEST ${ARGN})
738 # opencv_highgui is required for imread/imwrite
739 set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
740 ocv_check_dependencies(${test_deps})
742 if(OCV_DEPENDENCIES_FOUND)
743 set(the_target "opencv_test_${name}")
744 # project(${the_target})
746 ocv_module_include_directories(${test_deps} "${test_path}")
748 if(NOT OPENCV_TEST_${the_module}_SOURCES)
749 file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
750 file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
751 ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
752 ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
753 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
756 get_native_precompiled_header(${the_target} test_precomp.hpp)
757 add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
759 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
760 add_dependencies(opencv_tests ${the_target})
762 # Additional target properties
763 set_target_properties(${the_target} PROPERTIES
764 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
765 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
768 if(ENABLE_SOLUTION_FOLDERS)
769 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
773 get_target_property(LOC ${the_target} LOCATION)
774 add_test(${the_target} "${LOC}")
776 ocv_add_precompiled_headers(${the_target})
777 else(OCV_DEPENDENCIES_FOUND)
778 # TODO: warn about unsatisfied dependencies
779 endif(OCV_DEPENDENCIES_FOUND)
782 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
787 function(ocv_add_samples)
788 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
789 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
791 if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
792 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_highgui ${ARGN})
793 ocv_check_dependencies(${samples_deps})
795 if(OCV_DEPENDENCIES_FOUND)
796 file(GLOB sample_sources "${samples_path}/*.cpp")
797 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS})
799 foreach(source ${sample_sources})
800 get_filename_component(name "${source}" NAME_WE)
801 set(the_target "example_${module_id}_${name}")
803 add_executable(${the_target} "${source}")
804 target_link_libraries(${the_target} ${samples_deps})
806 set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
808 if(ENABLE_SOLUTION_FOLDERS)
809 set_target_properties(${the_target} PROPERTIES
810 OUTPUT_NAME "${module_id}-example-${name}"
811 FOLDER "samples/${module_id}")
815 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
821 if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
822 file(GLOB sample_files "${samples_path}/*")
823 install(FILES ${sample_files}
824 DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
825 PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
829 # internal macro; finds all link dependencies of the module
830 # should be used at the end of CMake processing
831 macro(__ocv_track_module_link_dependencies the_module optkind)
832 set(${the_module}_MODULE_DEPS_${optkind} "")
833 set(${the_module}_EXTRA_DEPS_${optkind} "")
835 get_target_property(__module_type ${the_module} TYPE)
836 if(__module_type STREQUAL "STATIC_LIBRARY")
837 #in case of static library we have to inherit its dependencies (in right order!!!)
838 if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
839 ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
842 set(__resolved_deps "")
843 set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
844 set(__has_cycle FALSE)
847 list(GET __mod_depends 0 __dep)
848 list(REMOVE_AT __mod_depends 0)
849 if(__dep STREQUAL the_module)
850 set(__has_cycle TRUE)
851 else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
852 ocv_regex_escape(__rdep "${__dep}")
853 if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
854 #all dependencies of this module are already resolved
855 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
857 get_target_property(__module_type ${__dep} TYPE)
858 if(__module_type STREQUAL "STATIC_LIBRARY")
859 if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
860 ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
862 list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
863 list(APPEND __resolved_deps "${__dep}")
864 elseif(NOT __module_type)
865 list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
869 # get_target_property(__dep_location "${__dep}" LOCATION)
873 ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
874 #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
875 ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
876 #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
879 # not sure if it can work
880 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
883 unset(__dep_location)
885 unset(__resolved_deps)
888 endif()#STATIC_LIBRARY
891 #message("${the_module}_MODULE_DEPS_${optkind}")
892 #message(" ${${the_module}_MODULE_DEPS_${optkind}}")
893 #message(" ${OPENCV_MODULE_${the_module}_DEPS}")
895 #message("${the_module}_EXTRA_DEPS_${optkind}")
896 #message(" ${${the_module}_EXTRA_DEPS_${optkind}}")
900 # creates lists of build dependencies needed for external projects
901 macro(ocv_track_build_dependencies)
902 foreach(m ${OPENCV_MODULES_BUILD})
903 __ocv_track_module_link_dependencies("${m}" OPT)
904 __ocv_track_module_link_dependencies("${m}" DBG)