Skip to content

Commit 8992e31

Browse files
authored
Move iOS package from framework to xcframework (#8805)
* additional changes * test package run * minor fix * minor fix * minor fix * Get around no arm64 simulator * fix objc pod build failure * downgrade_eigen * update objc podspec template
1 parent e259867 commit 8992e31

7 files changed

+107
-52
lines changed

tools/ci_build/github/apple/build_ios_framework.py

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
REPO_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "..", "..", "..", ".."))
1616
BUILD_PY = os.path.join(REPO_DIR, "tools", "ci_build", "build.py")
1717

18-
# We by default will build below 2 archs
19-
DEFAULT_BUILD_OSX_ARCHS = [
20-
{'sysroot': 'iphoneos', 'arch': 'arm64'},
21-
{'sysroot': 'iphonesimulator', 'arch': 'x86_64'},
22-
]
18+
# We by default will build below 3 archs
19+
DEFAULT_BUILD_OSX_ARCHS = {
20+
'iphoneos': ['arm64'],
21+
'iphonesimulator': ['arm64', 'x86_64'],
22+
}
2323

2424

2525
def _parse_build_settings(args):
@@ -40,40 +40,30 @@ def _parse_build_settings(args):
4040
return build_settings
4141

4242

43-
def _build_package(args):
44-
build_settings = _parse_build_settings(args)
45-
build_dir = os.path.abspath(args.build_dir)
46-
47-
# Temp dirs to hold building results
48-
intermediates_dir = os.path.join(build_dir, 'intermediates')
49-
build_config = args.config
50-
base_build_command = [sys.executable, BUILD_PY, '--config=' + build_config] + build_settings['build_params']
51-
43+
# Build fat framework for all archs of a single sysroot
44+
# For example, arm64 and x86_64 for iphonesimulator
45+
def _build_for_ios_sysroot(build_config, intermediates_dir, base_build_command,
46+
sysroot, archs, build_dynamic_framework):
5247
# paths of the onnxruntime libraries for different archs
5348
ort_libs = []
5449
info_plist_path = ''
5550

5651
# Build binary for each arch, one by one
57-
for osx_arch in build_settings['build_osx_archs']:
58-
sysroot = osx_arch['sysroot']
59-
current_arch = osx_arch['arch']
52+
for current_arch in archs:
6053
build_dir_current_arch = os.path.join(intermediates_dir, sysroot + "_" + current_arch)
6154
build_command = base_build_command + [
6255
'--ios_sysroot=' + sysroot,
6356
'--osx_arch=' + current_arch,
6457
'--build_dir=' + build_dir_current_arch
6558
]
6659

67-
if args.include_ops_by_config is not None:
68-
build_command += ['--include_ops_by_config=' + str(args.include_ops_by_config.resolve())]
69-
7060
# the actual build process for current arch
7161
subprocess.run(build_command, shell=False, check=True, cwd=REPO_DIR)
7262

7363
# get the compiled lib path
7464
framework_dir = os.path.join(
7565
build_dir_current_arch, build_config, build_config + "-" + sysroot,
76-
'onnxruntime.framework' if args.build_dynamic_framework
66+
'onnxruntime.framework' if build_dynamic_framework
7767
else os.path.join('static_framework', 'onnxruntime.framework'))
7868
ort_libs.append(os.path.join(framework_dir, 'onnxruntime'))
7969

@@ -84,12 +74,15 @@ def _build_package(args):
8474
headers = glob.glob(os.path.join(framework_dir, 'Headers', '*.h'))
8575

8676
# manually create the fat framework
87-
framework_dir = os.path.join(build_dir, 'framework_out', 'onnxruntime.framework')
77+
framework_dir = os.path.join(intermediates_dir, 'frameworks', sysroot, 'onnxruntime.framework')
78+
# remove the existing framework if any
79+
if os.path.exists(framework_dir):
80+
shutil.rmtree(framework_dir)
8881
pathlib.Path(framework_dir).mkdir(parents=True, exist_ok=True)
8982

