Skip to content

Instantly share code, notes, and snippets.

@humblehacker
Last active September 6, 2024 09:46
Show Gist options
  • Save humblehacker/6a2e38072b0e237de20ba6d1f2efc80f to your computer and use it in GitHub Desktop.
Save humblehacker/6a2e38072b0e237de20ba6d1f2efc80f to your computer and use it in GitHub Desktop.
Make opencv2.xcframework for iOS arm64, and iOSSimulator arm64 & x86_64

I had some trouble attempting to build an XCFramework of OpenCV, but I finally got a successful build with the following steps:

  1. Clone the OpenCV repo:
    git clone [email protected]:opencv/opencv.git
    cd opencv
    
  2. Disable building with libjpeg-turbo1 by applying a patch:
    git apply path/to/opencv-5-28-50.patch
  3. Make a build directory adjacent to the opencv repo
    cd ..
    mkdir build-opencv
    cd build-opencv
  4. 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

  1. 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!

diff --git forkSrcPrefix/platforms/ios/build_framework.py forkDstPrefix/platforms/ios/build_framework.py
index 08d9fd63dc8e2cbc77358b2e5f92e1ad2672b890..26d89b47a90823a03b490257e4674bdc40d03e46 100755
--- forkSrcPrefix/platforms/ios/build_framework.py
+++ forkDstPrefix/platforms/ios/build_framework.py
@@ -106,7 +106,7 @@ class Builder:
main_build_dir = self.getBuildDir(main_working_dir, target)
dirs.append(main_build_dir)
- cmake_flags = []
+ cmake_flags = ["-DBUILD_JPEG_TURBO_DISABLE=ON"]
if self.contrib:
cmake_flags.append("-DOPENCV_EXTRA_MODULES_PATH=%s" % self.contrib)
if xcode_ver >= 7 and target[1] == 'iPhoneOS' and self.bitcodedisabled == False:
@huyaoyu
Copy link

huyaoyu commented Jan 22, 2024

By reading the following
opencv/opencv#21571
https://github.com/opencv/opencv/tree/4.x/platforms/apple

The above problem is solved by

python3 ../opencv/platforms/apple/build_xcframework.py \
  --out . \
  --without=video \
  --iphoneos_archs=arm64 \
  --iphonesimulator_archs=arm64 \
  --iphoneos_deployment_target=17 \
  --disable-bitcode \
  --build_only_specified_archs

The --iphonesimulator_archs=amr64 was --iphonesimulator_archs x86_64,arm64. The difference is using = and only specifying arm64.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment