opencv

Форк
0
/
OpenCVModule.cmake 
1458 строк · 59.9 Кб
1
# Local variables (set for each module):
2
#
3
# name       - short name in lower case i.e. core
4
# the_module - full name in lower case i.e. opencv_core
5

6
# Global variables:
7
#
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
25

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}
32

33
# The verbose template for OpenCV module:
34
#
35
#   ocv_add_module(modname <dependencies>)
36
#   ocv_glob_module_sources(([EXCLUDE_OPENCL] [EXCLUDE_CUDA] <extra sources&headers>)
37
#                          or glob them manually and ocv_set_module_sources(...)
38
#   ocv_module_include_directories(<extra include directories>)
39
#   ocv_create_module()
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>)
46
#
47
#
48
# If module have no "extra" then you can define it in one line:
49
#
50
#   ocv_define_module(modname <dependencies>)
51

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})
55
  if(HAVE_${mod})
56
    unset(HAVE_${mod} CACHE)
57
  endif()
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
endforeach()
67

68
# clean modules info which needs to be recalculated
69
set(OPENCV_MODULES_PUBLIC         "" CACHE INTERNAL "List of OpenCV modules marked for export")
70
set(OPENCV_MODULES_BUILD          "" CACHE INTERNAL "List of OpenCV modules included into the build")
71
set(OPENCV_MODULES_DISABLED_USER  "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
72
set(OPENCV_MODULES_DISABLED_AUTO  "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
73
set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
74
unset(OPENCV_WORLD_MODULES CACHE)
75

76
# adds dependencies to OpenCV module
77
# Usage:
78
#   add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>] [WRAP <list of wrappers>])
79
# Notes:
80
# * <list of dependencies> - can include full names of modules or full paths to shared/static libraries or cmake targets
81
macro(ocv_add_dependencies full_modname)
82
  ocv_debug_message("ocv_add_dependencies(" ${full_modname} ${ARGN} ")")
83
  #we don't clean the dependencies here to allow this macro several times for every module
84
  foreach(d "REQUIRED" ${ARGN})
85
    if(d STREQUAL "REQUIRED")
86
      set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
87
    elseif(d STREQUAL "OPTIONAL")
88
      set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
89
    elseif(d STREQUAL "PRIVATE_REQUIRED")
90
      set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
91
    elseif(d STREQUAL "PRIVATE_OPTIONAL")
92
      set(__depsvar OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
93
    elseif(d STREQUAL "WRAP")
94
      set(__depsvar OPENCV_MODULE_${full_modname}_WRAPPERS)
95
    else()
96
      list(APPEND ${__depsvar} "${d}")
97
    endif()
98
  endforeach()
99
  unset(__depsvar)
100

101
  ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
102
  ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
103
  ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS)
104
  ocv_list_unique(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS)
105
  ocv_list_unique(OPENCV_MODULE_${full_modname}_WRAPPERS)
106

107
  set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS}
108
    CACHE INTERNAL "Required dependencies of ${full_modname} module")
109
  set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS}
110
    CACHE INTERNAL "Optional dependencies of ${full_modname} module")
111
  set(OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_REQ_DEPS}
112
    CACHE INTERNAL "Required private dependencies of ${full_modname} module")
113
  set(OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS ${OPENCV_MODULE_${full_modname}_PRIVATE_OPT_DEPS}
114
    CACHE INTERNAL "Optional private dependencies of ${full_modname} module")
115
  set(OPENCV_MODULE_${full_modname}_WRAPPERS ${OPENCV_MODULE_${full_modname}_WRAPPERS}
116
    CACHE INTERNAL "List of wrappers supporting module ${full_modname}")
117
endmacro()
118

119
# declare new OpenCV module in current folder
120
# Usage:
121
#   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
122
# Example:
123
#   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_cudev)
124
macro(ocv_add_module _name)
125
  ocv_debug_message("ocv_add_module(" ${_name} ${ARGN} ")")
126
  string(TOLOWER "${_name}" name)
127
  set(the_module opencv_${name})
128

129
  # the first pass - collect modules info, the second pass - create targets
130
  if(OPENCV_INITIAL_PASS)
131
    #guard against redefinition
132
    if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
