OpenCVModule.cmake 55.3 KB
Newer Older
1 2 3 4 5 6 7 8
# Local variables (set for each module):
#
# name       - short name in lower case i.e. core
# the_module - full name in lower case i.e. opencv_core

# Global variables:
#
# OPENCV_MODULE_${the_module}_LOCATION
Alexander Alekhin's avatar
Alexander Alekhin committed
9
# OPENCV_MODULE_${the_module}_BINARY_DIR
10
# OPENCV_MODULE_${the_module}_DESCRIPTION
11
# OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
12 13 14
# OPENCV_MODULE_${the_module}_HEADERS
# OPENCV_MODULE_${the_module}_SOURCES
# OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
Alexander Alekhin's avatar
Alexander Alekhin committed
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 18
# OPENCV_MODULE_${the_module}_REQ_DEPS
# OPENCV_MODULE_${the_module}_OPT_DEPS
19 20
# OPENCV_MODULE_${the_module}_PRIVATE_REQ_DEPS
# OPENCV_MODULE_${the_module}_PRIVATE_OPT_DEPS
Alexander Alekhin's avatar
Alexander Alekhin committed
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 25 26 27
# HAVE_${the_module} - for fast check of module availability

# To control the setup of the module you could also set:
# 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 31
# OPENCV_MODULE_IS_PART_OF_WORLD - ON|OFF (default ON) - should the module be added to the opencv_world?
# BUILD_${the_module}_INIT - ON|OFF (default ON) - initial value for BUILD_${the_module}
32 33 34 35

# The verbose template for OpenCV module:
#
#   ocv_add_module(modname <dependencies>)
36 37
#   ocv_glob_module_sources(([EXCLUDE_CUDA] <extra sources&headers>)
#                          or glob them manually and ocv_set_module_sources(...)
38 39 40 41 42 43 44
#   ocv_module_include_directories(<extra include directories>)
#   ocv_create_module()
#   <add extra link dependencies, compiler options, etc>
#   ocv_add_precompiled_headers(${the_module})
#   <add extra installation rules>
#   ocv_add_accuracy_tests(<extra dependencies>)
#   ocv_add_perf_tests(<extra dependencies>)
45
#   ocv_add_samples(<extra dependencies>)
46 47 48 49 50 51 52 53
#
#
# If module have no "extra" then you can define it in one line:
#
#   ocv_define_module(modname <dependencies>)

# clean flags for modules enabled on previous cmake run
# 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})
55 56 57
  if(HAVE_${mod})
    unset(HAVE_${mod} CACHE)
  endif()
58 59
  unset(OPENCV_MODULE_${mod}_DEPS CACHE)
  unset(OPENCV_MODULE_${mod}_DEPS_EXT CACHE)
60 61
  unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
  unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
62 63
  unset(OPENCV_MODULE_${mod}_PRIVATE_REQ_DEPS CACHE)
  unset(OPENCV_MODULE_${mod}_PRIVATE_OPT_DEPS CACHE)
Alexander Alekhin's avatar
Alexander Alekhin committed
64
  unset(OPENCV_MODULE_${mod}_LINK_DEPS CACHE)
Maksim Shabunin's avatar
Maksim Shabunin committed
65
  unset(OPENCV_MODULE_${mod}_WRAPPERS CACHE)
66
  unset(OPENCV_DEPENDANT_TARGETS_${mod} CACHE)
67 68 69 70 71 72 73 74
endforeach()

# clean modules info which needs to be recalculated
set(OPENCV_MODULES_PUBLIC         "" CACHE INTERNAL "List of OpenCV modules marked for export")
set(OPENCV_MODULES_BUILD          "" CACHE INTERNAL "List of OpenCV modules included into the build")
set(OPENCV_MODULES_DISABLED_USER  "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
set(OPENCV_MODULES_DISABLED_AUTO  "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
Alexander Alekhin's avatar
Alexander Alekhin committed
75
unset(OPENCV_WORLD_MODULES CACHE)
76 77 78

# adds dependencies to OpenCV module
# Usage:
79
#   add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>] [WRAP <list of wrappers>])
80
# Notes:
luz.paz's avatar
luz.paz committed
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)
Alexander Alekhin's avatar
Alexander Alekhin committed
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 86
  foreach(d "REQUIRED" ${ARGN})
    if(d STREQUAL "REQUIRED")
87 88 89
      set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
    elseif(d STREQUAL "OPTIONAL")
      set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
90 91 92 93
    elseif(d STREQUAL "PRIVATE_REQUIRED")
      set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
    elseif(d STREQUAL "PRIVATE_OPTIONAL")
      set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
94 95
    elseif(d STREQUAL "WRAP")
      set(__depsvar OPENCV_MODULE_${full_modname}_WRAPPERS)
96 97 98 99
    else()
      list(APPEND ${__depsvar} "${d}")
    endif()
  endforeach()
100 101
  unset(__depsvar)

Maksim Shabunin's avatar
Maksim Shabunin committed
102 103 104 105 106
  # hack for python
  set(__python_idx)
  list(FIND OPENCV_MODULE_${full_modname}_WRAPPERS "python" __python_idx)
  if (NOT __python_idx EQUAL -1)
    list(REMOVE_ITEM OPENCV_MODULE_${full_modname}_WRAPPERS "python")
107
    list(APPEND OPENCV_MODULE_${full_modname}_WRAPPERS "python_bindings_generator" "python2" "python3")
Maksim Shabunin's avatar
Maksim Shabunin committed
108 109 110
  endif()
  unset(__python_idx)

111 112
  ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
  ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
113 114
  ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
  ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
115
  ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
116 117 118 119 120 121 122 123 124

  set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
    CACHE INTERNAL "Required dependencies of ${full_modname} module")
  set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
    CACHE INTERNAL "Optional dependencies of ${full_modname} module")
  set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
    CACHE INTERNAL "Required private dependencies of ${full_modname} module")
  set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
    CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
125 126
  set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
    CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
127 128
endmacro()

129 130
# declare new OpenCV module in current folder
# Usage:
131
#   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
132
# Example:
133
#   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
134
macro(ocv_add_module _name)
Alexander Alekhin's avatar
Alexander Alekhin committed
135
  ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
136 137
  string(TOLOWER "${_name}" name)
  set(the_module opencv_${name})
138

