OpenCVModule.cmake 32.6 KB
Newer Older
1 2 3 4 5 6 7 8 9
# 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
# OPENCV_MODULE_${the_module}_DESCRIPTION
10
# OPENCV_MODULE_${the_module}_CLASS - PUBLIC|INTERNAL|BINDINGS
11 12 13 14 15 16 17 18 19 20 21
# OPENCV_MODULE_${the_module}_HEADERS
# OPENCV_MODULE_${the_module}_SOURCES
# OPENCV_MODULE_${the_module}_DEPS - final flattened set of module dependencies
# OPENCV_MODULE_${the_module}_DEPS_EXT
# OPENCV_MODULE_${the_module}_REQ_DEPS
# OPENCV_MODULE_${the_module}_OPT_DEPS
# 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
# OPENCV_MODULE_TYPE - STATIC|SHARED - set to force override global settings for current module
22 23
# 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}
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

# The verbose template for OpenCV module:
#
#   ocv_add_module(modname <dependencies>)
#   ocv_glob_module_sources() or glob them manually and ocv_set_module_sources(...)
#   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>)
#
#
# 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
44
foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
45 46 47
  if(HAVE_${mod})
    unset(HAVE_${mod} CACHE)
  endif()
48 49
  unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
  unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
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")

# adds dependencies to OpenCV module
# Usage:
#   add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
# Notes:
# * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
macro(ocv_add_dependencies full_modname)
  #we don't clean the dependencies here to allow this macro several times for every module
66 67
  foreach(d "REQUIRED" ${ARGN})
    if(d STREQUAL "REQUIRED")
68 69 70 71 72 73 74
      set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
    elseif(d STREQUAL "OPTIONAL")
      set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
    else()
      list(APPEND ${__depsvar} "${d}")
    endif()
  endforeach()
75 76 77 78
  unset(__depsvar)

  ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
  ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
79 80 81

  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")
82 83
endmacro()

84 85
# declare new OpenCV module in current folder
# Usage:
86
#   ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
87
# Example:
88
#   ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_gpu)
89 90 91 92
macro(ocv_add_module _name)
  string(TOLOWER "${_name}" name)
  string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
  set(the_module opencv_${name})
93

94 95
  # the first pass - collect modules info, the second pass - create targets
  if(OPENCV_INITIAL_PASS)
96 97 98 99 100 101 102 103
    #guard agains redefinition
    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()

104 105 106
    if(NOT DEFINED the_description)
      set(the_description "The ${name} OpenCV module")
    endif()
107

108 109
    if(NOT DEFINED BUILD_${the_module}_INIT)
      set(BUILD_${the_module}_INIT ON)
110
    endif()
111 112

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

115 116 117 118 119
    # 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")

    # parse list of dependencies
120
    if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
121
      set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The cathegory of the module")
122 123 124 125
      set(__ocv_argn__ ${ARGN})
      list(REMOVE_AT __ocv_argn__ 0)
      ocv_add_dependencies(${the_module} ${__ocv_argn__})
      unset(__ocv_argn__)
126
    else()
127
      set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The cathegory of the module")
128 129
      ocv_add_dependencies(${the_module} ${ARGN})
      if(BUILD_${the_module})
130 131
        set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
      endif()
132
    endif()
133

134 135 136 137
    # add self to the world dependencies
    if(NOT DEFINED OPENCV_MODULE_IS_PART_OF_WORLD AND NOT OPENCV_MODULE_${the_module}_CLASS STREQUAL "BINDINGS" OR OPENCV_MODULE_IS_PART_OF_WORLD)
      ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
    endif()
138 139

    if(BUILD_${the_module})
140
      set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
141 142
    else()
      set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
143
    endif()
144

145
    # TODO: add submodules if any
146

147
    # stop processing of current file
148 149 150
    return()
  else(OPENCV_INITIAL_PASS)
    if(NOT BUILD_${the_module})
151
      return() # extra protection from redefinition
152
    endif()
153 154
    project(${the_module})
  endif(OPENCV_INITIAL_PASS)
155 156
endmacro()

157
# excludes module from current configuration
158 159 160 161 162 163 164
macro(ocv_module_disable module)
  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")
