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
15 # OPENCV_MODULE_${the_module}_REQ_DEPS
16 # OPENCV_MODULE_${the_module}_OPT_DEPS
17 # HAVE_${the_module} - for fast check of module availability
19 # To control the setup of the module you could also set:
20 # the_description - text to be used as current module description
21 # OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
22 # OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
23 # BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
25 # The verbose template for OpenCV module:
27 # ocv_add_module(modname <dependencies>)
28 # ocv_glob_module_sources() or glob them manually and ocv_set_module_sources(...)
29 # ocv_module_include_directories(<extra include directories>)
31 # <add extra link dependencies, compiler options, etc>
32 # ocv_add_precompiled_headers(${the_module})
33 # <add extra installation rules>
34 # ocv_add_accuracy_tests(<extra dependencies>)
35 # ocv_add_perf_tests(<extra dependencies>)
38 # If module have no "extra" then you can define it in one line:
40 # ocv_define_module(modname <dependencies>)
42 # clean flags for modules enabled on previous cmake run
43 # this is necessary to correctly handle modules removal
44 foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
46 unset(HAVE_${mod} CACHE)
48 unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
49 unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
52 # clean modules info which needs to be recalculated
53 set(OPENCV_MODULES_PUBLIC "" CACHE INTERNAL "List of OpenCV modules marked for export")
54 set(OPENCV_MODULES_BUILD "" CACHE INTERNAL "List of OpenCV modules included into the build")
55 set(OPENCV_MODULES_DISABLED_USER "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
56 set(OPENCV_MODULES_DISABLED_AUTO "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
57 set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
59 # adds dependencies to OpenCV module
61 # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
63 # * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
64 macro(ocv_add_dependencies full_modname)
65 #we don't clean the dependencies here to allow this macro several times for every module
66 foreach(d "REQUIRED" ${ARGN})
67 if(d STREQUAL "REQUIRED")
68 set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
69 elseif(d STREQUAL "OPTIONAL")
70 set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
72 list(APPEND ${__depsvar} "${d}")
77 ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
78 ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
80 set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS} CACHE INTERNAL "Required dependencies of ${full_modname} module")
81 set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS} CACHE INTERNAL "Optional dependencies of ${full_modname} module")
84 # declare new OpenCV module in current folder
86 # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
88 # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_gpu)
89 macro(ocv_add_module _name)
90 string(TOLOWER "${_name}" name)
91 string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
92 set(the_module opencv_${name})
94 # the first pass - collect modules info, the second pass - create targets
95 if(OPENCV_INITIAL_PASS)
96 #guard agains redefinition
97 if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
98 message(FATAL_ERROR "Redefinition of the ${the_module} module.
99 at: ${CMAKE_CURRENT_SOURCE_DIR}
100 previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
104 if(NOT DEFINED the_description)
105 set(the_description "The ${name} OpenCV module")
108 if(NOT DEFINED BUILD_${the_module}_INIT)
109 set(BUILD_${the_module}_INIT ON)
112 # create option to enable/disable this module
113 option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
115 # remember the module details
116 set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
117 set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
119 # parse list of dependencies
120 if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
121 set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The cathegory of the module")
122 set(__ocv_argn__ ${ARGN})
123 list(REMOVE_AT __ocv_argn__ 0)
124 ocv_add_dependencies(${the_module} ${__ocv_argn__})
127 set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The cathegory of the module")
128 ocv_add_dependencies(${the_module} ${ARGN})
129 if(BUILD_${the_module})
130 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
134 # add self to the world dependencies
135 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)
136 ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
139 if(BUILD_${the_module})
140 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
142 set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
145 # TODO: add submodules if any
147 # stop processing of current file
149 else(OPENCV_INITIAL_PASS)
150 if(NOT BUILD_${the_module})
151 return() # extra protection from redefinition
153 project(${the_module})
154 endif(OPENCV_INITIAL_PASS)
157 # excludes module from current configuration
158 macro(ocv_module_disable module)
159 set(__modname ${module})
160 if(NOT __modname MATCHES "^opencv_")
161 set(__modname opencv_${module})
163 list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
164 set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
165 set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
166 set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
167 if(BUILD_${__modname})
168 # touch variable controlling build of the module to suppress "unused variable" CMake warning
171 return() # leave the current folder
175 # Internal macro; partly disables OpenCV module
176 macro(__ocv_module_turn_off the_module)
177 list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
178 list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
179 list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
180 list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
181 set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
184 # Internal macro for dependencies tracking
185 macro(__ocv_flatten_module_required_dependencies the_module)
186 set(__flattened_deps "")
187 set(__resolved_deps "")
188 set(__req_depends ${OPENCV_MODULE_${the_module}_REQ_DEPS})
191 ocv_list_pop_front(__req_depends __dep)
192 if(__dep STREQUAL the_module)
193 __ocv_module_turn_off(${the_module}) # TODO: think how to deal with cyclic dependency
195 elseif(";${OPENCV_MODULES_DISABLED_USER};${OPENCV_MODULES_DISABLED_AUTO};" MATCHES ";${__dep};")
196 __ocv_module_turn_off(${the_module}) # depends on disabled module
197 list(APPEND __flattened_deps "${__dep}")
198 elseif(";${OPENCV_MODULES_BUILD};" MATCHES ";${__dep};")
199 if(";${__resolved_deps};" MATCHES ";${__dep};")
200 list(APPEND __flattened_deps "${__dep}") # all dependencies of this module are already resolved
202 # put all required subdependencies before this dependency and mark it as resolved
203 list(APPEND __resolved_deps "${__dep}")
204 list(INSERT __req_depends 0 ${OPENCV_MODULE_${__dep}_REQ_DEPS} ${__dep})
206 elseif(__dep MATCHES "^opencv_")
207 __ocv_module_turn_off(${the_module}) # depends on missing module
208 message(WARNING "Unknown \"${__dep}\" module is listened in the dependencies of \"${the_module}\" module")
216 list(REMOVE_DUPLICATES __flattened_deps)
217 set(OPENCV_MODULE_${the_module}_DEPS ${__flattened_deps})
219 set(OPENCV_MODULE_${the_module}_DEPS "")
222 ocv_clear_vars(__resolved_deps __flattened_deps __req_depends __dep)
225 # Internal macro for dependencies tracking
226 macro(__ocv_flatten_module_optional_dependencies the_module)
227 set(__flattened_deps "")
228 set(__resolved_deps "")
229 set(__opt_depends ${OPENCV_MODULE_${the_module}_REQ_DEPS} ${OPENCV_MODULE_${the_module}_OPT_DEPS})
232 ocv_list_pop_front(__opt_depends __dep)
233 if(__dep STREQUAL the_module)
234 __ocv_module_turn_off(${the_module}) # TODO: think how to deal with cyclic dependency
236 elseif(";${OPENCV_MODULES_BUILD};" MATCHES ";${__dep};")
237 if(";${__resolved_deps};" MATCHES ";${__dep};")
238 list(APPEND __flattened_deps "${__dep}") # all dependencies of this module are already resolved
240 # put all subdependencies before this dependency and mark it as resolved
241 list(APPEND __resolved_deps "${__dep}")
242 list(INSERT __opt_depends 0 ${OPENCV_MODULE_${__dep}_REQ_DEPS} ${OPENCV_MODULE_${__dep}_OPT_DEPS} ${__dep})
245 # skip non-modules or missing modules
250 list(REMOVE_DUPLICATES __flattened_deps)
251 set(OPENCV_MODULE_${the_module}_DEPS ${__flattened_deps})
253 set(OPENCV_MODULE_${the_module}_DEPS "")
256 ocv_clear_vars(__resolved_deps __flattened_deps __opt_depends __dep)
259 macro(__ocv_flatten_module_dependencies)
260 foreach(m ${OPENCV_MODULES_DISABLED_USER})
261 set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
263 foreach(m ${OPENCV_MODULES_BUILD})
264 set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
265 __ocv_flatten_module_required_dependencies(${m})
266 set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened required dependencies of ${m} module")
269 foreach(m ${OPENCV_MODULES_BUILD})
270 __ocv_flatten_module_optional_dependencies(${m})
272 # save dependencies from other modules
273 set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
274 # save extra dependencies
275 set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_OPT_DEPS})
276 if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
277 list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS})
279 ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
280 set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
283 # order modules by dependencies
284 set(OPENCV_MODULES_BUILD_ "")
285 foreach(m ${OPENCV_MODULES_BUILD})
286 list(APPEND OPENCV_MODULES_BUILD_ ${OPENCV_MODULE_${m}_DEPS} ${m})
288 ocv_list_unique(OPENCV_MODULES_BUILD_)
290 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} CACHE INTERNAL "List of OpenCV modules marked for export")
291 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD_} CACHE INTERNAL "List of OpenCV modules included into the build")
292 set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
295 # collect modules from specified directories
296 # NB: must be called only once!
297 macro(ocv_glob_modules)
298 if(DEFINED OPENCV_INITIAL_PASS)
299 message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
301 set(__directories_observed "")
304 set(OPENCV_INITIAL_PASS ON)
305 foreach(__path ${ARGN})
306 get_filename_component(__path "${__path}" ABSOLUTE)
308 list(FIND __directories_observed "${__path}" __pathIdx)
309 if(__pathIdx GREATER -1)
310 message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
312 list(APPEND __directories_observed "${__path}")
314 file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
316 list(SORT __ocvmodules)
317 foreach(mod ${__ocvmodules})
318 get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
319 if(EXISTS "${__modpath}/CMakeLists.txt")
321 list(FIND __directories_observed "${__modpath}" __pathIdx)
322 if(__pathIdx GREATER -1)
323 message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
325 list(APPEND __directories_observed "${__modpath}")
327 if(OCV_MODULE_RELOCATE_ON_INITIAL_PASS)
328 file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
329 file(COPY "${__modpath}/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
330 add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
331 if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
332 set(OPENCV_MODULE_opencv_${mod}_LOCATION "${__modpath}" CACHE PATH "" FORCE)
335 add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
341 ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
343 # resolve dependencies
344 __ocv_flatten_module_dependencies()
347 set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
348 set(OPENCV_INITIAL_PASS OFF)
349 foreach(m ${OPENCV_MODULES_BUILD})
350 if(m MATCHES "^opencv_")
351 string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
352 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
358 # setup include paths for the list of passed modules
359 macro(ocv_include_modules)
361 if(d MATCHES "^opencv_" AND HAVE_${d})
362 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
363 ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
365 elseif(EXISTS "${d}")
366 ocv_include_directories("${d}")
371 # setup include paths for the list of passed modules and recursively add dependent modules
372 macro(ocv_include_modules_recurse)
374 if(d MATCHES "^opencv_" AND HAVE_${d})
375 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
376 ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
378 if(OPENCV_MODULE_${d}_DEPS)
379 ocv_include_modules_recurse(${OPENCV_MODULE_${d}_DEPS})
381 elseif(EXISTS "${d}")
382 ocv_include_directories("${d}")
387 # setup include path for OpenCV headers for specified module
388 # ocv_module_include_directories(<extra include directories/extra include modules>)
389 macro(ocv_module_include_directories)
390 ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/include"
391 "${OPENCV_MODULE_${the_module}_LOCATION}/src"
392 "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
394 ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
398 # sets header and source files for the current module
399 # NB: all files specified as headers will be installed
401 # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
402 macro(ocv_set_module_sources)
403 set(OPENCV_MODULE_${the_module}_HEADERS "")
404 set(OPENCV_MODULE_${the_module}_SOURCES "")
406 foreach(f "HEADERS" ${ARGN})
407 if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
408 set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
410 list(APPEND ${__filesvar} "${f}")
414 # the hacky way to embeed any files into the OpenCV without modification of its build system
415 if(COMMAND ocv_get_module_external_sources)
416 ocv_get_module_external_sources()
419 # use full paths for module to be independent from the module location
420 ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
422 set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
423 set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
426 # finds and sets headers and sources for the standard OpenCV module
428 # ocv_glob_module_sources(<extra sources&headers in the same format as used in ocv_set_module_sources>)
429 macro(ocv_glob_module_sources)
430 file(GLOB_RECURSE lib_srcs "src/*.cpp")
431 file(GLOB_RECURSE lib_int_hdrs "src/*.hpp" "src/*.h")
432 file(GLOB lib_hdrs "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h")
433 file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h")
435 file(GLOB lib_cuda_srcs "src/cuda/*.cu")
437 set(lib_cuda_hdrs "")
439 if(HAVE_CUDA AND lib_cuda_srcs)
440 ocv_include_directories(${CUDA_INCLUDE_DIRS})
441 file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
443 ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
444 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
447 file(GLOB cl_kernels "src/opencl/*.cl")
449 if(HAVE_OPENCL AND cl_kernels)
450 ocv_include_directories(${OPENCL_INCLUDE_DIRS})
452 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp"
453 COMMAND ${CMAKE_COMMAND} -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
454 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
455 source_group("Src\\OpenCL" FILES ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp")
456 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp")
459 source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs})
460 source_group("Include" FILES ${lib_hdrs})
461 source_group("Include\\detail" FILES ${lib_hdrs_detail})
463 ocv_set_module_sources(${ARGN} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
464 SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
467 # creates OpenCV module in current folder
468 # creates new target, configures standard dependencies, compilers flags, install rules
470 # ocv_create_module(<extra link dependencies>)
471 # ocv_create_module(SKIP_LINK)
472 macro(ocv_create_module)
473 # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
474 # be called at some point in the future. We can't look into the future, though,
475 # so this will have to do.
476 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp")
477 get_native_precompiled_header(${the_module} precomp.hpp)
480 add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
481 "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
482 ${${the_module}_pch})
484 if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
485 target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
487 target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
489 if(HAVE_OPENCL AND OPENCL_LIBRARIES)
490 target_link_libraries(${the_module} ${OPENCL_LIBRARIES})
494 add_dependencies(opencv_modules ${the_module})
496 if(ENABLE_SOLUTION_FOLDERS)
497 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
500 set_target_properties(${the_module} PROPERTIES
501 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
502 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
503 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
504 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
505 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
509 # For dynamic link numbering convenions
511 # Android SDK build scripts can include only .so files into final .apk
512 # As result we should not set version properties for Android
513 set_target_properties(${the_module} PROPERTIES
514 VERSION ${OPENCV_LIBVERSION}
515 SOVERSION ${OPENCV_SOVERSION}
519 if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
520 OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
522 set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
524 add_definitions(-DCVAPI_EXPORTS)
529 if(CMAKE_CROSSCOMPILING)
530 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
532 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
535 install(TARGETS ${the_module}
536 RUNTIME DESTINATION bin COMPONENT main
537 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
538 ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
541 # only "public" headers need to be installed
542 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
543 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
544 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
545 if(hdr2 MATCHES "^(opencv2/.*)/[^/]+.h(..)?$")
546 install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT main)
552 # opencv precompiled headers macro (can add pch to modules and tests)
553 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
555 # ocv_add_precompiled_headers(${the_module})
556 macro(ocv_add_precompiled_headers the_target)
557 if("${the_target}" MATCHES "^opencv_test_.*$")
558 SET(pch_path "test/test_")
559 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
560 SET(pch_path "perf/perf_")
564 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
568 # short command for adding simple OpenCV module
569 # see ocv_add_module for argument details
571 # ocv_define_module(module_name [INTERNAL] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
572 macro(ocv_define_module module_name)
573 ocv_add_module(${module_name} ${ARGN})
574 ocv_module_include_directories()
575 ocv_glob_module_sources()
577 ocv_add_precompiled_headers(${the_module})
579 ocv_add_accuracy_tests()
583 # ensures that all passed modules are available
584 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
585 macro(ocv_check_dependencies)
586 set(OCV_DEPENDENCIES_FOUND TRUE)
588 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
589 set(OCV_DEPENDENCIES_FOUND FALSE)
595 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
596 macro(__ocv_parse_test_sources tests_type)
597 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
598 set(OPENCV_${tests_type}_${the_module}_DEPS "")
599 set(__file_group_name "")
600 set(__file_group_sources "")
601 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
602 if(arg STREQUAL "FILES")
603 set(__currentvar "__file_group_sources")
604 if(__file_group_name AND __file_group_sources)
605 source_group("${__file_group_name}" FILES ${__file_group_sources})
606 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
608 set(__file_group_name "")
609 set(__file_group_sources "")
610 elseif(arg STREQUAL "DEPENDS_ON")
611 set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
612 elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
613 set(__file_group_name "${arg}")
615 list(APPEND ${__currentvar} "${arg}")
618 unset(__file_group_name)
619 unset(__file_group_sources)
623 # this is a command for adding OpenCV performance tests to the module
624 # ocv_add_perf_tests(<extra_dependencies>)
625 function(ocv_add_perf_tests)
626 set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
627 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
628 __ocv_parse_test_sources(PERF ${ARGN})
630 # opencv_highgui is required for imread/imwrite
631 set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
632 ocv_check_dependencies(${perf_deps})
634 if(OCV_DEPENDENCIES_FOUND)
635 set(the_target "opencv_perf_${name}")
636 # project(${the_target})
638 ocv_module_include_directories(${perf_deps} "${perf_path}")
640 if(NOT OPENCV_PERF_${the_module}_SOURCES)
641 file(GLOB perf_srcs "${perf_path}/*.cpp")
642 file(GLOB perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
643 source_group("Src" FILES ${perf_srcs})
644 source_group("Include" FILES ${perf_hdrs})
645 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
648 get_native_precompiled_header(${the_target} test_precomp.hpp)
650 add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
651 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
652 add_dependencies(opencv_perf_tests ${the_target})
654 # Additional target properties
655 set_target_properties(${the_target} PROPERTIES
656 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
657 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
660 if(ENABLE_SOLUTION_FOLDERS)
661 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
664 ocv_add_precompiled_headers(${the_target})
666 else(OCV_DEPENDENCIES_FOUND)
667 # TODO: warn about unsatisfied dependencies
668 endif(OCV_DEPENDENCIES_FOUND)
672 # this is a command for adding OpenCV accuracy/regression tests to the module
673 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
674 function(ocv_add_accuracy_tests)
675 set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
676 ocv_check_dependencies(${test_deps})
677 if(BUILD_TESTS AND EXISTS "${test_path}")
678 __ocv_parse_test_sources(TEST ${ARGN})
680 # opencv_highgui is required for imread/imwrite
681 set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
682 ocv_check_dependencies(${test_deps})
684 if(OCV_DEPENDENCIES_FOUND)
685 set(the_target "opencv_test_${name}")
686 # project(${the_target})
688 ocv_module_include_directories(${test_deps} "${test_path}")
690 if(NOT OPENCV_TEST_${the_module}_SOURCES)
691 file(GLOB test_srcs "${test_path}/*.cpp")
692 file(GLOB test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
693 source_group("Src" FILES ${test_srcs})
694 source_group("Include" FILES ${test_hdrs})
695 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
698 get_native_precompiled_header(${the_target} test_precomp.hpp)
700 add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
701 target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
702 add_dependencies(opencv_tests ${the_target})
704 # Additional target properties
705 set_target_properties(${the_target} PROPERTIES
706 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
707 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
710 if(ENABLE_SOLUTION_FOLDERS)
711 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
715 get_target_property(LOC ${the_target} LOCATION)
716 add_test(${the_target} "${LOC}")
718 ocv_add_precompiled_headers(${the_target})
719 else(OCV_DEPENDENCIES_FOUND)
720 # TODO: warn about unsatisfied dependencies
721 endif(OCV_DEPENDENCIES_FOUND)
725 # internal macro; finds all link dependencies of the module
726 # should be used at the end of CMake processing
727 macro(__ocv_track_module_link_dependencies the_module optkind)
728 set(${the_module}_MODULE_DEPS_${optkind} "")
729 set(${the_module}_EXTRA_DEPS_${optkind} "")
731 get_target_property(__module_type ${the_module} TYPE)
732 if(__module_type STREQUAL "STATIC_LIBRARY")
733 #in case of static library we have to inherit its dependencies (in right order!!!)
734 if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
735 ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
738 set(__resolved_deps "")
739 set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
740 set(__has_cycle FALSE)
743 list(GET __mod_depends 0 __dep)
744 list(REMOVE_AT __mod_depends 0)
745 if(__dep STREQUAL the_module)
746 set(__has_cycle TRUE)
747 else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
748 ocv_regex_escape(__rdep "${__dep}")
749 if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
750 #all dependencies of this module are already resolved
751 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
753 get_target_property(__module_type ${__dep} TYPE)
754 if(__module_type STREQUAL "STATIC_LIBRARY")
755 if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
756 ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
758 list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
759 list(APPEND __resolved_deps "${__dep}")
760 elseif(NOT __module_type)
761 list(APPEND ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
765 # get_target_property(__dep_location "${__dep}" LOCATION)
769 ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
770 #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
771 ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
772 #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})
775 # not sure if it can work
776 list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
779 unset(__dep_location)
781 unset(__resolved_deps)
784 endif()#STATIC_LIBRARY
787 #message("${the_module}_MODULE_DEPS_${optkind}")
788 #message(" ${${the_module}_MODULE_DEPS_${optkind}}")
789 #message(" ${OPENCV_MODULE_${the_module}_DEPS}")
791 #message("${the_module}_EXTRA_DEPS_${optkind}")
792 #message(" ${${the_module}_EXTRA_DEPS_${optkind}}")
796 # creates lists of build dependencies needed for external projects
797 macro(ocv_track_build_dependencies)
798 foreach(m ${OPENCV_MODULES_BUILD})
799 __ocv_track_module_link_dependencies("${m}" OPT)
800 __ocv_track_module_link_dependencies("${m}" DBG)