Skip to content

add opencv android #7065

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: dev
Choose a base branch
from
Open

add opencv android #7065

wants to merge 22 commits into from

Conversation

kekopom
Copy link

@kekopom kekopom commented May 4, 2025

Testing locally by running xmake l scripts/test.lua -v -D --shallow -p android --ndk=~/android-ndk-r28b --ndk_sdkver=32 -a arm64-v8a opencv
and for shared:
xmake l scripts/test.lua -v -D --shallow -p android --ndk=~/android-ndk-r27c --ndk_sdkver=32 -a arm64-v8a -k shared opencv

@kekopom kekopom marked this pull request as draft May 4, 2025 23:57
@kekopom kekopom force-pushed the add-android-opencv branch 3 times, most recently from 69e064f to fdf7b63 Compare May 5, 2025 22:22
@kekopom kekopom force-pushed the add-android-opencv branch from fdf7b63 to d94daed Compare May 5, 2025 22:26
@kekopom
Copy link
Author

kekopom commented May 5, 2025

current state:

CMake Error at /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake:113 (cmake_minimum_required):
Compatibility with CMake < 3.5 has been removed from CMake.

Update the VERSION argument value. Or, use the ... syntax
to tell CMake that the project requires at least but has been updated
to work with policies introduced by or earlier.

Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.

==> Looks like adding
table.insert(configs, "-DCMAKE_POLICY_VERSION_MINIMUM=3.5")
fixed the issue

@luadebug
Copy link
Contributor

luadebug commented May 5, 2025

current state:

CMake Error at /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake:113 (cmake_minimum_required): Compatibility with CMake < 3.5 has been removed from CMake.

Update the VERSION argument value. Or, use the ... syntax to tell CMake that the project requires at least but has been updated to work with policies introduced by or earlier.

Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.

==> Looks like adding table.insert(configs, "-DCMAKE_POLICY_VERSION_MINIMUM=3.5") fixed the issue

If you update xmake by xmake update dev, would it resolve this issue, so there would not need to add "-DCMAKE_POLICY_VERSION_MINIMUM=3.5"?

@kekopom
Copy link
Author

kekopom commented May 5, 2025

current state:
CMake Error at /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake:113 (cmake_minimum_required): Compatibility with CMake < 3.5 has been removed from CMake.
Update the VERSION argument value. Or, use the ... syntax to tell CMake that the project requires at least but has been updated to work with policies introduced by or earlier.
Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.
==> Looks like adding table.insert(configs, "-DCMAKE_POLICY_VERSION_MINIMUM=3.5") fixed the issue

If you update xmake by xmake update dev, would it resolve this issue, so there would not need to add "-DCMAKE_POLICY_VERSION_MINIMUM=3.5"?

Looks like it didn't work :(
using
xmake --version
xmake v3.0.0+dev.3d3f6c3

FAILED: unix-install/opencv4.pc /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/bd/unix-install/opencv4.pc
cd /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/bd && /home/kekopom/.xmake/packages/c/cmake/4.0.1/ee0342c3438b4e54a11f5d4d26f5299a/bin/cmake -DCMAKE_HELPER_SCRIPT=/home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/bd/OpenCVGenPkgConfig.info.cmake -P /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake
CMake Error at /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake:113 (cmake_minimum_required):
Compatibility with CMake < 3.5 has been removed from CMake.

Update the VERSION argument value. Or, use the ... syntax
to tell CMake that the project requires at least but has been updated
to work with policies introduced by or earlier.

Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.

==> Looks like the warning has became an error since CMake 4.0.0 which is pulled by XMake if I am not misleading

@luadebug
Copy link
Contributor

luadebug commented May 6, 2025

current state:
CMake Error at /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake:113 (cmake_minimum_required): Compatibility with CMake < 3.5 has been removed from CMake.
Update the VERSION argument value. Or, use the ... syntax to tell CMake that the project requires at least but has been updated to work with policies introduced by or earlier.
Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.
==> Looks like adding table.insert(configs, "-DCMAKE_POLICY_VERSION_MINIMUM=3.5") fixed the issue

If you update xmake by xmake update dev, would it resolve this issue, so there would not need to add "-DCMAKE_POLICY_VERSION_MINIMUM=3.5"?

Looks like it didn't work :( using xmake --version xmake v3.0.0+dev.3d3f6c3

