Build OpenCV Contrib as a .xcframework, for iOS, on a M1

Context

We needed an OpenCV Contrib module in an iOS app. When reviewing options, it was decided we would expose a Swift package, wrapping an Objective-C wrapper, that would itself be the one importing OpenCV.

This meant exposing OpenCV as a Swift Package Manager binary target. Which requires us to build a .xcframework.

Goal

Build a .xcframework that we can run on both OS devices and Intel & Apple M1 Mac iPhone / iPad Simulators. Why a .xcframework and not simply a .framework? Because while Swift Package Manager can import binaries, it can only import .xcframework ones, not .framework ones.

Requirements

  • cmake (installable using brew)
  • python3 (because python2.7 is obsolete) (installable using brew)
  • opencv source (via git clone https://github.com/opencv/opencv.git)
  • opencv_contrib source (via git clone https://github.com/opencv/opencv_contrib.git)