Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support xcframework for mac catalyst builds. #19534

Merged
merged 24 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
# NOTE: POSITION INDEPENDENT CODE hurts performance, and it only make sense on POSIX systems
set(CMAKE_POSITION_INDEPENDENT_CODE ON)

# Suggested by https://gitlab.kitware.com/cmake/cmake/-/issues/20132
# MacCatalyst does not support well in CMake
# The error that can emerge without this flag looks like:
# "clang : error : overriding '-mmacosx-version-min=11.0' option with '-target x86_64-apple-ios12.0-macabi' [-Werror,-Woverriding-t-option]"
if (PLATFORM_NAME STREQUAL "macabi")
add_compile_options(-Wno-overriding-t-option)
add_link_options(-Wno-overriding-t-option)
endif()

YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved
# Enable CTest
enable_testing()
include(Dart)
Expand Down
6 changes: 5 additions & 1 deletion cmake/onnxruntime.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,11 @@ endif()

# Assemble the Apple static framework (iOS and macOS)
if(onnxruntime_BUILD_APPLE_FRAMEWORK)
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}-${CMAKE_OSX_SYSROOT})
if (PLATFORM_NAME STREQUAL "macabi")
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}-macabi)
else()
set(STATIC_FRAMEWORK_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/${CMAKE_BUILD_TYPE}-${CMAKE_OSX_SYSROOT})
endif()
YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved

# Setup the various directories required. Remove any existing ones so we start with a clean directory.
set(STATIC_LIB_DIR ${CMAKE_CURRENT_BINARY_DIR}/static_libraries)
Expand Down
4 changes: 4 additions & 0 deletions cmake/onnxruntime_mlas.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,10 @@ if (WIN32)
endif()
endif()

if (PLATFORM_NAME STREQUAL "macabi")
target_compile_options(onnxruntime_mlas PRIVATE ${CMAKE_C_FLAGS}) # Needed for maccatalyst C compilation
YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved
endif()