139 140
  # the first pass - collect modules info, the second pass - create targets
  if(OPENCV_INITIAL_PASS)
luz.paz's avatar
luz.paz committed
141
    #guard against redefinition
142 143 144 145 146 147 148
    if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
      message(FATAL_ERROR "Redefinition of the ${the_module} module.
  at:                    ${CMAKE_CURRENT_SOURCE_DIR}
  previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
")
    endif()

149 150 151
    if(NOT DEFINED the_description)
      set(the_description "The ${name} OpenCV module")
    endif()
152

153 154
    if(NOT DEFINED BUILD_${the_module}_INIT)
      set(BUILD_${the_module}_INIT ON)
155
    endif()
156 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})
159

160 161 162 163
    # remember the module details
    set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
    set(OPENCV_MODULE_${the_module}_LOCATION    "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")

Alexander Alekhin's avatar
Alexander Alekhin committed
164 165
    set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")

166 167 168 169
    set(ADD_MODULE_ARGN ${ARGN})
    ocv_cmake_hook(PRE_ADD_MODULE)
    ocv_cmake_hook(PRE_ADD_MODULE_${the_module})

170
    # parse list of dependencies
171
    if(" ${ARGV1}" STREQUAL " INTERNAL" OR " ${ARGV1}" STREQUAL " BINDINGS")
Ilya Lavrenov's avatar
Ilya Lavrenov committed
172
      set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
173
      set(__ocv_argn__ ${ADD_MODULE_ARGN})
174 175 176
      list(REMOVE_AT __ocv_argn__ 0)
      ocv_add_dependencies(${the_module} ${__ocv_argn__})
      unset(__ocv_argn__)
177
    else()
Ilya Lavrenov's avatar
Ilya Lavrenov committed
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 182
        set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
      endif()
183
    endif()
184

185
    # add self to the world dependencies
186 187
    if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD
        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"))
Alexander Alekhin's avatar
Alexander Alekhin committed
190 191 192
        OR OPENCV_MODULE_IS_PART_OF_WORLD
        )
      set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
193
      ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
Alexander Alekhin's avatar
Alexander Alekhin committed
194 195
    else()
      set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
196
    endif()
197

198 199 200 201 202 203 204 205 206
    if(NOT DEFINED the_label)
      if(OPENCV_PROCESSING_EXTRA_MODULES)
        set(the_label "Extra")
      else()
        set(the_label "Main")
      endif()
    endif()
    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")
209 210
    else()
      set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
211
    endif()
212

213 214 215 216 217
    # add reverse wrapper dependencies
    foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
      ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
    endforeach()

218
    # stop processing of current file
219 220
    ocv_cmake_hook(POST_ADD_MODULE)
    ocv_cmake_hook(POST_ADD_MODULE_${the_module})
221
    return()
Alexander Alekhin's avatar
Alexander Alekhin committed
222 223
  else()
    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
226
    endif()
227 228 229 230
    if(NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR NOT ${BUILD_opencv_world})
      if (NOT ${the_module} STREQUAL opencv_world)
        project(${the_module})
      endif()
231 232
      add_definitions(
        -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)
234
      )
Alexander Alekhin's avatar
Alexander Alekhin committed
235 236
    endif()
  endif()
237 238
endmacro()

239
# excludes module from current configuration
240
macro(ocv_module_disable_ module)
241 242 243 244 245 246
  set(__modname ${module})
  if(NOT __modname MATCHES "^opencv_")
    set(__modname opencv_${module})
  endif()
  list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
  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 250 251
  if(BUILD_${__modname})
    # touch variable controlling build of the module to suppress "unused variable" CMake warning
  endif()
252 253
  unset(__modname)
endmacro()
254

255 256 257 258
macro(ocv_module_disable module)
  ocv_module_disable_(${module})
  return() # leave the current folder
endmacro()
259

260 261 262 263 264
# gather acceptable locations and generate names for them
# if folder contains CMakeLists.txt - it is accepted,
# otherwise all first-level subfolders containing CMakeLists.txt are accepted.
# Usage: _glob_locations(<output paths list> <output names list> <folder> [<folder> ...])
function(_glob_locations out_paths out_names)
265 266
  set(PATHS ${ARGN})
  foreach(path ${PATHS})
267
    #message(STATUS "Inspect: ${path}")
268 269 270 271 272 273 274 275 276 277 278
    list(LENGTH paths before)
    get_filename_component(path "${path}" ABSOLUTE)
    # Either module itself
    if(NOT path STREQUAL CMAKE_CURRENT_SOURCE_DIR AND EXISTS "${path}/CMakeLists.txt")
      get_filename_component(name "${path}" NAME)
      list(APPEND paths "${path}")
      list(APPEND names "${name}")
    else()
      # Either flat collection of modules
      file(GLOB subdirs RELATIVE "${path}" "${path}/*")
      foreach(subdir ${subdirs})
279
        #message(STATUS "Inspect: ${path}/${subdir}")
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
        if(EXISTS "${path}/${subdir}/CMakeLists.txt")
          list(APPEND paths "${path}/${subdir}")
          list(APPEND names "${subdir}")
        endif()
      endforeach()
    endif()
    list(LENGTH paths after)
    if(before EQUAL after)
      message(SEND_ERROR "No modules has been found: ${path}")
    endif()
  endforeach()
  # Return
  set(${out_paths} ${paths} PARENT_SCOPE)
  set(${out_names} ${names} PARENT_SCOPE)
endfunction()

# Calls 'add_subdirectory' for each location.
# Note: both input lists should have same length.
# Usage: _add_modules_1(<list with paths> <list with names>)
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
macro(_add_modules_1 paths names)
  ocv_debug_message("_add_modules_1(paths=${paths}, names=${names}, ... " ${ARGN} ")")
  list(LENGTH ${paths} __len)
  if(NOT __len EQUAL 0)
    list(LENGTH ${names} __len_verify)
    if(NOT __len EQUAL __len_verify)
      message(FATAL_ERROR "Bad configuration! ${__len} != ${__len_verify}")
    endif()
    math(EXPR __len "${__len} - 1")
    foreach(i RANGE ${__len})
      list(GET ${paths} ${i} __path)
      list(GET ${names} ${i} __name)
      #message(STATUS "First pass: ${__name} => ${__path}")
      include("${__path}/cmake/init.cmake" OPTIONAL)
      add_subdirectory("${__path}" "${CMAKE_CURRENT_BINARY_DIR}/.firstpass/${__name}")
    endforeach()
