NiceModules.cmake 14 KB

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