165
  set(OPENCV_MODULE_${__modname}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${__modname} module sources")
166
  set(OPENCV_MODULES_DISABLED_FORCE "${OPENCV_MODULES_DISABLED_FORCE}" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
167 168 169
  if(BUILD_${__modname})
    # touch variable controlling build of the module to suppress "unused variable" CMake warning
  endif()
170
  unset(__modname)
171
  return() # leave the current folder
172
endmacro()
173 174


175 176
# Internal macro; partly disables OpenCV module
macro(__ocv_module_turn_off the_module)
177
  list(REMOVE_ITEM OPENCV_MODULES_DISABLED_AUTO "${the_module}")
178 179 180 181 182 183 184
  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")
endmacro()

# Internal macro for dependencies tracking
185 186 187 188
macro(__ocv_flatten_module_required_dependencies the_module)
  set(__flattened_deps "")
  set(__resolved_deps "")
  set(__req_depends ${OPENCV_MODULE_${the_module}_REQ_DEPS})
189

190
  while(__req_depends)
191
    ocv_list_pop_front(__req_depends __dep)
192
    if(__dep STREQUAL the_module)
193
      __ocv_module_turn_off(${the_module}) # TODO: think how to deal with cyclic dependency
194
      break()
195 196
    elseif(";${OPENCV_MODULES_DISABLED_USER};${OPENCV_MODULES_DISABLED_AUTO};" MATCHES ";${__dep};")
      __ocv_module_turn_off(${the_module}) # depends on disabled module
197
      list(APPEND __flattened_deps "${__dep}")
198 199 200
    elseif(";${OPENCV_MODULES_BUILD};" MATCHES ";${__dep};")
      if(";${__resolved_deps};" MATCHES ";${__dep};")
        list(APPEND __flattened_deps "${__dep}") # all dependencies of this module are already resolved
201
      else()
202
        # put all required subdependencies before this dependency and mark it as resolved
203 204 205 206
        list(APPEND __resolved_deps "${__dep}")
        list(INSERT __req_depends 0 ${OPENCV_MODULE_${__dep}_REQ_DEPS} ${__dep})
      endif()
    elseif(__dep MATCHES "^opencv_")
207 208
      __ocv_module_turn_off(${the_module}) # depends on missing module
      message(WARNING "Unknown \"${__dep}\" module is listened in the dependencies of \"${the_module}\" module")
209 210
      break()
    else()
211
      # skip non-modules
212 213 214 215 216 217 218 219 220
    endif()
  endwhile()

  if(__flattened_deps)
    list(REMOVE_DUPLICATES __flattened_deps)
    set(OPENCV_MODULE_${the_module}_DEPS ${__flattened_deps})
  else()
    set(OPENCV_MODULE_${the_module}_DEPS "")
  endif()
221

222
  ocv_clear_vars(__resolved_deps __flattened_deps __req_depends __dep)
223
endmacro()
224

225
# Internal macro for dependencies tracking
226
macro(__ocv_flatten_module_optional_dependencies the_module)
227 228 229
  set(__flattened_deps "")
  set(__resolved_deps "")
  set(__opt_depends ${OPENCV_MODULE_${the_module}_REQ_DEPS} ${OPENCV_MODULE_${the_module}_OPT_DEPS})
230

231
  while(__opt_depends)
232
    ocv_list_pop_front(__opt_depends __dep)
233
    if(__dep STREQUAL the_module)
234
      __ocv_module_turn_off(${the_module}) # TODO: think how to deal with cyclic dependency
235
      break()
236 237 238
    elseif(";${OPENCV_MODULES_BUILD};" MATCHES ";${__dep};")
      if(";${__resolved_deps};" MATCHES ";${__dep};")
        list(APPEND __flattened_deps "${__dep}") # all dependencies of this module are already resolved
239
      else()
240
        # put all subdependencies before this dependency and mark it as resolved
241 242 243 244
        list(APPEND __resolved_deps "${__dep}")
        list(INSERT __opt_depends 0 ${OPENCV_MODULE_${__dep}_REQ_DEPS} ${OPENCV_MODULE_${__dep}_OPT_DEPS} ${__dep})
      endif()
    else()
245
      # skip non-modules or missing modules
246 247
    endif()
  endwhile()
248

249 250 251 252 253 254
  if(__flattened_deps)
    list(REMOVE_DUPLICATES __flattened_deps)
    set(OPENCV_MODULE_${the_module}_DEPS ${__flattened_deps})
  else()
    set(OPENCV_MODULE_${the_module}_DEPS "")
  endif()
255

256
  ocv_clear_vars(__resolved_deps __flattened_deps __opt_depends __dep)
257
endmacro()
258

259 260 261 262 263
macro(__ocv_flatten_module_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})
264
    set(HAVE_${m} ON CACHE INTERNAL "Module ${m} will be built in current configuration")
265
    __ocv_flatten_module_required_dependencies(${m})
266
    set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened required dependencies of ${m} module")
267
  endforeach()
268

269 270
  foreach(m ${OPENCV_MODULES_BUILD})
    __ocv_flatten_module_optional_dependencies(${m})
271

272
    # save dependencies from other modules
273
    set(OPENCV_MODULE_${m}_DEPS ${OPENCV_MODULE_${m}_DEPS} CACHE INTERNAL "Flattened dependencies of ${m} module")
274
    # save extra dependencies
275 276 277 278 279 280 281
    set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_REQ_DEPS} ${OPENCV_MODULE_${m}_OPT_DEPS})
    if(OPENCV_MODULE_${m}_DEPS_EXT AND OPENCV_MODULE_${m}_DEPS)
      list(REMOVE_ITEM OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS})
    endif()
    ocv_list_filterout(OPENCV_MODULE_${m}_DEPS_EXT "^opencv_[^ ]+$")
    set(OPENCV_MODULE_${m}_DEPS_EXT ${OPENCV_MODULE_${m}_DEPS_EXT} CACHE INTERNAL "Extra dependencies of ${m} module")
  endforeach()
