onnxruntime/tools/ci_build/github/apple/test_ios_packages.py
Guoyu Wang c5038063ed
Add iOS/macOS static framework (#8357)
* Add ability to generate ios static framework

* Fix typos

* Add pod cache clean, update some comments of previous commit

* Fix CI failure with newly added cpuinfo library

* Update test model (CoreML requires node has a name)

* Addressed CR comments
2021-07-14 16:39:17 -07:00

124 lines
5.1 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import argparse
import os
import pathlib
import shutil
import subprocess
SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
REPO_DIR = os.path.normpath(os.path.join(SCRIPT_DIR, "..", "..", "..", ".."))
from package_assembly_utils import ( # noqa: E402
gen_file_from_template, load_framework_info)
def _test_ios_packages(args):
# check if CocoaPods is installed
if shutil.which('pod') is None:
if args.fail_if_cocoapods_missing:
raise ValueError('CocoaPods is required for this test')
else:
print('CocoaPods is not installed, ignore this test')
return
# Now we need to create a zip file contains the framework and the podspec file, both of these 2 files
# should be under the c_framework_dir
c_framework_dir = args.c_framework_dir.resolve()
if not c_framework_dir.is_dir():
raise FileNotFoundError('c_framework_dir {} is not a folder.'.format(c_framework_dir))
framework_path = os.path.join(c_framework_dir, 'onnxruntime.framework')
if not pathlib.Path(framework_path).exists():
raise FileNotFoundError('{} does not have onnxruntime.framework'.format(c_framework_dir))
# create a temp folder
import tempfile
with tempfile.TemporaryDirectory() as temp_dir:
# This is for debugging only
# temp_dir = <a local directory>
# shutil.rmtree(temp_dir)
# create a zip file contains the framework
# TODO, move this into a util function
local_pods_dir = os.path.join(temp_dir, 'local_pods')
os.makedirs(local_pods_dir, exist_ok=True)
# shutil.make_archive require target file as full path without extension
zip_base_filename = os.path.join(local_pods_dir, 'onnxruntime-mobile-c')
zip_file_path = zip_base_filename + '.zip'
shutil.make_archive(zip_base_filename, 'zip', root_dir=c_framework_dir, base_dir='onnxruntime.framework')
# copy the test project to the temp_dir
test_proj_path = os.path.join(REPO_DIR, 'onnxruntime', 'test', 'platform', 'ios', 'ios_package_test')
target_proj_path = os.path.join(temp_dir, 'ios_package_test')
shutil.copytree(test_proj_path, target_proj_path)
# generate the podspec file from the template
framework_info = load_framework_info(args.framework_info_file.resolve())
with open(os.path.join(REPO_DIR, 'VERSION_NUMBER')) as version_file:
ORT_VERSION = version_file.readline().strip()
variable_substitutions = {
"VERSION": ORT_VERSION,
"IOS_DEPLOYMENT_TARGET": framework_info["IOS_DEPLOYMENT_TARGET"],
"WEAK_FRAMEWORK": framework_info["WEAK_FRAMEWORK"],
"LICENSE_FILE": '"LICENSE"',
}
podspec_template = os.path.join(SCRIPT_DIR, "c", "onnxruntime-mobile-c.podspec.template")
podspec = os.path.join(target_proj_path, "onnxruntime-mobile-c.podspec")
gen_file_from_template(podspec_template, podspec, variable_substitutions)
# update the podspec to point to the local framework zip file
with open(podspec, 'r') as file:
file_data = file.read()
file_data = file_data.replace('file:///http_source_placeholder', 'file:' + zip_file_path)
with open(podspec, 'w') as file:
file.write(file_data)
# clean the Cocoapods cache first, in case the same pod was cached in previous runs
subprocess.run(['pod', 'cache', 'clean', '--all'], shell=False, check=True, cwd=target_proj_path)
# install pods
subprocess.run(['pod', 'install'], shell=False, check=True, cwd=target_proj_path)
# run the tests
subprocess.run(['xcrun', 'xcodebuild', 'test',
'-workspace', './ios_package_test.xcworkspace',
'-scheme', 'ios_package_test',
'-destination', 'platform=iOS Simulator,OS=latest,name=iPhone SE (2nd generation)'],
shell=False, check=True, cwd=target_proj_path)
def parse_args():
parser = argparse.ArgumentParser(
os.path.basename(__file__),
description='Test iOS framework using CocoaPods package.'
)
parser.add_argument('--fail_if_cocoapods_missing', action='store_true',
help='This script will fail if CocoaPods is not installed, '
'will not throw error unless fail_if_cocoapod_missing is set.')
parser.add_argument("--framework_info_file", type=pathlib.Path, required=True,
help="Path to the framework_info.json file containing additional values for the podspec. "
"This file should be generated by CMake in the build directory.")
parser.add_argument('--c_framework_dir', type=pathlib.Path, required=True,
help='Provide the parent directory for C/C++ framework')
return parser.parse_args()
def main():
args = parse_args()
_test_ios_packages(args)
if __name__ == '__main__':
main()