315
  endif()
316
endmacro()
317 318 319

# Calls 'add_subdirectory' for each module name.
# Usage: _add_modules_2([<module> ...])
320 321
macro(_add_modules_2)
  ocv_debug_message("_add_modules_2(" ${ARGN} ")")
322 323
  foreach(m ${ARGN})
    set(the_module "${m}")
324
    ocv_cmake_hook(PRE_MODULES_CREATE_${the_module})
325 326 327 328 329 330 331
    if(BUILD_opencv_world AND m STREQUAL "opencv_world"
        OR NOT BUILD_opencv_world
        OR NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
      if(NOT m MATCHES "^opencv_")
        message(WARNING "Incorrect module name: ${m}")
      endif()
      string(REGEX REPLACE "^opencv_" "" name "${m}")
332
      #message(STATUS "Second pass: ${name} => ${OPENCV_MODULE_${m}_LOCATION}")
333 334
      add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${name}")
    endif()
335
    ocv_cmake_hook(POST_MODULES_CREATE_${the_module})
336
  endforeach()
337 338
  unset(the_module)
endmacro()
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357

# Check if list of input items is unique.
# Usage: _assert_uniqueness(<failure message> <element> [<element> ...])
function(_assert_uniqueness msg)
  ocv_get_duplicates(dups ${ARGN})
  if(dups)
    foreach(e ${ARGN})
      list(FIND dups "${e}" idx)
      if(NOT idx EQUAL -1)
        set(prefix " > ")
      else()
        set(prefix "   ")
      endif()
      message("${prefix}${e}")
    endforeach()
    message(FATAL_ERROR "${msg}")
  endif()
endfunction()

358 359
# collect modules from specified directories
# NB: must be called only once!
360 361
# Usage: ocv_glob_modules(<main location> [<extra location> ...])
macro(ocv_glob_modules main_root)
362
  ocv_cmake_hook(INIT_MODULES_GLOB)
363 364 365 366
  if(DEFINED OPENCV_INITIAL_PASS)
    message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
  endif()

367
  # collect modules
368
  set(OPENCV_INITIAL_PASS ON)
369 370 371 372
  _glob_locations(__main_paths __main_names ${main_root})
  _glob_locations(__extra_paths __extra_names ${ARGN})
  _assert_uniqueness("Duplicated modules LOCATIONS has been found" ${__main_paths} ${__extra_paths})
  _assert_uniqueness("Duplicated modules NAMES has been found" ${__main_names} ${__extra_names})
Alexander Alekhin's avatar
Alexander Alekhin committed
373
  set(OPENCV_PROCESSING_EXTRA_MODULES 0)
374
  ocv_cmake_hook(PRE_MODULES_SCAN)
375 376
  _add_modules_1(__main_paths __main_names)
  set(OPENCV_PROCESSING_EXTRA_MODULES 1)
377
  ocv_cmake_hook(PRE_MODULES_SCAN_EXTRA)
378 379
  _add_modules_1(__extra_paths __extra_names)
  ocv_clear_vars(__main_names __extra_names __main_paths __extra_paths)
380
  ocv_cmake_hook(POST_MODULES_SCAN)
381

382
  # resolve dependencies
383
  __ocv_resolve_dependencies()
384

385
  # create modules
386
  set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
387
  set(OPENCV_INITIAL_PASS OFF)
388
  ocv_cmake_hook(PRE_MODULES_CREATE)
389
  _add_modules_2(${OPENCV_MODULES_BUILD})
390
  ocv_cmake_hook(POST_MODULES_CREATE)
391 392
endmacro()

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409

# disables OpenCV module with missing dependencies
function(__ocv_module_turn_off the_module)
  list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
  list(APPEND OPENCV_MODULES_DISABLED_AUTO "${the_module}")
  list(REMOVE_ITEM OPENCV_MODULES_BUILD "${the_module}")
  list(REMOVE_ITEM OPENCV_MODULES_PUBLIC "${the_module}")
  set(HAVE_${the_module} OFF CACHE INTERNAL "Module ${the_module} can not be built in current configuration")

  set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
  set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
  set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
endfunction()

# sort modules by dependencies
function(__ocv_sort_modules_by_deps __lst)
  ocv_list_sort(${__lst})
410 411
  set(input ${${__lst}})
  set(result "")
412
  set(result_extra "")
413 414 415 416 417 418 419
  while(input)
    list(LENGTH input length_before)
    foreach (m ${input})
      # check if module is in the result already
      if (NOT ";${result};" MATCHES ";${m};")
        # scan through module dependencies...
        set(unresolved_deps_found FALSE)
420
        foreach (d ${OPENCV_MODULE_${m}_DEPS})
421 422 423 424 425 426
          # ... which are not already in the result and are enabled
          if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
            set(unresolved_deps_found TRUE)
            break()
          endif()
        endforeach()
luz.paz's avatar
luz.paz committed
427
        # check if all dependencies for this module has been resolved
428 429 430
        if (NOT unresolved_deps_found)
          list(APPEND result ${m})
          list(REMOVE_ITEM input ${m})
431 432 433
        endif()
      endif()
    endforeach()
434 435 436
    list(LENGTH input length_after)
    # check for infinite loop or unresolved dependencies
    if (NOT length_after LESS length_before)
437 438 439 440 441 442 443 444 445 446
      if(NOT BUILD_SHARED_LIBS)
        if (";${input};" MATCHES ";opencv_world;")
          list(REMOVE_ITEM input "opencv_world")
          list(APPEND result_extra "opencv_world")
        else()
          # We can't do here something
          list(APPEND result ${input})
          break()
        endif()
      else()
447
        message(FATAL_ERROR "FATAL: Unresolved dependencies or loop in dependency graph (${length_after})\n"
448 449 450 451 452 453 454
          "Processed ${__lst}: ${${__lst}}\n"
          "Good modules: ${result}\n"
          "Bad modules: ${input}"
        )
        list(APPEND result ${input})
        break()
      endif()
455
    endif()
456
  endwhile()
457
  set(${__lst} "${result};${result_extra}" PARENT_SCOPE)
458 459
endfunction()

Kuang Fangjun's avatar
Kuang Fangjun committed
460
# resolve dependencies
461 462 463 464 465 466 467 468
function(__ocv_resolve_dependencies)
  foreach(m ${OPENCV_MODULES_DISABLED_USER})
    set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
  endforeach()
  foreach(m ${OPENCV_MODULES_BUILD})
    set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
  endforeach()

469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
  # Whitelist feature
  if(BUILD_LIST)
    # Prepare the list
    string(REGEX REPLACE "[ ,:]+" ";" whitelist "${BUILD_LIST}" )
    if(BUILD_opencv_world)
      list(APPEND whitelist world)
    endif()
    ocv_list_add_prefix(whitelist "opencv_")
    ocv_list_sort(whitelist)
    ocv_list_unique(whitelist)
    message(STATUS "Using whitelist: ${whitelist}")
    # Expand the list
    foreach(depth RANGE 10)
      set(new_whitelist ${whitelist})
      foreach(m ${whitelist})
        list(APPEND new_whitelist ${OPENCV_MODULE_${m}_REQ_DEPS})
        list(APPEND new_whitelist ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
      endforeach()
      ocv_list_sort(new_whitelist)
      ocv_list_unique(new_whitelist)
      if("${whitelist}" STREQUAL "${new_whitelist}")
        break()
      endif()
      set(whitelist "${new_whitelist}")
    endforeach()
    # Disable modules not in whitelist
    foreach(m ${OPENCV_MODULES_BUILD})
      list(FIND whitelist ${m} idx)
      if(idx EQUAL -1)
        message(STATUS "Module ${m} disabled by whitelist")
        __ocv_module_turn_off(${m})
      endif()
    endforeach()
  endif()

504 505 506 507 508 509 510 511
  # disable MODULES with unresolved dependencies
  set(has_changes ON)
  while(has_changes)
    set(has_changes OFF)
    foreach(m ${OPENCV_MODULES_BUILD})
      set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
      while(__deps)
        ocv_list_pop_front(__deps d)
512 513 514 515 516 517 518 519 520 521
        string(TOLOWER "${d}" upper_d)
        if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
          if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
            message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
            __ocv_module_turn_off(${m})
            set(has_changes ON)
            break()
          else()
            message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
          endif()
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
        endif()
      endwhile()
    endforeach()
  endwhile()

#  message(STATUS "List of active modules: ${OPENCV_MODULES_BUILD}")

  foreach(m ${OPENCV_MODULES_BUILD})
    set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
    foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
        if(HAVE_${d} OR TARGET ${d})
          list(APPEND deps_${m} ${d})
        endif()
      endif()
    endforeach()
#    message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
  endforeach()

  # propagate dependencies
  set(has_changes ON)
  while(has_changes)
    set(has_changes OFF)
    foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
      foreach(m ${OPENCV_MODULES_BUILD})
        if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
          foreach(d ${deps_${m2}})
            if(NOT (";${deps_${m}};" MATCHES ";${d};"))
#              message(STATUS "  Transfer dependency ${d} from ${m2} to ${m}")
              list(APPEND deps_${m} ${d})
              set(has_changes ON)
            endif()
554 555 556 557 558 559 560 561 562 563 564
            if(BUILD_opencv_world
                AND NOT "${m}" STREQUAL "opencv_world"
                AND NOT "${m2}" STREQUAL "opencv_world"
                AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD
                AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
              if(NOT (";${deps_${m}};" MATCHES ";opencv_world;"))
#                message(STATUS "  Transfer dependency opencv_world alias ${m2} to ${m}")
                list(APPEND deps_${m} opencv_world)
                set(has_changes ON)
              endif()
            endif()
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
          endforeach()
        endif()
      endforeach()
    endforeach()
  endwhile()

  # process private deps
  foreach(m ${OPENCV_MODULES_BUILD})
    foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
        list(APPEND deps_${m} ${d})
      endif()
    endforeach()
    foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
        if(HAVE_${d} OR TARGET ${d})
          list(APPEND deps_${m} ${d})
        endif()
      endif()
    endforeach()
  endforeach()

  ocv_list_sort(OPENCV_MODULES_BUILD)

  foreach(m ${OPENCV_MODULES_BUILD})
#    message(STATUS "FULL deps of ${m}: ${deps_${m}}")
    set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
    set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
    ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
    if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
      list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
    endif()
  endforeach()

  # reorder dependencies
  foreach(m ${OPENCV_MODULES_BUILD})
    __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)

Alexander Alekhin's avatar
Alexander Alekhin committed
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617
    set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})

    # process world
    if(BUILD_opencv_world)
      if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
        list(APPEND OPENCV_WORLD_MODULES ${m})
      endif()
      foreach(m2 ${OPENCV_MODULES_BUILD})
        if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
          if(";${LINK_DEPS};" MATCHES ";${m2};")
            list(REMOVE_ITEM LINK_DEPS ${m2})
            if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
              list(APPEND LINK_DEPS opencv_world)
            endif()
          endif()