282

283 284 285 286 287 288 289
  # order modules by dependencies
  set(OPENCV_MODULES_BUILD_ "")
  foreach(m ${OPENCV_MODULES_BUILD})
    list(APPEND OPENCV_MODULES_BUILD_ ${OPENCV_MODULE_${m}_DEPS} ${m})
  endforeach()
  ocv_list_unique(OPENCV_MODULES_BUILD_)

290
  set(OPENCV_MODULES_PUBLIC        ${OPENCV_MODULES_PUBLIC}        CACHE INTERNAL "List of OpenCV modules marked for export")
291
  set(OPENCV_MODULES_BUILD         ${OPENCV_MODULES_BUILD_}        CACHE INTERNAL "List of OpenCV modules included into the build")
292 293
  set(OPENCV_MODULES_DISABLED_AUTO ${OPENCV_MODULES_DISABLED_AUTO} CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
endmacro()
294

295 296 297
# collect modules from specified directories
# NB: must be called only once!
macro(ocv_glob_modules)
298 299 300 301 302
  if(DEFINED OPENCV_INITIAL_PASS)
    message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
  endif()
  set(__directories_observed "")

303
  # collect modules
304 305
  set(OPENCV_INITIAL_PASS ON)
  foreach(__path ${ARGN})
306
    get_filename_component(__path "${__path}" ABSOLUTE)
307

308 309 310 311 312 313
    list(FIND __directories_observed "${__path}" __pathIdx)
    if(__pathIdx GREATER -1)
      message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
    endif()
    list(APPEND __directories_observed "${__path}")

314 315 316 317
    file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
    if(__ocvmodules)
      list(SORT __ocvmodules)
      foreach(mod ${__ocvmodules})
318
        get_filename_component(__modpath "${__path}/${mod}" ABSOLUTE)
319
        if(EXISTS "${__modpath}/CMakeLists.txt")
320

321 322 323 324 325 326
          list(FIND __directories_observed "${__modpath}" __pathIdx)
          if(__pathIdx GREATER -1)
            message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
          endif()
          list(APPEND __directories_observed "${__modpath}")

327 328 329 330 331 332 333 334 335 336
          if(OCV_MODULE_RELOCATE_ON_INITIAL_PASS)
            file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
            file(COPY "${__modpath}/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
            add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
            if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
              set(OPENCV_MODULE_opencv_${mod}_LOCATION "${__modpath}" CACHE PATH "" FORCE)
            endif()
          else()
            add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
          endif()
337
        endif()
338
      endforeach()
339
    endif()
340
  endforeach()
341
  ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
342

343
  # resolve dependencies
344
  __ocv_flatten_module_dependencies()
345

346
  # create modules
347
  set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
348
  set(OPENCV_INITIAL_PASS OFF)
349
  foreach(m ${OPENCV_MODULES_BUILD})
350 351 352 353
    if(m MATCHES "^opencv_")
      string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
      add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
    endif()
354 355
  endforeach()
  unset(__shortname)
356 357
endmacro()

358 359 360 361 362
# 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")
363
        ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
364 365
      endif()
    elseif(EXISTS "${d}")
366
      ocv_include_directories("${d}")
367 368 369
    endif()
  endforeach()
endmacro()
370

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
# setup include paths for the list of passed modules and recursively add dependent modules
macro(ocv_include_modules_recurse)
  foreach(d ${ARGN})
    if(d MATCHES "^opencv_" AND HAVE_${d})
      if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
        ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
      endif()
      if(OPENCV_MODULE_${d}_DEPS)
        ocv_include_modules_recurse(${OPENCV_MODULE_${d}_DEPS})
      endif()
    elseif(EXISTS "${d}")
      ocv_include_directories("${d}")
    endif()
  endforeach()
endmacro()

387 388 389
# setup include path for OpenCV headers for specified module
# ocv_module_include_directories(<extra include directories/extra include modules>)
macro(ocv_module_include_directories)
390 391
  ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/include"
                          "${OPENCV_MODULE_${the_module}_LOCATION}/src"
392
                          "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
393
                          )
394 395
  ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
endmacro()
396 397


398 399 400 401 402 403 404
# 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)
  set(OPENCV_MODULE_${the_module}_HEADERS "")
  set(OPENCV_MODULE_${the_module}_SOURCES "")
405

406 407 408 409 410 411 412
  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()
413

414 415 416 417
  # the hacky way to embeed any files into the OpenCV without modification of its build system
  if(COMMAND ocv_get_module_external_sources)
    ocv_get_module_external_sources()
  endif()
418

419
  # use full paths for module to be independent from the module location
420
  ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
421

422 423 424
  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()
425

426 427 428 429
# finds and sets headers and sources for the standard OpenCV module
# Usage:
# ocv_glob_module_sources(<extra sources&headers in the same format as used in ocv_set_module_sources>)
macro(ocv_glob_module_sources)
430 431
  file(GLOB lib_srcs     "src/*.cpp")
  file(GLOB lib_int_hdrs "src/*.hpp" "src/*.h")
432
  file(GLOB lib_hdrs     "include/opencv2/*.hpp" "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h")
433
  file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h")
434

435 436 437
  file(GLOB lib_cuda_srcs "src/cuda/*.cu")
  set(cuda_objs "")
  set(lib_cuda_hdrs "")
438

439
  if(HAVE_CUDA AND lib_cuda_srcs)
440
    ocv_include_directories(${CUDA_INCLUDE_DIRS})
441
    file(GLOB lib_cuda_hdrs "src/cuda/*.hpp")
442

443 444
    ocv_cuda_compile(cuda_objs ${lib_cuda_srcs} ${lib_cuda_hdrs})
    source_group("Src\\Cuda"      FILES ${lib_cuda_srcs} ${lib_cuda_hdrs})
445 446
  endif()

Andrey Kamaev's avatar
Andrey Kamaev committed
447
  file(GLOB cl_kernels "src/opencl/*.cl")
448

449 450 451 452 453 454 455 456 457 458
  if(HAVE_OPENCL AND cl_kernels)
    ocv_include_directories(${OPENCL_INCLUDE_DIRS})
    add_custom_command(
      OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp"
      COMMAND ${CMAKE_COMMAND} -DCL_DIR="${CMAKE_CURRENT_SOURCE_DIR}/src/opencl" -DOUTPUT="${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp" -P "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake"
      DEPENDS ${cl_kernels} "${OpenCV_SOURCE_DIR}/cmake/cl2cpp.cmake")
    source_group("Src\\OpenCL" FILES ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp")
    list(APPEND lib_srcs ${cl_kernels} "${CMAKE_CURRENT_BINARY_DIR}/kernels.cpp")
  endif()

459
  ocv_set_module_sources(${ARGN} HEADERS ${lib_hdrs} ${lib_hdrs_detail}
460
                                 SOURCES ${lib_srcs} ${lib_int_hdrs} ${cuda_objs} ${lib_cuda_srcs} ${lib_cuda_hdrs})
461

462 463 464
  source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs})
  source_group("Include" FILES ${lib_hdrs})
  source_group("Include\\detail" FILES ${lib_hdrs_detail})