9083
# copy the Info.plist, framework_info.json, and header files
9184
shutil.copy(info_plist_path, framework_dir)
92-
shutil.copy(framework_info_path, build_dir)
85+
shutil.copy(framework_info_path, os.path.dirname(framework_dir))
9386
header_dir = os.path.join(framework_dir, 'Headers')
9487
pathlib.Path(header_dir).mkdir(parents=True, exist_ok=True)
9588
for _header in headers:
@@ -101,14 +94,62 @@ def _build_package(args):
10194
lipo_command += ['-output', os.path.join(framework_dir, 'onnxruntime')]
10295
subprocess.run(lipo_command, shell=False, check=True)
10396

97+
return framework_dir
98+
99+
100+
def _build_package(args):
101+
build_settings = _parse_build_settings(args)
102+
build_dir = os.path.abspath(args.build_dir)
103+
104+
# Temp dirs to hold building results
105+
intermediates_dir = os.path.join(build_dir, 'intermediates')
106+
build_config = args.config
107+
base_build_command = [sys.executable, BUILD_PY, '--config=' + build_config] + build_settings['build_params']
108+
if args.include_ops_by_config is not None:
109+
base_build_command += ['--include_ops_by_config=' + str(args.include_ops_by_config.resolve())]
110+
111+
# build framework for individual sysroot
112+
framework_dirs = []
113+
framework_info_path = ''
114+
public_headers_path = ''
115+
for sysroot in build_settings['build_osx_archs']:
116+
framework_dir = _build_for_ios_sysroot(
117+
build_config, intermediates_dir, base_build_command, sysroot,
118+
build_settings['build_osx_archs'][sysroot], args.build_dynamic_framework)
119+
framework_dirs.append(framework_dir)
120+
# podspec and headers for each sysroot are the same, pick one of them
121+
if not framework_info_path:
122+
framework_info_path = os.path.join(os.path.dirname(framework_dir), 'framework_info.json')
123+
public_headers_path = os.path.join(os.path.dirname(framework_dir), 'onnxruntime.framework', 'Headers')
124+
125+
# create the folder for xcframework and copy the LICENSE and podspec file
126+
xcframework_dir = os.path.join(build_dir, 'framework_out')
127+
pathlib.Path(xcframework_dir).mkdir(parents=True, exist_ok=True)
128+
shutil.copy(os.path.join(REPO_DIR, 'LICENSE'), xcframework_dir)
129+
shutil.copytree(public_headers_path, os.path.join(xcframework_dir, 'Headers'), dirs_exist_ok=True)
130+
shutil.copy(framework_info_path, build_dir)
131+
132+
# remove existing xcframework if any
133+
xcframework_path = os.path.join(xcframework_dir, 'onnxruntime.xcframework')
134+
if os.path.exists(xcframework_path):
135+
shutil.rmtree(xcframework_path)
136+
137+
# Assemble the final xcframework
138+
build_xcframework_cmd = ['xcrun', 'xcodebuild', '-create-xcframework',
139+
'-output', xcframework_path]
140+
for framework_dir in framework_dirs:
141+
build_xcframework_cmd.extend(['-framework', framework_dir])
142+
143+
subprocess.run(build_xcframework_cmd, shell=False, check=True, cwd=REPO_DIR)
144+
104145

