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! ↩
Thank you for sharing this. By following your instructions, I successfully compile the framework on a M3 chip machine. I need to make some simple tweaks to make it work in my setting. I'm using OpenCV 4.9.0, Swift 5.9, and Xcode 15. I encountered some python error and Swift compiler not found error.
To resolve those errors and in addition to your patch, I need to do the following patch. The solution comes from
In my case, I'm compiling for iOS 17, so I use
--iphoneos_deployment_target=17
in the command line. The compilation finished without error.However, I find that the compiled framework works if I choose a real iPhone as the target but leads to compilation failure for the simulator. It is probably more appropriate to ask the OpenCV developers but I figured that I can share with you to see if you have a quick fix. The error message itself is not very informative by just saying
/Users/yaoyuh/Library/Developer/Xcode/DerivedData/test-opencv-hirjnpdhjxoecfejzywvtbryrfik/Build/Products/Debug-iphonesimulator/opencv2.framework/Modules/opencv2.swiftmodule/arm64-apple-ios-simulator.private.swiftinterface:10:19 Could not build Objective-C module 'opencv2'
and/Users/yaoyuh/Library/Developer/Xcode/DerivedData/test-opencv-hirjnpdhjxoecfejzywvtbryrfik/Build/Products/Debug-iphonesimulator/opencv2.framework/Headers/opencv2-Swift.h:557:2 Unsupported Swift architecture
Any help would be appreciated, thank you!