From bb02aeff6128ba80270d066d89968285126d4b18 Mon Sep 17 00:00:00 2001 From: Ansh Dadwal Date: Sat, 20 Sep 2025 21:15:13 +0530 Subject: [PATCH] `opencv`: fix build --- pythonforandroid/recipes/opencv/__init__.py | 16 +++++++-- .../recipes/opencv/patches/p4a_build.patch | 33 +++++++------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/pythonforandroid/recipes/opencv/__init__.py b/pythonforandroid/recipes/opencv/__init__.py index 8f7e2b8c07..ce78879cd7 100644 --- a/pythonforandroid/recipes/opencv/__init__.py +++ b/pythonforandroid/recipes/opencv/__init__.py @@ -15,7 +15,7 @@ class OpenCVRecipe(NDKRecipe): build of most of the libraries of the opencv's package, so we can process images, videos, objects, photos... ''' - version = '4.5.1' + version = '4.12.0' url = 'https://github.com/opencv/opencv/archive/{version}.zip' depends = ['numpy'] patches = ['patches/p4a_build.patch'] @@ -68,8 +68,9 @@ def build_arch(self, arch): python_link_version = self.ctx.python_recipe.link_version python_library = join(python_link_root, 'libpython{}.so'.format(python_link_version)) - python_include_numpy = join(python_site_packages, - 'numpy', 'core', 'include') + python_include_numpy = join( + self.ctx.get_python_install_dir(arch.arch), "numpy/_core/include", + ) shprint(sh.cmake, '-DP4A=ON', @@ -136,6 +137,15 @@ def build_arch(self, arch): self.get_build_dir(arch.arch), _env=env) + + # patch link.txt for unsupported flag + link_txt = 'modules/python3/CMakeFiles/opencv_python3.dir/link.txt' + with open(link_txt, 'r+') as f: + content = f.read().replace('-version', ' ') + f.seek(0) + f.write(content) + f.truncate() + shprint(sh.make, '-j' + str(cpu_count()), 'opencv_python' + python_major) # Install python bindings (cv2.so) shprint(sh.cmake, '-DCOMPONENT=python', '-P', './cmake_install.cmake') diff --git a/pythonforandroid/recipes/opencv/patches/p4a_build.patch b/pythonforandroid/recipes/opencv/patches/p4a_build.patch index fd60c01d38..10b81f776b 100644 --- a/pythonforandroid/recipes/opencv/patches/p4a_build.patch +++ b/pythonforandroid/recipes/opencv/patches/p4a_build.patch @@ -1,31 +1,22 @@ -This patch allow that the opencv's build command correctly detects our version -of python, so we can successfully build the python bindings (cv2.so) ---- opencv-4.0.1/cmake/OpenCVDetectPython.cmake.orig 2018-12-22 08:03:30.000000000 +0100 -+++ opencv-4.0.1/cmake/OpenCVDetectPython.cmake 2019-01-31 11:33:10.896502978 +0100 -@@ -175,7 +175,7 @@ if(NOT ${found}) +diff '--color=auto' -uNr opencv-4.12.0/cmake/OpenCVDetectPython.cmake opencv-4.12.0.mod/cmake/OpenCVDetectPython.cmake +--- opencv-4.12.0/cmake/OpenCVDetectPython.cmake 2025-07-02 13:24:13.000000000 +0530 ++++ opencv-4.12.0.mod/cmake/OpenCVDetectPython.cmake 2025-09-20 22:22:14.961944470 +0530 +@@ -175,7 +175,7 @@ endif() endif() - -- if(NOT ANDROID AND NOT IOS) -+ if(P4A OR NOT ANDROID AND NOT IOS) + +- if(NOT ANDROID AND NOT IOS AND NOT XROS) ++ if(P4A OR NOT ANDROID AND NOT IOS AND NOT XROS) if(CMAKE_HOST_UNIX) - execute_process(COMMAND ${_executable} -c "from distutils.sysconfig import *; print(get_python_lib())" + execute_process(COMMAND ${_executable} -c "from sysconfig import *; print(get_path('purelib'))" RESULT_VARIABLE _cvpy_process -@@ -244,7 +244,7 @@ if(NOT ${found}) - OUTPUT_STRIP_TRAILING_WHITESPACE) - endif() - endif() -- endif(NOT ANDROID AND NOT IOS) -+ endif(P4A OR NOT ANDROID AND NOT IOS) - endif() - - # Export return values ---- opencv-4.0.1/modules/python/CMakeLists.txt.orig 2018-12-22 08:03:30.000000000 +0100 -+++ opencv-4.0.1/modules/python/CMakeLists.txt 2019-01-31 11:47:17.100494908 +0100 +diff '--color=auto' -uNr opencv-4.12.0/modules/python/CMakeLists.txt opencv-4.12.0.mod/modules/python/CMakeLists.txt +--- opencv-4.12.0/modules/python/CMakeLists.txt 2025-07-02 13:24:13.000000000 +0530 ++++ opencv-4.12.0.mod/modules/python/CMakeLists.txt 2025-09-20 22:23:15.124356524 +0530 @@ -3,7 +3,7 @@ # ---------------------------------------------------------------------------- if(DEFINED OPENCV_INITIAL_PASS) # OpenCV build - + -if(ANDROID OR APPLE_FRAMEWORK OR WINRT) +if(ANDROID AND NOT P4A OR APPLE_FRAMEWORK OR WINRT) ocv_module_disable_(python2)