618
          if("${m}" STREQUAL opencv_world)
Alexander Alekhin's avatar
Alexander Alekhin committed
619 620 621 622 623 624
            list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
          endif()
        endif()
      endforeach()
    endif()

625 626
    set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
    set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
Alexander Alekhin's avatar
Alexander Alekhin committed
627
    set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
628

Alexander Alekhin's avatar
Alexander Alekhin committed
629 630 631 632
#    message(STATUS "  module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
#    message(STATUS "  module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
#    message(STATUS "  extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
#    message(STATUS "")
633 634 635 636 637 638 639
  endforeach()

  __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)

  set(OPENCV_MODULES_PUBLIC        ${OPENCV_MODULES_PUBLIC}        CACHE INTERNAL "List of OpenCV modules marked for export")
  set(OPENCV_MODULES_BUILD         ${OPENCV_MODULES_BUILD}         CACHE INTERNAL "List of OpenCV modules included into the build")
  set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
Alexander Alekhin's avatar
Alexander Alekhin committed
640
  set(OPENCV_WORLD_MODULES         ${OPENCV_WORLD_MODULES}         CACHE INTERNAL "List of OpenCV modules included into the world")
641 642 643
endfunction()


644 645 646 647 648
# setup include paths for the list of passed modules
macro(ocv_include_modules)
  foreach(d ${ARGN})
    if(d MATCHES "^opencv_" AND HAVE_${d})
      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
