0
0
mirror of https://github.com/opencv/opencv.git synced 2026-01-18 17:21:42 +01:00

Merge pull request #24136 from komakai:visionos_support

Add experimental support for Apple VisionOS platform #24136

### Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

- [x] I agree to contribute to the project under Apache 2 License.
- [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV
- [x] The PR is proposed to the proper branch

This is dependent on cmake support for VisionOs which is currently in progress.
Creating PR now to test that there are no regressions in iOS and macOS builds
This commit is contained in:
Giles Payne
2023-12-20 21:35:10 +09:00
committed by GitHub
parent abbd878eb5
commit 3d9cb5329c
19 changed files with 325 additions and 100 deletions

View File

@@ -31,6 +31,8 @@ if __name__ == "__main__":
parser.add_argument('--framework_name', default='opencv2', help='Name of OpenCV xcframework (default: opencv2, will change to OpenCV in future version)')
parser.add_argument('--iphoneos_archs', default=None, help='select iPhoneOS target ARCHS. Default is "armv7,arm64"')
parser.add_argument('--iphonesimulator_archs', default=None, help='select iPhoneSimulator target ARCHS. Default is "x86_64,arm64"')
parser.add_argument('--visionos_archs', default=None, help='select visionOS target ARCHS. Default is "arm64"')
parser.add_argument('--visionsimulator_archs', default=None, help='select visionSimulator target ARCHS. Default is "arm64"')
parser.add_argument('--macos_archs', default=None, help='Select MacOS ARCHS. Default is "x86_64,arm64"')
parser.add_argument('--catalyst_archs', default=None, help='Select Catalyst ARCHS. Default is "x86_64,arm64"')
parser.add_argument('--build_only_specified_archs', default=False, action='store_true', help='if enabled, only directly specified archs are built and defaults are ignored')
@@ -52,6 +54,13 @@ if __name__ == "__main__":
iphonesimulator_archs = "x86_64,arm64"
print('Using iPhoneSimulator ARCHS={}'.format(iphonesimulator_archs))
# Parse architectures from args
visionos_archs = args.visionos_archs
print('Using visionOS ARCHS={}'.format(visionos_archs))
visionsimulator_archs = args.visionsimulator_archs
print('Using visionSimulator ARCHS={}'.format(visionsimulator_archs))
macos_archs = args.macos_archs
if not macos_archs and not args.build_only_specified_archs:
# Supply defaults
@@ -70,6 +79,7 @@ if __name__ == "__main__":
# Phase 1: build .frameworks for each platform
osx_script_path = os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../osx/build_framework.py')
ios_script_path = os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../ios/build_framework.py')
visionos_script_path = os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../ios/build_visionos_framework.py')
build_folders = []
@@ -91,6 +101,19 @@ if __name__ == "__main__":
command = ["python3", ios_script_path, build_folder, "--iphonesimulator_archs", iphonesimulator_archs, "--framework_name", args.framework_name, "--build_only_specified_archs"] + unknown_args
print_header("Building iPhoneSimulator frameworks")
execute(command, cwd=os.getcwd())
if visionos_archs:
build_folder = get_or_create_build_folder(args.out, "visionos")
build_folders.append(build_folder)
command = ["python3", visionos_script_path, build_folder, "--visionos_archs", visionos_archs, "--framework_name", args.framework_name, "--build_only_specified_archs"] + unknown_args
print_header("Building visionOS frameworks")
print(command)
execute(command, cwd=os.getcwd())
if visionsimulator_archs:
build_folder = get_or_create_build_folder(args.out, "visionsimulator")
build_folders.append(build_folder)
command = ["python3", visionos_script_path, build_folder, "--visionsimulator_archs", visionsimulator_archs, "--framework_name", args.framework_name, "--build_only_specified_archs"] + unknown_args
print_header("Building visionSimulator frameworks")
execute(command, cwd=os.getcwd())
if macos_archs:
build_folder = get_or_create_build_folder(args.out, "macos")
build_folders.append(build_folder)

View File

@@ -254,9 +254,9 @@ class Builder:
toolchain = self.getToolchain(arch, target)
cmakecmd = self.getCMakeArgs(arch, target) + \
(["-DCMAKE_TOOLCHAIN_FILE=%s" % toolchain] if toolchain is not None else [])
if target.lower().startswith("iphoneos"):
if target.lower().startswith("iphoneos") or target.lower().startswith("xros"):
cmakecmd.append("-DCPU_BASELINE=DETECT")
if target.lower().startswith("iphonesimulator"):
if target.lower().startswith("iphonesimulator") or target.lower().startswith("xrsimulator"):
build_arch = check_output(["uname", "-m"]).decode('utf-8').rstrip()
if build_arch != arch:
print("build_arch (%s) != arch (%s)" % (build_arch, arch))
@@ -341,7 +341,7 @@ class Builder:
def makeDynamicLib(self, builddir):
target = builddir[(builddir.rfind("build-") + 6):]
target_platform = target[(target.rfind("-") + 1):]
is_device = target_platform == "iphoneos" or target_platform == "catalyst"
is_device = target_platform == "iphoneos" or target_platform == "visionos" or target_platform == "catalyst"
framework_dir = os.path.join(builddir, "install", "lib", self.framework_name + ".framework")
if not os.path.exists(framework_dir):
os.makedirs(framework_dir)
@@ -379,7 +379,7 @@ class Builder:
"-framework", "CoreImage", "-framework", "CoreMedia", "-framework", "QuartzCore",
"-framework", "Accelerate", "-framework", "OpenCL",
]
elif target_platform == "iphoneos" or target_platform == "iphonesimulator":
elif target_platform == "iphoneos" or target_platform == "iphonesimulator" or target_platform == "xros" or target_platform == "xrsimulator":
framework_options = [
"-iframework", "%s/System/iOSSupport/System/Library/Frameworks" % sdk_dir,
"-framework", "AVFoundation", "-framework", "CoreGraphics",

View File

@@ -0,0 +1,116 @@
#!/usr/bin/env python
"""
The script builds OpenCV.framework for visionOS.
"""
from __future__ import print_function
import os, os.path, sys, argparse, traceback, multiprocessing
# import common code
# sys.path.insert(0, os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../ios'))
from build_framework import Builder
sys.path.insert(0, os.path.abspath(os.path.abspath(os.path.dirname(__file__))+'/../apple'))
from cv_build_utils import print_error, get_cmake_version
XROS_DEPLOYMENT_TARGET='1.0' # default, can be changed via command line options or environment variable
class visionOSBuilder(Builder):
def checkCMakeVersion(self):
assert get_cmake_version() >= (3, 17), "CMake 3.17 or later is required. Current version is {}".format(get_cmake_version())
def getObjcTarget(self, target):
return 'visionos'
def getToolchain(self, arch, target):
toolchain = os.path.join(self.opencv, "platforms", "ios", "cmake", "Toolchains", "Toolchain-%s_Xcode.cmake" % target)
return toolchain
def getCMakeArgs(self, arch, target):
args = Builder.getCMakeArgs(self, arch, target)
args = args + [
'-DVISIONOS_ARCH=%s' % arch
]
return args
def getBuildCommand(self, arch, target):
buildcmd = [
"xcodebuild",
"XROS_DEPLOYMENT_TARGET=" + os.environ['XROS_DEPLOYMENT_TARGET'],
"ARCHS=%s" % arch,
"-sdk", target.lower(),
"-configuration", "Debug" if self.debug else "Release",
"-parallelizeTargets",
"-jobs", str(multiprocessing.cpu_count())
]
return buildcmd
def getInfoPlist(self, builddirs):
return os.path.join(builddirs[0], "visionos", "Info.plist")
if __name__ == "__main__":
folder = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]), "../.."))
parser = argparse.ArgumentParser(description='The script builds OpenCV.framework for visionOS.')
# TODO: When we can make breaking changes, we should make the out argument explicit and required like in build_xcframework.py.
parser.add_argument('out', metavar='OUTDIR', help='folder to put built framework')
parser.add_argument('--opencv', metavar='DIR', default=folder, help='folder with opencv repository (default is "../.." relative to script location)')
parser.add_argument('--contrib', metavar='DIR', default=None, help='folder with opencv_contrib repository (default is "None" - build only main framework)')
parser.add_argument('--without', metavar='MODULE', default=[], action='append', help='OpenCV modules to exclude from the framework. To exclude multiple, specify this flag again, e.g. "--without video --without objc"')
parser.add_argument('--disable', metavar='FEATURE', default=[], action='append', help='OpenCV features to disable (add WITH_*=OFF). To disable multiple, specify this flag again, e.g. "--disable tbb --disable openmp"')
parser.add_argument('--dynamic', default=False, action='store_true', help='build dynamic framework (default is "False" - builds static framework)')
parser.add_argument('--enable_nonfree', default=False, dest='enablenonfree', action='store_true', help='enable non-free modules (disabled by default)')
parser.add_argument('--visionos_deployment_target', default=os.environ.get('XROS_DEPLOYMENT_TARGET', XROS_DEPLOYMENT_TARGET), help='specify XROS_DEPLOYMENT_TARGET')
parser.add_argument('--visionos_archs', default=None, help='select visionOS target ARCHS. Default is none')
parser.add_argument('--visionsimulator_archs', default=None, help='select visionSimulator target ARCHS. Default is none')
parser.add_argument('--debug', action='store_true', help='Build "Debug" binaries (CMAKE_BUILD_TYPE=Debug)')
parser.add_argument('--debug_info', action='store_true', help='Build with debug information (useful for Release mode: BUILD_WITH_DEBUG_INFO=ON)')
parser.add_argument('--framework_name', default='opencv2', dest='framework_name', help='Name of OpenCV framework (default: opencv2, will change to OpenCV in future version)')
parser.add_argument('--legacy_build', default=False, dest='legacy_build', action='store_true', help='Build legacy framework (default: False, equivalent to "--framework_name=opencv2 --without=objc")')
parser.add_argument('--run_tests', default=False, dest='run_tests', action='store_true', help='Run tests')
parser.add_argument('--build_docs', default=False, dest='build_docs', action='store_true', help='Build docs')
parser.add_argument('--disable-swift', default=False, dest='swiftdisabled', action='store_true', help='Disable building of Swift extensions')
args, unknown_args = parser.parse_known_args()
if unknown_args:
print("The following args are not recognized and will not be used: %s" % unknown_args)
os.environ['XROS_DEPLOYMENT_TARGET'] = args.visionos_deployment_target
print('Using XROS_DEPLOYMENT_TARGET=' + os.environ['XROS_DEPLOYMENT_TARGET'])
visionos_archs = None
if args.visionos_archs:
visionos_archs = args.visionos_archs.split(',')
print('Using visionOS ARCHS=' + str(visionos_archs))
visionsimulator_archs = None
if args.visionsimulator_archs:
visionsimulator_archs = args.visionsimulator_archs.split(',')
print('Using visionOS ARCHS=' + str(visionsimulator_archs))
# Prevent the build from happening if the same architecture is specified for multiple platforms.
# When `lipo` is run to stitch the frameworks together into a fat framework, it'll fail, so it's
# better to stop here while we're ahead.
if visionos_archs and visionsimulator_archs:
duplicate_archs = set(visionos_archs).intersection(visionsimulator_archs)
if duplicate_archs:
print_error("Cannot have the same architecture for multiple platforms in a fat framework! Consider using build_xcframework.py in the apple platform folder instead. Duplicate archs are %s" % duplicate_archs)
exit(1)
if args.legacy_build:
args.framework_name = "opencv2"
if not "objc" in args.without:
args.without.append("objc")
targets = []
if not visionos_archs and not visionsimulator_archs:
print_error("--visionos_archs and --visionsimulator_archs are undefined; nothing will be built.")
sys.exit(1)
if visionos_archs:
targets.append((visionos_archs, "XROS"))
if visionsimulator_archs:
targets.append((visionsimulator_archs, "XRSimulator")),
b = visionOSBuilder(args.opencv, args.contrib, args.dynamic, True, args.without, args.disable, args.enablenonfree, targets, args.debug, args.debug_info, args.framework_name, args.run_tests, args.build_docs, args.swiftdisabled)
b.build(args.out)

