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_gpu)
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/${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 file(GLOB lib_cuda_srcs "src/cuda/*.cu")
494 set(lib_cuda_hdrs "")
496 ocv_include_directories(${CUDA_INCLUDE_DIRS})
497 file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
499 ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
500 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
503 source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs})
505 file(GLOB cl_kernels "src/opencl/*.cl")
506 if(HAVE_opencv_ocl AND cl_kernels)
507 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
509 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp"
510 COMMAND ${CMAKE_COMMAND} -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
511 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
512 source_group("OpenCL" FILES ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
513 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.cpp" "${CMAKE_CURRENT_BINARY_DIR}/opencl_kernels.hpp")
516 source_group("Include" FILES ${lib_hdrs})
517 source_group("Include\\detail" FILES ${lib_hdrs_detail})
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})
540 if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
541 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS})
542 target_link_libraries(${the_module} LINK_INTERFACE_LIBRARIES ${OPENCV_MODULE_${the_module}_DEPS})
543 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
546 add_dependencies(opencv_modules ${the_module})
548 if(ENABLE_SOLUTION_FOLDERS)
549 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
552 set_target_properties(${the_module} PROPERTIES
553 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
554 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
555 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
556 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
557 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
561 # For dynamic link numbering convenions
563 # Android SDK build scripts can include only .so files into final .apk
564 # As result we should not set version properties for Android
565 set_target_properties(${the_module} PROPERTIES
566 VERSION ${OPENCV_LIBVERSION}
567 SOVERSION ${OPENCV_SOVERSION}
571 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
572 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
573 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
577 if(CMAKE_CROSSCOMPILING)
578 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
580 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
583 ocv_install_target(${the_module} EXPORT OpenCVModules
584 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
585 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs
586 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev
589 # only "public" headers need to be installed
590 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
591 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
592 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
593 if(hdr2 MATCHES "^(opencv2/.*)/[^/]+.h(..)?$")
594 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
600 # opencv precompiled headers macro (can add pch to modules and tests)
601 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
603 # ocv_add_precompiled_headers(${the_module})
604 macro(ocv_add_precompiled_headers the_target)
605 if("${the_target}" MATCHES "^opencv_test_.*$")
606 SET(pch_path "test/test_")
607 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
608 SET(pch_path "perf/perf_")
612 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
616 # short command for adding simple OpenCV module
617 # see ocv_add_module for argument details
619 # ocv_define_module(module_name [INTERNAL] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
620 macro(ocv_define_module module_name)
621 ocv_add_module(${module_name} ${ARGN})
622 ocv_module_include_directories()
623 ocv_glob_module_sources()
625 ocv_add_precompiled_headers(${the_module})
627 ocv_add_accuracy_tests()
632 # ensures that all passed modules are available
633 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
634 macro(ocv_check_dependencies)
635 set(OCV_DEPENDENCIES_FOUND TRUE)
637 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
638 set(OCV_DEPENDENCIES_FOUND FALSE)
644 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
645 macro(__ocv_parse_test_sources tests_type)
646 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
647 set(OPENCV_${tests_type}_${the_module}_DEPS "")
648 set(__file_group_name "")
649 set(__file_group_sources "")
650 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
651 if(arg STREQUAL "FILES")
652 set(__currentvar "__file_group_sources")
653 if(__file_group_name AND __file_group_sources)
654 source_group("${__file_group_name}" FILES ${__file_group_sources})
655 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
657 set(__file_group_name "")
658 set(__file_group_sources "")
659 elseif(arg STREQUAL "DEPENDS_ON")
660 set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
661 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
662 set(__file_group_name "${arg}")
664 list(APPEND ${__currentvar} "${arg}")
667 unset(__file_group_name)
668 unset(__file_group_sources)
672 # this is a command for adding OpenCV performance tests to the module
673 # ocv_add_perf_tests(<extra_dependencies>)
674 function(ocv_add_perf_tests)
675 set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
676 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
677 __ocv_parse_test_sources(PERF ${ARGN})
679 # opencv_highgui is required for imread/imwrite
680 set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
681 ocv_check_dependencies(${perf_deps})
683 if(OCV_DEPENDENCIES_FOUND)
684 set(the_target "opencv_perf_${name}")
685 # project(${the_target})
687 ocv_module_include_directories(${perf_deps} "${perf_path}")
689 if(NOT OPENCV_PERF_${the_module}_SOURCES)
690 file(GLOB perf_srcs "${perf_path}/*.cpp")
691 file(GLOB perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
692 source_group("Src" FILES ${perf_srcs})
693 source_group("Include" FILES ${perf_hdrs})
694 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
697 get_native_precompiled_header(${the_target} perf_precomp.hpp)
699 add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
700 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
701 add_dependencies(opencv_perf_tests ${the_target})
703 # Additional target properties
704 set_target_properties(${the_target} PROPERTIES
705 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
706 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
709 if(ENABLE_SOLUTION_FOLDERS)
710 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
713 ocv_add_precompiled_headers(${the_target})
715 else(OCV_DEPENDENCIES_FOUND)
716 # TODO: warn about unsatisfied dependencies
717 endif(OCV_DEPENDENCIES_FOUND)
719 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
724 # this is a command for adding OpenCV accuracy/regression tests to the module
725 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
726 function(ocv_add_accuracy_tests)
727 set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
728 ocv_check_dependencies(${test_deps})
729 if(BUILD_TESTS AND EXISTS "${test_path}")
730 __ocv_parse_test_sources(TEST ${ARGN})
732 # opencv_highgui is required for imread/imwrite
733 set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
734 ocv_check_dependencies(${test_deps})
736 if(OCV_DEPENDENCIES_FOUND)
737 set(the_target "opencv_test_${name}")
738 # project(${the_target})
740 ocv_module_include_directories(${test_deps} "${test_path}")
742 if(NOT OPENCV_TEST_${the_module}_SOURCES)
743 file(GLOB test_srcs "${test_path}/*.cpp")
744 file(GLOB test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
745 source_group("Src" FILES ${test_srcs})
746 source_group("Include" FILES ${test_hdrs})
747 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
750 get_native_precompiled_header(${the_target} test_precomp.hpp)
751 add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
753 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
754 add_dependencies(opencv_tests ${the_target})
756 # Additional target properties
757 set_target_properties(${the_target} PROPERTIES
758 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
759 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
762 if(ENABLE_SOLUTION_FOLDERS)
763 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
767 get_target_property(LOC ${the_target} LOCATION)
768 add_test(${the_target} "${LOC}")
770 ocv_add_precompiled_headers(${the_target})
771 else(OCV_DEPENDENCIES_FOUND)
772 # TODO: warn about unsatisfied dependencies
773 endif(OCV_DEPENDENCIES_FOUND)
776 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
781 function(ocv_add_samples)
782 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
783 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
785 if(BUILD_EXAMPLES AND EXISTS "${samples_path}")
786 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_highgui ${ARGN})
787 ocv_check_dependencies(${samples_deps})
789 if(OCV_DEPENDENCIES_FOUND)
790 file(GLOB sample_sources "${samples_path}/*.cpp")
791 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS})
793 foreach(source ${sample_sources})
794 get_filename_component(name "${source}" NAME_WE)
795 set(the_target "example_${module_id}_${name}")
797 add_executable(${the_target} "${source}")
798 target_link_libraries(${the_target} ${samples_deps})
800 set_target_properties(${the_target} PROPERTIES PROJECT_LABEL "(sample) ${name}")
802 if(ENABLE_SOLUTION_FOLDERS)
803 set_target_properties(${the_target} PROPERTIES
804 OUTPUT_NAME "${module_id}-example-${name}"
805 FOLDER "samples/${module_id}")
809 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
815 if(INSTALL_C_EXAMPLES AND NOT WIN32 AND EXISTS "${samples_path}")
816 file(GLOB sample_files "${samples_path}/*")
817 install(FILES ${sample_files}
818 DESTINATION ${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}
819 PERMISSIONS OWNER_READ GROUP_READ WORLD_READ COMPONENT samples)
823 # internal macro; finds all link dependencies of the module
824 # should be used at the end of CMake processing
825 macro(__ocv_track_module_link_dependencies the_module optkind)
826 set(${the_module}_MODULE_DEPS_${optkind} "")
827 set(${the_module}_EXTRA_DEPS_${optkind} "")
829 get_target_property(__module_type ${the_module} TYPE)
830 if(__module_type STREQUAL "STATIC_LIBRARY")
831 #in case of static library we have to inherit its dependencies (in right order!!!)
832 if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
833 ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
836 set(__resolved_deps "")
837 set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
838 set(__has_cycle FALSE)
841 list(GET __mod_depends 0 __dep)
842 list(REMOVE_AT __mod_depends 0)
843 if(__dep STREQUAL the_module)
844 set(__has_cycle TRUE)
845 else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
846 ocv_regex_escape(__rdep "${__dep}")
847 if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
848 #all dependencies of this module are already resolved
849 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
851 get_target_property(__module_type ${__dep} TYPE)
852 if(__module_type STREQUAL "STATIC_LIBRARY")
853 if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
854 ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
856 list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
857 list(APPEND __resolved_deps "${__dep}")
858 elseif(NOT __module_type)
859 list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
863 # get_target_property(__dep_location "${__dep}" LOCATION)
867 ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
868 #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
869 ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
870 #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
873 # not sure if it can work
874 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
877 unset(__dep_location)
879 unset(__resolved_deps)
882 endif()#STATIC_LIBRARY
885 #message("${the_module}_MODULE_DEPS_${optkind}")
886 #message(" ${${the_module}_MODULE_DEPS_${optkind}}")
887 #message(" ${OPENCV_MODULE_${the_module}_DEPS}")
889 #message("${the_module}_EXTRA_DEPS_${optkind}")
890 #message(" ${${the_module}_EXTRA_DEPS_${optkind}}")
894 # creates lists of build dependencies needed for external projects
895 macro(ocv_track_build_dependencies)
896 foreach(m ${OPENCV_MODULES_BUILD})
897 __ocv_track_module_link_dependencies("${m}" OPT)
898 __ocv_track_module_link_dependencies("${m}" DBG)