CMakeLists.txt 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. set(MODULE_NAME "python_bindings_generator")
  2. set(OPENCV_MODULE_IS_PART_OF_WORLD FALSE)
  3. ocv_add_module(${MODULE_NAME} INTERNAL)
  4. set(OPENCV_PYTHON_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_signatures.json" CACHE INTERNAL "")
  5. set(OPENCV_PYTHON_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
  6. # This file is included from a subdirectory
  7. set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
  8. if(NOT OPENCV_SKIP_PYTHON_LOADER)
  9. include("${PYTHON_SOURCE_DIR}/python_loader.cmake")
  10. endif()
  11. # get list of modules to wrap
  12. set(OPENCV_PYTHON_MODULES)
  13. foreach(m ${OPENCV_MODULES_BUILD})
  14. if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";python;" AND HAVE_${m})
  15. list(APPEND OPENCV_PYTHON_MODULES ${m})
  16. #message(STATUS "\t${m}")
  17. endif()
  18. endforeach()
  19. set(opencv_hdrs "")
  20. set(opencv_userdef_hdrs "")
  21. foreach(m ${OPENCV_PYTHON_MODULES})
  22. foreach (hdr ${OPENCV_MODULE_${m}_HEADERS})
  23. ocv_is_subdir(is_sub "${OPENCV_MODULE_${m}_LOCATION}/include" "${hdr}")
  24. if(is_sub)
  25. list(APPEND opencv_hdrs "${hdr}")
  26. endif()
  27. endforeach()
  28. # both wrapping and C++ implementation
  29. file(GLOB hdr2 ${OPENCV_MODULE_${m}_LOCATION}/misc/python/python_*.hpp)
  30. list(SORT hdr2)
  31. list(APPEND opencv_hdrs ${hdr2})
  32. list(APPEND opencv_userdef_hdrs ${hdr2})
  33. file(GLOB hdr ${OPENCV_MODULE_${m}_LOCATION}/misc/python/shadow*.hpp)
  34. list(SORT hdr)
  35. list(APPEND opencv_hdrs ${hdr})
  36. file(GLOB userdef_hdrs ${OPENCV_MODULE_${m}_LOCATION}/misc/python/pyopencv*.hpp)
  37. list(SORT userdef_hdrs)
  38. list(APPEND opencv_userdef_hdrs ${userdef_hdrs})
  39. endforeach(m)
  40. # header blacklist
  41. ocv_list_filterout(opencv_hdrs "modules/.*\\\\.h$")
  42. ocv_list_filterout(opencv_hdrs "modules/core/.*/cuda/")
  43. ocv_list_filterout(opencv_hdrs "modules/core/.*/hal/")
  44. ocv_list_filterout(opencv_hdrs "modules/core/.*/opencl/")
  45. ocv_list_filterout(opencv_hdrs "modules/.+/utils/.*")
  46. ocv_list_filterout(opencv_hdrs "modules/.*\\\\.inl\\\\.h*")
  47. ocv_list_filterout(opencv_hdrs "modules/.*_inl\\\\.h*")
  48. ocv_list_filterout(opencv_hdrs "modules/.*\\\\.details\\\\.h*")
  49. ocv_list_filterout(opencv_hdrs "modules/.*\\\\.private\\\\.h*")
  50. ocv_list_filterout(opencv_hdrs "modules/.*/private\\\\.h*")
  51. ocv_list_filterout(opencv_hdrs "modules/.*/legacy/.*")
  52. ocv_list_filterout(opencv_hdrs "modules/.*/detection_based_tracker\\\\.hpp") # Conditional compilation
  53. if(NOT HAVE_CUDA)
  54. ocv_list_filterout(opencv_hdrs "modules/cuda.*")
  55. ocv_list_filterout(opencv_hdrs "modules/cudev")
  56. endif()
  57. set(cv2_generated_files
  58. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_enums.h"
  59. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h"
  60. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h"
  61. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_modules.h"
  62. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_modules_content.h"
  63. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h"
  64. "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types_content.h"
  65. "${OPENCV_PYTHON_SIGNATURES_FILE}"
  66. )
  67. string(REPLACE ";" "\n" opencv_hdrs_ "${opencv_hdrs}")
  68. file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs_}")
  69. file(GLOB_RECURSE typing_stubs_generation_files "${PYTHON_SOURCE_DIR}/src2/typing_stubs_generation/*.py")
  70. add_custom_command(
  71. OUTPUT ${cv2_generated_files}
  72. COMMAND "${PYTHON_DEFAULT_EXECUTABLE}" "${PYTHON_SOURCE_DIR}/src2/gen2.py" "${CMAKE_CURRENT_BINARY_DIR}" "${CMAKE_CURRENT_BINARY_DIR}/headers.txt"
  73. DEPENDS "${PYTHON_SOURCE_DIR}/src2/gen2.py"
  74. "${PYTHON_SOURCE_DIR}/src2/hdr_parser.py"
  75. "${typing_stubs_generation_files}"
  76. "${PYTHON_SOURCE_DIR}/src2/typing_stubs_generator.py"
  77. # not a real build dependency (file(WRITE) result): ${CMAKE_CURRENT_BINARY_DIR}/headers.txt
  78. ${opencv_hdrs}
  79. COMMENT "Generate files for Python bindings and documentation"
  80. )
  81. add_custom_target(gen_opencv_python_source DEPENDS ${cv2_generated_files})
  82. if(TARGET copy_opencv_typing_stubs)
  83. add_dependencies(copy_opencv_typing_stubs gen_opencv_python_source)
  84. endif()
  85. set(cv2_custom_hdr "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_custom_headers.h")
  86. set(cv2_custom_hdr_str "//user-defined headers\n")
  87. foreach(uh ${opencv_userdef_hdrs})
  88. set(cv2_custom_hdr_str "${cv2_custom_hdr_str}#include \"${uh}\"\n")
  89. endforeach(uh)
  90. if(EXISTS "${cv2_custom_hdr}")
  91. file(READ "${cv2_custom_hdr}" __content)
  92. else()
  93. set(__content "")
  94. endif()
  95. if("${__content}" STREQUAL "${cv2_custom_hdr_str}")
  96. # Up-to-date
  97. else()
  98. file(WRITE "${cv2_custom_hdr}" "${cv2_custom_hdr_str}")
  99. endif()
  100. unset(__content)
  101. #
  102. # Configuration for standalone build of Python bindings
  103. #
  104. set(PYTHON_CONFIG_SCRIPT "")
  105. ocv_cmake_script_append_var(PYTHON_CONFIG_SCRIPT
  106. CMAKE_BUILD_TYPE
  107. BUILD_SHARED_LIBS
  108. CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
  109. CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
  110. CV_GCC CV_CLANG ENABLE_NOISY_WARNINGS
  111. CMAKE_MODULE_LINKER_FLAGS
  112. CMAKE_INSTALL_PREFIX
  113. OPENCV_PYTHON_INSTALL_PATH
  114. OpenCV_SOURCE_DIR
  115. OPENCV_FORCE_PYTHON_LIBS
  116. OPENCV_PYTHON_SKIP_LINKER_EXCLUDE_LIBS
  117. OPENCV_PYTHON_BINDINGS_DIR
  118. cv2_custom_hdr
  119. cv2_generated_files
  120. )
  121. set(CMAKE_HELPER_SCRIPT "${CMAKE_BINARY_DIR}/opencv_python_config.cmake")
  122. file(GENERATE OUTPUT "${CMAKE_HELPER_SCRIPT}" CONTENT "${PYTHON_CONFIG_SCRIPT}")