Merge pull request #20885 from alalek:cmake_fix_python_submodules

pull/20886/head^2
Alexander Alekhin 3 years ago
commit 0e86e292e4
  1. 5
      modules/python/CMakeLists.txt
  2. 4
      modules/python/bindings/CMakeLists.txt
  3. 40
      modules/python/common.cmake
  4. 3
      modules/python/package/cv2/__init__.py
  5. 14
      modules/python/python3/CMakeLists.txt
  6. 58
      modules/python/python_loader.cmake

@ -20,11 +20,6 @@ add_subdirectory(bindings)
add_subdirectory(test) add_subdirectory(test)
if(NOT OPENCV_SKIP_PYTHON_LOADER)
include("./python_loader.cmake")
message(STATUS "OpenCV Python: during development append to PYTHONPATH: ${CMAKE_BINARY_DIR}/python_loader")
endif()
if(__disable_python2) if(__disable_python2)
ocv_module_disable_(python2) ocv_module_disable_(python2)
endif() endif()

@ -8,6 +8,10 @@ set(OPENCV_PYTHON_BINDINGS_DIR "${CMAKE_CURRENT_BINARY_DIR}" CACHE INTERNAL "")
# This file is included from a subdirectory # This file is included from a subdirectory
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../") set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../")
if(NOT OPENCV_SKIP_PYTHON_LOADER)
include("${PYTHON_SOURCE_DIR}/python_loader.cmake")
endif()
# get list of modules to wrap # get list of modules to wrap
set(OPENCV_PYTHON_MODULES) set(OPENCV_PYTHON_MODULES)
foreach(m ${OPENCV_MODULES_BUILD}) foreach(m ${OPENCV_MODULES_BUILD})