649
        ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
650 651
      endif()
    elseif(EXISTS "${d}")
652
      ocv_include_directories("${d}")
653 654 655
    endif()
  endforeach()
endmacro()
656

Maksim Shabunin's avatar
Maksim Shabunin committed
657 658 659 660 661 662 663 664 665 666 667 668
# same as previous but with dependencies
macro(ocv_include_modules_recurse)
  ocv_include_modules(${ARGN})
  foreach(d ${ARGN})
    if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
      foreach (sub ${OPENCV_MODULE_${d}_DEPS})
        ocv_include_modules(${sub})
      endforeach()
    endif()
  endforeach()
endmacro()

Alexander Alekhin's avatar
Alexander Alekhin committed
669 670 671
# setup include paths for the list of passed modules
macro(ocv_target_include_modules target)
  foreach(d ${ARGN})
672 673
    if(d MATCHES "^opencv_")
      if(HAVE_${d} AND EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
Alexander Alekhin's avatar
Alexander Alekhin committed
674 675 676 677
        ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
      endif()
    elseif(EXISTS "${d}")
      ocv_target_include_directories(${target} "${d}")
678 679
    else()
      message(WARNING "Unexpected include: ${d} (module=${the_module})")
Alexander Alekhin's avatar
Alexander Alekhin committed
680 681 682 683
    endif()
  endforeach()
endmacro()

684
# setup include paths for the list of passed modules and recursively add dependent modules
Alexander Alekhin's avatar
Alexander Alekhin committed
685
macro(ocv_target_include_modules_recurse target)
686 687 688
  foreach(d ${ARGN})
    if(d MATCHES "^opencv_" AND HAVE_${d})
      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
Alexander Alekhin's avatar
Alexander Alekhin committed
689
        ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
690 691
      endif()
      if(OPENCV_MODULE_${d}_DEPS)
Alexander Alekhin's avatar
Alexander Alekhin committed
692
        ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
693 694
      endif()
    elseif(EXISTS "${d}")
Alexander Alekhin's avatar
Alexander Alekhin committed
695
      ocv_target_include_directories(${target} "${d}")
696 697 698 699
    endif()
  endforeach()
endmacro()

700 701 702
# setup include path for OpenCV headers for specified module
# ocv_module_include_directories(<extra include directories/extra include modules>)
macro(ocv_module_include_directories)
703 704 705
  if(ENABLE_PRECOMPILED_HEADERS OR OPENCV_INCLUDE_DIR_APPEND_MODULE_SRC)
    ocv_target_include_directories(${the_module} "${OPENCV_MODULE_${the_module}_LOCATION}/src")
  endif()
Alexander Alekhin's avatar
Alexander Alekhin committed
706 707 708 709
  ocv_target_include_directories(${the_module}
      "${OPENCV_MODULE_${the_module}_LOCATION}/include"
      "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
      )
710
  ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
711
endmacro()
712 713


714 715 716 717 718
# sets header and source files for the current module
# NB: all files specified as headers will be installed
# Usage:
# ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
macro(ocv_set_module_sources)
Alexander Alekhin's avatar
Alexander Alekhin committed
719 720
  ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")

721 722
  set(OPENCV_MODULE_${the_module}_HEADERS "")
  set(OPENCV_MODULE_${the_module}_SOURCES "")
723

724 725 726 727 728 729 730
  foreach(f "HEADERS" ${ARGN})
    if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
      set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
    else()
      list(APPEND ${__filesvar} "${f}")
    endif()
  endforeach()
731

Kuang Fangjun's avatar
Kuang Fangjun committed
732
  # the hacky way to embed any files into the OpenCV without modification of its build system
733 734 735
  if(COMMAND ocv_get_module_external_sources)
    ocv_get_module_external_sources()
  endif()
736

737 738 739 740
  if(OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED)
    list(APPEND OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED})
  endif()

741 742 743 744
  # TODO Update hooks above
  ocv_cmake_hook(INIT_MODULE_SOURCES)
  ocv_cmake_hook(INIT_MODULE_SOURCES_${the_module})

745
  # use full paths for module to be independent from the module location
746
  ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
747

748 749 750
  set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
  set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
endmacro()
751

752 753
# finds and sets headers and sources for the standard OpenCV module
# Usage:
754
# ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
755
macro(ocv_glob_module_sources)
Alexander Alekhin's avatar
Alexander Alekhin committed
756
  ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
757 758 759 760 761
  set(_argn ${ARGN})
  list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
  if(NOT exclude_cuda EQUAL -1)
    list(REMOVE_AT _argn ${exclude_cuda})
  endif()
762 763 764 765
  list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
  if(NOT exclude_opencl EQUAL -1)
    list(REMOVE_AT _argn ${exclude_opencl})
  endif()
766

