Support visionos build (#20365)

### Description
<!-- Describe your changes. -->

This PR supports a build of onnxruntime.xcframework for xros/xrsimulator
for visionos via the build command of

`python3 tools/ci_build/github/apple/build_apple_framework.py --config
Release/Debug
tools/ci_build/github/apple/default_vision_os_framework_build_settings.json`.

For officially include visionos in ios cocoapods package and testing in
CI, would require separate work for upgrading the Xcode version &
upgrade macOS CI agent to macos-13-arm64 or higher.

### Motivation and Context
<!-- - Why is this change required? What problem does it solve?
- If it fixes an open issue, please link to the issue here. -->

visionos support:
https://github.com/microsoft/onnxruntime/discussions/19313

---------

Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net>
Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
This commit is contained in:
Rachel Guo 2024-04-23 18:15:07 -07:00 committed by GitHub
parent 4ce7bbf6f1
commit 14fcf0a52d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 74 additions and 16 deletions

View file

@ -1680,8 +1680,8 @@ if (onnxruntime_USE_WINML)
endif() # if (onnxruntime_USE_WINML)
if (onnxruntime_BUILD_SHARED_LIB OR onnxruntime_BUILD_APPLE_FRAMEWORK)
if (onnxruntime_BUILD_APPLE_FRAMEWORK AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS")
message(FATAL_ERROR "onnxruntime_BUILD_APPLE_FRAMEWORK can only be enabled for macOS or iOS.")
if (onnxruntime_BUILD_APPLE_FRAMEWORK AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS")
message(FATAL_ERROR "onnxruntime_BUILD_APPLE_FRAMEWORK can only be enabled for macOS or iOS or visionOS.")
endif()
list(APPEND ONNXRUNTIME_CMAKE_FILES onnxruntime)
endif()

View file

@ -204,9 +204,9 @@ if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
endif()
endif()
# Mark symbols to be invisible, for macOS/iOS target only
# Mark symbols to be invisible, for macOS/iOS/visionOS target only
# Due to many dependencies have different symbol visibility settings, set global compile flags here.
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS")
if (${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS")
foreach(flags CMAKE_CXX_FLAGS CMAKE_OBJC_FLAGS CMAKE_OBJCXX_FLAGS)
string(APPEND ${flags} " -fvisibility=hidden -fvisibility-inlines-hidden")
endforeach()

View file

@ -2,6 +2,7 @@
# Licensed under the MIT License.
set(CMAKE_SYSTEM_NAME iOS)
if (NOT DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM AND NOT DEFINED CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED NO)
endif()

View file

@ -223,7 +223,7 @@ set_target_properties(onnxruntime_providers PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(onnxruntime_providers PROPERTIES FOLDER "ONNXRuntime")
if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS"
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
file(GLOB onnxruntime_providers_shared_cc_srcs CONFIGURE_DEPENDS
@ -273,4 +273,4 @@ if (NOT onnxruntime_BUILD_SHARED_LIB)
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
FRAMEWORK DESTINATION ${CMAKE_INSTALL_BINDIR})
endif()
endif()

View file

@ -693,7 +693,7 @@ if (onnxruntime_ENABLE_EXTERNAL_CUSTOM_OP_SCHEMAS)
endif()
if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS"
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android"
AND NOT onnxruntime_USE_ROCM
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten")

View file

@ -1727,7 +1727,7 @@ endif()
# limit to only test on windows first, due to a runtime path issue on linux
if (NOT onnxruntime_MINIMAL_BUILD AND NOT onnxruntime_EXTENDED_MINIMAL_BUILD
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS"
AND NOT ${CMAKE_SYSTEM_NAME} MATCHES "Darwin|iOS|visionOS"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Android"
AND NOT CMAKE_SYSTEM_NAME STREQUAL "Emscripten"
AND NOT onnxruntime_USE_ROCM)

View file

@ -0,0 +1,12 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
set(CMAKE_SYSTEM_NAME visionOS)
set(CMAKE_SYSTEM_PROCESSOR arm64)
if (NOT DEFINED CMAKE_XCODE_ATTRIBUTE_DEVELOPMENT_TEAM AND NOT DEFINED CMAKE_XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY)
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED NO)
endif()
SET(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_MODULES "YES")
SET(CMAKE_XCODE_ATTRIBUTE_CLANG_ENABLE_OBJC_ARC "YES")

View file

@ -123,7 +123,7 @@ class CPUIDInfo {
void X86Init();
#elif defined(CPUIDINFO_ARCH_ARM)
// Now the following var is only used in ARM build, but later one we may expand the usage.
bool pytorch_cpuinfo_init_{false};
[[maybe_unused]] bool pytorch_cpuinfo_init_{false};
#endif
#ifdef __linux__

View file

@ -397,9 +397,10 @@ def parse_arguments():
)
parser.add_argument("--gdk_platform", default="Scarlett", help="Sets the GDK target platform.")
parser.add_argument("--ios", action="store_true", help="build for ios")
parser.add_argument(
platform_group = parser.add_mutually_exclusive_group()
platform_group.add_argument("--ios", action="store_true", help="build for ios")
platform_group.add_argument("--visionos", action="store_true", help="build for visionOS")
platform_group.add_argument(
"--macos",
choices=["MacOSX", "Catalyst"],
help="Specify the target platform for macOS build. Only specify this argument when --build_apple_framework is present.",
@ -413,6 +414,11 @@ def parse_arguments():
default="",
help="Path to ios toolchain file, or cmake/onnxruntime_ios.toolchain.cmake will be used",
)
parser.add_argument(
"--visionos_toolchain_file",
default="",
help="Path to visionos toolchain file, or cmake/onnxruntime_visionos.toolchain.cmake will be used",
)
parser.add_argument(
"--xcode_code_signing_team_id", default="", help="The development team ID used for code signing in Xcode"
)
@ -902,7 +908,7 @@ def use_dev_mode(args):
return False
if args.use_armnn:
return False
if args.ios and is_macOS():
if (args.ios or args.visionos) and is_macOS():
return False
SYSTEM_COLLECTIONURI = os.getenv("SYSTEM_COLLECTIONURI") # noqa: N806
if SYSTEM_COLLECTIONURI and SYSTEM_COLLECTIONURI != "https://dev.azure.com/onnxruntime/":
@ -1327,12 +1333,12 @@ def generate_build_tree(
if args.use_snpe:
cmake_args += ["-Donnxruntime_USE_SNPE=ON"]
if args.macos or args.ios:
if args.macos or args.ios or args.visionos:
# Note: Xcode CMake generator doesn't have a good support for Mac Catalyst yet.
if args.macos == "Catalyst" and args.cmake_generator == "Xcode":
raise BuildError("Xcode CMake generator ('--cmake_generator Xcode') doesn't support Mac Catalyst build.")
if (args.ios or args.macos == "MacOSX") and not args.cmake_generator == "Xcode":
if (args.ios or args.visionos or args.macos == "MacOSX") and not args.cmake_generator == "Xcode":
raise BuildError(
"iOS/MacOS framework build requires use of the Xcode CMake generator ('--cmake_generator Xcode')."
)
@ -1381,6 +1387,17 @@ def generate_build_tree(
f"-DCMAKE_CC_FLAGS=--target={macabi_target}",
f"-DCMAKE_CC_FLAGS_RELEASE=-O3 -DNDEBUG --target={macabi_target}",
]
if args.visionos:
cmake_args += [
"-DCMAKE_SYSTEM_NAME=visionOS",
"-DCMAKE_TOOLCHAIN_FILE="
+ (
args.visionos_toolchain_file
if args.visionos_toolchain_file
else "../cmake/onnxruntime_visionos.toolchain.cmake"
),
"-Donnxruntime_ENABLE_CPUINFO=OFF",
]
if args.build_wasm:
emsdk_dir = os.path.join(cmake_dir, "external", "emsdk")
@ -2766,7 +2783,7 @@ def main():
if is_macOS():
if (
not args.ios
not (args.ios or args.visionos)
and args.macos != "Catalyst"
and not args.android
and args.osx_arch == "arm64"

View file

@ -0,0 +1,28 @@
{
"build_osx_archs": {
"xros": [
"arm64"
],
"xrsimulator": [
"arm64"
]
},
"build_params": {
"base": [
"--parallel",
"--build_apple_framework",
"--use_xcode",
"--use_coreml",
"--skip_tests",
"--cmake_extra_defines=onnxruntime_BUILD_UNIT_TESTS=OFF"
],
"xros": [
"--visionos",
"--apple_deploy_target=1.0"
],
"xrsimulator": [
"--visionos",
"--apple_deploy_target=1.0"
]
}
}