NiceModules.cmake 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. # clean flags for modules enabled on previous cmake run
  2. # this is necessary to correctly handle modules removal
  3. foreach(mod ${OPENCV_MODULES_BUILD} ${OPENCV_MODULES_DISABLED_USER} ${OPENCV_MODULES_DISABLED_AUTO} ${OPENCV_MODULES_DISABLED_FORCE})
  4. if(HAVE_${mod})
  5. unset(HAVE_${mod} CACHE)
  6. endif()
  7. unset(OPENCV_MODULE_${mod}_REQ_DEPS CACHE)
  8. unset(OPENCV_MODULE_${mod}_OPT_DEPS CACHE)
  9. endforeach()
  10. # clean modules info which needs to be recalculated
  11. set(OPENCV_MODULES_PUBLIC "" CACHE INTERNAL "List of OpenCV modules marked for export")
  12. set(OPENCV_MODULES_BUILD "" CACHE INTERNAL "List of OpenCV modules included into the build")
  13. set(OPENCV_MODULES_DISABLED_USER "" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
  14. set(OPENCV_MODULES_DISABLED_AUTO "" CACHE INTERNAL "List of OpenCV modules implicitly disabled due to dependencies")
  15. set(OPENCV_MODULES_DISABLED_FORCE "" CACHE INTERNAL "List of OpenCV modules which can not be build in current configuration")
  16. # collect modules from specified directories
  17. # NB: must be called only once!
  18. macro(ocv_glob_modules)
  19. if(DEFINED OPENCV_INITIAL_PASS)
  20. message(FATAL_ERROR "OpenCV has already loaded its modules. Calling ocv_glob_modules second time is not allowed.")
  21. endif()
  22. set(__directories_observed "")
  23. # collect modules
  24. set(OPENCV_INITIAL_PASS ON)
  25. foreach(__path ${ARGN})
  26. ocv_get_real_path(__path "${__path}")
  27. list(FIND __directories_observed "${__path}" __pathIdx)
  28. if(__pathIdx GREATER -1)
  29. message(FATAL_ERROR "The directory ${__path} is observed for OpenCV modules second time.")
  30. endif()
  31. list(APPEND __directories_observed "${__path}")
  32. file(GLOB __ocvmodules RELATIVE "${__path}" "${__path}/*")
  33. if(__ocvmodules)
  34. list(SORT __ocvmodules)
  35. foreach(mod ${__ocvmodules})
  36. ocv_get_real_path(__modpath "${__path}/${mod}")
  37. message(STATUS "${__modpath}")
  38. if(EXISTS "${__modpath}/CMakeLists.txt")
  39. message(STATUS "${__modpath} adding subdir")
  40. list(FIND __directories_observed "${__modpath}" __pathIdx)
  41. if(__pathIdx GREATER -1)
  42. message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
  43. endif()
  44. list(APPEND __directories_observed "${__modpath}")
  45. if(OCV_MODULE_RELOCATE_ON_INITIAL_PASS)
  46. #file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  47. file(COPY "${__modpath}/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  48. add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  49. if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  50. set(OPENCV_MODULE_opencv_${mod}_LOCATION "${__modpath}" CACHE PATH "" FORCE)
  51. endif()
  52. else()
  53. add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  54. endif()
  55. endif()
  56. endforeach()
  57. endif()
  58. endforeach()
  59. ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
  60. # resolve dependencies
  61. # __ocv_flatten_module_dependencies()
  62. # create modules
  63. set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
  64. set(OPENCV_INITIAL_PASS OFF)
  65. foreach(m ${OPENCV_MODULES_BUILD})
  66. message(STATUS "${OPENCV_MODULES_BUILD}")
  67. if(m MATCHES "^opencv_")
  68. string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
  69. add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
  70. endif()
  71. endforeach()
  72. unset(__shortname)
  73. endmacro()
  74. # adds dependencies to OpenCV module
  75. # Usage:
  76. # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
  77. # Notes:
  78. # * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
  79. macro(ocv_add_dependencies full_modname)
  80. #we don't clean the dependencies here to allow this macro several times for every module
  81. foreach(d "REQUIRED" ${ARGN})
  82. if(d STREQUAL "REQUIRED")
  83. set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
  84. elseif(d STREQUAL "OPTIONAL")
  85. set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
  86. else()
  87. list(APPEND ${__depsvar} "${d}")
  88. endif()
  89. endforeach()
  90. unset(__depsvar)
  91. ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
  92. ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
  93. set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS} CACHE INTERNAL "Required dependencies of ${full_modname} module")
  94. set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS} CACHE INTERNAL "Optional dependencies of ${full_modname} module")
  95. endmacro()
  96. # declare new OpenCV module in current folder
  97. # Usage:
  98. # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
  99. # Example:
  100. # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_gpu)
  101. macro(ocv_add_module _name)
  102. string(TOLOWER "${_name}" name)
  103. string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
  104. set(the_module opencv_${name})
  105. # the first pass - collect modules info, the second pass - create targets
  106. if(OPENCV_INITIAL_PASS)
  107. #guard agains redefinition
  108. if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
  109. message(FATAL_ERROR "Redefinition of the ${the_module} module.
  110. at: ${CMAKE_CURRENT_SOURCE_DIR}
  111. previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
  112. ")
  113. endif()
  114. if(NOT DEFINED the_description)
  115. set(the_description "The ${name} OpenCV module")
  116. endif()
  117. if(NOT DEFINED BUILD_${the_module}_INIT)
  118. set(BUILD_${the_module}_INIT ON)
  119. endif()
  120. # create option to enable/disable this module
  121. # option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
  122. # remember the module details
  123. set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
  124. set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
  125. # parse list of dependencies
  126. if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
  127. set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The cathegory of the module")
  128. set(__ocv_argn__ ${ARGN})
  129. list(REMOVE_AT __ocv_argn__ 0)
  130. ocv_add_dependencies(${the_module} ${__ocv_argn__})
  131. unset(__ocv_argn__)
  132. else()
  133. set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The cathegory of the module")
  134. ocv_add_dependencies(${the_module} ${ARGN})
  135. if(BUILD_${the_module})
  136. set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
  137. endif()
  138. endif()
  139. # add self to the world dependencies
  140. 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)
  141. ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
  142. endif()
  143. if(BUILD_${the_module})
  144. set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
  145. else()
  146. set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
  147. endif()
  148. # TODO: add submodules if any
  149. # stop processing of current file
  150. # return()
  151. else(OPENCV_INITIAL_PASS)
  152. if(NOT BUILD_${the_module})
  153. return() # extra protection from redefinition
  154. endif()
  155. project(${the_module})
  156. endif(OPENCV_INITIAL_PASS)
  157. endmacro()
  158. # setup include paths for the list of passed modules
  159. macro(ocv_include_modules)
  160. foreach(d ${ARGN})
  161. if(d MATCHES "^opencv_" AND HAVE_${d})
  162. if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  163. ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
  164. endif()
  165. elseif(EXISTS "${d}")
  166. ocv_include_directories("${d}")
  167. endif()
  168. endforeach()
  169. endmacro()
  170. # setup include path for OpenCV headers for specified module
  171. # ocv_module_include_directories(<extra include directories/extra include modules>)
  172. macro(ocv_module_include_directories)
  173. ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/include"
  174. "${OPENCV_MODULE_${the_module}_LOCATION}/src"
  175. "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
  176. )
  177. ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
  178. endmacro()