465 466
endmacro()

467 468 469
# creates OpenCV module in current folder
# creates new target, configures standard dependencies, compilers flags, install rules
# Usage:
470 471
#   ocv_create_module(<extra link dependencies>)
#   ocv_create_module(SKIP_LINK)
472
macro(ocv_create_module)
473 474 475 476 477 478 479
  # 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.
  if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/src/precomp.hpp")
    get_native_precompiled_header(${the_module} precomp.hpp)
  endif()

480
  add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES}
481 482
    "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/cvconfig.h" "${OPENCV_CONFIG_FILE_INCLUDE_DIR}/opencv2/opencv_modules.hpp"
    ${${the_module}_pch})
483 484 485
  if(NOT the_module STREQUAL opencv_ts)
    set_target_properties(${the_module} PROPERTIES COMPILE_DEFINITIONS OPENCV_NOSTL)
  endif()
486

487
  if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
488 489 490 491
    target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
    if (HAVE_CUDA)
      target_link_libraries(${the_module} ${CUDA_LIBRARIES} ${CUDA_npp_LIBRARY})
    endif()
492 493 494
    if(HAVE_OPENCL AND OPENCL_LIBRARIES)
      target_link_libraries(${the_module} ${OPENCL_LIBRARIES})
    endif()
495
  endif()
