예제 #1
0
def main():
    args = parse_args()

    framework_info = load_framework_info(args.framework_info_file.resolve())

    staging_dir = args.staging_dir.resolve()
    print(f"Assembling files in staging directory: {staging_dir}")
    if staging_dir.exists():
        print("Warning: staging directory already exists", file=sys.stderr)

    # copy the necessary files to the staging directory
    framework_dir = args.framework_dir.resolve()
    shutil.copytree(framework_dir, staging_dir / framework_dir.name, dirs_exist_ok=True)
    public_headers_dir = args.public_headers_dir.resolve()
    shutil.copytree(public_headers_dir, staging_dir / public_headers_dir.name, dirs_exist_ok=True)
    copy_repo_relative_to_dir(["LICENSE"], staging_dir)

    # generate the podspec file from the template

    variable_substitutions = {
        "VERSION": args.pod_version,
        "IOS_DEPLOYMENT_TARGET": framework_info["IOS_DEPLOYMENT_TARGET"],
        "WEAK_FRAMEWORK": framework_info["WEAK_FRAMEWORK"],
        "LICENSE_FILE": '"LICENSE"',
    }

    podspec_template = _script_dir / "onnxruntime-mobile-c.podspec.template"
    podspec = staging_dir / "onnxruntime-mobile-c.podspec"

    gen_file_from_template(podspec_template, podspec, variable_substitutions)

    return 0
예제 #2
0
def main():
    args = parse_args()

    framework_info = load_framework_info(args.framework_info_file.resolve())

    staging_dir = args.staging_dir.resolve()
    print(f"Assembling files in staging directory: {staging_dir}")
    if staging_dir.exists():
        print("Warning: staging directory already exists", file=sys.stderr)

    # copy the necessary files to the staging directory
    copy_repo_relative_to_dir([license_file] + source_files +
                              test_source_files + test_resource_files,
                              staging_dir)

    # generate the podspec file from the template

    def path_patterns_as_variable_value(patterns: list[str]):
        return ", ".join([f'"{pattern}"' for pattern in patterns])

    variable_substitutions = {
        "VERSION":
        args.pod_version,
        "IOS_DEPLOYMENT_TARGET":
        framework_info["IOS_DEPLOYMENT_TARGET"],
        "LICENSE_FILE":
        path_patterns_as_variable_value([license_file]),
        "INCLUDE_DIR_LIST":
        path_patterns_as_variable_value(include_dirs),
        "PUBLIC_HEADER_FILE_LIST":
        path_patterns_as_variable_value(public_header_files),
        "SOURCE_FILE_LIST":
        path_patterns_as_variable_value(source_files),
        "TEST_SOURCE_FILE_LIST":
        path_patterns_as_variable_value(test_source_files),
        "TEST_RESOURCE_FILE_LIST":
        path_patterns_as_variable_value(test_resource_files),
    }

    podspec_template = _script_dir / "onnxruntime-mobile-objc.podspec.template"
    podspec = staging_dir / "onnxruntime-mobile-objc.podspec"

    gen_file_from_template(podspec_template, podspec, variable_substitutions)

    return 0
예제 #3
0
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))

    has_framework = pathlib.Path(
        os.path.join(c_framework_dir, 'onnxruntime.framework')).exists()
    has_xcframework = pathlib.Path(
        os.path.join(c_framework_dir, 'onnxruntime.xcframework')).exists()

    if not has_framework and not has_xcframework:
        raise FileNotFoundError(
            '{} does not have onnxruntime.framework/xcframework'.format(
                c_framework_dir))

    if has_framework and has_xcframework:
        raise ValueError('Cannot proceed when both onnxruntime.framework '
                         'and onnxruntime.xcframework exist')

    framework_name = 'onnxruntime.framework' if has_framework else 'onnxruntime.xcframework'

    # 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=framework_name)

        # 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)

        # We will only publish xcframework, however, assembly of the xcframework is a post process
        # and it cannot be done by CMake for now. See, https://gitlab.kitware.com/cmake/cmake/-/issues/21752
        # For a single sysroot and arch built by build.py or cmake, we can only generate framework
        # We still need a way to test it, replace the xcframework with framework in the podspec
        if has_framework:
            file_data = file_data.replace('onnxruntime.xcframework',
                                          'onnxruntime.framework')
        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)