From 768f4cb7bc18872c3a8179e79a2e9c8406ed7c1f Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Sat, 9 Dec 2017 18:54:52 +0000 Subject: [PATCH] python: 'sub-module' for binding sources and documentation meta information --- cmake/OpenCVModule.cmake | 2 +- doc/CMakeLists.txt | 39 +++++++----- modules/python/CMakeLists.txt | 2 + modules/python/bindings/CMakeLists.txt | 69 ++++++++++++++++++++ modules/python/common.cmake | 88 +++++--------------------- modules/python/src2/cv2.cpp | 6 ++ modules/python/src2/gen2.py | 15 +---- 7 files changed, 117 insertions(+), 104 deletions(-) create mode 100644 modules/python/bindings/CMakeLists.txt diff --git a/cmake/OpenCVModule.cmake b/cmake/OpenCVModule.cmake index 4c1eaf650c..17f5b3b7dd 100644 --- a/cmake/OpenCVModule.cmake +++ b/cmake/OpenCVModule.cmake @@ -104,7 +104,7 @@ macro(ocv_add_dependencies full_modname) list(FIND OPENCV_MODULE_${full_modname}_WRAPPERS "python" __python_idx) if (NOT __python_idx EQUAL -1) list(REMOVE_ITEM OPENCV_MODULE_${full_modname}_WRAPPERS "python") - list(APPEND OPENCV_MODULE_${full_modname}_WRAPPERS "python2" "python3") + list(APPEND OPENCV_MODULE_${full_modname}_WRAPPERS "python_bindings_generator" "python2" "python3") endif() unset(__python_idx) diff --git a/doc/CMakeLists.txt b/doc/CMakeLists.txt index f23a1b3047..aa049f9189 100644 --- a/doc/CMakeLists.txt +++ b/doc/CMakeLists.txt @@ -33,7 +33,7 @@ endif(HAVE_DOC_GENERATOR) if(BUILD_DOCS AND DOXYGEN_FOUND) # not documented modules list - list(APPEND blacklist "ts" "java" "python2" "python3" "js" "world") + list(APPEND blacklist "ts" "java" "python_bindings_generator" "python2" "python3" "js" "world") unset(CMAKE_DOXYGEN_TUTORIAL_CONTRIB_ROOT) unset(CMAKE_DOXYGEN_TUTORIAL_JS_ROOT) @@ -104,7 +104,6 @@ if(BUILD_DOCS AND DOXYGEN_FOUND) list(APPEND deps ${bib_file}) endif() # Reference entry - # set(one_ref "@ref ${m} | ${m}\n") set(one_ref "\t- ${m}. @ref ${m}\n") list(FIND EXTRA_MODULES ${m} _pos) if(${_pos} EQUAL -1) @@ -205,9 +204,8 @@ if(BUILD_DOCS AND DOXYGEN_FOUND) list(APPEND js_tutorials_assets_deps "${f}" "${opencv_tutorial_html_dir}/${fname}") endforeach() - set(doxygen_result "${CMAKE_CURRENT_BINARY_DIR}/doxygen/html/modules.html") - - add_custom_target(doxygen_cpp + add_custom_target( + doxygen_cpp COMMAND ${DOXYGEN_EXECUTABLE} ${doxyfile} DEPENDS ${doxyfile} ${rootfile} ${bibfile} ${deps} ${js_tutorials_assets_deps} COMMENT "Generate Doxygen documentation" @@ -218,18 +216,27 @@ if(BUILD_DOCS AND DOXYGEN_FOUND) COMPONENT "docs" OPTIONAL ) - if(PYTHON2_EXECUTABLE) - add_custom_target(doxygen_python - COMMAND ${PYTHON2_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/tools/add_signatures.py" "${CMAKE_CURRENT_BINARY_DIR}/doxygen/html/" "${OPENCV_PYTHON2_SIGNATURES_FILE}" "python" - DEPENDS "${doxygen_result}" gen_opencv_python2 - ) - add_custom_target(doxygen - DEPENDS doxygen_cpp doxygen_python - ) - elseif(PYTHON3_EXECUTABLE) + if(NOT DEFINED HAVE_PYTHON_BS4 AND PYTHON_DEFAULT_EXECUTABLE) + # Documentation post-processing tool requires BuautifulSoup Python package + execute_process(COMMAND "${PYTHON_DEFAULT_EXECUTABLE}" -c "import bs4; from bs4 import BeautifulSoup; print(bs4.__version__)" + RESULT_VARIABLE _result + OUTPUT_VARIABLE _bs4_version + OUTPUT_STRIP_TRAILING_WHITESPACE) + + if(NOT _result EQUAL 0) + set(HAVE_PYTHON_BS4 0 CACHE INTERNAL "") + else() + message(STATUS "Python BeautifulSoup (bs4) version: ${_bs4_version}") + set(HAVE_PYTHON_BS4 1 CACHE INTERNAL "") + endif() + endif() + + if(PYTHON_DEFAULT_EXECUTABLE AND HAVE_PYTHON_BS4 + AND OPENCV_PYTHON_SIGNATURES_FILE AND TARGET gen_opencv_python_source) add_custom_target(doxygen_python - COMMAND ${PYTHON3_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/tools/add_signatures.py" "${CMAKE_CURRENT_BINARY_DIR}/doxygen/html/" "${OPENCV_PYTHON3_SIGNATURES_FILE}" "python" - DEPENDS "${doxygen_result}" gen_opencv_python3 + COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${CMAKE_CURRENT_SOURCE_DIR}/tools/add_signatures.py" "${CMAKE_CURRENT_BINARY_DIR}/doxygen/html/" "${OPENCV_PYTHON_SIGNATURES_FILE}" "python" + DEPENDS doxygen_cpp gen_opencv_python_source + COMMENT "Inject Python signatures into documentation" ) add_custom_target(doxygen DEPENDS doxygen_cpp doxygen_python diff --git a/modules/python/CMakeLists.txt b/modules/python/CMakeLists.txt index d85a516791..dffe9a794e 100644 --- a/modules/python/CMakeLists.txt +++ b/modules/python/CMakeLists.txt @@ -2,6 +2,8 @@ # CMake file for python support # ---------------------------------------------------------------------------- +add_subdirectory(bindings) + if(ANDROID OR APPLE_FRAMEWORK OR WINRT) set(__disable_python2 ON) set(__disable_python3 ON) diff --git a/modules/python/bindings/CMakeLists.txt b/modules/python/bindings/CMakeLists.txt new file mode 100644 index 0000000000..e407ea9a11 --- /dev/null +++ b/modules/python/bindings/CMakeLists.txt @@ -0,0 +1,69 @@ +set(MODULE_NAME "python_bindings_generator") +ocv_add_module(${MODULE_NAME} INTERNAL) + +set(OPENCV_PYTHON_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_signatures.json" CACHE INTERNAL "") +set(OPENCV_PYTHON_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "") + +# This file is included from a subdirectory +set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../") + +# get list of modules to wrap +set(OPENCV_PYTHON_MODULES) +foreach(m ${OPENCV_MODULES_BUILD}) + if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";${MODULE_NAME};" AND HAVE_${m}) + list(APPEND OPENCV_PYTHON_MODULES ${m}) + #message(STATUS "\t${m}") + endif() +endforeach() + +set(opencv_hdrs "") +set(opencv_userdef_hdrs "") +foreach(m ${OPENCV_PYTHON_MODULES}) + list(APPEND opencv_hdrs ${OPENCV_MODULE_${m}_HEADERS}) + file(GLOB userdef_hdrs ${OPENCV_MODULE_${m}_LOCATION}/misc/python/pyopencv*.hpp) + list(APPEND opencv_userdef_hdrs ${userdef_hdrs}) +endforeach(m) + +# header blacklist +ocv_list_filterout(opencv_hdrs "modules/.*\\\\.h$") +ocv_list_filterout(opencv_hdrs "modules/core/.*/cuda") +ocv_list_filterout(opencv_hdrs "modules/cuda.*") +ocv_list_filterout(opencv_hdrs "modules/cudev") +ocv_list_filterout(opencv_hdrs "modules/core/.*/hal/") +ocv_list_filterout(opencv_hdrs "modules/.+/utils/.*") +ocv_list_filterout(opencv_hdrs "modules/.*\\\\.inl\\\\.h*") +ocv_list_filterout(opencv_hdrs "modules/.*_inl\\\\.h*") +ocv_list_filterout(opencv_hdrs "modules/.*\\\\.details\\\\.h*") +ocv_list_filterout(opencv_hdrs "modules/.*/detection_based_tracker\\\\.hpp") # Conditional compilation + +set(cv2_generated_hdrs + "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" + "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h" + "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h" + "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h" + "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_ns_reg.h" +) + +set(cv2_generated_files ${cv2_generated_hdrs} + "${OPENCV_PYTHON_SIGNATURES_FILE}" +) + +string(REPLACE ";" "\n" opencv_hdrs_ "${opencv_hdrs}") +file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs_}") +add_custom_command( + OUTPUT ${cv2_generated_files} + COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${PYTHON_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" + DEPENDS ${PYTHON_SOURCE_DIR}/src2/gen2.py + DEPENDS ${PYTHON_SOURCE_DIR}/src2/hdr_parser.py + DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/headers.txt + DEPENDS ${opencv_hdrs} + COMMENT "Generate files for Python bindings and documentation" +) + +add_custom_target(gen_opencv_python_source DEPENDS ${cv2_generated_files}) + +set(cv2_custom_hdr "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_custom_headers.h") +file(WRITE ${cv2_custom_hdr} "//user-defined headers\n") +foreach(uh ${opencv_userdef_hdrs}) + file(APPEND ${cv2_custom_hdr} "#include \"${uh}\"\n") +endforeach(uh) diff --git a/modules/python/common.cmake b/modules/python/common.cmake index 9f87a043a3..fcccffe2ed 100644 --- a/modules/python/common.cmake +++ b/modules/python/common.cmake @@ -1,87 +1,26 @@ # This file is included from a subdirectory set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../") -# try to use dynamic symbols linking with libpython.so -set(OPENCV_FORCE_PYTHON_LIBS OFF CACHE BOOL "") -string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") -if(NOT WIN32 AND NOT APPLE AND NOT OPENCV_PYTHON_SKIP_LINKER_EXCLUDE_LIBS) - set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--exclude-libs=ALL") -endif() - -ocv_add_module(${MODULE_NAME} BINDINGS) +ocv_add_module(${MODULE_NAME} BINDINGS PRIVATE_REQUIRED opencv_python_bindings_generator) ocv_module_include_directories( "${${PYTHON}_INCLUDE_PATH}" +) +include_directories( ${${PYTHON}_NUMPY_INCLUDE_DIRS} "${PYTHON_SOURCE_DIR}/src2" - ) - -# get list of modules to wrap -# message(STATUS "Wrapped in ${MODULE_NAME}:") -set(OPENCV_PYTHON_MODULES) -foreach(m ${OPENCV_MODULES_BUILD}) - if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";${MODULE_NAME};" AND HAVE_${m}) - list(APPEND OPENCV_PYTHON_MODULES ${m}) - # message(STATUS "\t${m}") - endif() -endforeach() - -set(opencv_hdrs "") -set(opencv_userdef_hdrs "") -foreach(m ${OPENCV_PYTHON_MODULES}) - list(APPEND opencv_hdrs ${OPENCV_MODULE_${m}_HEADERS}) - file(GLOB userdef_hdrs ${OPENCV_MODULE_${m}_LOCATION}/misc/python/pyopencv*.hpp) - list(APPEND opencv_userdef_hdrs ${userdef_hdrs}) -endforeach(m) - -# header blacklist -ocv_list_filterout(opencv_hdrs "modules/.*\\\\.h$") -ocv_list_filterout(opencv_hdrs "modules/core/.*/cuda") -ocv_list_filterout(opencv_hdrs "modules/cuda.*") -ocv_list_filterout(opencv_hdrs "modules/cudev") -ocv_list_filterout(opencv_hdrs "modules/core/.*/hal/") -ocv_list_filterout(opencv_hdrs "modules/.+/utils/.*") -ocv_list_filterout(opencv_hdrs "modules/.*\\\\.inl\\\\.h*") -ocv_list_filterout(opencv_hdrs "modules/.*_inl\\\\.h*") -ocv_list_filterout(opencv_hdrs "modules/.*\\\\.details\\\\.h*") -ocv_list_filterout(opencv_hdrs "modules/.*/detection_based_tracker\\\\.hpp") # Conditional compilation - -set(cv2_generated_hdrs - "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_include.h" - "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_funcs.h" - "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_types.h" - "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_type_reg.h" - "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_generated_ns_reg.h" -) - -set(OPENCV_${PYTHON}_SIGNATURES_FILE "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_signatures.json" CACHE INTERNAL "") - -set(cv2_generated_files ${cv2_generated_hdrs} - "${OPENCV_${PYTHON}_SIGNATURES_FILE}" -) - -string(REPLACE ";" "\n" opencv_hdrs_ "${opencv_hdrs}") -file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${opencv_hdrs_}") -add_custom_command( - OUTPUT ${cv2_generated_files} - COMMAND ${PYTHON_DEFAULT_EXECUTABLE} "${PYTHON_SOURCE_DIR}/src2/gen2.py" ${CMAKE_CURRENT_BINARY_DIR} "${CMAKE_CURRENT_BINARY_DIR}/headers.txt" "${PYTHON}" - DEPENDS ${PYTHON_SOURCE_DIR}/src2/gen2.py - DEPENDS ${PYTHON_SOURCE_DIR}/src2/hdr_parser.py - DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/headers.txt - DEPENDS ${opencv_hdrs} - COMMENT "Generate files for ${the_module}" + "${OPENCV_PYTHON_BINDINGS_DIR}" ) -add_custom_target(gen_${the_module} DEPENDS ${cv2_generated_files}) - -set(cv2_custom_hdr "${CMAKE_CURRENT_BINARY_DIR}/pyopencv_custom_headers.h") -file(WRITE ${cv2_custom_hdr} "//user-defined headers\n") -foreach(uh ${opencv_userdef_hdrs}) - file(APPEND ${cv2_custom_hdr} "#include \"${uh}\"\n") -endforeach(uh) +# try to use dynamic symbols linking with libpython.so +set(OPENCV_FORCE_PYTHON_LIBS OFF CACHE BOOL "") +string(REPLACE "-Wl,--no-undefined" "" CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS}") +if(NOT WIN32 AND NOT APPLE AND NOT OPENCV_PYTHON_SKIP_LINKER_EXCLUDE_LIBS) + set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} -Wl,--exclude-libs=ALL") +endif() ocv_add_library(${the_module} MODULE ${PYTHON_SOURCE_DIR}/src2/cv2.cpp ${cv2_generated_hdrs} ${opencv_userdef_hdrs} ${cv2_custom_hdr}) -add_dependencies(${the_module} gen_${the_module}) +add_dependencies(${the_module} gen_opencv_python_source) if(APPLE) set_target_properties(${the_module} PROPERTIES LINK_FLAGS "-undefined dynamic_lookup") @@ -92,7 +31,10 @@ elseif(WIN32 OR OPENCV_FORCE_PYTHON_LIBS) ocv_target_link_libraries(${the_module} LINK_PRIVATE ${${PYTHON}_LIBRARIES}) endif() endif() -ocv_target_link_libraries(${the_module} LINK_PRIVATE ${OPENCV_MODULE_${the_module}_DEPS}) + +set(deps ${OPENCV_MODULE_${the_module}_DEPS}) +list(REMOVE_ITEM deps opencv_python_bindings_generator) # don't add dummy module to target_link_libraries list +ocv_target_link_libraries(${the_module} LINK_PRIVATE ${deps}) if(DEFINED ${PYTHON}_CVPY_SUFFIX) set(CVPY_SUFFIX "${${PYTHON}_CVPY_SUFFIX}") diff --git a/modules/python/src2/cv2.cpp b/modules/python/src2/cv2.cpp index 30c84f46b9..8f1eb3a214 100644 --- a/modules/python/src2/cv2.cpp +++ b/modules/python/src2/cv2.cpp @@ -9,6 +9,12 @@ #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include +#if PY_MAJOR_VERSION >= 3 +# define CV_PYTHON_TYPE_HEAD_INIT() PyVarObject_HEAD_INIT(&PyType_Type, 0) +#else +# define CV_PYTHON_TYPE_HEAD_INIT() PyObject_HEAD_INIT(&PyType_Type) 0, +#endif + #include "pyopencv_generated_include.h" #include "opencv2/core/types_c.h" diff --git a/modules/python/src2/gen2.py b/modules/python/src2/gen2.py index 4f15715ccc..27928bc485 100755 --- a/modules/python/src2/gen2.py +++ b/modules/python/src2/gen2.py @@ -46,20 +46,7 @@ gen_template_func_body = Template("""$code_decl } """) -py_major_version = sys.version_info[0] -if __name__ == "__main__": - if len(sys.argv) > 3: - if sys.argv[3] == 'PYTHON3': - py_major_version = 3 - elif sys.argv[3] == 'PYTHON2': - py_major_version = 2 - else: - raise Exception('Incorrect argument: expected PYTHON2 or PYTHON3, received: ' + sys.argv[3]) -if py_major_version >= 3: - head_init_str = "PyVarObject_HEAD_INIT(&PyType_Type, 0)" -else: - head_init_str = """PyObject_HEAD_INIT(&PyType_Type) -0,""" +head_init_str = "CV_PYTHON_TYPE_HEAD_INIT()" gen_template_simple_type_decl = Template(""" struct pyopencv_${name}_t