496

497
  add_dependencies(opencv_modules ${the_module})
498

499 500 501
  if(ENABLE_SOLUTION_FOLDERS)
    set_target_properties(${the_module} PROPERTIES FOLDER "modules")
  endif()
502

503 504 505 506
  set_target_properties(${the_module} PROPERTIES
    OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
    DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
    ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
507
    LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
508 509 510
    RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
    INSTALL_NAME_DIR lib
  )
511

512 513 514 515 516
  # For dynamic link numbering convenions
  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
517
      VERSION ${OPENCV_LIBVERSION}
518 519 520 521
      SOVERSION ${OPENCV_SOVERSION}
    )
  endif()

522 523
  if((NOT DEFINED OPENCV_MODULE_TYPE AND BUILD_SHARED_LIBS)
      OR (DEFINED OPENCV_MODULE_TYPE AND OPENCV_MODULE_TYPE STREQUAL SHARED))
524 525 526 527
    if(MSVC)
      set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
    else()
      add_definitions(-DCVAPI_EXPORTS)
Andrey Kamaev's avatar
Andrey Kamaev committed
528
    endif()
529
  endif()
Andrey Kamaev's avatar
Andrey Kamaev committed
530

531 532 533
  if(MSVC)
    if(CMAKE_CROSSCOMPILING)
      set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
534
    endif()
535
    set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
536
  endif()
537

538 539 540 541 542
  install(TARGETS ${the_module}
    RUNTIME DESTINATION bin COMPONENT main
    LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
    ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
    )
543

544
  # only "public" headers need to be installed
545
  if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
546
    foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
547
      string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
548
      if(NOT hdr2 MATCHES "opencv2/${the_module}/private.*" AND hdr2 MATCHES "^(opencv2/?.*)/[^/]+.h(..)?$" )
549
        install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT main)
550 551
      endif()
    endforeach()
552 553 554 555 556 557 558 559
  endif()
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})
macro(ocv_add_precompiled_headers the_target)
560 561 562 563 564 565 566 567 568
  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)
569
endmacro()
570

571 572 573 574 575 576 577
# short command for adding simple OpenCV module
# see ocv_add_module for argument details
# Usage:
# ocv_define_module(module_name  [INTERNAL] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
macro(ocv_define_module module_name)
  ocv_add_module(${module_name} ${ARGN})
  ocv_module_include_directories()
578
  ocv_glob_module_sources()
579 580 581 582 583 584
  ocv_create_module()
  ocv_add_precompiled_headers(${the_module})

  ocv_add_accuracy_tests()
  ocv_add_perf_tests()
endmacro()
585

586 587 588 589 590 591 592 593
# 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()
594
    endif()
595
  endforeach()
596 597
endmacro()

598
# auxiliary macro to parse arguments of ocv_add_accuracy_tests and ocv_add_perf_tests commands
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
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")
      set(__currentvar "OPENCV_TEST_${the_module}_DEPS")
    elseif("${__currentvar}" STREQUAL "__file_group_sources" AND NOT __file_group_name)
      set(__file_group_name "${arg}")
    else()
      list(APPEND ${__currentvar} "${arg}")
    endif()
  endforeach()
  unset(__file_group_name)
  unset(__file_group_sources)
  unset(__currentvar)
