NiceModules.cmake 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 "${mod}")
  38. message(STATUS "${__modpath}")
  39. if(EXISTS "${__modpath}/CMakeLists.txt")
  40. message(STATUS "${__modpath} adding subdir")
  41. list(FIND __directories_observed "${__modpath}" __pathIdx)
  42. if(__pathIdx GREATER -1)
  43. message(FATAL_ERROR "The module from ${__modpath} is already loaded.")
  44. endif()
  45. list(APPEND __directories_observed "${__modpath}")
  46. if(OCV_MODULE_RELOCATE_ON_INITIAL_PASS)
  47. #file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  48. file(COPY "${__modpath}/CMakeLists.txt" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  49. add_subdirectory("${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  50. if("${OPENCV_MODULE_opencv_${mod}_LOCATION}" STREQUAL "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  51. set(OPENCV_MODULE_opencv_${mod}_LOCATION "${__modpath}" CACHE PATH "" FORCE)
  52. endif()
  53. else()
  54. add_subdirectory("${__modpath}" "${CMAKE_CURRENT_BINARY_DIR}/${mod}/.${mod}")
  55. endif()
  56. endif()
  57. endforeach()
  58. endif()
  59. endforeach()
  60. ocv_clear_vars(__ocvmodules __directories_observed __path __modpath __pathIdx)
  61. # resolve dependencies
  62. # __ocv_flatten_module_dependencies()
  63. # create modules
  64. set(OPENCV_INITIAL_PASS OFF PARENT_SCOPE)
  65. set(OPENCV_INITIAL_PASS OFF)
  66. foreach(m ${OPENCV_MODULES_BUILD})
  67. message(STATUS "${OPENCV_MODULES_BUILD}")
  68. if(m MATCHES "^opencv_")
  69. string(REGEX REPLACE "^opencv_" "" __shortname "${m}")
  70. add_subdirectory("${OPENCV_MODULE_${m}_LOCATION}" "${CMAKE_CURRENT_BINARY_DIR}/${__shortname}")
  71. endif()
  72. endforeach()
  73. unset(__shortname)
  74. endmacro()
  75. # adds dependencies to OpenCV module
  76. # Usage:
  77. # add_dependencies(opencv_<name> [REQUIRED] [<list of dependencies>] [OPTIONAL <list of modules>])
  78. # Notes:
  79. # * <list of dependencies> - can include full names of modules or full pathes to shared/static libraries or cmake targets
  80. macro(ocv_add_dependencies full_modname)
  81. #we don't clean the dependencies here to allow this macro several times for every module
  82. foreach(d "REQUIRED" ${ARGN})
  83. if(d STREQUAL "REQUIRED")
  84. set(__depsvar OPENCV_MODULE_${full_modname}_REQ_DEPS)
  85. elseif(d STREQUAL "OPTIONAL")
  86. set(__depsvar OPENCV_MODULE_${full_modname}_OPT_DEPS)
  87. else()
  88. list(APPEND ${__depsvar} "${d}")
  89. endif()
  90. endforeach()
  91. unset(__depsvar)
  92. ocv_list_unique(OPENCV_MODULE_${full_modname}_REQ_DEPS)
  93. ocv_list_unique(OPENCV_MODULE_${full_modname}_OPT_DEPS)
  94. set(OPENCV_MODULE_${full_modname}_REQ_DEPS ${OPENCV_MODULE_${full_modname}_REQ_DEPS} CACHE INTERNAL "Required dependencies of ${full_modname} module")
  95. set(OPENCV_MODULE_${full_modname}_OPT_DEPS ${OPENCV_MODULE_${full_modname}_OPT_DEPS} CACHE INTERNAL "Optional dependencies of ${full_modname} module")
  96. endmacro()
  97. # declare new OpenCV module in current folder
  98. # Usage:
  99. # ocv_add_module(<name> [INTERNAL|BINDINGS] [REQUIRED] [<list of dependencies>] [OPTIONAL <list of optional dependencies>])
  100. # Example:
  101. # ocv_add_module(yaom INTERNAL opencv_core opencv_highgui opencv_flann OPTIONAL opencv_gpu)
  102. macro(ocv_add_module _name)
  103. string(TOLOWER "${_name}" name)
  104. string(REGEX REPLACE "^opencv_" "" ${name} "${name}")
  105. set(the_module opencv_${name})
  106. # the first pass - collect modules info, the second pass - create targets
  107. if(OPENCV_INITIAL_PASS)
  108. #guard agains redefinition
  109. if(";${OPENCV_MODULES_BUILD};${OPENCV_MODULES_DISABLED_USER};" MATCHES ";${the_module};")
  110. message(FATAL_ERROR "Redefinition of the ${the_module} module.
  111. at: ${CMAKE_CURRENT_SOURCE_DIR}
  112. previously defined at: ${OPENCV_MODULE_${the_module}_LOCATION}
  113. ")
  114. endif()
  115. if(NOT DEFINED the_description)
  116. set(the_description "The ${name} OpenCV module")
  117. endif()
  118. if(NOT DEFINED BUILD_${the_module}_INIT)
  119. set(BUILD_${the_module}_INIT ON)
  120. endif()
  121. # create option to enable/disable this module
  122. # option(BUILD_${the_module} "Include ${the_module} module into the OpenCV build" ${BUILD_${the_module}_INIT})
  123. # remember the module details
  124. set(OPENCV_MODULE_${the_module}_DESCRIPTION "${the_description}" CACHE INTERNAL "Brief description of ${the_module} module")
  125. set(OPENCV_MODULE_${the_module}_LOCATION "${CMAKE_CURRENT_SOURCE_DIR}" CACHE INTERNAL "Location of ${the_module} module sources")
  126. # parse list of dependencies
  127. if("${ARGV1}" STREQUAL "INTERNAL" OR "${ARGV1}" STREQUAL "BINDINGS")
  128. set(OPENCV_MODULE_${the_module}_CLASS "${ARGV1}" CACHE INTERNAL "The cathegory of the module")
  129. set(__ocv_argn__ ${ARGN})
  130. list(REMOVE_AT __ocv_argn__ 0)
  131. ocv_add_dependencies(${the_module} ${__ocv_argn__})
  132. unset(__ocv_argn__)
  133. else()
  134. set(OPENCV_MODULE_${the_module}_CLASS "PUBLIC" CACHE INTERNAL "The cathegory of the module")
  135. ocv_add_dependencies(${the_module} ${ARGN})
  136. if(BUILD_${the_module})
  137. set(OPENCV_MODULES_PUBLIC ${OPENCV_MODULES_PUBLIC} "${the_module}" CACHE INTERNAL "List of OpenCV modules marked for export")
  138. endif()
  139. endif()
  140. # add self to the world dependencies
  141. 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)
  142. ocv_add_dependencies(opencv_world OPTIONAL ${the_module})
  143. endif()
  144. if(BUILD_${the_module})
  145. set(OPENCV_MODULES_BUILD ${OPENCV_MODULES_BUILD} "${the_module}" CACHE INTERNAL "List of OpenCV modules included into the build")
  146. else()
  147. set(OPENCV_MODULES_DISABLED_USER ${OPENCV_MODULES_DISABLED_USER} "${the_module}" CACHE INTERNAL "List of OpenCV modules explicitly disabled by user")
  148. endif()
  149. # TODO: add submodules if any
  150. # stop processing of current file
  151. # return()
  152. else(OPENCV_INITIAL_PASS)
  153. if(NOT BUILD_${the_module})
  154. return() # extra protection from redefinition
  155. endif()
  156. project(${the_module})
  157. endif(OPENCV_INITIAL_PASS)
  158. endmacro()
  159. # setup include paths for the list of passed modules
  160. macro(ocv_include_modules)
  161. foreach(d ${ARGN})
  162. if(d MATCHES "^opencv_" AND HAVE_${d})
  163. # message(STATUS "${OPENCV_MODULE_${d}_LOCATION}")
  164. if (EXISTS "${OPENCV_MODULE_${d}_LOCATION}/include")
  165. ocv_include_directories("${OPENCV_MODULE_${d}_LOCATION}/include")
  166. endif()
  167. elseif(EXISTS "${d}")
  168. ocv_include_directories("${d}")
  169. endif()
  170. endforeach()
  171. endmacro()
  172. # setup include path for OpenCV headers for specified module
  173. # ocv_module_include_directories(<extra include directories/extra include modules>)
  174. macro(ocv_module_include_directories)
  175. # ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/include"
  176. # "${OPENCV_MODULE_${the_module}_LOCATION}/src"
  177. # "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
  178. # )
  179. ocv_include_directories("${OPENCV_MODULE_${the_module}_LOCATION}/"
  180. "${CMAKE_CURRENT_BINARY_DIR}" # for precompiled headers
  181. )
  182. ocv_include_modules(${OPENCV_MODULE_${the_module}_DEPS} ${ARGN})
  183. endmacro()
  184. # sets header and source files for the current module
  185. # NB: all files specified as headers will be installed
  186. # Usage:
  187. # ocv_set_module_sources([HEADERS] <list of files> [SOURCES] <list of files>)
  188. macro(ocv_set_module_sources)
  189. set(OPENCV_MODULE_${the_module}_HEADERS "")
  190. set(OPENCV_MODULE_${the_module}_SOURCES "")
  191. foreach(f "HEADERS" ${ARGN})
  192. if(f STREQUAL "HEADERS" OR f STREQUAL "SOURCES")
  193. set(__filesvar "OPENCV_MODULE_${the_module}_${f}")
  194. else()
  195. list(APPEND ${__filesvar} "${f}")
  196. endif()
  197. endforeach()
  198. # the hacky way to embeed any files into the OpenCV without modification of its build system
  199. if(COMMAND ocv_get_module_external_sources)
  200. ocv_get_module_external_sources()
  201. endif()
  202. # use full paths for module to be independent from the module location
  203. ocv_convert_to_full_paths(OPENCV_MODULE_${the_module}_HEADERS)
  204. set(OPENCV_MODULE_${the_module}_HEADERS ${OPENCV_MODULE_${the_module}_HEADERS} CACHE INTERNAL "List of header files for ${the_module}")
  205. set(OPENCV_MODULE_${the_module}_SOURCES ${OPENCV_MODULE_${the_module}_SOURCES} CACHE INTERNAL "List of source files for ${the_module}")
  206. endmacro()
  207. # finds and sets headers and sources for the standard OpenCV module
  208. # Usage:
  209. # ocv_glob_module_sources(<extra sources&headers in the same format as used in ocv_set_module_sources>)
  210. macro(ocv_glob_module_sources)
  211. # file(GLOB_RECURSE lib_srcs "src/*.cpp")
  212. # file(GLOB_RECURSE lib_int_hdrs "src/*.hpp" "src/*.h")
  213. # file(GLOB lib_hdrs "include/opencv2/${name}/*.hpp" "include/opencv2/${name}/*.h")
  214. # file(GLOB lib_hdrs_detail "include/opencv2/${name}/detail/*.hpp" "include/opencv2/${name}/detail/*.h")
  215. # source_group("Src" FILES ${lib_srcs} ${lib_int_hdrs})
  216. # source_group("Include" FILES ${lib_hdrs})
  217. # source_group("Include\\detail" FILES ${lib_hdrs_detail})
  218. # ocv_set_module_sources(${ARGN} HEADERS ${lib_hdrs} ${lib_hdrs_detail} SOURCES ${lib_srcs} ${lib_int_hdrs})
  219. file(GLOB_RECURSE lib_srcs "*.cpp" "*.tcc")
  220. #file(GLOB_RECURSE lib_int_hdrs "./*.hpp" "./*.h")
  221. #file(GLOB_RECURSE lib_int_hdrs "./*.hpp" "./*.h")
  222. file(GLOB lib_hdrs "*.hpp" "*.h")
  223. message(status "lib_srcs: ${lib_srcs}")
  224. message(status "lib_hdrs: ${lib_hdrs}")
  225. source_group("Src" FILES ${lib_srcs})
  226. source_group("Include" FILES ${lib_hdrs})
  227. ocv_set_module_sources(${ARGN} HEADERS ${lib_hdrs} SOURCES ${lib_srcs})
  228. endmacro()
  229. # creates OpenCV module in current folder
  230. # creates new target, configures standard dependencies, compilers flags, install rules
  231. # Usage:
  232. # ocv_create_module(<extra link dependencies>)
  233. # ocv_create_module(SKIP_LINK)
  234. macro(ocv_create_module)
  235. #add_library(${the_module} ${OPENCV_MODULE_TYPE} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES})
  236. add_library(${the_module} ${NICE_BUILD_LIBS_STATIC_SHARED} ${OPENCV_MODULE_${the_module}_HEADERS} ${OPENCV_MODULE_${the_module}_SOURCES})
  237. if(NOT "${ARGN}" STREQUAL "SKIP_LINK")
  238. target_link_libraries(${the_module} ${OPENCV_MODULE_${the_module}_DEPS} ${OPENCV_MODULE_${the_module}_DEPS_EXT} ${OPENCV_LINKER_LIBS} ${IPP_LIBS} ${ARGN})
  239. endif()
  240. #jojo add_dependencies(opencv_modules ${the_module})
  241. if(ENABLE_SOLUTION_FOLDERS)
  242. set_target_properties(${the_module} PROPERTIES FOLDER "modules")
  243. endif()
  244. set_target_properties(${the_module} PROPERTIES
  245. OUTPUT_NAME "${the_module}${OPENCV_DLLVERSION}"
  246. DEBUG_POSTFIX "${OPENCV_DEBUG_POSTFIX}"
  247. ARCHIVE_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT_PATH}
  248. RUNTIME_OUTPUT_DIRECTORY ${EXECUTABLE_OUTPUT_PATH}
  249. INSTALL_NAME_DIR lib
  250. )
  251. # For dynamic link numbering convenions
  252. if(NOT ANDROID)
  253. # Android SDK build scripts can include only .so files into final .apk
  254. # As result we should not set version properties for Android
  255. set_target_properties(${the_module} PROPERTIES
  256. VERSION ${OPENCV_VERSION}
  257. SOVERSION ${OPENCV_SOVERSION}
  258. )
  259. endif()
  260. if(BUILD_SHARED_LIBS)
  261. if(MSVC)
  262. set_target_properties(${the_module} PROPERTIES DEFINE_SYMBOL CVAPI_EXPORTS)
  263. else()
  264. add_definitions(-DCVAPI_EXPORTS)
  265. endif()
  266. endif()
  267. if(MSVC)
  268. if(CMAKE_CROSSCOMPILING)
  269. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:secchk")
  270. endif()
  271. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:libc /DEBUG")
  272. endif()
  273. install(TARGETS ${the_module}
  274. RUNTIME DESTINATION bin COMPONENT main
  275. LIBRARY DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
  276. ARCHIVE DESTINATION ${OPENCV_LIB_INSTALL_PATH} COMPONENT main
  277. )
  278. # only "public" headers need to be installed
  279. if(OPENCV_MODULE_${the_module}_HEADERS AND ";${OPENCV_MODULES_PUBLIC};" MATCHES ";${the_module};")
  280. foreach(hdr ${OPENCV_MODULE_${the_module}_HEADERS})
  281. string(REGEX REPLACE "^.*opencv2/" "opencv2/" hdr2 "${hdr}")
  282. if(hdr2 MATCHES "^(opencv2/.*)/[^/]+.h(..)?$")
  283. install(FILES ${hdr} DESTINATION "${OPENCV_INCLUDE_INSTALL_PATH}/${CMAKE_MATCH_1}" COMPONENT main)
  284. endif()
  285. endforeach()
  286. endif()
  287. endmacro()