133
      message(FATAL_ERROR "Redefinition of the ${the_module} module.
134
  at:                    ${CMAKE_CURRENT_SOURCE_DIR}
135
  previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
136
")
137
    endif()
138

139
    if(NOT DEFINED the_description)
140
      set(the_description "The ${name} OpenCV module")
141
    endif()
142

143
    if(NOT DEFINED BUILD_${the_module}_INIT)
144
      set(BUILD_${the_module}_INIT ON)
145
    endif()
146

147
    # create option to enable/disable this module
148
    option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
149

150
    # remember the module details
151
    set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
152
    set(OPENCV_MODULE_${the_module}_LOCATION    "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
153

154
    set(OPENCV_MODULE_${the_module}_LINK_DEPS "" CACHE INTERNAL "")
155

156
    set(ADD_MODULE_ARGN ${ARGN})
157
    ocv_cmake_hook(PRE_ADD_MODULE)
158
    ocv_cmake_hook(PRE_ADD_MODULE_${the_module})
159

160
    # parse list of dependencies
161
    if(" ${ARGV1}" STREQUAL " INTERNAL" OR " ${ARGV1}" STREQUAL " BINDINGS")
162
      set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The category of the module")
163
      set(__ocv_argn__ ${ADD_MODULE_ARGN})
164
      list(REMOVE_AT __ocv_argn__ 0)
165
      ocv_add_dependencies(${the_module} ${__ocv_argn__})
166
      unset(__ocv_argn__)
167
    else()
168
      set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The category of the module")
169
      ocv_add_dependencies(${the_module} ${ADD_MODULE_ARGN})
170
      if(BUILD_${the_module})
171
        set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
172
      endif()
173
    endif()
174

175
    # add self to the world dependencies
176
    if((NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD
177
        AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS"
178
        AND (NOT DEFINED OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
179
        AND (NOT OPENCV_PROCESSING_EXTRA_MODULES OR NOT OPENCV_WORLD_EXCLUDE_EXTRA_MODULES)
180
        AND (NOT BUILD_SHARED_LIBS OR NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC"))
181
        OR OPENCV_MODULE_IS_PART_OF_WORLD
182
        )
183
      set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD ON CACHE INTERNAL "")
184
      ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
185
    else()
186
      set(OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OFF CACHE INTERNAL "")
187
    endif()
188

189
    if(NOT DEFINED the_label)
190
      if(OPENCV_PROCESSING_EXTRA_MODULES)
191
        set(the_label "Extra")
192
      else()
193
        set(the_label "Main")
194
      endif()
195
    endif()
196
    set(OPENCV_MODULE_${the_module}_LABEL "${the_label};${the_module}" CACHE INTERNAL "")
197

198
    if(BUILD_${the_module})
199
      set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
200
    else()
201
      set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
202
    endif()
203

204
    # stop processing of current file
205
    ocv_cmake_hook(POST_ADD_MODULE)
206
    ocv_cmake_hook(POST_ADD_MODULE_${the_module})
207
    return()
208
  else()
209
    set(OPENCV_MODULE_${the_module}_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
210
    if(NOT BUILD_${the_module})
211
      return() # extra protection from redefinition
212
    endif()
213
    if(NOT OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD OR NOT ${BUILD_opencv_world})
214
      if (NOT ${the_module} STREQUAL opencv_world)
215
        project(${the_module})
216
      endif()
217
      add_definitions(
218
        -D_USE_MATH_DEFINES  # M_PI constant in MSVS
219
        -D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_FORMAT_MACROS  # to use C libraries from C++ code (ffmpeg)
220
      )
221
    endif()
222
  endif()
223
endmacro()
224

225
# excludes module from current configuration
226
macro(ocv_module_disable_ module)
227
  set(__modname ${module})
228
  if(NOT __modname MATCHES "^opencv_")
229
    set(__modname opencv_${module})
230
  endif()
231
  list(APPEND OPENCV_MODULES_DISABLED_FORCE "${__modname}")
232
  set(HAVE_${__modname} OFF CACHE INTERNAL "Module ${__modname} can not be built in current configuration")
233
  set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
234
  set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
235
  if(BUILD_${__modname})
236
    # touch variable controlling build of the module to suppress "unused variable" CMake warning
237
  endif()
238
  unset(__modname)
239
endmacro()
240

241
macro(ocv_module_disable module)
242
  ocv_module_disable_(${module})
243
  return() # leave the current folder
244
endmacro()
245

246
# gather acceptable locations and generate names for them
247
# if folder contains CMakeLists.txt - it is accepted,
248
# otherwise all first-level subfolders containing CMakeLists.txt are accepted.
249
# Usage: _glob_locations(<output paths list> <output names list> <folder> [<folder> ...])
250
function(_glob_locations out_paths out_names)
251
  set(PATHS ${ARGN})
252
  foreach(path ${PATHS})
253
    #message(STATUS "Inspect: ${path}")
254
    list(LENGTH paths before)
255
    get_filename_component(path "${path}" ABSOLUTE)
256
    # Either module itself
257
    if(NOT path STREQUAL "${OpenCV_SOURCE_DIR}/modules" AND EXISTS "${path}/CMakeLists.txt")
258
      get_filename_component(name "${path}" NAME)
259
      list(APPEND paths "${path}")
260
      list(APPEND names "${name}")
261
    else()
262
      # Either flat collection of modules
263
      file(GLOB subdirs RELATIVE "${path}" "${path}/*")
264
      foreach(subdir ${subdirs})
265
        #message(STATUS "Inspect: ${path}/${subdir}")
266
        if(EXISTS "${path}/${subdir}/CMakeLists.txt")
267
          list(APPEND paths "${path}/${subdir}")
268
          list(APPEND names "${subdir}")
269
        endif()
270
      endforeach()
271
    endif()
272
    list(LENGTH paths after)
273
    if(before EQUAL after)
274
      message(SEND_ERROR "No modules has been found: ${path}")
275
    endif()
276
  endforeach()
277
  # Return
278
  set(${out_paths} ${paths} PARENT_SCOPE)
279
  set(${out_names} ${names} PARENT_SCOPE)
280
endfunction()
281

282
# Calls 'add_subdirectory' for each location.
283
# Note: both input lists should have same length.
284
# Usage: _add_modules_1(<list with paths> <list with names>)
285
macro(_add_modules_1 paths names)
286
  ocv_debug_message("_add_modules_1(paths=${paths}, names=${names}, ... " ${ARGN} ")")
287
  list(LENGTH ${paths} __len)
288
  if(NOT __len EQUAL 0)
289
    list(LENGTH ${names} __len_verify)
290
    if(NOT __len EQUAL __len_verify)
291
      message(FATAL_ERROR "Bad configuration! ${__len} != ${__len_verify}")
292
    endif()
293
    math(EXPR __len "${__len} - 1")
294
    foreach(i RANGE ${__len})
295
      list(GET ${paths} ${i} __path)
296
      list(GET ${names} ${i} __name)
297
      #message(STATUS "First pass: ${__name} => ${__path}")
298
      include("${__path}/cmake/init.cmake" OPTIONAL)
299
      add_subdirectory("${__path}" "${OpenCV_BINARY_DIR}/modules/.firstpass/${__name}")
300
    endforeach()
301
  endif()
302
endmacro()
303

304
# Calls 'add_subdirectory' for each module name.
305
# Usage: _add_modules_2([<module> ...])
306
macro(_add_modules_2)
307
  ocv_debug_message("_add_modules_2(" ${ARGN} ")")
308
  foreach(m ${ARGN})
309
    set(the_module "${m}")
310
    ocv_cmake_hook(PRE_MODULES_CREATE_${the_module})
311
    if(BUILD_opencv_world AND m STREQUAL "opencv_world"
312
        OR NOT BUILD_opencv_world
313
        OR NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
314
      if(NOT m MATCHES "^opencv_")
315
        message(WARNING "Incorrect module name: ${m}")
316
      endif()
317
      string(REGEX REPLACE "^opencv_" "" name "${m}")
318
      #message(STATUS "Second pass: ${name} => ${OPENCV_MODULE_${m}_LOCATION}")
319
      add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${OpenCV_BINARY_DIR}/modules/${name}")
320
    endif()
321
    ocv_cmake_hook(POST_MODULES_CREATE_${the_module})
322
  endforeach()
323
  unset(the_module)
324
endmacro()
325

326
# Check if list of input items is unique.
327
# Usage: _assert_uniqueness(<failure message> <element> [<element> ...])
328
function(_assert_uniqueness msg)
329
  ocv_get_duplicates(dups ${ARGN})
330
  if(dups)
331
    foreach(e ${ARGN})
332
      list(FIND dups "${e}" idx)
333
      if(NOT idx EQUAL -1)
334
        set(prefix " > ")
335
      else()
336
        set(prefix "   ")
337
      endif()
338
      message("${prefix}${e}")
339
    endforeach()
340
    message(FATAL_ERROR "${msg}")
341
  endif()
342
endfunction()
343

344
# collect modules from specified directories
345
# NB: must be called only once!
346
# Usage: ocv_glob_modules(<main location> [<extra location> ...])
347
macro(ocv_glob_modules main_root)
348
  ocv_cmake_hook(INIT_MODULES_GLOB)
349
  if(DEFINED OPENCV_INITIAL_PASS)
350
    message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
351
  endif()
352

353
  # collect modules
354
  set(OPENCV_INITIAL_PASS ON)
355
  _glob_locations(__main_paths __main_names ${main_root})
356
  _glob_locations(__extra_paths __extra_names ${ARGN})
357
  _assert_uniqueness("Duplicated modules LOCATIONS has been found" ${__main_paths} ${__extra_paths})
358
  _assert_uniqueness("Duplicated modules NAMES has been found" ${__main_names} ${__extra_names})
359
  set(OPENCV_PROCESSING_EXTRA_MODULES 0)
360
  ocv_cmake_hook(PRE_MODULES_SCAN)
361
  _add_modules_1(__main_paths __main_names)
362
  set(OPENCV_PROCESSING_EXTRA_MODULES 1)
363
  ocv_cmake_hook(PRE_MODULES_SCAN_EXTRA)
364
  _add_modules_1(__extra_paths __extra_names)
365
  ocv_clear_vars(__main_names __extra_names __main_paths __extra_paths)
366
  ocv_cmake_hook(POST_MODULES_SCAN)
367

368
  # resolve dependencies
369
  __ocv_resolve_dependencies()
370

371
  # optionally configure delay load
372
  if(MSVC AND BUILD_SHARED_LIBS AND ENABLE_DELAYLOAD AND NOT BUILD_opencv_world)
373
    if(${CMAKE_SHARED_LINKER_FLAGS} MATCHES "delayimp.lib")
374
      set(DELAYFLAGS "")
375
    else()
376
      set(DELAYFLAGS "delayimp.lib")
377
    endif()
378

379
    foreach(mod ${OPENCV_MODULES_BUILD})
380
      if(NOT ${mod} STREQUAL "opencv_core" AND NOT ${mod} MATCHES "bindings_generator|python")
381
        set(DELAYFLAGS "${DELAYFLAGS} /DELAYLOAD:${mod}${OPENCV_VERSION_MAJOR}${OPENCV_VERSION_MINOR}${OPENCV_VERSION_PATCH}.dll")
382
      endif()
383
    endforeach()
384

385
    if(NOT ${CMAKE_SHARED_LINKER_FLAGS} MATCHES "/IGNORE:4199")
386
      set(DELAYFLAGS "${DELAYFLAGS} /IGNORE:4199")
387
    endif()
388

389
    set(CMAKE_EXE_LINKER_FLAGS       "${CMAKE_EXE_LINKER_FLAGS} ${DELAYFLAGS}")
390
    set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${DELAYFLAGS}")
391
    set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${DELAYFLAGS}")
392
  endif()
393

394
  # create modules
395
  set(OPENCV_INITIAL_PASS OFF)
396
  ocv_cmake_hook(PRE_MODULES_CREATE)
397
  _add_modules_2(${OPENCV_MODULES_BUILD})
398
  ocv_cmake_hook(POST_MODULES_CREATE)
399
endmacro()
400

401

402
# called by root CMakeLists.txt
403
macro(ocv_register_modules)
404
  if(NOT OPENCV_MODULES_PATH)
405
    set(OPENCV_MODULES_PATH "${OpenCV_SOURCE_DIR}/modules")
406
  endif()
407

408
  ocv_glob_modules(${OPENCV_MODULES_PATH} ${OPENCV_EXTRA_MODULES_PATH})
409

410
  # build lists of modules to be documented
411
  set(OPENCV_MODULES_MAIN "")
412
  set(OPENCV_MODULES_EXTRA "")
413

414
  foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
415
    string(REGEX REPLACE "^opencv_" "" mod "${mod}")
416
    if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${OpenCV_SOURCE_DIR}/modules/${mod}")
417
      list(APPEND OPENCV_MODULES_MAIN ${mod})
418
    else()
419
      list(APPEND OPENCV_MODULES_EXTRA ${mod})
420
    endif()
421
  endforeach()
422
  ocv_list_sort(OPENCV_MODULES_MAIN)
423
  ocv_list_sort(OPENCV_MODULES_EXTRA)
424
  set(FIXED_ORDER_MODULES core imgproc imgcodecs videoio highgui video calib3d features2d objdetect dnn ml flann photo stitching)
425
  list(REMOVE_ITEM OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES})
426
  set(OPENCV_MODULES_MAIN ${FIXED_ORDER_MODULES} ${OPENCV_MODULES_MAIN})
427

428
  set(OPENCV_MODULES_MAIN ${OPENCV_MODULES_MAIN} CACHE INTERNAL "List of main modules" FORCE)
429
  set(OPENCV_MODULES_EXTRA ${OPENCV_MODULES_EXTRA} CACHE INTERNAL "List of extra modules" FORCE)
430
endmacro()
431

432

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

441
  set(OPENCV_MODULES_DISABLED_AUTO "${OPENCV_MODULES_DISABLED_AUTO}" CACHE INTERNAL "")
442
  set(OPENCV_MODULES_BUILD "${OPENCV_MODULES_BUILD}" CACHE INTERNAL "")
443
  set(OPENCV_MODULES_PUBLIC "${OPENCV_MODULES_PUBLIC}" CACHE INTERNAL "")
444
endfunction()
445

446
# sort modules by dependencies
447
function(__ocv_sort_modules_by_deps __lst)
448
  ocv_list_sort(${__lst})
449
  set(input ${${__lst}})
450
  set(result "")
451
  set(result_extra "")
452
  while(input)
453
    list(LENGTH input length_before)
454
    foreach (m ${input})
455
      # check if module is in the result already
456
      if (NOT ";${result};" MATCHES ";${m};")
457
        # scan through module dependencies...
458
        set(unresolved_deps_found FALSE)
459
        foreach (d ${OPENCV_MODULE_${m}_DEPS})
460
          # ... which are not already in the result and are enabled
461
          if ((NOT ";${result};" MATCHES ";${d};") AND HAVE_${d})
462
            set(unresolved_deps_found TRUE)
463
            break()
464
          endif()
465
        endforeach()
466
        # check if all dependencies for this module has been resolved
467
        if (NOT unresolved_deps_found)
468
          list(APPEND result ${m})
469
          list(REMOVE_ITEM input ${m})
470
        endif()
471
      endif()
472
    endforeach()
473
    list(LENGTH input length_after)
474
    # check for infinite loop or unresolved dependencies
475
    if (NOT length_after LESS length_before)
476
      if(NOT BUILD_SHARED_LIBS)
477
        if (";${input};" MATCHES ";opencv_world;")
478
          list(REMOVE_ITEM input "opencv_world")
479
          list(APPEND result_extra "opencv_world")
480
        else()
481
          # We can't do here something
482
          list(APPEND result ${input})
483
          break()
484
        endif()
485
      else()
486
        message(FATAL_ERROR "FATAL: Unresolved dependencies or loop in dependency graph (${length_after})\n"
487
          "Processed ${__lst}: ${${__lst}}\n"
488
          "Good modules: ${result}\n"
489
          "Bad modules: ${input}"
490
        )
491
        list(APPEND result ${input})
492
        break()
493
      endif()
494
    endif()
495
  endwhile()
496
  set(${__lst} "${result};${result_extra}" PARENT_SCOPE)
497
endfunction()
498

499
# resolve dependencies
500
function(__ocv_resolve_dependencies)
501
  foreach(m ${OPENCV_MODULES_DISABLED_USER})
502
    set(HAVE_${m} OFF CACHE INTERNAL "Module ${m} will not be built in current configuration")
503
  endforeach()
504
  foreach(m ${OPENCV_MODULES_BUILD})
505
    set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
506
  endforeach()
507

508
  # Whitelist feature
509
  if(BUILD_LIST)
510
    # Prepare the list
511
    string(REGEX REPLACE "[ ,:]+" ";" whitelist "${BUILD_LIST}" )
512
    if(BUILD_opencv_world)
513
      list(APPEND whitelist world)
514
    endif()
515
    ocv_list_add_prefix(whitelist "opencv_")
516
    ocv_list_sort(whitelist)
517
    ocv_list_unique(whitelist)
518
    message(STATUS "Using whitelist: ${whitelist}")
519
    # Expand the list
520
    foreach(depth RANGE 10)
521
      set(new_whitelist ${whitelist})
522
      foreach(m ${whitelist})
523
        list(APPEND new_whitelist ${OPENCV_MODULE_${m}_REQ_DEPS})
524
        list(APPEND new_whitelist ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
525
      endforeach()
526
      ocv_list_sort(new_whitelist)
527
      ocv_list_unique(new_whitelist)
528
      if("${whitelist}" STREQUAL "${new_whitelist}")
529
        break()
530
      endif()
531
      set(whitelist "${new_whitelist}")
532
    endforeach()
533
    # Disable modules not in whitelist
534
    foreach(m ${OPENCV_MODULES_BUILD})
535
      list(FIND whitelist ${m} idx)
536
      if(idx EQUAL -1)
537
        message(STATUS "Module ${m} disabled by whitelist")
538
        __ocv_module_turn_off(${m})
539
      endif()
540
    endforeach()
541
  endif()
542

543
  # add reverse wrapper dependencies (BINDINDS)
544
  foreach(the_module ${OPENCV_MODULES_BUILD})
545
    foreach (wrapper ${OPENCV_MODULE_${the_module}_WRAPPERS})
546
      if(wrapper STREQUAL "python")  # hack for python (BINDINDS)
547
        ocv_add_dependencies(opencv_python2 OPTIONAL ${the_module})
548
        ocv_add_dependencies(opencv_python3 OPTIONAL ${the_module})
549
      else()
550
        ocv_add_dependencies(opencv_${wrapper} OPTIONAL ${the_module})
551
      endif()
552
      if(DEFINED OPENCV_MODULE_opencv_${wrapper}_bindings_generator_CLASS)
553
        ocv_add_dependencies(opencv_${wrapper}_bindings_generator OPTIONAL ${the_module})
554
      endif()
555
    endforeach()
556
  endforeach()
557

558
  # disable MODULES with unresolved dependencies
559
  set(has_changes ON)
560
  while(has_changes)
561
    set(has_changes OFF)
562
    foreach(m ${OPENCV_MODULES_BUILD})
563
      set(__deps ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
564
      while(__deps)
565
        ocv_list_pop_front(__deps d)
566
        string(TOLOWER "${d}" upper_d)
567
        if(NOT (HAVE_${d} OR HAVE_${upper_d} OR TARGET ${d} OR EXISTS ${d}))
568
          if(d MATCHES "^opencv_") # TODO Remove this condition in the future and use HAVE_ variables only
569
            message(STATUS "Module ${m} disabled because ${d} dependency can't be resolved!")
570
            __ocv_module_turn_off(${m})
571
            set(has_changes ON)
572
            break()
573
          else()
574
            message(STATUS "Assume that non-module dependency is available: ${d} (for module ${m})")
575
          endif()
576
        endif()
577
      endwhile()
578
    endforeach()
579
  endwhile()
580

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

583
  foreach(m ${OPENCV_MODULES_BUILD})
584
    set(deps_${m} ${OPENCV_MODULE_${m}_REQ_DEPS})
585
    foreach(d ${OPENCV_MODULE_${m}_OPT_DEPS})
586
      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
587
        if(HAVE_${d} OR TARGET ${d})
588
          list(APPEND deps_${m} ${d})
589
        endif()
590
      endif()
591
    endforeach()
592
#    message(STATUS "Initial deps of ${m} (w/o private deps): ${deps_${m}}")
593
  endforeach()
594

595
  # propagate dependencies
596
  set(has_changes ON)
597
  while(has_changes)
598
    set(has_changes OFF)
599
    foreach(m2 ${OPENCV_MODULES_BUILD}) # transfer deps of m2 to m
600
      foreach(m ${OPENCV_MODULES_BUILD})
601
        if((NOT m STREQUAL m2) AND ";${deps_${m}};" MATCHES ";${m2};")
602
          foreach(d ${deps_${m2}})
603
            if(NOT (";${deps_${m}};" MATCHES ";${d};"))
604
#              message(STATUS "  Transfer dependency ${d} from ${m2} to ${m}")
605
              list(APPEND deps_${m} ${d})
606
              set(has_changes ON)
607
            endif()
608
            if(BUILD_opencv_world
609
                AND NOT "${m}" STREQUAL "opencv_world"
610
                AND NOT "${m2}" STREQUAL "opencv_world"
611
                AND OPENCV_MODULE_${m2}_IS_PART_OF_WORLD
612
                AND NOT OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
613
              if(NOT (";${deps_${m}};" MATCHES ";opencv_world;"))
614
#                message(STATUS "  Transfer dependency opencv_world alias ${m2} to ${m}")
615
                list(APPEND deps_${m} opencv_world)
616
                set(has_changes ON)
617
              endif()
618
            endif()
619
          endforeach()
620
        endif()
621
      endforeach()
622
    endforeach()
623
  endwhile()
624

625
  # process private deps
626
  foreach(m ${OPENCV_MODULES_BUILD})
627
    foreach(d ${OPENCV_MODULE_${m}_PRIVATE_REQ_DEPS})
628
      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
629
        list(APPEND deps_${m} ${d})
630
      endif()
631
    endforeach()
632
    foreach(d ${OPENCV_MODULE_${m}_PRIVATE_OPT_DEPS})
633
      if(NOT (";${deps_${m}};" MATCHES ";${d};"))
634
        if(HAVE_${d} OR TARGET ${d})
635
          list(APPEND deps_${m} ${d})
636
        endif()
637
      endif()
638
    endforeach()
639
  endforeach()
640

641
  ocv_list_sort(OPENCV_MODULES_BUILD)
642

643
  foreach(m ${OPENCV_MODULES_BUILD})
644
#    message(STATUS "FULL deps of ${m}: ${deps_${m}}")
645
    set(OPENCV_MODULE_${m}_DEPS ${deps_${m}})
646
    set(OPENCV_MODULE_${m}_DEPS_EXT ${deps_${m}})
647
    ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
648
    if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
649
      list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS_EXT})
650
    endif()
651
  endforeach()
652

653
  # reorder dependencies
654
  foreach(m ${OPENCV_MODULES_BUILD})
655
    __ocv_sort_modules_by_deps(OPENCV_MODULE_${m}_DEPS)
656

657
    set(LINK_DEPS ${OPENCV_MODULE_${m}_DEPS})
658

659
    # process world
660
    if(BUILD_opencv_world)
661
      if(OPENCV_MODULE_${m}_IS_PART_OF_WORLD)
662
        list(APPEND OPENCV_WORLD_MODULES ${m})
663
      endif()
664
      foreach(m2 ${OPENCV_MODULES_BUILD})
665
        if(OPENCV_MODULE_${m2}_IS_PART_OF_WORLD)
666
          if(";${LINK_DEPS};" MATCHES ";${m2};")
667
            list(REMOVE_ITEM LINK_DEPS ${m2})
668
            if(NOT (";${LINK_DEPS};" MATCHES ";opencv_world;") AND NOT (${m} STREQUAL opencv_world))
669
              list(APPEND LINK_DEPS opencv_world)
670
            endif()
671
          endif()
672
          if("${m}" STREQUAL opencv_world)
673
            list(APPEND OPENCV_MODULE_opencv_world_DEPS_EXT ${OPENCV_MODULE_${m2}_DEPS_EXT})
674
          endif()
675
        endif()
676
      endforeach()
677
    endif()
678

679
    set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
680
    set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
681
    set(OPENCV_MODULE_${m}_DEPS_TO_LINK ${LINK_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module (for linker)")
682

683
#    message(STATUS "  module deps of ${m}: ${OPENCV_MODULE_${m}_DEPS}")
684
#    message(STATUS "  module link deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_TO_LINK}")
685
#    message(STATUS "  extra deps of ${m}: ${OPENCV_MODULE_${m}_DEPS_EXT}")
686
#    message(STATUS "")
687
  endforeach()
688

689
  __ocv_sort_modules_by_deps(OPENCV_MODULES_BUILD)
690

691
  set(OPENCV_MODULES_PUBLIC        ${OPENCV_MODULES_PUBLIC}        CACHE INTERNAL "List of OpenCV modules marked for export")
692
  set(OPENCV_MODULES_BUILD         ${OPENCV_MODULES_BUILD}         CACHE INTERNAL "List of OpenCV modules included into the build")
693
  set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
694
  set(OPENCV_WORLD_MODULES         ${OPENCV_WORLD_MODULES}         CACHE INTERNAL "List of OpenCV modules included into the world")
695
endfunction()
696

697

698
# setup include paths for the list of passed modules
699
macro(ocv_include_modules)
700
  foreach(d ${ARGN})
701
    if(d MATCHES "^opencv_" AND HAVE_${d})
702
      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
703
        ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
704
      endif()
705
    elseif(EXISTS "${d}")
706
      ocv_include_directories("${d}")
707
    endif()
708
  endforeach()
709
endmacro()
710

711
# same as previous but with dependencies
712
macro(ocv_include_modules_recurse)
713
  ocv_include_modules(${ARGN})
714
  foreach(d ${ARGN})
715
    if(d MATCHES "^opencv_" AND HAVE_${d} AND DEFINED OPENCV_MODULE_${d}_DEPS)
716
      foreach (sub ${OPENCV_MODULE_${d}_DEPS})
717
        ocv_include_modules(${sub})
718
      endforeach()
719
    endif()
720
  endforeach()
721
endmacro()
722

723
# setup include paths for the list of passed modules
724
macro(ocv_target_include_modules target)
725
  foreach(d ${ARGN})
726
    if(d MATCHES "^opencv_")
727
      if(HAVE_${d} AND EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
728
        ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
729
      endif()
730
    elseif(EXISTS "${d}")
731
      ocv_target_include_directories(${target} "${d}")
732
    else()
733
      message(WARNING "Unexpected include: ${d} (module=${the_module})")
734
    endif()
735
  endforeach()
736
endmacro()
737

738
# setup include paths for the list of passed modules and recursively add dependent modules
739
macro(ocv_target_include_modules_recurse target)
740
  foreach(d ${ARGN})
741
    if(d MATCHES "^opencv_" AND HAVE_${d})
742
      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
743
        ocv_target_include_directories(${target} "${OPENCV_MODULE_${d}_LOCATION}/include")
744
      endif()
745
      if(OPENCV_MODULE_${d}_DEPS)
746
        ocv_target_include_modules(${target} ${OPENCV_MODULE_${d}_DEPS})
747
      endif()
748
    elseif(EXISTS "${d}")
749
      ocv_target_include_directories(${target} "${d}")
750
    endif()
751
  endforeach()
752
endmacro()
753

754
# setup include path for OpenCV headers for specified module
755
# ocv_module_include_directories(<extra include directories/extra include modules>)
756
macro(ocv_module_include_directories)
757
  if(ENABLE_PRECOMPILED_HEADERS OR OPENCV_INCLUDE_DIR_APPEND_MODULE_SRC)
758
    ocv_target_include_directories(${the_module} "${OPENCV_MODULE_${the_module}_LOCATION}/src")
759
  endif()
760
  ocv_target_include_directories(${the_module}
761
      "${OPENCV_MODULE_${the_module}_LOCATION}/include"
762
      "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
763
      )
764
  ocv_target_include_modules(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
765
endmacro()
766

767

768
# sets header and source files for the current module
769
# NB: all files specified as headers will be installed
770
# Usage:
771
# ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
772
macro(ocv_set_module_sources)
773
  ocv_debug_message("ocv_set_module_sources(" ${ARGN} ")")
774

775
  set(OPENCV_MODULE_${the_module}_HEADERS "")
776
  set(OPENCV_MODULE_${the_module}_SOURCES "")
777

778
  foreach(f "HEADERS" ${ARGN})
779
    if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
780
      set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
781
    else()
782
      list(APPEND ${__filesvar} "${f}")
783
    endif()
784
  endforeach()
785

786
  # the hacky way to embed any files into the OpenCV without modification of its build system
787
  if(COMMAND ocv_get_module_external_sources)
788
    ocv_get_module_external_sources()
789
  endif()
790

791
  if(OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED)
792
    list(APPEND OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES_DISPATCHED})
793
  endif()
794

795
  # TODO Update hooks above
796
  ocv_cmake_hook(INIT_MODULE_SOURCES)
797
  ocv_cmake_hook(INIT_MODULE_SOURCES_${the_module})
798

799
  # use full paths for module to be independent from the module location
800
  ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
801

802
  set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
803
  set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
804
endmacro()
805

806
# finds and sets headers and sources for the standard OpenCV module
807
# Usage:
808
# ocv_glob_module_sources([EXCLUDE_CUDA] [EXCLUDE_OPENCL] <extra sources&headers in the same format as used in ocv_set_module_sources>)
809
macro(ocv_glob_module_sources)
810
  ocv_debug_message("ocv_glob_module_sources(" ${ARGN} ")")
811
  set(_argn ${ARGN})
812
  list(FIND _argn "EXCLUDE_CUDA" exclude_cuda)
813
  if(NOT exclude_cuda EQUAL -1)
814
    list(REMOVE_AT _argn ${exclude_cuda})
815
  endif()
816
  list(FIND _argn "EXCLUDE_OPENCL" exclude_opencl)
817
  if(NOT exclude_opencl EQUAL -1)
818
    list(REMOVE_AT _argn ${exclude_opencl})
819
  endif()
820

821
  file(GLOB_RECURSE lib_srcs
822
       "${CMAKE_CURRENT_LIST_DIR}/src/*.cpp"
823
  )
824
  file(GLOB_RECURSE lib_int_hdrs
825
       "${CMAKE_CURRENT_LIST_DIR}/src/*.hpp"
826
       "${CMAKE_CURRENT_LIST_DIR}/src/*.h"
827
  )
828
  file(GLOB lib_hdrs
829
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
830
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
831
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.h"
832
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.hpp"
833
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/hal/*.h"
834
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.hpp"
835
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/utils/*.h"
836
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/legacy/*.h"
837
  )
838
  file(GLOB lib_hdrs_detail
839
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.hpp"
840
       "${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/detail/*.h"
841
  )
842
  if (APPLE)
843
    file(GLOB_RECURSE lib_srcs_apple
844
         "${CMAKE_CURRENT_LIST_DIR}/src/*.mm"
845
         "${CMAKE_CURRENT_LIST_DIR}/src/*.swift"
846
    )
847
    list(APPEND lib_srcs ${lib_srcs_apple})
848
  endif()
849

850
  ocv_source_group("Src" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/src" FILES ${lib_srcs} ${lib_int_hdrs})
851
  ocv_source_group("Include" DIRBASE "${CMAKE_CURRENT_LIST_DIR}/include" FILES ${lib_hdrs} ${lib_hdrs_detail})
852

853
  set(lib_cuda_srcs "")
854
  set(lib_cuda_hdrs "")
855
  if(HAVE_CUDA AND exclude_cuda EQUAL -1)
856
    file(GLOB lib_cuda_srcs
857
         "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.cu"
858
    )
859
    file(GLOB lib_cuda_hdrs
860
         "${CMAKE_CURRENT_LIST_DIR}/src/cuda/*.hpp"
861
    )
862
    source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
863
  endif()
864

865
  file(GLOB cl_kernels
866
       "${CMAKE_CURRENT_LIST_DIR}/src/opencl/*.cl"
867
  )
868
  if(cl_kernels AND exclude_opencl EQUAL -1)
869
    set(OCL_NAME opencl_kernels_${name})
870
    add_custom_command(
871
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp"  # don't add .hpp file here to optimize build process
872
      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"
873
      DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
874
      COMMENT "Processing OpenCL kernels (${name})"
875
    )
876
    ocv_source_group("Src\\opencl\\kernels" FILES ${cl_kernels})
877
    ocv_source_group("Src\\opencl\\kernels\\autogenerated" FILES "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
878
    set_source_files_properties("${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp"
879
        PROPERTIES GENERATED TRUE
880
    )
881
    list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.cpp" "${CMAKE_CURRENT_BINARY_DIR}/${OCL_NAME}.hpp")
882
  endif()
883

884
  ocv_set_module_sources(${_argn} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
885
                         SOURCES ${lib_srcs} ${lib_int_hdrs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
886
endmacro()
887

888
# creates OpenCV module in current folder
889
# creates new target, configures standard dependencies, compilers flags, install rules
890
# Usage:
891
#   ocv_create_module(<extra link dependencies>)
892
#   ocv_create_module()
893
macro(ocv_create_module)
894
  ocv_debug_message("${the_module}: ocv_create_module(" ${ARGN} ")")
895
  if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS")
896
    message(FATAL_ERROR "Bindings module can't call ocv_create_module()")
897
  endif()
898
  if(NOT " ${ARGN}" STREQUAL " ")
899
    set(OPENCV_MODULE_${the_module}_LINK_DEPS "${OPENCV_MODULE_${the_module}_LINK_DEPS};${ARGN}" CACHE INTERNAL "")
900
  endif()
901
  if(BUILD_opencv_world AND OPENCV_MODULE_${the_module}_IS_PART_OF_WORLD)
902
    # nothing
903
    set(the_module_target opencv_world)
904
  else()
905
    _ocv_create_module(${ARGN})
906
    set(the_module_target ${the_module})
907
  endif()
908

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

920
    if("${the_module}" STREQUAL "opencv_ts")
921
      # copy required dll files; WinRT apps need these dlls that are usually substituted by Visual Studio
922
      # however they are not on path and need to be placed with executables to run from console w/o APPCONTAINER
923
      add_custom_command(TARGET ${the_module}
924
        POST_BUILD
925
        COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcp$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcp$(PlatformToolsetVersion)_app.dll\""
926
        COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\msvcr$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\msvcr$(PlatformToolsetVersion)_app.dll\""
927
        COMMAND copy /y "\"$(VCInstallDir)redist\\$(PlatformTarget)\\Microsoft.VC$(PlatformToolsetVersion).CRT\\vccorlib$(PlatformToolsetVersion).dll\"" "\"${CMAKE_BINARY_DIR}\\bin\\$(Configuration)\\vccorlib$(PlatformToolsetVersion)_app.dll\"")
928
    endif()
929
  endif()
930
endmacro()
931

932
macro(_ocv_create_module)
933
  add_definitions(-D__OPENCV_BUILD=1)
934

935
  ocv_compiler_optimization_process_sources(OPENCV_MODULE_${the_module}_SOURCES OPENCV_MODULE_${the_module}_DEPS_EXT ${the_module})
936
  set(__module_headers ${OPENCV_MODULE_${the_module}_HEADERS})
937
  if(__module_headers)
938
    list(SORT __module_headers)  # fix headers order, useful for bindings
939
  endif()
940
  set(OPENCV_MODULE_${the_module}_HEADERS ${__module_headers} CACHE INTERNAL "List of header files for ${the_module}")
941
  set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
942

943
  # The condition we ought to be testing here is whether ocv_add_precompiled_headers will
944
  # be called at some point in the future. We can't look into the future, though,
945
  # so this will have to do.
946
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp" AND NOT ${the_module} STREQUAL opencv_world)
947
    get_native_precompiled_header(${the_module} precomp.hpp)
948
  endif()
949

950
  if(WIN32
951
      AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
952
      AND NOT OPENCV_VS_VERSIONINFO_SKIP)
953
    if(DEFINED OPENCV_VS_VERSIONINFO_FILE)
954
      set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_FILE}")
955
    elseif(DEFINED OPENCV_VS_VERSIONINFO_${the_module}_FILE)
956
      set(_VS_VERSION_FILE "${OPENCV_VS_VERSIONINFO_${the_module}_FILE}")
957
    elseif(NOT OPENCV_VS_VERSIONINFO_SKIP_GENERATION)
958
      set(_VS_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/vs_version.rc")
959
      ocv_generate_vs_version_file("${_VS_VERSION_FILE}"
960
        NAME "${the_module}"
961
        FILEDESCRIPTION "OpenCV module: ${OPENCV_MODULE_${the_module}_DESCRIPTION}"
962
        INTERNALNAME "${the_module}${OPENCV_DLLVERSION}"
963
        ORIGINALFILENAME "${the_module}${OPENCV_DLLVERSION}.dll"
964
      )
965
    endif()
966
    if(_VS_VERSION_FILE)
967
      if(NOT EXISTS "${_VS_VERSION_FILE}")
968
        message(STATUS "${the_module}: Required .rc file is missing: ${_VS_VERSION_FILE}")
969
      endif()
970
      source_group("Src" FILES "${_VS_VERSION_FILE}")
971
    endif()
972
  endif()
973
  if(WIN32 AND NOT (
974
          "${the_module}" STREQUAL "opencv_core" OR
975
          "${the_module}" STREQUAL "opencv_world" OR
976
          "${the_module}" STREQUAL "opencv_cudev"
977
      )
978
      AND (BUILD_SHARED_LIBS AND NOT "x${OPENCV_MODULE_TYPE}" STREQUAL "xSTATIC")
979
      AND NOT OPENCV_SKIP_DLLMAIN_GENERATION
980
  )
981
      set(_DLLMAIN_FILE "${CMAKE_CURRENT_BINARY_DIR}/${the_module}_main.cpp")
982
      configure_file("${OpenCV_SOURCE_DIR}/cmake/templates/dllmain.cpp.in" "${_DLLMAIN_FILE}" @ONLY)
983
  endif()
984

985
  source_group("Include" FILES "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp")
986
  source_group("Src" FILES "${${the_module}_pch}")
987
  ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY)
988
  ocv_cmake_hook(PRE_CREATE_MODULE_LIBRARY_${the_module})
989
  ocv_add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
990
    "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
991
    ${${the_module}_pch}
992
    ${_VS_VERSION_FILE}
993
    ${_DLLMAIN_FILE}
994
  )
995
  set_target_properties(${the_module} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
996
  set_source_files_properties(${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES} ${${the_module}_pch}
997
    PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};Module")
998

999
  ocv_target_link_libraries(${the_module} PUBLIC    ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
1000
                                          INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_TO_LINK}
1001
  )
1002
  ocv_target_link_libraries(${the_module} PUBLIC    ${OPENCV_MODULE_${the_module}_DEPS_EXT}
1003
                                          INTERFACE ${OPENCV_MODULE_${the_module}_DEPS_EXT}
1004
  )
1005
  ocv_target_link_libraries(${the_module} PRIVATE ${OPENCV_LINKER_LIBS} ${OPENCV_HAL_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
1006
  if (NOT ENABLE_CUDA_FIRST_CLASS_LANGUAGE AND HAVE_CUDA)
1007
    ocv_target_link_libraries(${the_module} PRIVATE ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
1008
  endif()
1009

1010
  if(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS)
1011
    target_compile_definitions(${the_module} ${OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS})
1012
    unset(OPENCV_MODULE_${the_module}_COMPILE_DEFINITIONS CACHE)
1013
  endif()
1014

1015
  add_dependencies(opencv_modules ${the_module})
1016

1017
  if(ENABLE_SOLUTION_FOLDERS)
1018
    set_target_properties(${the_module} PROPERTIES FOLDER "modules")
1019
  endif()
1020

1021
  set_target_properties(${the_module} PROPERTIES
1022
    OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
1023
    DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1024
    COMPILE_PDB_NAME "${the_module}${OPENCV_DLLVERSION}"
1025
    COMPILE_PDB_NAME_DEBUG "${the_module}${OPENCV_DLLVERSION}${OPENCV_DEBUG_POSTFIX}"
1026
    ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
1027
    COMPILE_PDB_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
1028
    LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
1029
    RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
1030
    DEFINE_SYMBOL CVAPI_EXPORTS
1031
  )
1032

1033
  if(BUILD_FAT_JAVA_LIB)  # force exports from static modules too
1034
    if(BUILD_SHARED_LIBS)
1035
      message(FATAL_ERROR "Assertion failed: BUILD_SHARED_LIBS=OFF must be off if BUILD_FAT_JAVA_LIB=ON")
1036
    endif()
1037
    target_compile_definitions(${the_module} PRIVATE CVAPI_EXPORTS)
1038
  endif()
1039

1040
  # For dynamic link numbering conventions
1041
  if(NOT ANDROID)
1042
    # Android SDK build scripts can include only .so files into final .apk
1043
    # As result we should not set version properties for Android
1044
    set_target_properties(${the_module} PROPERTIES
1045
      VERSION ${OPENCV_LIBVERSION}
1046
      SOVERSION ${OPENCV_SOVERSION}
1047
    )
1048
  endif()
1049

1050
  if (ENABLE_GNU_STL_DEBUG)
1051
    target_compile_definitions(${the_module} PUBLIC _GLIBCXX_DEBUG)
1052
  endif()
1053

1054
  if(MSVC)
1055
    if(CMAKE_CROSSCOMPILING)
1056
      set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
1057
    endif()
1058
    set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
1059
  endif()
1060

1061
  get_target_property(_target_type ${the_module} TYPE)
1062
  if(OPENCV_MODULE_${the_module}_CLASS STREQUAL "PUBLIC" AND
1063
      ("${_target_type}" STREQUAL "SHARED_LIBRARY" OR (NOT BUILD_SHARED_LIBS OR NOT INSTALL_CREATE_DISTRIB)))
1064
    ocv_install_target(${the_module} EXPORT OpenCVModules OPTIONAL
1065
      RUNTIME DESTINATION ${OPENCV_BIN_INSTALL_PATH} COMPONENT libs
1066
      LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT libs NAMELINK_SKIP
1067
      ARCHIVE DESTINATION ${OPENCV_LIB_ARCHIVE_INSTALL_PATH} COMPONENT dev
1068
      )
1069
  endif()
1070
  if("${_target_type}" STREQUAL "SHARED_LIBRARY")
1071
    install(TARGETS ${the_module}
1072
      LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT dev NAMELINK_ONLY)
1073
  endif()
1074

1075
  # only "public" headers need to be installed
1076
  ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS)
1077
  ocv_cmake_hook(PRE_INSTALL_MODULE_HEADERS_${the_module})
1078
  if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
1079
    foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
1080
      string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
1081
      if(NOT hdr2 MATCHES "private" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
1082
        install(FILES ${hdr} OPTIONAL DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT dev)
1083
      else()
1084
        #message("Header file will be NOT installed: ${hdr}")
1085
      endif()
1086
    endforeach()
1087
  endif()
1088

1089
  _ocv_add_precompiled_headers(${the_module})
1090

1091
  ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY)
1092
  ocv_cmake_hook(POST_CREATE_MODULE_LIBRARY_${the_module})
1093
endmacro()
1094

1095
# opencv precompiled headers macro (can add pch to modules and tests)
1096
# this macro must be called after any "add_definitions" commands, otherwise precompiled headers will not work
1097
# Usage:
1098
# ocv_add_precompiled_headers(${the_module})
1099
macro(_ocv_add_precompiled_headers the_target)
1100
  ocv_debug_message("ocv_add_precompiled_headers(" ${the_target} ${ARGN} ")")
1101

1102
  if("${the_target}" MATCHES "^opencv_test_.*$")
1103
    SET(pch_path "test/test_")
1104
  elseif("${the_target}" MATCHES "^opencv_perf_.*$")
1105
    SET(pch_path "perf/perf_")
1106
  else()
1107
    SET(pch_path "src/")
1108
  endif()
1109
  ocv_add_precompiled_header_to_target(${the_target} "${CMAKE_CURRENT_SOURCE_DIR}/${pch_path}precomp.hpp")
1110
  unset(pch_path)
1111
endmacro()
1112

1113
# short command for adding simple OpenCV module
1114
# see ocv_add_module for argument details
1115
# Usage:
1116
# ocv_define_module(module_name  [INTERNAL] [EXCLUDE_CUDA] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>] [WRAP <list of wrappers>])
1117
macro(ocv_define_module module_name)
1118
  ocv_debug_message("ocv_define_module(" ${module_name} ${ARGN} ")")
1119
  set(_argn ${ARGN})
1120
  set(exclude_cuda "")
1121
  foreach(arg ${_argn})
1122
    if("${arg}" STREQUAL "EXCLUDE_CUDA")
1123
      set(exclude_cuda "${arg}")
1124
      list(REMOVE_ITEM _argn ${arg})
1125
    endif()
1126
  endforeach()
1127

1128
  ocv_add_module(${module_name} ${_argn})
1129
  ocv_glob_module_sources(${exclude_cuda})
1130
  ocv_module_include_directories()
1131
  ocv_create_module()
1132

1133
  ocv_add_accuracy_tests()
1134
  ocv_add_perf_tests()
1135
  ocv_add_samples()
1136
endmacro()
1137

1138
# ensures that all passed modules are available
1139
# sets OCV_DEPENDENCIES_FOUND variable to TRUE/FALSE
1140
macro(ocv_check_dependencies)
1141
  set(OCV_DEPENDENCIES_FOUND TRUE)
1142
  foreach(d ${ARGN})
1143
    if(d MATCHES "^opencv_[^ ]+$" AND NOT HAVE_${d})
1144
      set(OCV_DEPENDENCIES_FOUND FALSE)
1145
      break()
1146
    endif()
1147
  endforeach()
1148
endmacro()
1149

1150
################################################################################
1151
# OpenCV tests
1152
################################################################################
1153

1154
if(DEFINED OPENCV_BUILD_TEST_MODULES_LIST)
1155
  string(REPLACE "," ";" OPENCV_BUILD_TEST_MODULES_LIST "${OPENCV_BUILD_TEST_MODULES_LIST}")  # support comma-separated list (,) too
1156
endif()
1157
if(DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST)
1158
  string(REPLACE "," ";" OPENCV_BUILD_PERF_TEST_MODULES_LIST "${OPENCV_BUILD_PERF_TEST_MODULES_LIST}")  # support comma-separated list (,) too
1159
endif()
1160

1161
# auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
1162
macro(__ocv_parse_test_sources tests_type)
1163
  set(OPENCV_${tests_type}_${the_module}_SOURCES "")
1164
  set(OPENCV_${tests_type}_${the_module}_DEPS "")
1165
  set(__file_group_name "")
1166
  set(__file_group_sources "")
1167
  foreach(arg "DEPENDS_ON" ${ARGN} "FILES")
1168
    if(arg STREQUAL "FILES")
1169
      set(__currentvar "__file_group_sources")
1170
      if(__file_group_name AND __file_group_sources)
1171
        source_group("${__file_group_name}" FILES ${__file_group_sources})
1172
        list(APPEND OPENCV_${tests_type}_${the_module}_SOURCES ${__file_group_sources})
1173
      endif()
1174
      set(__file_group_name "")
1175
      set(__file_group_sources "")
1176
    elseif(arg STREQUAL "DEPENDS_ON")
1177
      set(__currentvar "OPENCV_${tests_type}_${the_module}_DEPS")
1178
    elseif(" ${__currentvar}" STREQUAL " __file_group_sources" AND NOT __file_group_name) # spaces to avoid CMP0054
1179
      set(__file_group_name "${arg}")
1180
    else()
1181
      list(APPEND ${__currentvar} "${arg}")
1182
    endif()
1183
  endforeach()
1184
  unset(__file_group_name)
1185
  unset(__file_group_sources)
1186
  unset(__currentvar)
1187
endmacro()
1188

1189
ocv_check_environment_variables(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
1190

1191
# this is a command for adding OpenCV performance tests to the module
1192
# ocv_add_perf_tests(<extra_dependencies>)
1193
function(ocv_add_perf_tests)
1194
  ocv_debug_message("ocv_add_perf_tests(" ${ARGN} ")")
1195

1196
  if(WINRT)
1197
    set(OPENCV_DEBUG_POSTFIX "")
1198
  endif()
1199

1200
  set(perf_path "${CMAKE_CURRENT_LIST_DIR}/perf")
1201
  if(BUILD_PERF_TESTS AND EXISTS "${perf_path}"
1202
      AND (NOT DEFINED OPENCV_BUILD_PERF_TEST_MODULES_LIST
1203
          OR OPENCV_BUILD_PERF_TEST_MODULES_LIST STREQUAL "all"
1204
          OR ";${OPENCV_BUILD_PERF_TEST_MODULES_LIST};" MATCHES ";${name};"
1205
      )
1206
  )
1207
    __ocv_parse_test_sources(PERF ${ARGN})
1208

1209
    # opencv_imgcodecs is required for imread/imwrite
1210
    set(perf_deps opencv_ts ${the_module} opencv_imgcodecs ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1211
    ocv_check_dependencies(${perf_deps})
1212

1213
    if(OCV_DEPENDENCIES_FOUND)
1214
      set(the_target "opencv_perf_${name}")
1215
      # project(${the_target})
1216

1217
      if(NOT OPENCV_PERF_${the_module}_SOURCES)
1218
        file(GLOB_RECURSE perf_srcs "${perf_path}/*.cpp")
1219
        file(GLOB_RECURSE perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
1220
        ocv_source_group("Src" DIRBASE "${perf_path}" FILES ${perf_srcs})
1221
        ocv_source_group("Include" DIRBASE "${perf_path}" FILES ${perf_hdrs})
1222
        set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
1223
      endif()
1224

1225
      ocv_compiler_optimization_process_sources(OPENCV_PERF_${the_module}_SOURCES OPENCV_PERF_${the_module}_DEPS ${the_target})
1226

1227
      if(NOT BUILD_opencv_world)
1228
        get_native_precompiled_header(${the_target} perf_precomp.hpp)
1229
      endif()
1230

1231
      source_group("Src" FILES "${${the_target}_pch}")
1232
      ocv_add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
1233
      ocv_target_include_modules(${the_target} ${perf_deps})
1234
      ocv_target_link_libraries(${the_target} PRIVATE ${perf_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_PERF_${the_module}_DEPS})
1235
      add_dependencies(opencv_perf_tests ${the_target})
1236

1237
      if(TARGET opencv_videoio_plugins)
1238
        add_dependencies(${the_target} opencv_videoio_plugins)
1239
      endif()
1240
      if(TARGET opencv_highgui_plugins)
1241
        add_dependencies(${the_target} opencv_highgui_plugins)
1242
      endif()
1243

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

1249
      set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1250
      set_source_files_properties(${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch}
1251
        PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};PerfTest")
1252

1253
      # Additional target properties
1254
      set_target_properties(${the_target} PROPERTIES
1255
        DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1256
        RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1257
      )
1258
      if(ENABLE_SOLUTION_FOLDERS)
1259
        set_target_properties(${the_target} PROPERTIES FOLDER "tests performance")
1260
      endif()
1261

1262
      if(WINRT)
1263
        # removing APPCONTAINER from tests to run from console
1264
        # look for detailed description inside of ocv_create_module macro above
1265
        add_custom_command(TARGET "opencv_perf_${name}"
1266
                           POST_BUILD
1267
                           COMMAND link.exe /edit /APPCONTAINER:NO $(TargetPath))
1268
      endif()
1269

1270
      if(NOT BUILD_opencv_world)
1271
        _ocv_add_precompiled_headers(${the_target})
1272
      endif()
1273

1274
      ocv_add_test_from_target("${the_target}" "Performance" "${the_target}")
1275
      ocv_add_test_from_target("opencv_sanity_${name}" "Sanity" "${the_target}"
1276
                               "--perf_min_samples=1"
1277
                               "--perf_force_samples=1"
1278
                               "--perf_verify_sanity")
1279
    else(OCV_DEPENDENCIES_FOUND)
1280
      # TODO: warn about unsatisfied dependencies
1281
    endif(OCV_DEPENDENCIES_FOUND)
1282
    if(INSTALL_TESTS)
1283
      install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1284
    endif()
1285
  endif()
1286
endfunction()
1287

1288
# this is a command for adding OpenCV accuracy/regression tests to the module
1289
# ocv_add_accuracy_tests(<list of extra dependencies>)
1290
function(ocv_add_accuracy_tests)
1291
  ocv_debug_message("ocv_add_accuracy_tests(" ${ARGN} ")")
1292

1293
  set(test_path "${CMAKE_CURRENT_LIST_DIR}/test")
1294
  if(BUILD_TESTS AND EXISTS "${test_path}"
1295
      AND (NOT DEFINED OPENCV_BUILD_TEST_MODULES_LIST
1296
          OR OPENCV_BUILD_TEST_MODULES_LIST STREQUAL "all"
1297
          OR ";${OPENCV_BUILD_TEST_MODULES_LIST};" MATCHES ";${name};"
1298
      )
1299
  )
1300
    __ocv_parse_test_sources(TEST ${ARGN})
1301

1302
    # opencv_imgcodecs is required for imread/imwrite
1303
    set(test_deps opencv_ts ${the_module} opencv_imgcodecs opencv_videoio ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
1304
    ocv_check_dependencies(${test_deps})
1305
    if(OCV_DEPENDENCIES_FOUND)
1306
      set(the_target "opencv_test_${name}")
1307
      # project(${the_target})
1308

1309
      if(NOT OPENCV_TEST_${the_module}_SOURCES)
1310
        file(GLOB_RECURSE test_srcs "${test_path}/*.cpp")
1311
        file(GLOB_RECURSE test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
1312
        ocv_source_group("Src" DIRBASE "${test_path}" FILES ${test_srcs})
1313
        ocv_source_group("Include" DIRBASE "${test_path}" FILES ${test_hdrs})
1314
        set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
1315
      endif()
1316

1317
      if(OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED)
1318
        list(APPEND OPENCV_TEST_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_TEST_SOURCES_DISPATCHED})
1319
      endif()
1320
      ocv_compiler_optimization_process_sources(OPENCV_TEST_${the_module}_SOURCES OPENCV_TEST_${the_module}_DEPS ${the_target})
1321

1322
      if(NOT BUILD_opencv_world)
1323
        get_native_precompiled_header(${the_target} test_precomp.hpp)
1324
      endif()
1325

1326
      source_group("Src" FILES "${${the_target}_pch}")
1327
      ocv_add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
1328
      ocv_target_include_modules(${the_target} ${test_deps})
1329
      if(EXISTS "${CMAKE_CURRENT_BINARY_DIR}/test")
1330
        ocv_target_include_directories(${the_target} "${CMAKE_CURRENT_BINARY_DIR}/test")
1331
      endif()
1332
      ocv_target_link_libraries(${the_target} PRIVATE ${test_deps} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_LINKER_LIBS} ${OPENCV_TEST_${the_module}_DEPS})
1333
      add_dependencies(opencv_tests ${the_target})
1334

1335
      if(TARGET opencv_videoio_plugins)
1336
        add_dependencies(${the_target} opencv_videoio_plugins)
1337
      endif()
1338
      if(TARGET opencv_highgui_plugins)
1339
        add_dependencies(${the_target} opencv_highgui_plugins)
1340
      endif()
1341

1342
      if(HAVE_HPX)
1343
        message("Linking HPX to Perf test of module ${name}")
1344
        ocv_target_link_libraries(${the_target} LINK_PRIVATE "${HPX_LIBRARIES}")
1345
      endif()
1346

1347
      set_target_properties(${the_target} PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1348
      set_source_files_properties(${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch}
1349
        PROPERTIES LABELS "${OPENCV_MODULE_${the_module}_LABEL};AccuracyTest")
1350

1351
      # Additional target properties
1352
      set_target_properties(${the_target} PROPERTIES
1353
        DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
1354
        RUNTIME_OUTPUT_DIRECTORY "${EXECUTABLE_OUTPUT_PATH}"
1355
      )
1356

1357
      ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "__OPENCV_TESTS=1")
1358

1359
      if(ENABLE_SOLUTION_FOLDERS)
1360
        set_target_properties(${the_target} PROPERTIES FOLDER "tests accuracy")
1361
      endif()
1362

1363
      if(OPENCV_TEST_BIGDATA)
1364
        ocv_append_target_property(${the_target} COMPILE_DEFINITIONS "OPENCV_TEST_BIGDATA=1")
1365
      endif()
1366

1367
      if(NOT BUILD_opencv_world)
1368
        _ocv_add_precompiled_headers(${the_target})
1369
      endif()
1370

1371
      if(OPENCV_TEST_EXTRA_CXX_FLAGS_Release)
1372
        target_compile_options(${the_target} PRIVATE "$<$<CONFIG:Release>:${OPENCV_TEST_EXTRA_CXX_FLAGS_Release}>")
1373
      endif()
1374

1375
      ocv_add_test_from_target("${the_target}" "Accuracy" "${the_target}")
1376
    else(OCV_DEPENDENCIES_FOUND)
1377
      # TODO: warn about unsatisfied dependencies
1378
    endif(OCV_DEPENDENCIES_FOUND)
1379

1380
    if(INSTALL_TESTS)
1381
      install(TARGETS ${the_target} RUNTIME DESTINATION ${OPENCV_TEST_INSTALL_PATH} COMPONENT tests)
1382
    endif()
1383
  endif()
1384
endfunction()
1385

1386
function(ocv_add_samples)
1387
  ocv_debug_message("ocv_add_samples(" ${ARGN} ")")
1388

1389
  set(samples_path "${CMAKE_CURRENT_SOURCE_DIR}/samples")
1390
  if(NOT EXISTS "${samples_path}")
1391
    return()
1392
  endif()
1393

1394
  string(REGEX REPLACE "^opencv_" "" module_id ${the_module})
1395

1396
  if(BUILD_EXAMPLES)
1397
    set(samples_deps ${the_module} ${OPENCV_MODULE_${the_module}_DEPS} opencv_imgcodecs opencv_videoio opencv_highgui ${ARGN})
1398
    ocv_check_dependencies(${samples_deps})
1399

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

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

1407
        ocv_add_executable(${the_target} "${source}")
1408
        ocv_target_include_modules(${the_target} ${samples_deps})
1409
        ocv_target_link_libraries(${the_target} PRIVATE ${samples_deps})
1410

1411
        set_target_properties(${the_target} PROPERTIES
1412
          PROJECT_LABEL "(sample) ${name}"
1413
          LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1414
        set_source_files_properties("${source}" PROPERTIES
1415
          LABELS "${OPENCV_MODULE_${the_module}_LABEL};Sample")
1416
        if(ENABLE_SOLUTION_FOLDERS)
1417
          set_target_properties(${the_target} PROPERTIES
1418
            FOLDER "samples/${module_id}")
1419
        endif()
1420
        # Add single target to build all samples for the module: 'make opencv_samples_bioinspired'
1421
        set(parent_target opencv_samples_${module_id})
1422
        if(NOT TARGET ${parent_target})
1423
          add_custom_target(${parent_target})
1424
          add_dependencies(opencv_samples ${parent_target})
1425
        endif()
1426
        add_dependencies(${parent_target} ${the_target})
1427

1428
        if(TARGET opencv_videoio_plugins)
1429
          add_dependencies(${the_target} opencv_videoio_plugins)
1430
        endif()
1431
        if(TARGET opencv_highgui_plugins)
1432
          add_dependencies(${the_target} opencv_highgui_plugins)
1433
        endif()
1434

1435
        if(INSTALL_BIN_EXAMPLES)
1436
          install(TARGETS ${the_target} RUNTIME DESTINATION "${OPENCV_SAMPLES_BIN_INSTALL_PATH}/${module_id}" COMPONENT samples)
1437
        endif()
1438
      endforeach()
1439
    endif()
1440
  endif()
1441

1442
  if(INSTALL_C_EXAMPLES)
1443
    file(GLOB DEPLOY_FILES_AND_DIRS "${samples_path}/*")
1444
    foreach(ITEM ${DEPLOY_FILES_AND_DIRS})
1445
        IF( IS_DIRECTORY "${ITEM}" )
1446
            LIST( APPEND sample_dirs "${ITEM}" )
1447
        ELSE()
1448
            LIST( APPEND sample_files "${ITEM}" )
1449
        ENDIF()
1450
    endforeach()
1451
    install(FILES ${sample_files}
1452
            DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1453
            COMPONENT samples)
1454
    install(DIRECTORY ${sample_dirs}
1455
            DESTINATION "${OPENCV_SAMPLES_SRC_INSTALL_PATH}/${module_id}"
1456
            COMPONENT samples)
1457
  endif()
1458
endfunction()
1459

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.