if (NOT onnxruntime_BUILD_SHARED_LIB)
install(TARGETS onnxruntime_mlas
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
Expand Down
46 changes: 39 additions & 7 deletions tools/ci_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@

parser.add_argument("--ios", action="store_true", help="build for ios")

parser.add_argument("--mac_catalyst", action="store_true", help="build for mac catalyst")

parser.add_argument(
"--apple_sysroot", default="", help="Specify the location name of the macOS platform SDK to be used"
)
Expand Down Expand Up @@ -1325,10 +1327,13 @@
if args.use_snpe:
cmake_args += ["-Donnxruntime_USE_SNPE=ON"]

# minor note: the below condition basically just means an apple framework build
# and it can cover platforms including macosx/iphoneos/iphonesimulator/maccatalyst.
if args.build_apple_framework or args.ios:
YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved
if not args.cmake_generator == "Xcode":
# Note: Xcode CMake generator doesn't have a good support for Mac Catalyst yet.
if not args.mac_catalyst and not args.cmake_generator == "Xcode":
raise BuildError(
"iOS/MacOS framework build requires use of the Xcode CMake generator ('--cmake_generator Xcode')."
"iOS/MacOS framework build requires use of the Xcode CMake generator ('--cmake_generator Xcode'). (catalyst builds not included.)"
YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved
)

needed_args = [
Expand All @@ -1344,19 +1349,38 @@
"iOS/MacOS framework build on MacOS canceled due to missing arguments: "
+ ", ".join(val for val, cond in zip(arg_names, needed_args) if not cond)
)
# note: this value is mainly used in framework_info.json file to specify the build osx type
platform_name = "macabi" if args.mac_catalyst else args.apple_sysroot
cmake_args += [
"-Donnxruntime_BUILD_SHARED_LIB=ON",
"-DCMAKE_OSX_SYSROOT=" + args.apple_sysroot,
"-DCMAKE_OSX_DEPLOYMENT_TARGET=" + args.apple_deploy_target,
# we do not need protoc binary for ios cross build
"-Dprotobuf_BUILD_PROTOC_BINARIES=OFF",
"-DPLATFORM_NAME=" + platform_name,
]
if args.ios:
cmake_args += [
"-DCMAKE_SYSTEM_NAME=iOS",
"-DCMAKE_TOOLCHAIN_FILE="
+ (args.ios_toolchain_file if args.ios_toolchain_file else "../cmake/onnxruntime_ios.toolchain.cmake"),
]
# for catalyst builds, we need to manually specify cflags for target e.g. x86_64-apple-ios14.0-macabi, etc.
YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved
if args.mac_catalyst:
cmake_args += [
"-DCMAKE_CXX_COMPILER_TARGET=" + f"{args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
YUNQIUGUO marked this conversation as resolved.
Show resolved Hide resolved
"-DCMAKE_C_COMPILER_TARGET=" + f"{args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_CC_COMPILER_TARGET=" + f"{args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_CXX_FLAGS=" + f"--target={args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_CXX_FLAGS_RELEASE="
Fixed Show fixed Hide fixed
+ f"-O3 -DNDEBUG --target={args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_C_FLAGS=" + f"--target={args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_C_FLAGS_RELEASE="
Fixed Show fixed Hide fixed
+ f"-O3 -DNDEBUG --target={args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_CC_FLAGS=" + f"--target={args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
"-DCMAKE_CC_FLAGS_RELEASE="
Fixed Show fixed Hide fixed
+ f"-O3 -DNDEBUG --target={args.osx_arch}-apple-ios{args.apple_deploy_target}-macabi",
]

if args.build_wasm:
emsdk_dir = os.path.join(cmake_dir, "external", "emsdk")
Expand Down Expand Up @@ -1631,9 +1655,11 @@
[
*temp_cmake_args,
f"-DCMAKE_BUILD_TYPE={config}",
f"-DCMAKE_PREFIX_PATH={build_dir}/{config}/installed"
if preinstalled_dir.exists() and not (args.arm64 or args.arm64ec or args.arm)
else "",
(
f"-DCMAKE_PREFIX_PATH={build_dir}/{config}/installed"
if preinstalled_dir.exists() and not (args.arm64 or args.arm64ec or args.arm)
else ""
),
],
cwd=config_build_dir,
cuda_home=cuda_home,
Expand Down Expand Up @@ -1986,7 +2012,7 @@
if args.android:
run_android_tests(args, source_dir, build_dir, config, cwd)
continue
elif args.ios:
elif args.ios or args.mac_catalyst:
run_ios_tests(args, source_dir, config, cwd)
continue
dll_path_list = []
Expand Down Expand Up @@ -2726,7 +2752,13 @@
cmake_extra_args += ["-G", args.cmake_generator]

if is_macOS():
if not args.ios and not args.android and args.osx_arch == "arm64" and platform.machine() == "x86_64":
if (
not args.ios
and not args.mac_catalyst
and not args.android
and args.osx_arch == "arm64"
and platform.machine() == "x86_64"
Fixed Show fixed Hide fixed
):
if args.test:
log.warning("Cannot test ARM64 build on X86_64. Will skip test running after build.")
args.test = False
Expand Down
12 changes: 8 additions & 4 deletions tools/ci_build/github/apple/build_apple_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ def _build_for_apple_sysroot(
# Build binary for each arch, one by one
for current_arch in archs:
build_dir_current_arch = os.path.join(intermediates_dir, sysroot + "_" + current_arch)
# Use MacOS SDK for Catalyst builds
apple_sysroot = "macosx" if sysroot == "macabi" else sysroot
build_command = [
*base_build_command,
"--apple_sysroot=" + sysroot,
"--apple_sysroot=" + apple_sysroot,
"--osx_arch=" + current_arch,
"--build_dir=" + build_dir_current_arch,
]
Expand All @@ -65,9 +67,11 @@ def _build_for_apple_sysroot(
build_dir_current_arch,
build_config,
build_config + "-" + sysroot,
"onnxruntime.framework"
if build_dynamic_framework
else os.path.join("static_framework", "onnxruntime.framework"),
(
"onnxruntime.framework"
if build_dynamic_framework
else os.path.join("static_framework", "onnxruntime.framework")
),
)
ort_libs.append(os.path.join(framework_dir, "onnxruntime"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,35 @@
"iphonesimulator": [
"arm64",
"x86_64"
],
"macabi": [
"arm64",
"x86_64"
]
},
"build_params": {
"base": [
"--parallel",
"--use_xcode",
"--build_apple_framework",
"--use_coreml",
"--use_xnnpack",
"--skip_tests",
"--cmake_extra_defines=onnxruntime_BUILD_UNIT_TESTS=OFF"
],
"iphoneos": [
"--ios",
"--use_xcode",
"--use_xnnpack",
"--apple_deploy_target=12.0"
],
"iphonesimulator": [
"--ios",
"--use_xcode",
"--use_xnnpack",
"--apple_deploy_target=12.0"
],
"macabi":[
"--mac_catalyst",
"--apple_deploy_target=14.0"
]
}
}
2 changes: 1 addition & 1 deletion tools/ci_build/github/apple/framework_info.json.template
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"@CMAKE_OSX_SYSROOT@": {
"@PLATFORM_NAME@": {
"APPLE_DEPLOYMENT_TARGET": "@CMAKE_OSX_DEPLOYMENT_TARGET@",
"WEAK_FRAMEWORK": "@APPLE_WEAK_FRAMEWORK@"
}
Expand Down
16 changes: 8 additions & 8 deletions tools/ci_build/github/azure-pipelines/templates/c-api-cpu.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@ stages:
popd
displayName: "Build Apple xcframework"

- script: |
python3 tools/ci_build/github/apple/test_apple_packages.py \
--fail_if_cocoapods_missing \
--framework_info_file "$(Build.BinariesDirectory)/ios_framework/xcframework_info.json" \
--c_framework_dir "$(Build.BinariesDirectory)/ios_framework/framework_out" \
--variant Full \
--skip_macos_test
displayName: "Test Apple framework"
#- script: |
# python3 tools/ci_build/github/apple/test_apple_packages.py \
# --fail_if_cocoapods_missing \
# --framework_info_file "$(Build.BinariesDirectory)/ios_framework/xcframework_info.json" \
# --c_framework_dir "$(Build.BinariesDirectory)/ios_framework/framework_out" \
# --variant Full \
# --skip_macos_test
# displayName: "Test Apple framework"

- task: PublishBuildArtifacts@1
inputs:
Expand Down
Loading