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}_BINARY_DIR
10 # OPENCV_MODULE_${the_module}_DESCRIPTION
11 # OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
12 # OPENCV_MODULE_${the_module}_HEADERS
13 # OPENCV_MODULE_${the_module}_SOURCES
14 # OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
15 # OPENCV_MODULE_${the_module}_DEPS_TO_LINK - differs from above for world build only
16 # OPENCV_MODULE_${the_module}_DEPS_EXT - non-module dependencies
17 # OPENCV_MODULE_${the_module}_REQ_DEPS
18 # OPENCV_MODULE_${the_module}_OPT_DEPS
19 # OPENCV_MODULE_${the_module}_PRIVATE_REQ_DEPS
20 # OPENCV_MODULE_${the_module}_PRIVATE_OPT_DEPS
21 # OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD
22 # OPENCV_MODULE_${the_module}_CUDA_OBJECTS - compiled CUDA objects list
23 # OPENCV_MODULE_${the_module}_WRAPPERS - list of wrappers supporting this module
24 # HAVE_${the_module} - for fast check of module availability
26 # To control the setup of the module you could also set:
27 # the_description - text to be used as current module description
28 # the_label - label for current module
29 # OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
30 # OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
31 # BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
33 # The verbose template for OpenCV module:
35 # ocv_add_module(modname <dependencies>)
36 # ocv_glob_module_sources(([EXCLUDE_CUDA] <extra sources&headers>)
37 # or glob them manually and ocv_set_module_sources(...)
38 # ocv_module_include_directories(<extra include directories>)
40 # <add extra link dependencies, compiler options, etc>
41 # ocv_add_precompiled_headers(${the_module})
42 # <add extra installation rules>
43 # ocv_add_accuracy_tests(<extra dependencies>)
44 # ocv_add_perf_tests(<extra dependencies>)
45 # ocv_add_samples(<extra dependencies>)
48 # If module have no "extra" then you can define it in one line:
50 # ocv_define_module(modname <dependencies>)
52 # clean flags for modules enabled on previous cmake run
53 # this is necessary to correctly handle modules removal
54 foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
56 unset(HAVE_${mod} CACHE)
58 unset(OPENCV_MODULE_${mod}_DEPS CACHE)
59 unset(OPENCV_MODULE_${mod}_DEPS_EXT CACHE)
60 unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
61 unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
62 unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
63 unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
64 unset(OPENCV_MODULE_${mod}_LINK_DEPS CACHE)
65 unset(OPENCV_MODULE_${mod}_WRAPPERS CACHE)
66 unset(OPENCV_DEPENDANT_TARGETS_${mod} CACHE)
69 # clean modules info which needs to be recalculated
70 set(OPENCV_MODULES_PUBLIC "" CACHE INTERNAL "List of OpenCV modules marked for export")
71 set(OPENCV_MODULES_BUILD "" CACHE INTERNAL "List of OpenCV modules included into the build")
72 set(OPENCV_MODULES_DISABLED_USER "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
73 set(OPENCV_MODULES_DISABLED_AUTO "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
74 set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
75 unset(OPENCV_WORLD_MODULES CACHE)
77 # adds dependencies to OpenCV module
79 # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>] [WRAP <list of wrappers>])
81 # * <list of dependencies> - can include full names of modules or full paths to shared/static libraries or cmake targets
82 macro(ocv_add_dependencies full_modname)
83 ocv_debug_message("ocv_add_dependencies(" ${full_modname} ${ARGN} ")")
84 #we don't clean the dependencies here to allow this macro several times for every module
85 foreach(d "REQUIRED" ${ARGN})
86 if(d STREQUAL "REQUIRED")
87 set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
88 elseif(d STREQUAL "OPTIONAL")
89 set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
90 elseif(d STREQUAL "PRIVATE_REQUIRED")
91 set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
92 elseif(d STREQUAL "PRIVATE_OPTIONAL")
93 set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
94 elseif(d STREQUAL "WRAP")
95 set(__depsvar OPENCV_MODULE_${full_modname}_WRAPPERS)
97 list(APPEND ${__depsvar} "${d}")
104 list(FIND OPENCV_MODULE_${full_modname}_WRAPPERS "python" __python_idx)
105 if (NOT __python_idx EQUAL -1)
106 list(REMOVE_ITEM OPENCV_MODULE_${full_modname}_WRAPPERS "python")
107 list(APPEND OPENCV_MODULE_${full_modname}_WRAPPERS "python_bindings_generator" "python2" "python3")
111 ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
112 ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
113 ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
114 ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
115 ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
117 set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
118 CACHE INTERNAL "Required dependencies of ${full_modname} module")
119 set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
120 CACHE INTERNAL "Optional dependencies of ${full_modname} module")
121 set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
122 CACHE INTERNAL "Required private dependencies of ${full_modname} module")
123 set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
124 CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
125 set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
126 CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
129 # declare new OpenCV module in current folder
131 # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
133 # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
134 macro(ocv_add_module _name)
135 ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
136 string(TOLOWER "${_name}" name)
137 set(the_module opencv_${name})
139 # the first pass - collect modules info, the second pass - create targets
140 if(OPENCV_INITIAL_PASS)
141 #guard against redefinition
142 if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
143 message(FATAL_ERROR "Redefinition of the ${the_module} module.
144 at: ${CMAKE_CURRENT_SOURCE_DIR}
145 previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
149 if(NOT DEFINED the_description)
150 set(the_description "The ${name} OpenCV module")
153 if(NOT DEFINED BUILD_${the_module}_INIT)
154 set(BUILD_${the_module}_INIT ON)
157 # create option to enable/disable this module
158 option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
160 # remember the module details
161 set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
162 set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
164 set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
166 set(ADD_MODULE_ARGN ${ARGN})
167 ocv_cmake_hook(PRE_ADD_MODULE)
168 ocv_cmake_hook(PRE_ADD_MODULE_${the_module})
170 # parse list of dependencies
171 if(" ${ARGV1}" STREQUAL " INTERNAL" OR " ${ARGV1}" STREQUAL " BINDINGS")
172 set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
173 set(__ocv_argn__ ${ADD_MODULE_ARGN})
174 list(REMOVE_AT __ocv_argn__ 0)
175 ocv_add_dependencies(${the_module} ${__ocv_argn__})
178 set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
179 ocv_add_dependencies(${the_module} ${ADD_MODULE_ARGN})
180 if(BUILD_${the_module})
181 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
185 # add self to the world dependencies
186 if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD
187 AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS"
188 AND (NOT OPENCV_PROCESSING_EXTRA_MODULES OR NOT OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
189 AND (NOT BUILD_SHARED_LIBS OR NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC"))
190 OR OPENCV_MODULE_IS_PART_OF_WORLD
192 set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
193 ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
195 set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
198 if(NOT DEFINED the_label)
199 if(OPENCV_PROCESSING_EXTRA_MODULES)
200 set(the_label "Extra")
202 set(the_label "Main")
205 set(OPENCV_MODULE_${the_module}_LABEL "${the_label};${the_module}" CACHE INTERNAL "")
207 if(BUILD_${the_module})
208 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
210 set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
213 # add reverse wrapper dependencies
214 foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
215 ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
218 # stop processing of current file
219 ocv_cmake_hook(POST_ADD_MODULE)
220 ocv_cmake_hook(POST_ADD_MODULE_${the_module})
223 set(OPENCV_MODULE_${the_module}_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
224 if(NOT BUILD_${the_module})
225 return() # extra protection from redefinition
227 if(NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR NOT ${BUILD_opencv_world})
228 if (NOT ${the_module} STREQUAL opencv_world)
229 project(${the_module})
232 -D_USE_MATH_DEFINES # M_PI constant in MSVS
233 -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_FORMAT_MACROS # to use C libraries from C++ code (ffmpeg)
239 # excludes module from current configuration
240 macro(ocv_module_disable_ module)
241 set(__modname ${module})
242 if(NOT __modname MATCHES "^opencv_")
243 set(__modname opencv_${module})
245 list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
246 set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
247 set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
248 set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
249 if(BUILD_${__modname})
250 # touch variable controlling build of the module to suppress "unused variable" CMake warning
255 macro(ocv_module_disable module)
256 ocv_module_disable_(${module})
257 return() # leave the current folder
260 # gather acceptable locations and generate names for them
261 # if folder contains CMakeLists.txt - it is accepted,
262 # otherwise all first-level subfolders containing CMakeLists.txt are accepted.
263 # Usage: _glob_locations(<output paths list> <output names list> <folder> [<folder> ...])
264 function(_glob_locations out_paths out_names)
266 foreach(path ${PATHS})
267 #message(STATUS "Inspect: ${path}")
268 list(LENGTH paths before)
269 get_filename_component(path "${path}" ABSOLUTE)
270 # Either module itself
271 if(NOT path STREQUAL CMAKE_CURRENT_SOURCE_DIR AND EXISTS "${path}/CMakeLists.txt")
272 get_filename_component(name "${path}" NAME)
273 list(APPEND paths "${path}")
274 list(APPEND names "${name}")
276 # Either flat collection of modules
277 file(GLOB subdirs RELATIVE "${path}" "${path}/*")
278 foreach(subdir ${subdirs})
279 #message(STATUS "Inspect: ${path}/${subdir}")
280 if(EXISTS "${path}/${subdir}/CMakeLists.txt")
281 list(APPEND paths "${path}/${subdir}")
282 list(APPEND names "${subdir}")
286 list(LENGTH paths after)
287 if(before EQUAL after)
288 message(SEND_ERROR "No modules has been found: ${path}")
292 set(${out_paths} ${paths} PARENT_SCOPE)
293 set(${out_names} ${names} PARENT_SCOPE)
296 # Calls 'add_subdirectory' for each location.
297 # Note: both input lists should have same length.
298 # Usage: _add_modules_1(<list with paths> <list with names>)
299 function(_add_modules_1 paths names)
300 list(LENGTH ${paths} len)
304 list(LENGTH ${names} len_verify)
305 if(NOT len EQUAL len_verify)
306 message(FATAL_ERROR "Bad configuration! ${len} != ${len_verify}")
308 math(EXPR len "${len} - 1")
309 foreach(i RANGE ${len})
310 list(GET ${paths} ${i} path)
311 list(GET ${names} ${i} name)
312 #message(STATUS "First pass: ${name} => ${path}")
313 include("${path}/cmake/init.cmake" OPTIONAL)
314 add_subdirectory("${path}" "${CMAKE_CURRENT_BINARY_DIR}/.firstpass/${name}")
318 # Calls 'add_subdirectory' for each module name.
319 # Usage: _add_modules_2([<module> ...])
320 function(_add_modules_2)
322 set(the_module "${m}")
323 ocv_cmake_hook(PRE_MODULES_CREATE_${the_module})
324 if(BUILD_opencv_world AND m STREQUAL "opencv_world"
325 OR NOT BUILD_opencv_world
326 OR NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
327 if(NOT m MATCHES "^opencv_")
328 message(WARNING "Incorrect module name: ${m}")
330 string(REGEX REPLACE "^opencv_" "" name "${m}")
331 #message(STATUS "Second pass: ${name} => ${OPENCV_MODULE_${m}_LOCATION}")
332 add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${name}")
334 ocv_cmake_hook(POST_MODULES_CREATE_${the_module})
338 # Check if list of input items is unique.
339 # Usage: _assert_uniqueness(<failure message> <element> [<element> ...])
340 function(_assert_uniqueness msg)
341 ocv_get_duplicates(dups ${ARGN})
344 list(FIND dups "${e}" idx)
350 message("${prefix}${e}")
352 message(FATAL_ERROR "${msg}")
356 # collect modules from specified directories
357 # NB: must be called only once!
358 # Usage: ocv_glob_modules(<main location> [<extra location> ...])
359 macro(ocv_glob_modules main_root)
360 ocv_cmake_hook(INIT_MODULES_GLOB)
361 if(DEFINED OPENCV_INITIAL_PASS)
362 message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
366 set(OPENCV_INITIAL_PASS ON)
367 _glob_locations(__main_paths __main_names ${main_root})
368 _glob_locations(__extra_paths __extra_names ${ARGN})
369 _assert_uniqueness("Duplicated modules LOCATIONS has been found" ${__main_paths} ${__extra_paths})
370 _assert_uniqueness("Duplicated modules NAMES has been found" ${__main_names} ${__extra_names})
371 set(OPENCV_PROCESSING_EXTRA_MODULES 0)
372 ocv_cmake_hook(PRE_MODULES_SCAN)
373 _add_modules_1(__main_paths __main_names)
374 set(OPENCV_PROCESSING_EXTRA_MODULES 1)
375 ocv_cmake_hook(PRE_MODULES_SCAN_EXTRA)
376 _add_modules_1(__extra_paths __extra_names)
377 ocv_clear_vars(__main_names __extra_names __main_paths __extra_paths)
378 ocv_cmake_hook(POST_MODULES_SCAN)
380 # resolve dependencies
381 __ocv_resolve_dependencies()
384 set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
385 set(OPENCV_INITIAL_PASS OFF)
386 ocv_cmake_hook(PRE_MODULES_CREATE)
387 _add_modules_2(${OPENCV_MODULES_BUILD})
388 ocv_cmake_hook(POST_MODULES_CREATE)
392 # disables OpenCV module with missing dependencies
393 function(__ocv_module_turn_off the_module)
394 list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
395 list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
396 list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
397 list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
398 set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")
400 set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
401 set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
402 set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
405 # sort modules by dependencies
406 function(__ocv_sort_modules_by_deps __lst)
407 ocv_list_sort(${__lst})
408 set(input ${${__lst}})
412 list(LENGTH input length_before)
414 # check if module is in the result already
415 if (NOT ";${result};" MATCHES ";${m};")
416 # scan through module dependencies...
417 set(unresolved_deps_found FALSE)
418 foreach (d ${OPENCV_MODULE_${m}_DEPS})
419 # ... which are not already in the result and are enabled
420 if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
421 set(unresolved_deps_found TRUE)
425 # check if all dependencies for this module has been resolved
426 if (NOT unresolved_deps_found)
427 list(APPEND result ${m})
428 list(REMOVE_ITEM input ${m})
432 list(LENGTH input length_after)
433 # check for infinite loop or unresolved dependencies
434 if (NOT length_after LESS length_before)
435 if(NOT BUILD_SHARED_LIBS)
436 if (";${input};" MATCHES ";opencv_world;")
437 list(REMOVE_ITEM input "opencv_world")
438 list(APPEND result_extra "opencv_world")
440 # We can't do here something
441 list(APPEND result ${input})
445 message(FATAL_ERROR "FATAL: Unresolved dependencies or loop in dependency graph (${length_after})\n"
446 "Processed ${__lst}: ${${__lst}}\n"
447 "Good modules: ${result}\n"
448 "Bad modules: ${input}"
450 list(APPEND result ${input})
455 set(${__lst} "${result};${result_extra}" PARENT_SCOPE)
458 # resolve dependencies
459 function(__ocv_resolve_dependencies)
460 foreach(m ${OPENCV_MODULES_DISABLED_USER})
461 set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
463 foreach(m ${OPENCV_MODULES_BUILD})
464 set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
470 string(REGEX REPLACE "[ ,:]+" ";" whitelist "${BUILD_LIST}" )
471 if(BUILD_opencv_world)
472 list(APPEND whitelist world)
474 ocv_list_add_prefix(whitelist "opencv_")
475 ocv_list_sort(whitelist)
476 ocv_list_unique(whitelist)
477 message(STATUS "Using whitelist: ${whitelist}")
479 foreach(depth RANGE 10)
480 set(new_whitelist ${whitelist})
481 foreach(m ${whitelist})
482 list(APPEND new_whitelist ${OPENCV_MODULE_${m}_REQ_DEPS})
483 list(APPEND new_whitelist ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
485 ocv_list_sort(new_whitelist)
486 ocv_list_unique(new_whitelist)
487 if("${whitelist}" STREQUAL "${new_whitelist}")
490 set(whitelist "${new_whitelist}")
492 # Disable modules not in whitelist
493 foreach(m ${OPENCV_MODULES_BUILD})
494 list(FIND whitelist ${m} idx)
496 message(STATUS "Module ${m} disabled by whitelist")
497 __ocv_module_turn_off(${m})
502 # disable MODULES with unresolved dependencies
506 foreach(m ${OPENCV_MODULES_BUILD})
507 set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
509 ocv_list_pop_front(__deps d)
510 string(TOLOWER "${d}" upper_d)
511 if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
512 if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
513 message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
514 __ocv_module_turn_off(${m})
518 message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
525 # message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")
527 foreach(m ${OPENCV_MODULES_BUILD})
528 set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
529 foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
530 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
531 if(HAVE_${d} OR TARGET ${d})
532 list(APPEND deps_${m} ${d})
536 # message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
539 # propagate dependencies
543 foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
544 foreach(m ${OPENCV_MODULES_BUILD})
545 if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
546 foreach(d ${deps_${m2}})
547 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
548 # message(STATUS " Transfer dependency ${d} from ${m2} to ${m}")
549 list(APPEND deps_${m} ${d})
552 if(BUILD_opencv_world
553 AND NOT "${m}" STREQUAL "opencv_world"
554 AND NOT "${m2}" STREQUAL "opencv_world"
555 AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD
556 AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
557 if(NOT (";${deps_${m}};" MATCHES ";opencv_world;"))
558 # message(STATUS " Transfer dependency opencv_world alias ${m2} to ${m}")
559 list(APPEND deps_${m} opencv_world)
569 # process private deps
570 foreach(m ${OPENCV_MODULES_BUILD})
571 foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
572 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
573 list(APPEND deps_${m} ${d})
576 foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
577 if(NOT (";${deps_${m}};" MATCHES ";${d};"))
578 if(HAVE_${d} OR TARGET ${d})
579 list(APPEND deps_${m} ${d})
585 ocv_list_sort(OPENCV_MODULES_BUILD)
587 foreach(m ${OPENCV_MODULES_BUILD})
588 # message(STATUS "FULL deps of ${m}: ${deps_${m}}")
589 set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
590 set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
591 ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
592 if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
593 list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
597 # reorder dependencies
598 foreach(m ${OPENCV_MODULES_BUILD})
599 __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
601 set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
604 if(BUILD_opencv_world)
605 if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
606 list(APPEND OPENCV_WORLD_MODULES ${m})
608 foreach(m2 ${OPENCV_MODULES_BUILD})
609 if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
610 if(";${LINK_DEPS};" MATCHES ";${m2};")
611 list(REMOVE_ITEM LINK_DEPS ${m2})
612 if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
613 list(APPEND LINK_DEPS opencv_world)
616 if(${m} STREQUAL opencv_world)
617 list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
623 set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
624 set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
625 set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
627 # message(STATUS " module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
628 # message(STATUS " module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
629 # message(STATUS " extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
633 __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
635 set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} CACHE INTERNAL "List of OpenCV modules marked for export")
636 set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} CACHE INTERNAL "List of OpenCV modules included into the build")
637 set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
638 set(OPENCV_WORLD_MODULES ${OPENCV_WORLD_MODULES} CACHE INTERNAL "List of OpenCV modules included into the world")
642 # setup include paths for the list of passed modules
643 macro(ocv_include_modules)
645 if(d MATCHES "^opencv_" AND HAVE_${d})
646 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
647 ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
649 elseif(EXISTS "${d}")
650 ocv_include_directories("${d}")
655 # same as previous but with dependencies
656 macro(ocv_include_modules_recurse)
657 ocv_include_modules(${ARGN})
659 if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
660 foreach (sub ${OPENCV_MODULE_${d}_DEPS})
661 ocv_include_modules(${sub})
667 # setup include paths for the list of passed modules
668 macro(ocv_target_include_modules target)
670 if(d MATCHES "^opencv_")
671 if(HAVE_${d} AND EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
672 ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
674 elseif(EXISTS "${d}")
675 ocv_target_include_directories(${target} "${d}")
677 message(WARNING "Unexpected include: ${d} (module=${the_module})")
682 # setup include paths for the list of passed modules and recursively add dependent modules
683 macro(ocv_target_include_modules_recurse target)
685 if(d MATCHES "^opencv_" AND HAVE_${d})
686 if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
687 ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
689 if(OPENCV_MODULE_${d}_DEPS)
690 ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
692 elseif(EXISTS "${d}")
693 ocv_target_include_directories(${target} "${d}")
698 # setup include path for OpenCV headers for specified module
699 # ocv_module_include_directories(<extra include directories/extra include modules>)
700 macro(ocv_module_include_directories)
701 if(ENABLE_PRECOMPILED_HEADERS OR OPENCV_INCLUDE_DIR_APPEND_MODULE_SRC)
702 ocv_target_include_directories(${the_module} "${OPENCV_MODULE_${the_module}_LOCATION}/src")
704 ocv_target_include_directories(${the_module}
705 "${OPENCV_MODULE_${the_module}_LOCATION}/include"
706 "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
708 ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
712 # sets header and source files for the current module
713 # NB: all files specified as headers will be installed
715 # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
716 macro(ocv_set_module_sources)
717 ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
719 set(OPENCV_MODULE_${the_module}_HEADERS "")
720 set(OPENCV_MODULE_${the_module}_SOURCES "")
722 foreach(f "HEADERS" ${ARGN})
723 if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
724 set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
726 list(APPEND ${__filesvar} "${f}")
730 # the hacky way to embed any files into the OpenCV without modification of its build system
731 if(COMMAND ocv_get_module_external_sources)
732 ocv_get_module_external_sources()
735 if(OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED)
736 list(APPEND OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED})
739 # TODO Update hooks above
740 ocv_cmake_hook(INIT_MODULE_SOURCES)
741 ocv_cmake_hook(INIT_MODULE_SOURCES_${the_module})
743 # use full paths for module to be independent from the module location
744 ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
746 set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
747 set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
750 # finds and sets headers and sources for the standard OpenCV module
752 # ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
753 macro(ocv_glob_module_sources)
754 ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
756 list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
757 if(NOT exclude_cuda EQUAL -1)
758 list(REMOVE_AT _argn ${exclude_cuda})
760 list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
761 if(NOT exclude_opencl EQUAL -1)
762 list(REMOVE_AT _argn ${exclude_opencl})
765 file(GLOB_RECURSE lib_srcs
766 "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
768 file(GLOB_RECURSE lib_int_hdrs
769 "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
770 "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
773 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
774 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
775 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
776 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp"
777 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h"
778 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.hpp"
779 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.h"
781 file(GLOB lib_hdrs_detail
782 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
783 "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
786 file(GLOB_RECURSE lib_srcs_apple
787 "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
789 list(APPEND lib_srcs ${lib_srcs_apple})
792 ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
793 ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
795 set(lib_cuda_srcs "")
796 set(lib_cuda_hdrs "")
797 if(HAVE_CUDA AND exclude_cuda EQUAL -1)
798 file(GLOB lib_cuda_srcs
799 "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
801 file(GLOB lib_cuda_hdrs
802 "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
804 source_group("Src\\Cuda" FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
808 "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
810 if(cl_kernels AND exclude_opencl EQUAL -1)
811 set(OCL_NAME opencl_kernels_${name})
813 OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" # don't add .hpp file here to optimize build process
814 COMMAND ${CMAKE_COMMAND} "-DMODULE_NAME=${name}" "-DCL_DIR=${CMAKE_CURRENT_LIST_DIR}/src/opencl" "-DOUTPUT=${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
815 DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
816 COMMENT "Processing OpenCL kernels (${name})"
818 ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
819 ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
820 set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
821 PROPERTIES GENERATED TRUE
823 list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
826 ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
827 SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
830 # creates OpenCV module in current folder
831 # creates new target, configures standard dependencies, compilers flags, install rules
833 # ocv_create_module(<extra link dependencies>)
834 # ocv_create_module()
835 macro(ocv_create_module)
836 ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
837 if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
838 message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
840 if(NOT " ${ARGN}" STREQUAL " ")
841 set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
843 if(${BUILD_opencv_world} AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
845 set(the_module_target opencv_world)
847 _ocv_create_module(${ARGN})
848 set(the_module_target ${the_module})
852 # removing APPCONTAINER from modules to run from console
853 # in case of usual starting of WinRT test apps output is missing
854 # so starting of console version w/o APPCONTAINER is required to get test results
855 # also this allows to use opencv_extra test data for these tests
856 if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
857 add_custom_command(TARGET ${the_module}
859 COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
862 if("${the_module}" STREQUAL "opencv_ts")
863 # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
864 # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
865 add_custom_command(TARGET ${the_module}
867 COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
868 COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
869 COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
874 macro(_ocv_create_module)
876 ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
877 set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
878 set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
880 # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
881 # be called at some point in the future. We can't look into the future, though,
882 # so this will have to do.
883 if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
884 get_native_precompiled_header(${the_module} precomp.hpp)
888 AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
889 AND NOT OPENCV_VS_VERSIONINFO_SKIP)
890 if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
891 set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
892 elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
893 set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
894 elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
895 set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
896 ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
898 FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
899 INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
900 ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
904 if(NOT EXISTS "${_VS_VERSION_FILE}")
905 message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
907 source_group("Src" FILES "${_VS_VERSION_FILE}")
911 source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
912 source_group("Src" FILES "${${the_module}_pch}")
913 ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
914 ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
915 ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
916 "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
920 set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
921 set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
922 PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
924 ocv_target_link_libraries(${the_module} LINK_PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
925 ocv_target_link_libraries(${the_module} LINK_PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_EXT})
926 ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
928 ocv_target_link_libraries(${the_module} LINK_PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
931 if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
932 target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
933 unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
936 add_dependencies(opencv_modules ${the_module})
938 if(ENABLE_SOLUTION_FOLDERS)
939 set_target_properties(${the_module} PROPERTIES FOLDER "modules")
942 set_target_properties(${the_module} PROPERTIES
943 OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
944 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
945 COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
946 COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
947 ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
948 COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
949 LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
950 RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
951 DEFINE_SYMBOL CVAPI_EXPORTS
954 if(BUILD_FAT_JAVA_LIB) # force exports from static modules too
955 if(BUILD_SHARED_LIBS)
956 message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
958 target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
961 # For dynamic link numbering conventions
963 # Android SDK build scripts can include only .so files into final .apk
964 # As result we should not set version properties for Android
965 set_target_properties(${the_module} PROPERTIES
966 VERSION ${OPENCV_LIBVERSION}
967 SOVERSION ${OPENCV_SOVERSION}
971 if (ENABLE_GNU_STL_DEBUG)
972 target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
976 if(CMAKE_CROSSCOMPILING)
977 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
979 set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
982 get_target_property(_target_type ${the_module} TYPE)
983 if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
984 ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
985 ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
986 RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
987 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
988 ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
991 if("${_target_type}" STREQUAL "SHARED_LIBRARY")
992 install(TARGETS ${the_module}
993 LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
996 # only "public" headers need to be installed
997 ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
998 ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
999 if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
1000 foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
1001 string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
1002 if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
1003 install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
1008 _ocv_add_precompiled_headers(${the_module})
1010 ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
1011 ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
1014 # opencv precompiled headers macro (can add pch to modules and tests)
1015 # this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
1017 # ocv_add_precompiled_headers(${the_module})
1018 macro(_ocv_add_precompiled_headers the_target)
1019 ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
1021 if("${the_target}" MATCHES "^opencv_test_.*$")
1022 SET(pch_path "test/test_")
1023 elseif("${the_target}" MATCHES "^opencv_perf_.*$")
1024 SET(pch_path "perf/perf_")
1026 SET(pch_path "src/")
1028 ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
1032 # short command for adding simple OpenCV module
1033 # see ocv_add_module for argument details
1035 # ocv_define_module(module_name [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
1036 macro(ocv_define_module module_name)
1037 ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
1039 set(exclude_cuda "")
1040 foreach(arg ${_argn})
1041 if("${arg}" STREQUAL "EXCLUDE_CUDA")
1042 set(exclude_cuda "${arg}")
1043 list(REMOVE_ITEM _argn ${arg})
1047 ocv_add_module(${module_name} ${_argn})
1048 ocv_glob_module_sources(${exclude_cuda})
1049 ocv_module_include_directories()
1052 ocv_add_accuracy_tests()
1053 ocv_add_perf_tests()
1057 # ensures that all passed modules are available
1058 # sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
1059 macro(ocv_check_dependencies)
1060 set(OCV_DEPENDENCIES_FOUND TRUE)
1062 if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
1063 set(OCV_DEPENDENCIES_FOUND FALSE)
1069 # auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
1070 macro(__ocv_parse_test_sources tests_type)
1071 set(OPENCV_${tests_type}_${the_module}_SOURCES "")
1072 set(OPENCV_${tests_type}_${the_module}_DEPS "")
1073 set(__file_group_name "")
1074 set(__file_group_sources "")
1075 foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
1076 if(arg STREQUAL "FILES")
1077 set(__currentvar "__file_group_sources")
1078 if(__file_group_name AND __file_group_sources)
1079 source_group("${__file_group_name}" FILES ${__file_group_sources})
1080 list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
1082 set(__file_group_name "")
1083 set(__file_group_sources "")
1084 elseif(arg STREQUAL "DEPENDS_ON")
1085 set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
1086 elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
1087 set(__file_group_name "${arg}")
1089 list(APPEND ${__currentvar} "${arg}")
1092 unset(__file_group_name)
1093 unset(__file_group_sources)
1097 # this is a command for adding OpenCV performance tests to the module
1098 # ocv_add_perf_tests(<extra_dependencies>)
1099 function(ocv_add_perf_tests)
1100 ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
1103 set(OPENCV_DEBUG_POSTFIX "")
1106 set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
1107 if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
1108 __ocv_parse_test_sources(PERF ${ARGN})
1110 # opencv_imgcodecs is required for imread/imwrite
1111 set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1112 ocv_check_dependencies(${perf_deps})
1114 if(OCV_DEPENDENCIES_FOUND)
1115 set(the_target "opencv_perf_${name}")
1116 # project(${the_target})
1118 if(NOT OPENCV_PERF_${the_module}_SOURCES)
1119 file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
1120 file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
1121 ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
1122 ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
1123 set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
1126 ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
1128 if(NOT BUILD_opencv_world)
1129 get_native_precompiled_header(${the_target} perf_precomp.hpp)
1132 source_group("Src" FILES "${${the_target}_pch}")
1133 ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
1134 ocv_target_include_modules(${the_target} ${perf_deps} "${perf_path}")
1135 ocv_target_link_libraries(${the_target} LINK_PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS})
1136 add_dependencies(opencv_perf_tests ${the_target})
1138 set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1139 set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
1140 PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1142 # Additional target properties
1143 set_target_properties(${the_target} PROPERTIES
1144 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1145 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1147 if(ENABLE_SOLUTION_FOLDERS)
1148 set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
1152 # removing APPCONTAINER from tests to run from console
1153 # look for detailed description inside of ocv_create_module macro above
1154 add_custom_command(TARGET "opencv_perf_${name}"
1156 COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
1159 if(NOT BUILD_opencv_world)
1160 _ocv_add_precompiled_headers(${the_target})
1163 ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
1164 ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
1165 "--perf_min_samples=1"
1166 "--perf_force_samples=1"
1167 "--perf_verify_sanity")
1168 else(OCV_DEPENDENCIES_FOUND)
1169 # TODO: warn about unsatisfied dependencies
1170 endif(OCV_DEPENDENCIES_FOUND)
1172 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1177 # this is a command for adding OpenCV accuracy/regression tests to the module
1178 # ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
1179 function(ocv_add_accuracy_tests)
1180 ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
1183 set(OPENCV_DEBUG_POSTFIX "")
1186 set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
1187 if(BUILD_TESTS AND EXISTS "${test_path}")
1188 __ocv_parse_test_sources(TEST ${ARGN})
1190 # opencv_imgcodecs is required for imread/imwrite
1191 set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1192 ocv_check_dependencies(${test_deps})
1193 if(OCV_DEPENDENCIES_FOUND)
1194 set(the_target "opencv_test_${name}")
1195 # project(${the_target})
1197 if(NOT OPENCV_TEST_${the_module}_SOURCES)
1198 file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
1199 file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
1200 ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
1201 ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
1202 set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
1205 ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
1207 if(NOT BUILD_opencv_world)
1208 get_native_precompiled_header(${the_target} test_precomp.hpp)
1211 source_group("Src" FILES "${${the_target}_pch}")
1212 ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
1213 ocv_target_include_modules(${the_target} ${test_deps} "${test_path}")
1214 ocv_target_link_libraries(${the_target} LINK_PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS})
1215 add_dependencies(opencv_tests ${the_target})
1217 set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1218 set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
1219 PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1221 # Additional target properties
1222 set_target_properties(${the_target} PROPERTIES
1223 DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1224 RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1227 ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
1229 if(ENABLE_SOLUTION_FOLDERS)
1230 set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
1233 if(OPENCV_TEST_BIGDATA)
1234 ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
1237 if(NOT BUILD_opencv_world)
1238 _ocv_add_precompiled_headers(${the_target})
1241 ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
1242 else(OCV_DEPENDENCIES_FOUND)
1243 # TODO: warn about unsatisfied dependencies
1244 endif(OCV_DEPENDENCIES_FOUND)
1247 install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1252 function(ocv_add_samples)
1253 ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
1255 set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1256 if(NOT EXISTS "${samples_path}")
1260 string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
1263 set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1264 ocv_check_dependencies(${samples_deps})
1266 if(OCV_DEPENDENCIES_FOUND)
1267 file(GLOB sample_sources "${samples_path}/*.cpp")
1269 foreach(source ${sample_sources})
1270 get_filename_component(name "${source}" NAME_WE)
1271 set(the_target "example_${module_id}_${name}")
1273 ocv_add_executable(${the_target} "${source}")
1274 ocv_target_include_modules(${the_target} ${samples_deps})
1275 ocv_target_link_libraries(${the_target} LINK_PRIVATE ${samples_deps})
1277 set_target_properties(${the_target} PROPERTIES
1278 PROJECT_LABEL "(sample) ${name}"
1279 LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1280 set_source_files_properties("${source}" PROPERTIES
1281 LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1282 if(ENABLE_SOLUTION_FOLDERS)
1283 set_target_properties(${the_target} PROPERTIES
1284 FOLDER "samples/${module_id}")
1286 # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
1287 set(parent_target opencv_samples_${module_id})
1288 if(NOT TARGET ${parent_target})
1289 add_custom_target(${parent_target})
1290 add_dependencies(opencv_samples ${parent_target})
1292 add_dependencies(${parent_target} ${the_target})
1295 install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
1301 if(INSTALL_C_EXAMPLES)
1302 file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1303 foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
1304 IF( IS_DIRECTORY "${ITEM}" )
1305 LIST( APPEND sample_dirs "${ITEM}" )
1307 LIST( APPEND sample_files "${ITEM}" )
1310 install(FILES ${sample_files}
1311 DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1313 install(DIRECTORY ${sample_dirs}
1314 DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"