View File

@@ -0,0 +1,5 @@
message(STATUS "Setting up visionOS toolchain for VISIONOS_ARCH='${VISIONOS_ARCH}'")
set(VISIONOS TRUE)
set(XROS 1)
include(${CMAKE_CURRENT_LIST_DIR}/common-ios-toolchain.cmake)
message(STATUS "visionOS toolchain loaded")

View File

@@ -0,0 +1,5 @@
message(STATUS "Setting up visionSimulator toolchain for VISIONOS_ARCH='${VISIONOS_ARCH}'")
set(VISIONSIMULATOR TRUE)
set(XROS 1)
include(${CMAKE_CURRENT_LIST_DIR}/common-ios-toolchain.cmake)
message(STATUS "visionSimulator toolchain loaded")

View File

@@ -61,26 +61,45 @@ else()
endmacro()
endif() # IN_TRY_COMPILE
if(NOT DEFINED IOS_ARCH)
if((IPHONEOS OR IPHONESIMULATOR) AND NOT DEFINED IOS_ARCH)
message(FATAL_ERROR "iOS toolchain requires ARCH option for proper configuration of compiler flags")
endif()
if(IOS_ARCH MATCHES "^arm64")
if((VISIONOS OR VISIONSIMULATOR) AND NOT DEFINED VISIONOS_ARCH)
message(FATAL_ERROR "visionOS toolchain requires ARCH option for proper configuration of compiler flags")
endif()
if((IOS_ARCH MATCHES "^arm64") OR (VISIONOS_ARCH MATCHES "^arm64"))
set(AARCH64 1)
elseif(IOS_ARCH MATCHES "^armv")
set(ARM 1)
elseif(IOS_ARCH MATCHES "^x86_64")
elseif((IOS_ARCH MATCHES "^x86_64") OR (VISIONOS_ARCH MATCHES "^x86_64"))
set(X86_64 1)
elseif(IOS_ARCH MATCHES "^i386")
set(X86 1)
else()
message(FATAL_ERROR "iOS toolchain doesn't recognize ARCH='${IOS_ARCH}' value")
if(IPHONEOS OR IPHONESIMULATOR)
message(FATAL_ERROR "invalid value of IOS_ARCH='${IOS_ARCH}'")
elseif(VISIONOS OR VISIONSIMULATOR)
message(FATAL_ERROR "invalid value of VISIONOS_ARCH='${VISIONOS_ARCH}'")
endif()
endif()
if(NOT DEFINED CMAKE_OSX_SYSROOT)
if(IPHONEOS)
set(CMAKE_OSX_SYSROOT "iphoneos")
set(SYSTEM "iOS")
set(ARCH "${IOS_ARCH}")
elseif(IPHONESIMULATOR)
set(CMAKE_OSX_SYSROOT "iphonesimulator")
set(SYSTEM "iOS")
set(ARCH "${IOS_ARCH}")
elseif(VISIONOS)
set(CMAKE_OSX_SYSROOT "xros")
set(SYSTEM "visionOS")
set(ARCH "${VISIONOS_ARCH}")
elseif(VISIONSIMULATOR)
set(CMAKE_OSX_SYSROOT "xrsimulator")
set(SYSTEM "visionOS")
set(ARCH "${VISIONOS_ARCH}")
elseif(MAC_CATALYST)
# Use MacOS SDK for Catalyst builds
set(CMAKE_OSX_SYSROOT "macosx")
@@ -90,14 +109,25 @@ set(CMAKE_MACOSX_BUNDLE YES)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED "NO")
if(APPLE_FRAMEWORK AND NOT BUILD_SHARED_LIBS)
set(CMAKE_OSX_ARCHITECTURES "${IOS_ARCH}" CACHE INTERNAL "Build architecture for iOS" FORCE)
set(CMAKE_OSX_ARCHITECTURES "${ARCH}" CACHE INTERNAL "Build architecture for iOS/visionOS" FORCE)
endif()
if(NOT DEFINED IPHONEOS_DEPLOYMENT_TARGET AND NOT MAC_CATALYST)
if((IPHONEOS OR IPHONESIMULATOR) AND NOT DEFINED IPHONEOS_DEPLOYMENT_TARGET)
if(NOT DEFINED ENV{IPHONEOS_DEPLOYMENT_TARGET})
message(FATAL_ERROR "IPHONEOS_DEPLOYMENT_TARGET is not specified")
endif()
set(IPHONEOS_DEPLOYMENT_TARGET "$ENV{IPHONEOS_DEPLOYMENT_TARGET}")
set(DEPLOYMENT_TARGET "${IPHONEOS_DEPLOYMENT_TARGET}")
set(DEPLOYMENT_TARGET_CMDLINE "IPHONEOS_DEPLOYMENT_TARGET=${IPHONEOS_DEPLOYMENT_TARGET}")
endif()
if((VISIONOS OR VISIONSIMULATOR) AND NOT DEFINED XROS_DEPLOYMENT_TARGET)
if(NOT DEFINED ENV{XROS_DEPLOYMENT_TARGET})
message(FATAL_ERROR "XROS_DEPLOYMENT_TARGET is not specified")
endif()
set(XROS_DEPLOYMENT_TARGET "$ENV{XROS_DEPLOYMENT_TARGET}")
set(DEPLOYMENT_TARGET "${XROS_DEPLOYMENT_TARGET}")
set(DEPLOYMENT_TARGET_CMDLINE "XROS_DEPLOYMENT_TARGET=${XROS_DEPLOYMENT_TARGET}")
endif()
if(NOT __IN_TRY_COMPILE)
@@ -124,9 +154,9 @@ if(NOT __IN_TRY_COMPILE)
message(FATAL_ERROR "Can't prepare xcodebuild_wrapper")
endif()
if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} IPHONEOS_DEPLOYMENT_TARGET=${IPHONEOS_DEPLOYMENT_TARGET} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO -sdk ${CMAKE_OSX_SYSROOT}")
set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} ${DEPLOYMENT_TARGET_CMDLINE} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO -sdk ${CMAKE_OSX_SYSROOT}")
else()
set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} IPHONEOS_DEPLOYMENT_TARGET=${IPHONEOS_DEPLOYMENT_TARGET} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO ARCHS=${IOS_ARCH} -sdk ${CMAKE_OSX_SYSROOT}")
set(XCODEBUILD_EXTRA_ARGS "${XCODEBUILD_EXTRA_ARGS} ${DEPLOYMENT_TARGET_CMDLINE} CODE_SIGN_IDENTITY='' CODE_SIGNING_REQUIRED=NO ARCHS=${ARCH} -sdk ${CMAKE_OSX_SYSROOT}")
endif()
configure_file("${CMAKE_CURRENT_LIST_DIR}/xcodebuild_wrapper.in" "${_xcodebuild_wrapper_tmp}" @ONLY)
file(COPY "${_xcodebuild_wrapper_tmp}" DESTINATION ${CMAKE_BINARY_DIR} FILE_PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE)
@@ -137,16 +167,16 @@ if(NOT __IN_TRY_COMPILE)
endif()
# Standard settings
set(CMAKE_SYSTEM_NAME iOS)
set(CMAKE_SYSTEM_NAME "${SYSTEM}")
# Apple Framework settings
if(APPLE_FRAMEWORK AND BUILD_SHARED_LIBS)
set(CMAKE_SYSTEM_VERSION "${IPHONEOS_DEPLOYMENT_TARGET}")
set(CMAKE_SYSTEM_VERSION "${DEPLOYMENT_TARGET}")
set(CMAKE_C_SIZEOF_DATA_PTR 4)
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
else()
set(CMAKE_SYSTEM_VERSION "${IPHONEOS_DEPLOYMENT_TARGET}")
set(CMAKE_SYSTEM_PROCESSOR "${IOS_ARCH}")
set(CMAKE_SYSTEM_VERSION "${DEPLOYMENT_TARGET}")
set(CMAKE_SYSTEM_PROCESSOR "${ARCH}")
if(AARCH64 OR X86_64)
set(CMAKE_C_SIZEOF_DATA_PTR 8)
@@ -190,4 +220,4 @@ if(NOT CMAKE_FIND_ROOT_PATH_MODE_PROGRAM)
set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER)
endif()
toolchain_save_config(IOS_ARCH IPHONEOS_DEPLOYMENT_TARGET)
toolchain_save_config(IOS_ARCH VISIONOS_ARCH ARCH DEPLOYMENT_TARGET)