105146
def parse_args():
106147
parser = argparse.ArgumentParser(
107148
os.path.basename(__file__),
108-
description='''Create iOS framework and podspec for one or more osx_archs (fat framework)
149+
description='''Create iOS framework and podspec for one or more osx_archs (xcframework)
109150
and building properties specified in the given build config file, see
110151
tools/ci_build/github/apple/default_mobile_ios_framework_build_settings.json for details.
111-
The output of the final framework and podspec can be found under [build_dir]/framework_out.
152+
The output of the final xcframework and podspec can be found under [build_dir]/framework_out.
112153
Please note, this building script will only work on macOS.
113154
'''
114155
)
@@ -124,12 +165,12 @@ def parse_args():
124165
choices=["Debug", "MinSizeRel", "Release", "RelWithDebInfo"],
125166
help="Configuration to build.")
126167

127-
parser.add_argument('build_settings_file', type=pathlib.Path,
128-
help='Provide the file contains settings for building iOS framework')
129-
130168
parser.add_argument("--build_dynamic_framework", action='store_true',
131169
help="Build Dynamic Framework (default is build static framework).")
132170

171+
parser.add_argument('build_settings_file', type=pathlib.Path,
172+
help='Provide the file contains settings for building iOS framework')
173+
133174
args = parser.parse_args()
134175

135176
if not args.build_settings_file.resolve().is_file():

tools/ci_build/github/apple/c/assemble_c_pod_package.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ def parse_args():
3131
parser.add_argument("--framework-info-file", type=pathlib.Path, required=True,
3232
help="Path to the framework_info.json file containing additional values for the podspec. "
3333
"This file should be generated by CMake in the build directory.")
34+
parser.add_argument("--public-headers-dir", type=pathlib.Path, required=True,
35+
help="Path to the public headers directory to include in the pod.")
3436
parser.add_argument("--framework-dir", type=pathlib.Path, required=True,
3537
help="Path to the onnxruntime.framework directory to include in the pod.")
3638

@@ -50,6 +52,8 @@ def main():
5052
# copy the necessary files to the staging directory
5153
framework_dir = args.framework_dir.resolve()
5254
shutil.copytree(framework_dir, staging_dir / framework_dir.name, dirs_exist_ok=True)
55+
public_headers_dir = args.public_headers_dir.resolve()
56+
shutil.copytree(public_headers_dir, staging_dir / public_headers_dir.name, dirs_exist_ok=True)
5357
copy_repo_relative_to_dir(["LICENSE"], staging_dir)
5458

5559
# generate the podspec file from the template

tools/ci_build/github/apple/c/onnxruntime-mobile-c.podspec.template

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,16 @@ Pod::Spec.new do |spec|
77
spec.source = { :http => "file:///http_source_placeholder" }
88
spec.summary = "ONNX Runtime Mobile C/C++ Pod"
99
spec.platform = :ios, "@IOS_DEPLOYMENT_TARGET@"
10-
spec.vendored_frameworks = "onnxruntime.framework"
10+
spec.vendored_frameworks = "onnxruntime.xcframework"
1111
spec.static_framework = true
1212
spec.weak_framework = [ @WEAK_FRAMEWORK@ ]
13-
spec.source_files = "onnxruntime.framework/Headers/*.h"
13+
spec.source_files = "Headers/*.h"
1414
spec.preserve_paths = [ @LICENSE_FILE@ ]
1515
spec.description = <<-DESC
1616
A pod for the ONNX Runtime Mobile C/C++ library.
1717
DESC
18-
spec.library = "c++"
19-
spec.pod_target_xcconfig = {
18+
spec.library = "c++"
19+
spec.pod_target_xcconfig = {
2020
"OTHER_CPLUSPLUSFLAGS" => "-fvisibility=hidden -fvisibility-inlines-hidden",
2121
}
22-
spec.user_target_xcconfig = {
23-
# TODO workaround - support arm64 iphonesimulator later
24-
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" => "arm64"
25-
}
2622
end

tools/ci_build/github/apple/default_mobile_ios_framework_build_settings.json

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
{
2-
"build_osx_archs": [
3-
{
4-
"sysroot": "iphoneos",
5-
"arch": "arm64"
6-
},
7-
{
8-
"sysroot": "iphonesimulator",
9-
"arch": "x86_64"
10-
}
11-
],
2+
"build_osx_archs": {
3+
"iphoneos": [
4+
"arm64"
5+
],
6+
"iphonesimulator": [
7+
"arm64",
8+
"x86_64"
9+
]
10+
},
1211
"build_params": [
1312
"--ios",
1413
"--parallel",

tools/ci_build/github/apple/objectivec/onnxruntime-mobile-objc.podspec.template

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Pod::Spec.new do |s|
3030
core.pod_target_xcconfig = {
3131
"HEADER_SEARCH_PATHS" => include_dirs.join(" "),
3232
"OTHER_CPLUSPLUSFLAGS" => "-fvisibility=hidden -fvisibility-inlines-hidden",
33-
"EXCLUDED_ARCHS[sdk=iphonesimulator*]" => "arm64",
3433
}
3534

3635
core.public_header_files = [

tools/ci_build/github/apple/test_ios_packages.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,17 @@ def _test_ios_packages(args):
3131
if not c_framework_dir.is_dir():
3232
raise FileNotFoundError('c_framework_dir {} is not a folder.'.format(c_framework_dir))
3333

34-
framework_path = os.path.join(c_framework_dir, 'onnxruntime.framework')
35-
if not pathlib.Path(framework_path).exists():
36-
raise FileNotFoundError('{} does not have onnxruntime.framework'.format(c_framework_dir))
34+
has_framework = pathlib.Path(os.path.join(c_framework_dir, 'onnxruntime.framework')).exists()
35+
has_xcframework = pathlib.Path(os.path.join(c_framework_dir, 'onnxruntime.xcframework')).exists()
36+
37+
if not has_framework and not has_xcframework:
38+
raise FileNotFoundError('{} does not have onnxruntime.framework/xcframework'.format(c_framework_dir))
39+
40+
if has_framework and has_xcframework:
41+
raise ValueError('Cannot proceed when both onnxruntime.framework '
42+
'and onnxruntime.xcframework exist')
43+
44+
framework_name = 'onnxruntime.framework' if has_framework else 'onnxruntime.xcframework'
3745

3846
# create a temp folder
3947
import tempfile
@@ -49,7 +57,7 @@ def _test_ios_packages(args):
4957
# shutil.make_archive require target file as full path without extension
5058
zip_base_filename = os.path.join(local_pods_dir, 'onnxruntime-mobile-c')
5159
zip_file_path = zip_base_filename + '.zip'
52-
shutil.make_archive(zip_base_filename, 'zip', root_dir=c_framework_dir, base_dir='onnxruntime.framework')
60+
shutil.make_archive(zip_base_filename, 'zip', root_dir=c_framework_dir, base_dir=framework_name)
5361

5462
# copy the test project to the temp_dir
5563
test_proj_path = os.path.join(REPO_DIR, 'onnxruntime', 'test', 'platform', 'ios', 'ios_package_test')
@@ -78,6 +86,13 @@ def _test_ios_packages(args):
7886
with open(podspec, 'r') as file:
7987
file_data = file.read()
8088
file_data = file_data.replace('file:///http_source_placeholder', 'file:' + zip_file_path)
89+
90+
# We will only publish xcframework, however, assembly of the xcframework is a post process
91+
# and it cannot be done by CMake for now. See, https://gitlab.kitware.com/cmake/cmake/-/issues/21752
92+
# For a single sysroot and arch built by build.py or cmake, we can only generate framework
93+
# We still need a way to test it, replace the xcframework with framework in the podspec
94+
if has_framework:
95+
file_data = file_data.replace('onnxruntime.xcframework', 'onnxruntime.framework')
8196
with open(podspec, 'w') as file:
8297
file.write(file_data)
8398

tools/ci_build/github/azure-pipelines/mac-ios-packaging-pipeline.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ jobs:
7474
--staging-dir "$(Build.BinariesDirectory)/staging/onnxruntime-mobile-c" \
7575
--pod-version ${ORT_POD_VERSION} \
7676
--framework-info-file "$(Build.BinariesDirectory)/ios_framework/framework_info.json" \
77-
--framework-dir "$(Build.BinariesDirectory)/ios_framework/framework_out/onnxruntime.framework"
77+
--framework-dir "$(Build.BinariesDirectory)/ios_framework/framework_out/onnxruntime.xcframework" \
78+
--public-headers-dir "$(Build.BinariesDirectory)/ios_framework/framework_out/Headers"
7879
displayName: "Assemble onnxruntime-mobile-c pod files"
7980
8081
- script: |

0 commit comments

Comments
 (0)