common.cmake 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. # This file is included from a subdirectory
  2. set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}")
  3. ocv_add_module(${MODULE_NAME} BINDINGS PRIVATE_REQUIRED opencv_python_bindings_generator)
  4. include_directories(SYSTEM
  5. "${${PYTHON}_INCLUDE_PATH}"
  6. ${${PYTHON}_NUMPY_INCLUDE_DIRS}
  7. )
  8. ocv_module_include_directories(
  9. "${PYTHON_SOURCE_DIR}/src2"
  10. "${OPENCV_PYTHON_BINDINGS_DIR}"
  11. )
  12. # try to use dynamic symbols linking with libpython.so
  13. set(OPENCV_FORCE_PYTHON_LIBS OFF CACHE BOOL "")
  14. string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}")
  15. if(NOT WIN32 AND NOT APPLE AND NOT OPENCV_PYTHON_SKIP_LINKER_EXCLUDE_LIBS)
  16. set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--exclude-libs=ALL")
  17. endif()
  18. ocv_add_library(${the_module} MODULE
  19. ${PYTHON_SOURCE_DIR}/src2/cv2.cpp
  20. ${PYTHON_SOURCE_DIR}/src2/cv2_util.cpp
  21. ${PYTHON_SOURCE_DIR}/src2/cv2_numpy.cpp
  22. ${PYTHON_SOURCE_DIR}/src2/cv2_convert.cpp
  23. ${PYTHON_SOURCE_DIR}/src2/cv2_highgui.cpp
  24. ${cv2_generated_hdrs}
  25. ${opencv_userdef_hdrs}
  26. ${cv2_custom_hdr}
  27. )
  28. if(TARGET gen_opencv_python_source)
  29. add_dependencies(${the_module} gen_opencv_python_source)
  30. endif()
  31. ocv_assert(${PYTHON}_VERSION_MAJOR)
  32. ocv_assert(${PYTHON}_VERSION_MINOR)
  33. if(${PYTHON}_LIMITED_API)
  34. # support only python3.3+
  35. ocv_assert(${PYTHON}_VERSION_MAJOR EQUAL 3 AND ${PYTHON}_VERSION_MINOR GREATER 2)
  36. target_compile_definitions(${the_module} PRIVATE CVPY_DYNAMIC_INIT)
  37. if(WIN32)
  38. string(REPLACE
  39. "python${${PYTHON}_VERSION_MAJOR}${${PYTHON}_VERSION_MINOR}.lib"
  40. "python${${PYTHON}_VERSION_MAJOR}.lib"
  41. ${PYTHON}_LIBRARIES
  42. "${${PYTHON}_LIBRARIES}")
  43. endif()
  44. endif()
  45. if(APPLE)
  46. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup")
  47. elseif(WIN32 OR OPENCV_FORCE_PYTHON_LIBS)
  48. if(${PYTHON}_DEBUG_LIBRARIES AND NOT ${PYTHON}_LIBRARIES MATCHES "optimized.*debug")
  49. ocv_target_link_libraries(${the_module} PRIVATE debug ${${PYTHON}_DEBUG_LIBRARIES} optimized ${${PYTHON}_LIBRARIES})
  50. else()
  51. ocv_target_link_libraries(${the_module} PRIVATE ${${PYTHON}_LIBRARIES})
  52. endif()
  53. endif()
  54. if(TARGET gen_opencv_python_source)
  55. set(deps ${OPENCV_MODULE_${the_module}_DEPS})
  56. list(REMOVE_ITEM deps opencv_python_bindings_generator) # don't add dummy module to target_link_libraries list
  57. endif()
  58. ocv_target_link_libraries(${the_module} PRIVATE ${deps})
  59. if(DEFINED ${PYTHON}_CVPY_SUFFIX)
  60. set(CVPY_SUFFIX "${${PYTHON}_CVPY_SUFFIX}")
  61. else()
  62. set(__python_ext_suffix_var "EXT_SUFFIX")
  63. if("${${PYTHON}_VERSION_MAJOR}" STREQUAL "2")
  64. set(__python_ext_suffix_var "SO")
  65. endif()
  66. execute_process(COMMAND ${${PYTHON}_EXECUTABLE} -c "import sysconfig; print(sysconfig.get_config_var('${__python_ext_suffix_var}'))"
  67. RESULT_VARIABLE PYTHON_CVPY_PROCESS
  68. OUTPUT_VARIABLE CVPY_SUFFIX
  69. OUTPUT_STRIP_TRAILING_WHITESPACE)
  70. if(NOT PYTHON_CVPY_PROCESS EQUAL 0)
  71. set(CVPY_SUFFIX ".so")
  72. endif()
  73. if(${PYTHON}_LIMITED_API)
  74. if(WIN32)
  75. string(REGEX REPLACE "\\.[^\\.]*\\." "." CVPY_SUFFIX "${CVPY_SUFFIX}")
  76. else()
  77. string(REGEX REPLACE "\\.[^\\.]*\\." ".abi${${PYTHON}_VERSION_MAJOR}." CVPY_SUFFIX "${CVPY_SUFFIX}")
  78. endif()
  79. endif()
  80. endif()
  81. ocv_update(OPENCV_PYTHON_EXTENSION_BUILD_PATH "${LIBRARY_OUTPUT_PATH}/${MODULE_INSTALL_SUBDIR}")
  82. set_target_properties(${the_module} PROPERTIES
  83. LIBRARY_OUTPUT_DIRECTORY "${OPENCV_PYTHON_EXTENSION_BUILD_PATH}"
  84. ARCHIVE_OUTPUT_NAME ${the_module} # prevent name conflict for python2/3 outputs
  85. PREFIX ""
  86. OUTPUT_NAME cv2
  87. SUFFIX "${CVPY_SUFFIX}")
  88. if(ENABLE_SOLUTION_FOLDERS)
  89. set_target_properties(${the_module} PROPERTIES FOLDER "bindings")
  90. endif()
  91. if(MSVC)
  92. add_definitions(-DCVAPI_EXPORTS)
  93. endif()
  94. if((CV_GCC OR CV_CLANG) AND NOT ENABLE_NOISY_WARNINGS)
  95. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-function")
  96. endif()
  97. if(MSVC AND NOT ENABLE_NOISY_WARNINGS)
  98. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4100") #unreferenced formal parameter
  99. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4127") #conditional expression is constant
  100. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /wd4505") #unreferenced local function has been removed
  101. string(REPLACE "/W4" "/W3" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}")
  102. endif()
  103. if(MSVC)
  104. ocv_warnings_disable(CMAKE_CXX_FLAGS /wd4996)
  105. else()
  106. ocv_warnings_disable(CMAKE_CXX_FLAGS
  107. -Wdeprecated-declarations
  108. -Woverloaded-virtual -Wunused-private-field
  109. -Wundef # accurate guard via #pragma doesn't work (C++ preprocessor doesn't handle #pragma)
  110. )
  111. endif()
  112. if(MSVC AND NOT BUILD_SHARED_LIBS)
  113. set_target_properties(${the_module} PROPERTIES LINK_FLAGS "/NODEFAULTLIB:atlthunk.lib /NODEFAULTLIB:atlsd.lib /DEBUG")
  114. endif()
  115. if(MSVC AND NOT ${PYTHON}_DEBUG_LIBRARIES)
  116. set(PYTHON_INSTALL_CONFIGURATIONS CONFIGURATIONS Release)
  117. else()
  118. set(PYTHON_INSTALL_CONFIGURATIONS "")
  119. endif()
  120. if(WIN32)
  121. set(PYTHON_INSTALL_ARCHIVE "")
  122. else()
  123. set(PYTHON_INSTALL_ARCHIVE ARCHIVE DESTINATION ${${PYTHON}_PACKAGES_PATH} COMPONENT python)
  124. endif()
  125. set(__python_loader_subdir "")
  126. if(NOT OPENCV_SKIP_PYTHON_LOADER)
  127. set(__python_loader_subdir "cv2/")
  128. endif()
  129. if(NOT " ${PYTHON}" STREQUAL " PYTHON"
  130. AND NOT DEFINED OPENCV_PYTHON_INSTALL_PATH
  131. )
  132. if(DEFINED OPENCV_${PYTHON}_INSTALL_PATH)
  133. set(OPENCV_PYTHON_INSTALL_PATH "${OPENCV_${PYTHON}_INSTALL_PATH}")
  134. elseif(NOT OPENCV_SKIP_PYTHON_LOADER)
  135. set(OPENCV_PYTHON_INSTALL_PATH "${${PYTHON}_PACKAGES_PATH}")
  136. endif()
  137. endif()
  138. if(NOT OPENCV_SKIP_PYTHON_LOADER AND DEFINED OPENCV_PYTHON_INSTALL_PATH)
  139. include("${CMAKE_CURRENT_LIST_DIR}/python_loader.cmake")
  140. set(OPENCV_PYTHON_INSTALL_PATH_SETUPVARS "${OPENCV_PYTHON_INSTALL_PATH}" CACHE INTERNAL "")
  141. endif()
  142. if(OPENCV_SKIP_PYTHON_LOADER)
  143. if(DEFINED OPENCV_${PYTHON}_INSTALL_PATH)
  144. set(__python_binary_install_path "${OPENCV_${PYTHON}_INSTALL_PATH}")
  145. elseif(DEFINED ${PYTHON}_PACKAGES_PATH)
  146. set(__python_binary_install_path "${${PYTHON}_PACKAGES_PATH}")
  147. else()
  148. message(FATAL_ERROR "Specify 'OPENCV_${PYTHON}_INSTALL_PATH' variable")
  149. endif()
  150. else()
  151. ocv_assert(DEFINED OPENCV_PYTHON_INSTALL_PATH)
  152. if(${PYTHON}_LIMITED_API)
  153. set(__python_binary_subdir "python-${${PYTHON}_VERSION_MAJOR}")
  154. else()
  155. set(__python_binary_subdir "python-${${PYTHON}_VERSION_MAJOR}.${${PYTHON}_VERSION_MINOR}")
  156. endif()
  157. set(__python_binary_install_path "${OPENCV_PYTHON_INSTALL_PATH}/${__python_loader_subdir}${__python_binary_subdir}")
  158. endif()
  159. install(TARGETS ${the_module}
  160. ${PYTHON_INSTALL_CONFIGURATIONS}
  161. RUNTIME DESTINATION "${__python_binary_install_path}" COMPONENT python
  162. LIBRARY DESTINATION "${__python_binary_install_path}" COMPONENT python
  163. ${PYTHON_INSTALL_ARCHIVE}
  164. )
  165. set(__INSTALL_PATH_${PYTHON} "${__python_binary_install_path}" CACHE INTERNAL "") # CMake status
  166. if(NOT OPENCV_SKIP_PYTHON_LOADER)
  167. ocv_assert(DEFINED OPENCV_PYTHON_INSTALL_PATH)
  168. if(OpenCV_FOUND)
  169. set(__loader_path "${OpenCV_BINARY_DIR}/python_loader")
  170. else()
  171. set(__loader_path "${CMAKE_BINARY_DIR}/python_loader")
  172. endif()
  173. set(__python_loader_install_tmp_path "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/install/python_loader/")
  174. set(OpenCV_PYTHON_LOADER_FULL_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/${OPENCV_PYTHON_INSTALL_PATH}/cv2")
  175. if(IS_ABSOLUTE "${OPENCV_PYTHON_INSTALL_PATH}")
  176. set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "'${OPENCV_PYTHON_INSTALL_PATH}/cv2'")
  177. else()
  178. set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "LOADER_DIR")
  179. endif()
  180. if(DEFINED ${PYTHON}_VERSION_MINOR AND NOT ${PYTHON}_LIMITED_API)
  181. set(__target_config "config-${${PYTHON}_VERSION_MAJOR}.${${PYTHON}_VERSION_MINOR}.py")
  182. else()
  183. set(__target_config "config-${${PYTHON}_VERSION_MAJOR}.py")
  184. endif()
  185. if(CMAKE_GENERATOR MATCHES "Visual Studio")
  186. set(CMAKE_PYTHON_EXTENSION_PATH "'${OPENCV_PYTHON_EXTENSION_BUILD_PATH}/Release'") # TODO: CMAKE_BUILD_TYPE is not defined
  187. else()
  188. set(CMAKE_PYTHON_EXTENSION_PATH "'${OPENCV_PYTHON_EXTENSION_BUILD_PATH}'")
  189. endif()
  190. configure_file("${PYTHON_SOURCE_DIR}/package/template/config-x.y.py.in" "${__loader_path}/cv2/${__target_config}" @ONLY)
  191. if(IS_ABSOLUTE __python_binary_install_path)
  192. set(CMAKE_PYTHON_EXTENSION_PATH "'${__python_binary_install_path}'")
  193. else()
  194. file(RELATIVE_PATH OpenCV_PYTHON_BINARY_RELATIVE_INSTALL_PATH "${OpenCV_PYTHON_LOADER_FULL_INSTALL_PATH}" "${CMAKE_INSTALL_PREFIX}/${__python_binary_install_path}")
  195. set(CMAKE_PYTHON_EXTENSION_PATH "os.path.join(${CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE}, '${OpenCV_PYTHON_BINARY_RELATIVE_INSTALL_PATH}')")
  196. endif()
  197. configure_file("${PYTHON_SOURCE_DIR}/package/template/config-x.y.py.in" "${__python_loader_install_tmp_path}/cv2/${__target_config}" @ONLY)
  198. install(FILES "${__python_loader_install_tmp_path}/cv2/${__target_config}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
  199. endif() # NOT OPENCV_SKIP_PYTHON_LOADER
  200. unset(PYTHON_SRC_DIR)
  201. unset(PYTHON_CVPY_PROCESS)
  202. unset(CVPY_SUFFIX)
  203. unset(PYTHON_INSTALL_CONFIGURATIONS)
  204. unset(PYTHON_INSTALL_ARCHIVE)