endmacro()
625

626 627
# this is a command for adding OpenCV performance tests to the module
# ocv_add_perf_tests(<extra_dependencies>)
628
function(ocv_add_perf_tests)
629 630 631
  set(perf_path "${CMAKE_CURRENT_SOURCE_DIR}/perf")
  if(BUILD_PERF_TESTS AND EXISTS "${perf_path}")
    __ocv_parse_test_sources(PERF ${ARGN})
632

633
    # opencv_highgui is required for imread/imwrite
634
    set(perf_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_PERF_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
635
    ocv_check_dependencies(${perf_deps})
636

637 638
    if(OCV_DEPENDENCIES_FOUND)
      set(the_target "opencv_perf_${name}")
639
      # project(${the_target})
640

641
      ocv_module_include_directories(${perf_deps} "${perf_path}")
642

643 644 645 646 647 648 649
      if(NOT OPENCV_PERF_${the_module}_SOURCES)
        file(GLOB perf_srcs "${perf_path}/*.cpp")
        file(GLOB perf_hdrs "${perf_path}/*.hpp" "${perf_path}/*.h")
        source_group("Src" FILES ${perf_srcs})
        source_group("Include" FILES ${perf_hdrs})
        set(OPENCV_PERF_${the_module}_SOURCES ${perf_srcs} ${perf_hdrs})
      endif()
650

651
      get_native_precompiled_header(${the_target} perf_precomp.hpp)
652 653

      add_executable(${the_target} ${OPENCV_PERF_${the_module}_SOURCES} ${${the_target}_pch})
654
      target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${perf_deps} ${OPENCV_LINKER_LIBS})
655
      add_dependencies(opencv_perf_tests ${the_target})
656

657 658 659 660 661 662 663 664 665 666 667 668 669
      # 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()

      ocv_add_precompiled_headers(${the_target})

    else(OCV_DEPENDENCIES_FOUND)
670
      # TODO: warn about unsatisfied dependencies
671 672
    endif(OCV_DEPENDENCIES_FOUND)
  endif()
673
endfunction()
674

675 676
# this is a command for adding OpenCV accuracy/regression tests to the module
# ocv_add_accuracy_tests([FILES <source group name> <list of sources>] [DEPENDS_ON] <list of extra dependencies>)
677
function(ocv_add_accuracy_tests)
678 679 680 681 682 683
  set(test_path "${CMAKE_CURRENT_SOURCE_DIR}/test")
  ocv_check_dependencies(${test_deps})
  if(BUILD_TESTS AND EXISTS "${test_path}")
    __ocv_parse_test_sources(TEST ${ARGN})

    # opencv_highgui is required for imread/imwrite
684
    set(test_deps ${the_module} opencv_ts opencv_highgui ${OPENCV_TEST_${the_module}_DEPS} ${OPENCV_MODULE_opencv_ts_DEPS})
685 686 687 688
    ocv_check_dependencies(${test_deps})

    if(OCV_DEPENDENCIES_FOUND)
      set(the_target "opencv_test_${name}")
689
      # project(${the_target})
690

691
      ocv_module_include_directories(${test_deps} "${test_path}")
692

693 694 695 696 697 698 699
      if(NOT OPENCV_TEST_${the_module}_SOURCES)
        file(GLOB test_srcs "${test_path}/*.cpp")
        file(GLOB test_hdrs "${test_path}/*.hpp" "${test_path}/*.h")
        source_group("Src" FILES ${test_srcs})
        source_group("Include" FILES ${test_hdrs})
        set(OPENCV_TEST_${the_module}_SOURCES ${test_srcs} ${test_hdrs})
      endif()
700

701 702 703
      get_native_precompiled_header(${the_target} test_precomp.hpp)

      add_executable(${the_target} ${OPENCV_TEST_${the_module}_SOURCES} ${${the_target}_pch})
704
      target_link_libraries(${the_target} ${OPENCV_MODULE_${the_module}_DEPS} ${test_deps} ${OPENCV_LINKER_LIBS})
705
      add_dependencies(opencv_tests ${the_target})
706

707 708 709 710 711 712 713 714 715
      # 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 accuracy")
      endif()
716

