Skip to content

Commit

Permalink
Support visionos build (microsoft#20365)
Browse files Browse the repository at this point in the history
### 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:
microsoft#19313

---------

Co-authored-by: rachguo <[email protected]>
Co-authored-by: rachguo <[email protected]>
  • Loading branch information
3 people authored Apr 24, 2024
1 parent 4ce7bbf commit 14fcf0a
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmake/adjust_global_compile_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions cmake/onnxruntime_ios.toolchain.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions cmake/onnxruntime_providers_cpu.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
2 changes: 1 addition & 1 deletion cmake/onnxruntime_python.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 12 additions & 0 deletions cmake/onnxruntime_visionos.toolchain.cmake
Original file line number Diff line number Diff line change
@@ -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")
2 changes: 1 addition & 1 deletion onnxruntime/core/common/cpuid_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
31 changes: 24 additions & 7 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,9 +397,10 @@ def convert_arg_line_to_args(self, arg_line):
)
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.",
Expand All @@ -413,6 +414,11 @@ def convert_arg_line_to_args(self, arg_line):
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"
)
Expand Down Expand Up @@ -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/":
Expand Down Expand Up @@ -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')."
)
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
]
}
}

0 comments on commit 14fcf0a

Please sign in to comment.