FAILED: unix-install/opencv4.pc /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/bd/unix-install/opencv4.pc cd /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/bd && /home/kekopom/.xmake/packages/c/cmake/4.0.1/ee0342c3438b4e54a11f5d4d26f5299a/bin/cmake -DCMAKE_HELPER_SCRIPT=/home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/bd/OpenCVGenPkgConfig.info.cmake -P /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake CMake Error at /home/kekopom/.xmake/cache/packages/2505/o/opencv/4.11.0/source/cmake/OpenCVGenPkgconfig.cmake:113 (cmake_minimum_required): Compatibility with CMake < 3.5 has been removed from CMake.

Update the VERSION argument value. Or, use the ... syntax to tell CMake that the project requires at least but has been updated to work with policies introduced by or earlier.

Or, add -DCMAKE_POLICY_VERSION_MINIMUM=3.5 to try configuring anyway.

==> Looks like the warning has became an error since CMake 4.0.0 which is pulled by XMake if I am not misleading

https://github.com/xmake-io/xmake/blob/ea13d87b271fbfe6ec96b90cb6b315220f4c53e9/xmake/modules/package/tools/cmake.lua#L951-L953 This supposed to pass -DCMAKE_POLICY_VERSION_MINIMUM=3.5 during build with CMake 4.0 & newer though automatically appended into configs...

@kekopom
Copy link
Author

kekopom commented May 6, 2025

@luadebug indeed I can see that the flag is well propagated to cmake
You will find below the full outputs of the command xmake l scripts/test.lua -v -D --shallow -p android --ndk=~/android-ndk-r27c --ndk_sdkver=32 -a arm64-v8a opencv
On my side I cannot understand what's wrong 😞

opencv_android.log

edit: I might have found an interesting link: opencv/opencv#27122
adding add_patches("4.11.0", "https://raw.githubusercontent.com/eweOS/packages/refs/heads/opencv/0001-Fix-configuring-with-CMake-4.patch", "c9ac48cbcde10146af6fd9ca22d05091d5a3a35943a723bdcf4d30d51bfbf4a4") leads to a different error, I assume that it fixed the error for now

I would like also to avoid to pull a .patch from a non-opencv repo, if someone knows a better way to use a commit id maybe

@luadebug
Copy link
Contributor

luadebug commented May 6, 2025

leads to a different error, I assume that it fixed the error for now

I suppose it leads to

error: D:\xm\xmake-repo\packages\o\opencv\xmake.lua:311: ...amdir\core\sandbox\modules\import\core\tool\compiler.lua:84: @programdir\modules\core\tools\gcc.lua:1020: C:\Users\Admin\AppData\Local\Temp\.xmake\250507\_C595EFB0B9474EA38934169C8482FD35.cpp:2:10: fatal error: 'opencv2/opencv.hpp' file not found
    2 | #include <opencv2/opencv.hpp>
      |          ^~~~~~~~~~~~~~~~~~~~
1 error generated.

Since package:installdir("include") folder is empty, you might need os.cp headers into this folder.

@kekopom
Copy link
Author

kekopom commented May 6, 2025

Yep I fixed it by adding package:add("includedirs", "sdk/native/jni/include")
I think it does the same as your suggestion? :)

@kekopom kekopom force-pushed the add-android-opencv branch from e81839e to abc7027 Compare May 6, 2025 23:59
@kekopom kekopom force-pushed the add-android-opencv branch from abc7027 to 6178b7f Compare May 7, 2025 00:00
@kekopom kekopom marked this pull request as ready for review May 7, 2025 01:56
@kekopom
Copy link
Author

kekopom commented May 7, 2025