717 718 719 720 721 722
      enable_testing()
      get_target_property(LOC ${the_target} LOCATION)
      add_test(${the_target} "${LOC}")

      ocv_add_precompiled_headers(${the_target})
    else(OCV_DEPENDENCIES_FOUND)
723
      # TODO: warn about unsatisfied dependencies
724 725
    endif(OCV_DEPENDENCIES_FOUND)
  endif()
726
endfunction()
727

728
# internal macro; finds all link dependencies of the module
729 730 731 732 733 734 735 736 737 738 739
# should be used at the end of CMake processing
macro(__ocv_track_module_link_dependencies the_module optkind)
  set(${the_module}_MODULE_DEPS_${optkind}   "")
  set(${the_module}_EXTRA_DEPS_${optkind}    "")

  get_target_property(__module_type ${the_module} TYPE)
  if(__module_type STREQUAL "STATIC_LIBRARY")
    #in case of static library we have to inherit its dependencies (in right order!!!)
    if(NOT DEFINED ${the_module}_LIB_DEPENDS_${optkind})
      ocv_split_libs_list(${the_module}_LIB_DEPENDS ${the_module}_LIB_DEPENDS_DBG ${the_module}_LIB_DEPENDS_OPT)
    endif()
740

741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
    set(__resolved_deps "")
    set(__mod_depends ${${the_module}_LIB_DEPENDS_${optkind}})
    set(__has_cycle FALSE)

    while(__mod_depends)
      list(GET __mod_depends 0 __dep)
      list(REMOVE_AT __mod_depends 0)
      if(__dep STREQUAL the_module)
        set(__has_cycle TRUE)
      else()#if("${OPENCV_MODULES_BUILD}" MATCHES "(^|;)${__dep}(;|$)")
        ocv_regex_escape(__rdep "${__dep}")
        if(__resolved_deps MATCHES "(^|;)${__rdep}(;|$)")
          #all dependencies of this module are already resolved
          list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${__dep}")
        else()
          get_target_property(__module_type ${__dep} TYPE)
          if(__module_type STREQUAL "STATIC_LIBRARY")
            if(NOT DEFINED ${__dep}_LIB_DEPENDS_${optkind})
              ocv_split_libs_list(${__dep}_LIB_DEPENDS ${__dep}_LIB_DEPENDS_DBG ${__dep}_LIB_DEPENDS_OPT)
            endif()
            list(INSERT __mod_depends 0 ${${__dep}_LIB_DEPENDS_${optkind}} ${__dep})
            list(APPEND __resolved_deps "${__dep}")
          elseif(NOT __module_type)
            list(APPEND  ${the_module}_EXTRA_DEPS_${optkind} "${__dep}")
          endif()
766
        endif()
767 768 769 770
      #else()
       # get_target_property(__dep_location "${__dep}" LOCATION)
      endif()
    endwhile()
771

772 773 774 775 776 777
    ocv_list_unique(${the_module}_MODULE_DEPS_${optkind})
    #ocv_list_reverse(${the_module}_MODULE_DEPS_${optkind})
    ocv_list_unique(${the_module}_EXTRA_DEPS_${optkind})
    #ocv_list_reverse(${the_module}_EXTRA_DEPS_${optkind})

    if(__has_cycle)
778
      # not sure if it can work
779
      list(APPEND ${the_module}_MODULE_DEPS_${optkind} "${the_module}")
780 781
    endif()

782 783 784 785 786 787 788 789
    unset(__dep_location)
    unset(__mod_depends)
    unset(__resolved_deps)
    unset(__has_cycle)
    unset(__rdep)
  endif()#STATIC_LIBRARY
  unset(__module_type)

790 791 792 793 794 795 796
  #message("${the_module}_MODULE_DEPS_${optkind}")
  #message("       ${${the_module}_MODULE_DEPS_${optkind}}")
  #message("       ${OPENCV_MODULE_${the_module}_DEPS}")
  #message("")
  #message("${the_module}_EXTRA_DEPS_${optkind}")
  #message("       ${${the_module}_EXTRA_DEPS_${optkind}}")
  #message("")
797 798
endmacro()

799 800 801 802 803 804 805
# creates lists of build dependencies needed for external projects
macro(ocv_track_build_dependencies)
  foreach(m ${OPENCV_MODULES_BUILD})
    __ocv_track_module_link_dependencies("${m}" OPT)
    __ocv_track_module_link_dependencies("${m}" DBG)
  endforeach()
endmacro()