Skip to content

Commit

Permalink
Refactor build_apple_framework.py to support macOS framework structure
Browse files Browse the repository at this point in the history
  • Loading branch information
vraspar committed Jul 28, 2024
1 parent bbbaef3 commit a2b433b
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions tools/ci_build/github/apple/build_apple_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,48 @@ def _build_for_apple_sysroot(
pathlib.Path(framework_dir).mkdir(parents=True, exist_ok=True)

# copy the Info.plist, framework_info.json, and header files
shutil.copy(info_plist_path, framework_dir)
shutil.copy(framework_info_path, os.path.dirname(framework_dir))
header_dir = os.path.join(framework_dir, "Headers")
pathlib.Path(header_dir).mkdir(parents=True, exist_ok=True)
for _header in headers:
shutil.copy(_header, header_dir)

# use lipo to create a fat ort library
lipo_command = ["lipo", "-create"]
lipo_command += ort_libs
lipo_command += ["-output", os.path.join(framework_dir, "onnxruntime")]
subprocess.run(lipo_command, shell=False, check=True)

# macos requires different framework structure:
# https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/FrameworkAnatomy.html
if sysroot == "macosx":
# create headers and resources directory
header_dir = os.path.join(framework_dir, "Versions", "A", "Headers")
resource_dir = os.path.join(framework_dir, "Versions", "A", "Resources")
pathlib.Path(header_dir).mkdir(parents=True, exist_ok=True)
pathlib.Path(resource_dir).mkdir(parents=True, exist_ok=True)

shutil.copy(info_plist_path, resource_dir)
shutil.copy(framework_info_path, os.path.dirname(framework_dir))

for _header in headers:
shutil.copy(_header, header_dir)

# use lipo to create a fat ort library
lipo_command = ["lipo", "-create"]
lipo_command += ort_libs
lipo_command += ["-output", os.path.join(framework_dir, "Versions", "A", "onnxruntime")]
subprocess.run(lipo_command, shell=False, check=True)

# create the symbolic link
pathlib.Path(os.path.join(framework_dir, "Versions", "Current")).symlink_to("A", target_is_directory=True)
pathlib.Path(os.path.join(framework_dir, "Headers")).symlink_to("Versions/Current/Headers")
pathlib.Path(os.path.join(framework_dir, "Resources")).symlink_to("Versions/Current/Resources")
pathlib.Path(os.path.join(framework_dir, "onnxruntime")).symlink_to("Versions/Current/onnxruntime")

else:
shutil.copy(info_plist_path, framework_dir)
shutil.copy(framework_info_path, os.path.dirname(framework_dir))
header_dir = os.path.join(framework_dir, "Headers")
pathlib.Path(header_dir).mkdir(parents=True, exist_ok=True)

for _header in headers:
shutil.copy(_header, header_dir)

# use lipo to create a fat ort library
lipo_command = ["lipo", "-create"]
lipo_command += ort_libs
lipo_command += ["-output", os.path.join(framework_dir, "onnxruntime")]
subprocess.run(lipo_command, shell=False, check=True)

return framework_dir

Expand Down Expand Up @@ -163,6 +193,7 @@ def _build_package(args):
public_headers_path = os.path.join(os.path.dirname(framework_dir), "onnxruntime.framework", "Headers")

# create the folder for xcframework and copy the LICENSE and framework_info.json file
# changes here
xcframework_dir = os.path.join(build_dir, "framework_out")
pathlib.Path(xcframework_dir).mkdir(parents=True, exist_ok=True)
shutil.copy(os.path.join(REPO_DIR, "LICENSE"), xcframework_dir)
Expand Down

0 comments on commit a2b433b

Please sign in to comment.