I had some trouble attempting to build an XCFramework of OpenCV, but I finally got a successful build with the following steps:
- Clone the OpenCV repo:
git clone [email protected]:opencv/opencv.git cd opencv
- Disable building with
libjpeg-turbo
1 by applying a patch:git apply path/to/opencv-5-28-50.patch
- Make a build directory adjacent to the
opencv
repocd .. mkdir build-opencv cd build-opencv
- Build the xcframework (change these options per your requirements):
python3 ../opencv/platforms/apple/build_xcframework.py \ --out . \ --without=video \ --iphoneos_archs=arm64 \ --iphonesimulator_archs x86_64,arm64 \ --iphoneos_deployment_target=13 \ --disable-bitcode \ --build_only_specified_archs
If all goes well, after quite some time you should see:
============================================================
Finished building ./opencv2.xcframework
============================================================
Footnotes
-
Why do we need to disable
libjpeg-turbo
? Because it fails to build during the configuration stage with:-- libjpeg-turbo: VERSION = 2.1.2, BUILD = opencv-4.6.0-libjpeg-turbo -- Check size of size_t CMake Error at /usr/local/share/cmake-3.25/Modules/CheckTypeSize.cmake:148 (try_compile): Cannot copy output executable '' to destination specified by COPY_FILE: 'opencv-build-ios13/iphoneos/build/build-arm64-iphoneos/CMakeFiles/CheckTypeSize/SIZEOF_SIZE_T.bin' Recorded try_compile output location doesn't exist: opencv-build-ios13/iphoneos/build/build-arm64-iphoneos/CMakeFiles/CMakeScratch/TryCompile-kqujHS/Release/cmTC_afcdc.app/cmTC_afcdc Call Stack (most recent call first): /usr/local/share/cmake-3.25/Modules/CheckTypeSize.cmake:278 (__check_type_size_impl) 3rdparty/libjpeg-turbo/CMakeLists.txt:25 (check_type_size) -- Check size of unsigned long CMake Error at /usr/local/share/cmake-3.25/Modules/CheckTypeSize.cmake:148 (try_compile): Cannot copy output executable '' to destination specified by COPY_FILE: 'opencv-build-ios13/iphoneos/build/build-arm64-iphoneos/CMakeFiles/CheckTypeSize/SIZEOF_UNSIGNED_LONG.bin' Recorded try_compile output location doesn't exist: opencv-build-ios13/iphoneos/build/build-arm64-iphoneos/CMakeFiles/CMakeScratch/TryCompile-Mi900g/Release/cmTC_68bed.app/cmTC_68bed Call Stack (most recent call first): /usr/local/share/cmake-3.25/Modules/CheckTypeSize.cmake:278 (__check_type_size_impl) 3rdparty/libjpeg-turbo/CMakeLists.txt:26 (check_type_size)
I'm not sure what the consequences are of this change, so use at your own risk! ↩
By reading the following
opencv/opencv#21571
https://github.com/opencv/opencv/tree/4.x/platforms/apple
The above problem is solved by
The
--iphonesimulator_archs=amr64
was--iphonesimulator_archs x86_64,arm64
. The difference is using=
and only specifyingarm64
.