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(([EXCLUDE_CUDA] <extra sources&headers>)
31 # or glob them manually and ocv_set_module_sources(...)
32 # ocv_module_include_directories(<extra include directories>)
34 # <add extra link dependencies, compiler options, etc>
35 # ocv_add_precompiled_headers(${the_module})
36 # <add extra installation rules>
37 # ocv_add_accuracy_tests(<extra dependencies>)
38 # ocv_add_perf_tests(<extra dependencies>)
39 # ocv_add_samples(<extra dependencies>)
42 # If module have no "extra" then you can define it in one line:
44 # ocv_define_module(modname <dependencies>)
46 # clean flags for modules enabled on previous cmake run
47 # this is necessary to correctly handle modules removal
48 foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
50 unset(HAVE_${mod} CACHE)
52 unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
53 unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
54 unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
55 unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
58 # clean modules info which needs to be recalculated
59 set(OPENCV_MODULES_PUBLIC "" CACHE INTERNAL "List of OpenCV modules marked for export")
60 set(OPENCV_MODULES_BUILD "" CACHE INTERNAL "List of OpenCV modules included into the build")
61 set(OPENCV_MODULES_DISABLED_USER "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
62 set(OPENCV_MODULES_DISABLED_AUTO "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
63 set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
65 # adds dependencies to OpenCV module
67 # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
69 # * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
70 macro(ocv_add_dependencies full_modname)
71 #we don't clean the dependencies here to allow this macro several times for every module
72 foreach(d "REQUIRED" ${ARGN})
73 if(d STREQUAL "REQUIRED")
74 set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
75 elseif(d STREQUAL "OPTIONAL")
76 set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
77 elseif(d STREQUAL "PRIVATE_REQUIRED")
78 set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
79 elseif(d STREQUAL "PRIVATE_OPTIONAL")
80 set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
82 list(APPEND ${__depsvar} "${d}")
87 ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
88 ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
89 ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
90 ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
92 set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
93 CACHE INTERNAL "Required dependencies of ${full_modname} module")
94 set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
95 CACHE INTERNAL "Optional dependencies of ${full_modname} module")
96 set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
97 CACHE INTERNAL "Required private dependencies of ${full_modname} module")
98 set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
99 CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
102 # declare new OpenCV module in current folder
104 # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
106 # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cuda)
107 macro(ocv_add_module _name)
108 string(TOLOWER "${_name}" name)
109 string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
110 set(the_module opencv_${name})
112 # the first pass - collect modules info, the second pass - create targets
113 if(OPENCV_INITIAL_PASS)
114 #guard agains redefinition
115 if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
116 message(FATAL_ERROR "Redefinition of the ${the_module} module.
117 at: ${CMAKE_CURRENT_SOURCE_DIR}
118 previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
122 if(NOT DEFINED the_description)
123 set(the_description "The ${name} OpenCV module")
126 if(NOT DEFINED BUILD_${the_module}_INIT)
127 set(BUILD_${the_module}_INIT ON)
130 # create option to enable/disable this module
131 option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
133 # remember the module details
134 set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
135 set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
137 # parse list of dependencies
138 if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
139 set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
140 set(__ocv_argn__ ${ARGN})
141 list(REMOVE_AT __ocv_argn__ 0)
142 ocv_add_dependencies(${the_module} ${__ocv_argn__})
145 set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
146 ocv_add_dependencies(${the_module} ${ARGN})
147 if(BUILD_${the_module})
148 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
152 # add self to the world dependencies
153 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)
154 ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
157 if(BUILD_${the_module})
158 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
160 set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
163 # TODO: add submodules if any
165 # stop processing of current file
167 else(OPENCV_INITIAL_PASS)
168 if(NOT BUILD_${the_module})
169 return() # extra protection from redefinition
171 project(${the_module})
172 endif(OPENCV_INITIAL_PASS)
175 # excludes module from current configuration
176 macro(ocv_module_disable module)
177 set(__modname ${module})
178 if(NOT __modname MATCHES "^opencv_")
179 set(__modname opencv_${module})
181 list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
182 set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
183 set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
184 set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
185 if(BUILD_${__modname})
186 # touch variable controlling build of the module to suppress "unused variable" CMake warning
189 return() # leave the current folder
193 # collect modules from specified directories
194 # NB: must be called only once!
195 macro(ocv_glob_modules)
196 if(DEFINED OPENCV_INITIAL_PASS)
197 message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
199 set(__directories_observed "")
202 set(OPENCV_INITIAL_PASS ON)
203 foreach(__path ${ARGN})
204 get_filename_component(__path "${__path}" ABSOLUTE)
206 list(FIND __directories_observed "${__path}" __pathIdx)
207 if(__pathIdx GREATER -1)
208 message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
210 list(APPEND __directories_observed "${__path}")
212 file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
214 list(SORT __ocvmodules)
215 foreach(mod ${__ocvmodules})
216 get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
217 if(EXISTS "${__modpath}/CMakeLists.txt")
219 list(FIND __directories_observed "${__modpath}" __pathIdx)
220 if(__pathIdx GREATER -1)
221 message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
223 list(APPEND __directories_observed "${__modpath}")
225 if(OCV_MODULE_RELOCATE_ON_INITIAL_PASS)
226 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
227 file(COPY "${__modpath}/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
228 add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
229 if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
230 set(OPENCV_MODULE_opencv_${mod}_LOCATION "${__modpath}" CACHE PATH "" FORCE)
233 add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
239 ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
241 # resolve dependencies
242 __ocv_resolve_dependencies()
245 set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
246 set(OPENCV_INITIAL_PASS OFF)
247 foreach(m ${OPENCV_MODULES_BUILD})
248 if(m MATCHES "^opencv_")
249 string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
250 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
252 message(WARNING "Check module name: ${m}")
253 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${m}")
260 # disables OpenCV module with missing dependencies
261 function(__ocv_module_turn_off the_module)
262 list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
263 list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
264 list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
265 list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
266 set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
268 set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
269 set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
270 set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
273 # sort modules by dependencies
274 function(__ocv_sort_modules_by_deps __lst)
275 ocv_list_sort(${__lst})
276 set(${__lst}_ORDERED ${${__lst}} CACHE INTERNAL "")
278 foreach (m ${${__lst}})
279 list(LENGTH __result __lastindex)
280 set(__index ${__lastindex})
281 foreach (__d ${__result})
282 set(__deps "${OPENCV_MODULE_${__d}_DEPS}")
283 if(";${__deps};" MATCHES ";${m};")
284 list(FIND __result "${__d}" __i)
285 if(__i LESS "${__index}")
286 set(__index "${__i}")
290 if(__index STREQUAL __lastindex)
291 list(APPEND __result "${m}")
293 list(INSERT __result ${__index} "${m}")
296 set(${__lst} "${__result}" PARENT_SCOPE)
299 # resolve dependensies
300 function(__ocv_resolve_dependencies)
301 foreach(m ${OPENCV_MODULES_DISABLED_USER})
302 set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
304 foreach(m ${OPENCV_MODULES_BUILD})
305 set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
308 # disable MODULES with unresolved dependencies
312 foreach(m ${OPENCV_MODULES_BUILD})
313 set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
315 ocv_list_pop_front(__deps d)
316 string(TOLOWER "${d}" upper_d)
317 if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
318 if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
319 message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
320 __ocv_module_turn_off(${m})
324 message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
331 # message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
333 foreach(m ${OPENCV_MODULES_BUILD})
334 set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
335 foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
336 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
337 if(HAVE_${d} OR TARGET ${d})
338 list(APPEND deps_${m} ${d})
342 # message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
345 # propagate dependencies
349 foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
350 foreach(m ${OPENCV_MODULES_BUILD})
351 if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
352 foreach(d ${deps_${m2}})
353 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
354 # message(STATUS " Transfer dependency ${d} from ${m2} to ${m}")
355 list(APPEND deps_${m} ${d})
364 # process private deps
365 foreach(m ${OPENCV_MODULES_BUILD})
366 foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
367 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
368 list(APPEND deps_${m} ${d})
371 foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
372 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
373 if(HAVE_${d} OR TARGET ${d})
374 list(APPEND deps_${m} ${d})
380 ocv_list_sort(OPENCV_MODULES_BUILD)
382 foreach(m ${OPENCV_MODULES_BUILD})
383 # message(STATUS "FULL deps of ${m}: ${deps_${m}}")
384 set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
385 set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
386 ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
387 if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
388 list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
392 # reorder dependencies
393 foreach(m ${OPENCV_MODULES_BUILD})
394 __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
395 ocv_list_sort(OPENCV_MODULE_${m}_DEPS_EXT)
397 set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
398 set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
400 # message(STATUS " module deps: ${OPENCV_MODULE_${m}_DEPS}")
401 # message(STATUS " extra deps: ${OPENCV_MODULE_${m}_DEPS_EXT}")
404 __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
406 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} CACHE INTERNAL "List of OpenCV modules marked for export")
407 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} CACHE INTERNAL "List of OpenCV modules included into the build")
408 set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
412 # setup include paths for the list of passed modules
413 macro(ocv_include_modules)
415 if(d MATCHES "^opencv_" AND HAVE_${d})
416 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
417 ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
419 elseif(EXISTS "${d}")
420 ocv_include_directories("${d}")
425 # setup include paths for the list of passed modules and recursively add dependent modules
426 macro(ocv_include_modules_recurse)
428 if(d MATCHES "^opencv_" AND HAVE_${d})
429 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
430 ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
432 if(OPENCV_MODULE_${d}_DEPS)
433 ocv_include_modules(${OPENCV_MODULE_${d}_DEPS})
435 elseif(EXISTS "${d}")
436 ocv_include_directories("${d}")
441 # setup include path for OpenCV headers for specified module
442 # ocv_module_include_directories(<extra include directories/extra include modules>)
443 macro(ocv_module_include_directories)
444 ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/include"
445 "${OPENCV_MODULE_${the_module}_LOCATION}/src"
446 "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
448 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
452 # sets header and source files for the current module
453 # NB: all files specified as headers will be installed
455 # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
456 macro(ocv_set_module_sources)
457 set(OPENCV_MODULE_${the_module}_HEADERS "")
458 set(OPENCV_MODULE_${the_module}_SOURCES "")
460 foreach(f "HEADERS" ${ARGN})
461 if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
462 set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
464 list(APPEND ${__filesvar} "${f}")
468 # the hacky way to embeed any files into the OpenCV without modification of its build system
469 if(COMMAND ocv_get_module_external_sources)
470 ocv_get_module_external_sources()
473 # use full paths for module to be independent from the module location
474 ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
476 set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
477 set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
480 # finds and sets headers and sources for the standard OpenCV module
482 # ocv_glob_module_sources([EXCLUDE_CUDA] <extra sources&headers in the same format as used in ocv_set_module_sources>)
483 macro(ocv_glob_module_sources)
485 list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
486 if(NOT exclude_cuda EQUAL -1)
487 list(REMOVE_AT _argn ${exclude_cuda})
490 file(GLOB_RECURSE lib_srcs "src/*.cpp")
491 file(GLOB_RECURSE lib_int_hdrs "src/*.hpp" "src/*.h")
492 file(GLOB lib_hdrs "include/opencv2/*.hpp" "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h")
493 file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h")
494 file(GLOB_RECURSE lib_srcs_apple "src/*.mm")
496 list(APPEND lib_srcs ${lib_srcs_apple})
499 ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
500 ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_SOURCE_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
502 if (exclude_cuda EQUAL -1)
503 file(GLOB lib_cuda_srcs "src/cuda/*.cu")
505 set(lib_cuda_hdrs "")
507 ocv_include_directories(${CUDA_INCLUDE_DIRS})
508 file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
510 ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
511 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
515 set(lib_cuda_srcs "")
516 set(lib_cuda_hdrs "")
519 file(GLOB cl_kernels "src/opencl/*.cl")
521 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
522 string(REGEX REPLACE "opencv_" "" the_module_barename "${the_module}")
524 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp"
525 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"
526 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
527 ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
528 ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
529 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
532 ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
533 SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
536 # creates OpenCV module in current folder
537 # creates new target, configures standard dependencies, compilers flags, install rules
539 # ocv_create_module(<extra link dependencies>)
540 # ocv_create_module(SKIP_LINK)
541 macro(ocv_create_module)
542 # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
543 # be called at some point in the future. We can't look into the future, though,
544 # so this will have to do.
545 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp")
546 get_native_precompiled_header(${the_module} precomp.hpp)
549 add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
550 "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
551 ${${the_module}_pch})
552 if(NOT the_module STREQUAL opencv_ts)
553 set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
556 if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
557 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
558 target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS})
559 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
561 target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
565 add_dependencies(opencv_modules ${the_module})
567 if(ENABLE_SOLUTION_FOLDERS)
568 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
571 set_target_properties(${the_module} PROPERTIES
572 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
573 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
574 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
575 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
576 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
580 # For dynamic link numbering convenions
582 # Android SDK build scripts can include only .so files into final .apk
583 # As result we should not set version properties for Android
584 set_target_properties(${the_module} PROPERTIES
585 VERSION ${OPENCV_LIBVERSION}
586 SOVERSION ${OPENCV_SOVERSION}
590 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
591 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
592 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
596 if(CMAKE_CROSSCOMPILING)
597 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
599 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
602 ocv_install_target(${the_module} EXPORT OpenCVModules
603 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
604 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
605 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
608 # only "public" headers need to be installed
609 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
610 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
611 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
612 if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
613 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
619 # opencv precompiled headers macro (can add pch to modules and tests)
620 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
622 # ocv_add_precompiled_headers(${the_module})
623 macro(ocv_add_precompiled_headers the_target)
624 if("${the_target}" MATCHES "^opencv_test_.*$")
625 SET(pch_path "test/test_")
626 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
627 SET(pch_path "perf/perf_")
631 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
635 # short command for adding simple OpenCV module
636 # see ocv_add_module for argument details
638 # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
639 macro(ocv_define_module module_name)
642 foreach(arg ${_argn})
643 if("${arg}" STREQUAL "EXCLUDE_CUDA")
644 set(exclude_cuda "${arg}")
645 list(REMOVE_ITEM _argn ${arg})
649 ocv_add_module(${module_name} ${_argn})
650 ocv_module_include_directories()
651 ocv_glob_module_sources(${exclude_cuda})
653 ocv_add_precompiled_headers(${the_module})
655 ocv_add_accuracy_tests()
660 # ensures that all passed modules are available
661 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
662 macro(ocv_check_dependencies)
663 set(OCV_DEPENDENCIES_FOUND TRUE)
665 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
666 set(OCV_DEPENDENCIES_FOUND FALSE)
672 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
673 macro(__ocv_parse_test_sources tests_type)
674 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
675 set(OPENCV_${tests_type}_${the_module}_DEPS "")
676 set(__file_group_name "")
677 set(__file_group_sources "")
678 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
679 if(arg STREQUAL "FILES")
680 set(__currentvar "__file_group_sources")
681 if(__file_group_name AND __file_group_sources)
682 source_group("${__file_group_name}" FILES ${__file_group_sources})
683 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
685 set(__file_group_name "")
686 set(__file_group_sources "")
687 elseif(arg STREQUAL "DEPENDS_ON")
688 set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
689 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
690 set(__file_group_name "${arg}")
692 list(APPEND ${__currentvar} "${arg}")
695 unset(__file_group_name)
696 unset(__file_group_sources)
700 # this is a command for adding OpenCV performance tests to the module
701 # ocv_add_perf_tests(<extra_dependencies>)
702 function(ocv_add_perf_tests)
703 set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
704 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
705 __ocv_parse_test_sources(PERF ${ARGN})
707 # opencv_imgcodecs is required for imread/imwrite
708 set(perf_deps ${the_module} opencv_ts opencv_imgcodecs ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
709 ocv_check_dependencies(${perf_deps})
711 if(OCV_DEPENDENCIES_FOUND)
712 set(the_target "opencv_perf_${name}")
713 # project(${the_target})
715 ocv_module_include_directories(${perf_deps} "${perf_path}")
717 if(NOT OPENCV_PERF_${the_module}_SOURCES)
718 file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
719 file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
720 ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
721 ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
722 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
725 get_native_precompiled_header(${the_target} perf_precomp.hpp)
727 add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
728 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
729 add_dependencies(opencv_perf_tests ${the_target})
731 # Additional target properties
732 set_target_properties(${the_target} PROPERTIES
733 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
734 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
737 if(ENABLE_SOLUTION_FOLDERS)
738 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
741 ocv_add_precompiled_headers(${the_target})
743 else(OCV_DEPENDENCIES_FOUND)
744 # TODO: warn about unsatisfied dependencies
745 endif(OCV_DEPENDENCIES_FOUND)
747 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
752 # this is a command for adding OpenCV accuracy/regression tests to the module
753 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
754 function(ocv_add_accuracy_tests)
755 set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
756 ocv_check_dependencies(${test_deps})
757 if(BUILD_TESTS AND EXISTS "${test_path}")
758 __ocv_parse_test_sources(TEST ${ARGN})
760 # opencv_imgcodecs is required for imread/imwrite
761 set(test_deps ${the_module} opencv_ts opencv_imgcodecs opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
762 ocv_check_dependencies(${test_deps})
764 if(OCV_DEPENDENCIES_FOUND)
765 set(the_target "opencv_test_${name}")
766 # project(${the_target})
768 ocv_module_include_directories(${test_deps} "${test_path}")
770 if(NOT OPENCV_TEST_${the_module}_SOURCES)
771 file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
772 file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
773 ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
774 ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
775 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
778 get_native_precompiled_header(${the_target} test_precomp.hpp)
779 add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
781 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
782 add_dependencies(opencv_tests ${the_target})
784 # Additional target properties
785 set_target_properties(${the_target} PROPERTIES
786 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
787 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
790 if(ENABLE_SOLUTION_FOLDERS)
791 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
795 get_target_property(LOC ${the_target} LOCATION)
796 add_test(${the_target} "${LOC}")
798 ocv_add_precompiled_headers(${the_target})
799 else(OCV_DEPENDENCIES_FOUND)
800 # TODO: warn about unsatisfied dependencies
801 endif(OCV_DEPENDENCIES_FOUND)
804 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
809 function(ocv_add_samples)
810 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
811 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
813 if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
814 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_highgui ${ARGN})
815 ocv_check_dependencies(${samples_deps})
817 if(OCV_DEPENDENCIES_FOUND)
818 file(GLOB sample_sources "${samples_path}/*.cpp")
819 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS})
821 foreach(source ${sample_sources})
822 get_filename_component(name "${source}" NAME_WE)
823 set(the_target "example_${module_id}_${name}")
825 add_executable(${the_target} "${source}")
826 target_link_libraries(${the_target} ${samples_deps})
828 set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
830 if(ENABLE_SOLUTION_FOLDERS)
831 set_target_properties(${the_target} PROPERTIES
832 OUTPUT_NAME "${module_id}-example-${name}"
833 FOLDER "samples/${module_id}")
837 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
843 if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
844 file(GLOB sample_files "${samples_path}/*")
845 install(FILES ${sample_files}
846 DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
847 PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
851 # internal macro; finds all link dependencies of the module
852 # should be used at the end of CMake processing
853 macro(__ocv_track_module_link_dependencies the_module optkind)
854 set(${the_module}_MODULE_DEPS_${optkind} "")
855 set(${the_module}_EXTRA_DEPS_${optkind} "")
857 get_target_property(__module_type ${the_module} TYPE)
858 if(__module_type STREQUAL "STATIC_LIBRARY")
859 #in case of static library we have to inherit its dependencies (in right order!!!)
860 if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
861 ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
864 set(__resolved_deps "")
865 set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
866 set(__has_cycle FALSE)
869 list(GET __mod_depends 0 __dep)
870 list(REMOVE_AT __mod_depends 0)
871 if(__dep STREQUAL the_module)
872 set(__has_cycle TRUE)
873 else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
874 ocv_regex_escape(__rdep "${__dep}")
875 if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
876 #all dependencies of this module are already resolved
877 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
879 get_target_property(__module_type ${__dep} TYPE)
880 if(__module_type STREQUAL "STATIC_LIBRARY")
881 if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
882 ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
884 list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
885 list(APPEND __resolved_deps "${__dep}")
886 elseif(NOT __module_type)
887 list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
891 # get_target_property(__dep_location "${__dep}" LOCATION)
895 ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
896 #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
897 ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
898 #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
901 # not sure if it can work
902 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
905 unset(__dep_location)
907 unset(__resolved_deps)
910 endif()#STATIC_LIBRARY
913 #message("${the_module}_MODULE_DEPS_${optkind}")
914 #message(" ${${the_module}_MODULE_DEPS_${optkind}}")
915 #message(" ${OPENCV_MODULE_${the_module}_DEPS}")
917 #message("${the_module}_EXTRA_DEPS_${optkind}")
918 #message(" ${${the_module}_EXTRA_DEPS_${optkind}}")
922 # creates lists of build dependencies needed for external projects
923 macro(ocv_track_build_dependencies)
924 foreach(m ${OPENCV_MODULES_BUILD})
925 __ocv_track_module_link_dependencies("${m}" OPT)
926 __ocv_track_module_link_dependencies("${m}" DBG)