@ -1,31 +1,6 @@
# This file is included from a subdirectory # This file is included from a subdirectory
set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}") set(PYTHON_SOURCE_DIR "${CMAKE_CURRENT_LIST_DIR}")
function(ocv_add_python_files_from_path search_path)
file(GLOB_RECURSE extra_py_files
RELATIVE "${search_path}"
# Plain Python code
"${search_path}/*.py"
# Type annotations
"${search_path}/*.pyi"
)
message(DEBUG "Extra Py files for ${search_path}: ${extra_py_files}")
if(extra_py_files)
list(SORT extra_py_files)
foreach(filename ${extra_py_files})
get_filename_component(module "${filename}" DIRECTORY)
if(NOT ${module} IN_LIST extra_modules)
list(APPEND extra_modules ${module})
endif()
configure_file("${search_path}/${filename}" "${__loader_path}/cv2/${filename}" COPYONLY)
install(FILES "${search_path}/${filename}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/${module}/" COMPONENT python)
endforeach()
message(STATUS "Found ${extra_modules} Python modules from ${search_path}")
else()
message(WARNING "Can't add Python files and modules from ${module_path}. There is no .py or .pyi files")
endif()
endfunction()
ocv_add_module(${MODULE_NAME} BINDINGS PRIVATE_REQUIRED opencv_python_bindings_generator) ocv_add_module(${MODULE_NAME} BINDINGS PRIVATE_REQUIRED opencv_python_bindings_generator)
include_directories(SYSTEM include_directories(SYSTEM
@ -243,21 +218,6 @@ if(NOT OPENCV_SKIP_PYTHON_LOADER)
endif() endif()
configure_file("${PYTHON_SOURCE_DIR}/package/template/config-x.y.py.in" "${__python_loader_install_tmp_path}/cv2/${__target_config}" @ONLY) configure_file("${PYTHON_SOURCE_DIR}/package/template/config-x.y.py.in" "${__python_loader_install_tmp_path}/cv2/${__target_config}" @ONLY)
install(FILES "${__python_loader_install_tmp_path}/cv2/${__target_config}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python) install(FILES "${__python_loader_install_tmp_path}/cv2/${__target_config}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
# handle Python extra code
foreach(m ${OPENCV_MODULES_BUILD})
if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";python;" AND HAVE_${m}
AND EXISTS "${OPENCV_MODULE_${m}_LOCATION}/misc/python/package"
)
ocv_add_python_files_from_path("${OPENCV_MODULE_${m}_LOCATION}/misc/python/package")
endif()
endforeach(m)
if(NOT "${OCV_PYTHON_EXTRA_MODULES_PATH}" STREQUAL "")
foreach(extra_ocv_py_modules_path ${OCV_PYTHON_EXTRA_MODULES_PATH})
ocv_add_python_files_from_path(${extra_ocv_py_modules_path})
endforeach()
endif()
endif() # NOT OPENCV_SKIP_PYTHON_LOADER endif() # NOT OPENCV_SKIP_PYTHON_LOADER
unset(PYTHON_SRC_DIR) unset(PYTHON_SRC_DIR)

@ -41,7 +41,7 @@ def __load_extra_py_code_for_module(base, name, enable_debug_print=False):
setattr(py_module, "_native", native_module) setattr(py_module, "_native", native_module)
for k, v in filter(lambda kv: not hasattr(py_module, kv[0]), for k, v in filter(lambda kv: not hasattr(py_module, kv[0]),
native_module.__dict__.items()): native_module.__dict__.items()):
if enable_debug_print: print(' symbol: {} = {}'.format(k, v)) if enable_debug_print: print(' symbol({}): {} = {}'.format(name, k, v))
setattr(py_module, k, v) setattr(py_module, k, v)
return True return True
@ -51,6 +51,7 @@ def __collect_extra_submodules(enable_debug_print=False):
return all(( return all((
# module is not internal # module is not internal
not module.startswith("_"), not module.startswith("_"),
not module.startswith("python-"),
# it is not a file # it is not a file
os.path.isdir(os.path.join(_extra_submodules_init_path, module)) os.path.isdir(os.path.join(_extra_submodules_init_path, module))
)) ))

@ -15,23 +15,9 @@ set(the_description "The python3 bindings")
set(MODULE_NAME python3) set(MODULE_NAME python3)
set(MODULE_INSTALL_SUBDIR python3) set(MODULE_INSTALL_SUBDIR python3)
set(_ocv_extra_modules_path ${CMAKE_CURRENT_LIST_DIR}/../package/extra_modules)
set(_old_ocv_python_extra_modules_path ${OCV_PYTHON_EXTRA_MODULES_PATH})
if("${OCV_PYTHON_EXTRA_MODULES_PATH}" STREQUAL "")
set(OCV_PYTHON_EXTRA_MODULES_PATH ${_ocv_extra_modules_path})
else()
list(APPEND OCV_PYTHON_EXTRA_MODULES_PATH ${_ocv_extra_modules_path})
endif()
unset(_ocv_extra_modules_path)
set(PYTHON PYTHON3) set(PYTHON PYTHON3)
include(../common.cmake) include(../common.cmake)
set(OCV_PYTHON_EXTRA_MODULES_PATH ${_old_ocv_python_extra_modules_path})
unset(_old_ocv_python_extra_modules_path)
unset(MODULE_NAME) unset(MODULE_NAME)
unset(MODULE_INSTALL_SUBDIR) unset(MODULE_INSTALL_SUBDIR)

@ -22,6 +22,12 @@ else()
set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "os.path.join(LOADER_DIR, 'not_installed')") set(CMAKE_PYTHON_EXTENSION_INSTALL_PATH_BASE "os.path.join(LOADER_DIR, 'not_installed')")
endif() endif()
if(OpenCV_FOUND)
return() # Ignore "standalone" builds of Python bindings
endif()
set(PYTHON_LOADER_FILES set(PYTHON_LOADER_FILES
"setup.py" "cv2/__init__.py" "setup.py" "cv2/__init__.py"
"cv2/load_config_py2.py" "cv2/load_config_py3.py" "cv2/load_config_py2.py" "cv2/load_config_py3.py"
@ -39,7 +45,8 @@ foreach(fname ${PYTHON_LOADER_FILES})
endif() endif()
endforeach() endforeach()
if(NOT OpenCV_FOUND) # Ignore "standalone" builds of Python bindings
if(WIN32) if(WIN32)
if(CMAKE_GENERATOR MATCHES "Visual Studio") if(CMAKE_GENERATOR MATCHES "Visual Studio")
list(APPEND CMAKE_PYTHON_BINARIES_PATH "'${EXECUTABLE_OUTPUT_PATH}/Release'") # TODO: CMAKE_BUILD_TYPE is not defined list(APPEND CMAKE_PYTHON_BINARIES_PATH "'${EXECUTABLE_OUTPUT_PATH}/Release'") # TODO: CMAKE_BUILD_TYPE is not defined
@ -52,6 +59,8 @@ if(NOT OpenCV_FOUND) # Ignore "standalone" builds of Python bindings
string(REPLACE ";" ",\n " CMAKE_PYTHON_BINARIES_PATH "${CMAKE_PYTHON_BINARIES_PATH}") string(REPLACE ";" ",\n " CMAKE_PYTHON_BINARIES_PATH "${CMAKE_PYTHON_BINARIES_PATH}")
configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__loader_path}/cv2/config.py" @ONLY) configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__loader_path}/cv2/config.py" @ONLY)
# install # install
if(DEFINED OPENCV_PYTHON_INSTALL_PATH) if(DEFINED OPENCV_PYTHON_INSTALL_PATH)
if(WIN32) if(WIN32)
@ -69,4 +78,51 @@ if(NOT OpenCV_FOUND) # Ignore "standalone" builds of Python bindings
configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__python_loader_install_tmp_path}/cv2/config.py" @ONLY) configure_file("${PYTHON_SOURCE_DIR}/package/template/config.py.in" "${__python_loader_install_tmp_path}/cv2/config.py" @ONLY)
install(FILES "${__python_loader_install_tmp_path}/cv2/config.py" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python) install(FILES "${__python_loader_install_tmp_path}/cv2/config.py" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/" COMPONENT python)
endif() endif()
#
# Handle Python extra code (submodules)
#
function(ocv_add_python_files_from_path search_path)
file(GLOB_RECURSE extra_py_files
RELATIVE "${search_path}"
# Plain Python code
"${search_path}/*.py"
# Type annotations
"${search_path}/*.pyi"
)
ocv_debug_message("Extra Py files for ${search_path}: ${extra_py_files}")
if(extra_py_files)
list(SORT extra_py_files)
foreach(filename ${extra_py_files})
get_filename_component(module "${filename}" DIRECTORY)
if(NOT ${module} IN_LIST extra_modules)
list(APPEND extra_modules ${module})
endif()
configure_file("${search_path}/${filename}" "${__loader_path}/cv2/${filename}" COPYONLY)
if(DEFINED OPENCV_PYTHON_INSTALL_PATH)
install(FILES "${search_path}/${filename}" DESTINATION "${OPENCV_PYTHON_INSTALL_PATH}/cv2/${module}/" COMPONENT python)
endif()
endforeach()
message(STATUS "Found '${extra_modules}' Python modules from ${search_path}")
else()
message(WARNING "Can't add Python files and modules from '${module_path}'. There is no .py or .pyi files")
endif()
endfunction()
ocv_add_python_files_from_path("${PYTHON_SOURCE_DIR}/package/extra_modules")
foreach(m ${OPENCV_MODULES_BUILD})
if (";${OPENCV_MODULE_${m}_WRAPPERS};" MATCHES ";python;" AND HAVE_${m}
AND EXISTS "${OPENCV_MODULE_${m}_LOCATION}/misc/python/package"
)
ocv_add_python_files_from_path("${OPENCV_MODULE_${m}_LOCATION}/misc/python/package")
endif()
endforeach(m)
if(NOT "${OPENCV_PYTHON_EXTRA_MODULES_PATH}" STREQUAL "")
foreach(extra_ocv_py_modules_path ${OPENCV_PYTHON_EXTRA_MODULES_PATH})
ocv_add_python_files_from_path(${extra_ocv_py_modules_path})
endforeach()
endif() endif()

Loading…
Cancel
Save