Alexander Alekhin's avatar
Alexander Alekhin committed
767 768 769 770 771 772 773 774 775 776 777
  file(GLOB_RECURSE lib_srcs
       "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
  )
  file(GLOB_RECURSE lib_int_hdrs
       "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
       "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
  )
  file(GLOB lib_hdrs
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
778 779
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp"
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h"
780 781
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.hpp"
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.h"
782
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/legacy/*.h"
Alexander Alekhin's avatar
Alexander Alekhin committed
783 784 785 786 787
  )
  file(GLOB lib_hdrs_detail
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
  )
788
  if (APPLE)
Alexander Alekhin's avatar
Alexander Alekhin committed
789 790 791
    file(GLOB_RECURSE lib_srcs_apple
         "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
    )
792 793
    list(APPEND lib_srcs ${lib_srcs_apple})
  endif()
794

Alexander Alekhin's avatar
Alexander Alekhin committed
795 796
  ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
  ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
797

798 799 800
  set(lib_cuda_srcs "")
  set(lib_cuda_hdrs "")
  if(HAVE_CUDA AND exclude_cuda EQUAL -1)
Alexander Alekhin's avatar
Alexander Alekhin committed
801 802 803
    file(GLOB lib_cuda_srcs
         "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
    )
804 805 806 807
    file(GLOB lib_cuda_hdrs
         "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
    )
    source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
808 809
  endif()

Alexander Alekhin's avatar
Alexander Alekhin committed
810 811 812
  file(GLOB cl_kernels
       "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
  )
813
  if(cl_kernels AND exclude_opencl EQUAL -1)
Alexander Alekhin's avatar
Alexander Alekhin committed
814
    set(OCL_NAME opencl_kernels_${name})
815
    add_custom_command(
816
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp"  # don't add .hpp file here to optimize build process
817
      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"
818 819 820
      DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
      COMMENT "Processing OpenCL kernels (${name})"
    )
821
    ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
Alexander Alekhin's avatar
Alexander Alekhin committed
822
    ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
823 824 825
    set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
        PROPERTIES GENERATED TRUE
    )
Alexander Alekhin's avatar
Alexander Alekhin committed
826
    list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
827 828
  endif()

829
  ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
830
                         SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
831 832
endmacro()

833 834 835
# creates OpenCV module in current folder
# creates new target, configures standard dependencies, compilers flags, install rules
# Usage:
836
#   ocv_create_module(<extra link dependencies>)
Alexander Alekhin's avatar
Alexander Alekhin committed
837
#   ocv_create_module()
838
macro(ocv_create_module)
839
  ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
840 841 842
  if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
    message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
  endif()
843 844 845
  if(NOT " ${ARGN}" STREQUAL " ")
    set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
  endif()
846
  if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
Alexander Alekhin's avatar
Alexander Alekhin committed
847 848 849 850 851 852
    # nothing
    set(the_module_target opencv_world)
  else()
    _ocv_create_module(${ARGN})
    set(the_module_target ${the_module})
  endif()
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874

  if(WINRT)
    # removing APPCONTAINER from modules to run from console
    # in case of usual starting of WinRT test apps output is missing
    # so starting of console version w/o APPCONTAINER is required to get test results
    # also this allows to use opencv_extra test data for these tests
    if(NOT "${the_module}" STREQUAL "opencv_ts" AND NOT "${the_module}" STREQUAL "opencv_hal")
      add_custom_command(TARGET ${the_module}
                         POST_BUILD
                         COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
    endif()

    if("${the_module}" STREQUAL "opencv_ts")
      # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
      # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
      add_custom_command(TARGET ${the_module}
        POST_BUILD
        COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
        COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
        COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
    endif()
  endif()
Alexander Alekhin's avatar
Alexander Alekhin committed
875 876 877
endmacro()

macro(_ocv_create_module)
878 879 880 881 882

  ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
  set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
  set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")

883 884 885
  # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
  # be called at some point in the future. We can't look into the future, though,
  # so this will have to do.
Alexander Alekhin's avatar
Alexander Alekhin committed
886
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
887 888 889
    get_native_precompiled_header(${the_module} precomp.hpp)
  endif()

890 891 892
  if(WIN32
      AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
      AND NOT OPENCV_VS_VERSIONINFO_SKIP)
893 894 895
    if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
      set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
    elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
Alexander Alekhin's avatar
Alexander Alekhin committed
896
      set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
    elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
      set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
      ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
        NAME "${the_module}"
        FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
        INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
        ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
      )
    endif()
    if(_VS_VERSION_FILE)
      if(NOT EXISTS "${_VS_VERSION_FILE}")
        message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
      endif()
      source_group("Src" FILES "${_VS_VERSION_FILE}")
    endif()
  endif()
913 914 915 916 917
  if(WIN32 AND NOT (
          "${the_module}" STREQUAL "opencv_core" OR
          "${the_module}" STREQUAL "opencv_world" OR
          "${the_module}" STREQUAL "opencv_cudev"
      )
918 919 920 921 922 923
      AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
      AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
  )
      set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
      configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
  endif()
924

925 926
  source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
  source_group("Src" FILES "${${the_module}_pch}")
927 928
  ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
  ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
Alexander Alekhin's avatar
Alexander Alekhin committed
929
  ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
930
    "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
931
    ${${the_module}_pch}
932
    ${_VS_VERSION_FILE}
933
    ${_DLLMAIN_FILE}
934
  )
935 936 937 938
  set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
  set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
    PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")

Alexander Alekhin's avatar
Alexander Alekhin committed
939 940 941 942 943
  ocv_target_link_libraries(${the_module} LINK_PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK})
  ocv_target_link_libraries(${the_module} LINK_PUBLIC ${OPENCV_MODULE_${the_module}_DEPS_EXT})
  ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
  if (HAVE_CUDA)
    ocv_target_link_libraries(${the_module} LINK_PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
944
  endif()
945

946 947 948 949 950
  if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
    target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
    unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
  endif()

951
  add_dependencies(opencv_modules ${the_module})
952

953 954 955
  if(ENABLE_SOLUTION_FOLDERS)
    set_target_properties(${the_module} PROPERTIES FOLDER "modules")
  endif()
956

957 958 959
  set_target_properties(${the_module} PROPERTIES
    OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
    DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
960 961
    COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
    COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
962
    ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
963
    COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
964
    LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
965
    RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
966
    DEFINE_SYMBOL CVAPI_EXPORTS
967
  )
968

969 970 971 972
  if(BUILD_FAT_JAVA_LIB)  # force exports from static modules too
    if(BUILD_SHARED_LIBS)
      message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
    endif()
973 974 975
    target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
  endif()

Kuang Fangjun's avatar
Kuang Fangjun committed
976
  # For dynamic link numbering conventions
977 978 979 980
  if(NOT ANDROID)
    # Android SDK build scripts can include only .so files into final .apk
    # As result we should not set version properties for Android
    set_target_properties(${the_module} PROPERTIES
981
      VERSION ${OPENCV_LIBVERSION}
982 983 984 985
      SOVERSION ${OPENCV_SOVERSION}
    )
  endif()

986 987 988 989
  if (ENABLE_GNU_STL_DEBUG)
    target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
  endif()

990 991 992
  if(MSVC)
    if(CMAKE_CROSSCOMPILING)
      set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
993
    endif()
994
    set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
995
  endif()
996

997
  get_target_property(_target_type ${the_module} TYPE)
998 999
  if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
      ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
1000 1001 1002
    ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
      RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
      LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
1003
      ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
1004 1005
      )
  endif()
1006 1007 1008 1009
  if("${_target_type}" STREQUAL "SHARED_LIBRARY")
    install(TARGETS ${the_module}
      LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
  endif()
1010

1011
  # only "public" headers need to be installed
1012 1013
  ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
  ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
1014 1015 1016 1017 1018
  if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
    foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
      string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
      if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
        install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
1019 1020
      else()
        #message("Header file will be NOT installed: ${hdr}")
1021 1022 1023
      endif()
    endforeach()
  endif()
1024

Alexander Alekhin's avatar
Alexander Alekhin committed
1025
  _ocv_add_precompiled_headers(${the_module})
1026 1027 1028

  ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
  ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
1029 1030 1031 1032 1033 1034
endmacro()

# opencv precompiled headers macro (can add pch to modules and tests)
# this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
# Usage:
# ocv_add_precompiled_headers(${the_module})
Alexander Alekhin's avatar
Alexander Alekhin committed
1035 1036 1037
macro(_ocv_add_precompiled_headers the_target)
  ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")

1038 1039 1040 1041 1042 1043 1044 1045 1046
  if("${the_target}" MATCHES "^opencv_test_.*$")
    SET(pch_path "test/test_")
  elseif("${the_target}" MATCHES "^opencv_perf_.*$")
    SET(pch_path "perf/perf_")
  else()
    SET(pch_path "src/")
  endif()
  ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
  unset(pch_path)
1047
endmacro()
1048

1049 1050 1051
# short command for adding simple OpenCV module
# see ocv_add_module for argument details
# Usage:
1052
# ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
1053
macro(ocv_define_module module_name)
Alexander Alekhin's avatar
Alexander Alekhin committed
1054
  ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
1055 1056 1057 1058 1059 1060
  set(_argn ${ARGN})
  set(exclude_cuda "")
  foreach(arg ${_argn})
    if("${arg}" STREQUAL "EXCLUDE_CUDA")
      set(exclude_cuda "${arg}")
      list(REMOVE_ITEM _argn ${arg})
1061 1062 1063
    endif()
  endforeach()

1064 1065
  ocv_add_module(${module_name} ${_argn})
  ocv_glob_module_sources(${exclude_cuda})
Alexander Alekhin's avatar
Alexander Alekhin committed
1066
  ocv_module_include_directories()
1067 1068 1069 1070
  ocv_create_module()

  ocv_add_accuracy_tests()
  ocv_add_perf_tests()
1071
  ocv_add_samples()
1072
endmacro()
1073

1074 1075 1076 1077 1078 1079 1080 1081
# ensures that all passed modules are available
# sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
macro(ocv_check_dependencies)
  set(OCV_DEPENDENCIES_FOUND TRUE)
  foreach(d ${ARGN})
    if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
      set(OCV_DEPENDENCIES_FOUND FALSE)
      break()
1082
    endif()
1083
  endforeach()
1084 1085
endmacro()

1086
# auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
macro(__ocv_parse_test_sources tests_type)
  set(OPENCV_${tests_type}_${the_module}_SOURCES "")
  set(OPENCV_${tests_type}_${the_module}_DEPS "")
  set(__file_group_name "")
  set(__file_group_sources "")
  foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
    if(arg STREQUAL "FILES")
      set(__currentvar "__file_group_sources")
      if(__file_group_name AND __file_group_sources)
        source_group("${__file_group_name}" FILES ${__file_group_sources})
        list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
      endif()
      set(__file_group_name "")
      set(__file_group_sources "")
    elseif(arg STREQUAL "DEPENDS_ON")
Alexander Alekhin's avatar
Alexander Alekhin committed
1102
      set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
1103
    elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
1104 1105 1106 1107 1108 1109 1110 1111 1112
      set(__file_group_name "${arg}")
    else()
      list(APPEND ${__currentvar} "${arg}")
    endif()
  endforeach()
  unset(__file_group_name)
  unset(__file_group_sources)
  unset(__currentvar)
endmacro()
1113

1114 1115
# this is a command for adding OpenCV performance tests to the module
# ocv_add_perf_tests(<extra_dependencies>)
1116
function(ocv_add_perf_tests)
Alexander Alekhin's avatar
Alexander Alekhin committed
1117 1118
  ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")

1119 1120 1121 1122
  if(WINRT)
    set(OPENCV_DEBUG_POSTFIX "")
  endif()

Alexander Alekhin's avatar
Alexander Alekhin committed
1123
  set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
1124 1125
  if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
    __ocv_parse_test_sources(PERF ${ARGN})
1126

1127
    # opencv_imgcodecs is required for imread/imwrite
1128
    set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1129
    ocv_check_dependencies(${perf_deps})
1130

1131 1132
    if(OCV_DEPENDENCIES_FOUND)
      set(the_target "opencv_perf_${name}")
1133
      # project(${the_target})
1134

1135
      if(NOT OPENCV_PERF_${the_module}_SOURCES)
1136 1137
        file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
        file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
1138 1139
        ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
        ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
1140 1141
        set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
      endif()
1142

1143 1144
      ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})

Alexander Alekhin's avatar
Alexander Alekhin committed
1145 1146 1147
      if(NOT BUILD_opencv_world)
        get_native_precompiled_header(${the_target} perf_precomp.hpp)
      endif()
1148

1149
      source_group("Src" FILES "${${the_target}_pch}")
Alexander Alekhin's avatar
Alexander Alekhin committed
1150 1151
      ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
      ocv_target_include_modules(${the_target} ${perf_deps} "${perf_path}")
1152
      ocv_target_link_libraries(${the_target} LINK_PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
1153
      add_dependencies(opencv_perf_tests ${the_target})
1154

1155 1156 1157 1158 1159
      if(HAVE_HPX)
        message("Linking HPX to Perf test of module ${name}")
        ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
      endif()

1160 1161 1162 1163
      set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
      set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
        PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")

1164 1165 1166 1167 1168 1169 1170 1171 1172
      # Additional target properties
      set_target_properties(${the_target} PROPERTIES
        DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
        RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
      )
      if(ENABLE_SOLUTION_FOLDERS)
        set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
      endif()

1173 1174 1175 1176 1177 1178 1179 1180
      if(WINRT)
        # removing APPCONTAINER from tests to run from console
        # look for detailed description inside of ocv_create_module macro above
        add_custom_command(TARGET "opencv_perf_${name}"
                           POST_BUILD
                           COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
      endif()

Alexander Alekhin's avatar
Alexander Alekhin committed
1181 1182 1183
      if(NOT BUILD_opencv_world)
        _ocv_add_precompiled_headers(${the_target})
      endif()
1184 1185 1186 1187 1188 1189

      ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
      ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
                               "--perf_min_samples=1"
                               "--perf_force_samples=1"
                               "--perf_verify_sanity")
1190
    else(OCV_DEPENDENCIES_FOUND)
1191
      # TODO: warn about unsatisfied dependencies
1192
    endif(OCV_DEPENDENCIES_FOUND)
1193
    if(INSTALL_TESTS)
1194
      install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1195
    endif()
1196
  endif()
1197
endfunction()
1198

1199
# this is a command for adding OpenCV accuracy/regression tests to the module
1200
# ocv_add_accuracy_tests(<list of extra dependencies>)
1201
function(ocv_add_accuracy_tests)
Alexander Alekhin's avatar
Alexander Alekhin committed
1202 1203
  ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")

1204 1205 1206 1207
  if(WINRT)
    set(OPENCV_DEBUG_POSTFIX "")
  endif()

Alexander Alekhin's avatar
Alexander Alekhin committed
1208
  set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
1209 1210 1211
  if(BUILD_TESTS AND EXISTS "${test_path}")
    __ocv_parse_test_sources(TEST ${ARGN})

1212
    # opencv_imgcodecs is required for imread/imwrite
1213
    set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1214 1215 1216
    ocv_check_dependencies(${test_deps})
    if(OCV_DEPENDENCIES_FOUND)
      set(the_target "opencv_test_${name}")
1217
      # project(${the_target})
1218

1219
      if(NOT OPENCV_TEST_${the_module}_SOURCES)
1220 1221
        file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
        file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
1222 1223
        ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
        ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
1224 1225
        set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
      endif()
1226

1227 1228 1229
      if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
        list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
      endif()
1230 1231
      ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})

Alexander Alekhin's avatar
Alexander Alekhin committed
1232 1233 1234
      if(NOT BUILD_opencv_world)
        get_native_precompiled_header(${the_target} test_precomp.hpp)
      endif()
1235

1236
      source_group("Src" FILES "${${the_target}_pch}")
Alexander Alekhin's avatar
Alexander Alekhin committed
1237 1238
      ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
      ocv_target_include_modules(${the_target} ${test_deps} "${test_path}")
1239 1240 1241
      if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
        ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
      endif()
1242
      ocv_target_link_libraries(${the_target} LINK_PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
1243
      add_dependencies(opencv_tests ${the_target})
1244

1245 1246 1247 1248 1249
      if(HAVE_HPX)
        message("Linking HPX to Perf test of module ${name}")
        ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
      endif()

1250 1251 1252 1253
      set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
      set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
        PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")

1254 1255 1256 1257 1258 1259
      # Additional target properties
      set_target_properties(${the_target} PROPERTIES
        DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
        RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
      )

1260 1261
      ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")

1262 1263 1264
      if(ENABLE_SOLUTION_FOLDERS)
        set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
      endif()
1265

1266 1267 1268 1269
      if(OPENCV_TEST_BIGDATA)
        ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
      endif()

Alexander Alekhin's avatar
Alexander Alekhin committed
1270 1271 1272
      if(NOT BUILD_opencv_world)
        _ocv_add_precompiled_headers(${the_target})
      endif()
1273 1274

      ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
1275
    else(OCV_DEPENDENCIES_FOUND)
1276
      # TODO: warn about unsatisfied dependencies
1277
    endif(OCV_DEPENDENCIES_FOUND)
1278 1279

    if(INSTALL_TESTS)
1280
      install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1281
    endif()
1282
  endif()
1283
endfunction()
1284

1285
function(ocv_add_samples)
Alexander Alekhin's avatar
Alexander Alekhin committed
1286 1287
  ocv_debug_message("ocv_add_samples(" ${ARGN} ")")

1288
  set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1289 1290 1291 1292
  if(NOT EXISTS "${samples_path}")
    return()
  endif()

1293 1294
  string(REGEX REPLACE "^opencv_" "" module_id ${the_module})

1295
  if(BUILD_EXAMPLES)
1296
    set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1297 1298 1299 1300 1301 1302 1303 1304 1305
    ocv_check_dependencies(${samples_deps})

    if(OCV_DEPENDENCIES_FOUND)
      file(GLOB sample_sources "${samples_path}/*.cpp")

      foreach(source ${sample_sources})
        get_filename_component(name "${source}" NAME_WE)
        set(the_target "example_${module_id}_${name}")

Alexander Alekhin's avatar
Alexander Alekhin committed
1306 1307
        ocv_add_executable(${the_target} "${source}")
        ocv_target_include_modules(${the_target} ${samples_deps})
Alexander Alekhin's avatar
Alexander Alekhin committed
1308
        ocv_target_link_libraries(${the_target} LINK_PRIVATE ${samples_deps})
1309

1310 1311 1312 1313 1314
        set_target_properties(${the_target} PROPERTIES
          PROJECT_LABEL "(sample) ${name}"
          LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
        set_source_files_properties("${source}" PROPERTIES
          LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1315 1316 1317 1318
        if(ENABLE_SOLUTION_FOLDERS)
          set_target_properties(${the_target} PROPERTIES
            FOLDER "samples/${module_id}")
        endif()
1319 1320 1321 1322 1323 1324 1325
        # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
        set(parent_target opencv_samples_${module_id})
        if(NOT TARGET ${parent_target})
          add_custom_target(${parent_target})
          add_dependencies(opencv_samples ${parent_target})
        endif()
        add_dependencies(${parent_target} ${the_target})
1326 1327

        if(WIN32)
1328
          install(TARGETS ${the_target} RUNTIME DESTINATION "samples/${module_id}" COMPONENT samples)
1329 1330 1331 1332 1333
        endif()
      endforeach()
    endif()
  endif()

1334 1335
  if(INSTALL_C_EXAMPLES)
    file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1336 1337 1338 1339 1340 1341 1342
    foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
        IF( IS_DIRECTORY "${ITEM}" )
            LIST( APPEND sample_dirs "${ITEM}" )
        ELSE()
            LIST( APPEND sample_files "${ITEM}" )
        ENDIF()
    endforeach()
1343
    install(FILES ${sample_files}
1344 1345
            DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
            COMPONENT samples)
1346
    install(DIRECTORY ${sample_dirs}
1347 1348
            DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
            COMPONENT samples)
1349 1350
  endif()
endfunction()