I think I've been able to finally build it
�[0m�[38;2;255;255;0m => �[0minstall opencv 4.11.0 .. �[38;2;0;255;0;1mok�[0m

@kekopom kekopom force-pushed the add-android-opencv branch from f96abba to 6f90649 Compare May 7, 2025 13:05
@kekopom
Copy link
Author

kekopom commented May 7, 2025

Both (shared / static) builds are OK

@kekopom
Copy link
Author

kekopom commented May 8, 2025

seems related to #1228

@kekopom
Copy link
Author

kekopom commented May 8, 2025

I've been able to build and run this exemple https://docs.opencv.org/4.x/d9/d15/samples_2cpp_2cout_mat_8cpp-example.html

On my Android phone!

@kekopom kekopom requested a review from luadebug May 9, 2025 08:05
@kekopom
Copy link
Author

kekopom commented May 9, 2025

@luadebug thanks for helping fixing windows build :D
with your patch, it complains to not find links

Could it be linked with elseif package:is_arch("arm64") then arch = "ARM64" line 255?
Since you added -DAARCH64=ON should it be now aarch64 instead of "ARM64"? (in case of arm only)

@luadebug
Copy link
Contributor

luadebug commented May 9, 2025

It looks like linkdir for windows arm64 is x64 for local arch. Need some workaround like new implementation would support old implementation as well..
C:/Users/runneradmin/AppData/Local/.xmake/packages/o/opencv/4.11.0/954bb460d4834f52b7ff0e8b3ff4d938/x64/vc17/staticlib/opencv_dpm4110.lib

@kekopom
Copy link
Author

kekopom commented May 10, 2025

@luadebug do you know what's happening?
C:\Program Files (x86)\Windows Kits\10\include\10.0.26100.0\ucrt\wchar.h(254): error C2664: '__n64 __uint64x1_t_to_n64(uint64x1_t)': cannot convert argument 1 from 'uint16x4_t' to 'uint64x1_t'

Only related topic I could find is https://developercommunity.visualstudio.com/t/wcharh-header-fails-compilation-on-ARM6/10868106?sort=newest
with no solution in it ..

Btw since we haven't changed anything for Windows ARM I would have expected it to work, I am a bit surprised

@luadebug
Copy link
Contributor

luadebug commented May 11, 2025

@luadebug do you know what's happening? C:\Program Files (x86)\Windows Kits\10\include\10.0.26100.0\ucrt\wchar.h(254): error C2664: '__n64 __uint64x1_t_to_n64(uint64x1_t)': cannot convert argument 1 from 'uint16x4_t' to 'uint64x1_t'
Only related topic I could find is https://developercommunity.visualstudio.com/t/wcharh-header-fails-compilation-on-ARM6/10868106?sort=newest with no solution in it ..

Why is this being mentioned, is it some current Github CI failure? Looks like issue present for 2022 worker.
The only mention is microsoft/vcpkg#44310 as it is Windows SDK version breaking changes.

Btw since we haven't changed anything for Windows ARM I would have expected it to work, I am a bit surprised

Maybe new version update broke something.

@kekopom
Copy link
Author

kekopom commented May 11, 2025

So what do we do?
can we disable windows arm support for now?

@luadebug
Copy link
Contributor

luadebug commented May 12, 2025

So what do we do? can we disable windows arm support for now?

It looks like this package has windows@arm64 introduced exactly for 4.10.0->4.11.0... exactly for 2022 worker... about 3 months ago 4.11.0 was pushed.

    if on_check then
        on_check("windows|arm64", function (package)
            if package:version() and package:version():lt("4.10.0") then
                raise("current opencv version does not support windows/arm64!")
            end
            local vs = package:toolchain("msvc"):config("vs")
            assert(tonumber(vs) >= 2022, "opencv requires Visual Studio 2022 and later for arm targets")
        end)
    end

I wish we could limit Windows SDK that being used by 2022 worker to be newer version (on_check should assert newer Windows SDK version) and at same time keep mine workaround (resolve https://github.com/xmake-io/xmake-repo/actions/runs/14924396539/job/41926501716#step:5:1292), and find fix links (add_links, linkdirs) for Windows-11 arm worker at same time. This looks tough to limit arm64 ones.
Though I'm open to suggestions.
I tried to filter out Windows-11 arm and 2022 workers by vs sdkver, but it seems it stopped Windows-11 arm worker as well (Im 100% sure they use same SDK version 10.0.26100.0, the only difference is bin\Host), I suppose there is some way to differ them, so vs sdkver is only what comes up, need to skip 2022 worker, but Windows-11 arm worker should build & work.
Yet, need to fix links so Windows-11 arm worker would succeed.

@luadebug luadebug requested review from star-hengxing and xq114 May 12, 2025 01:40
Copy link
Contributor

@luadebug luadebug left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix links for Windows-11 arm worker I guess so.

 -- Installing: C:/Users/runneradmin/AppData/Local/.xmake/packages/o/opencv/4.11.0/8de6f34073014aeda5283f5b8d6a5e84/x64/vc17/staticlib/opencv_objdetect4110.lib

@luadebug
Copy link
Contributor

luadebug commented May 13, 2025

Since you added -DAARCH64=ON should it be now aarch64 instead of "ARM64"? (in case of arm only)

Afaik -DARM in case of arm but not arm64 for arm64 it is -DAARCH64

Could it be linked with elseif package:is_arch("arm64") then arch = "ARM64" line 255?

I resolved this by os.trymv but now executable opencv version issues for /MT vs runtime... This one is Im unsure what to do next.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants