99
1010import argparse
1111import json
12- import os
13- from pathlib import Path
1412import shutil
1513import sys
14+ from pathlib import Path
1615
1716_repo_dir = Path (__file__ ).resolve ().parents [2 ]
1817sys .path .insert (0 , str (_repo_dir / "tools" ))
@@ -98,7 +97,7 @@ def build_framework_for_platform_and_arch(
9897
9998 cmake_defines = []
10099
101- if platform != "macosx" and platform != "maccatalyst" : #ios simulator or iphoneos platform
100+ if platform != "macosx" and platform != "maccatalyst" : # ios simulator or iphoneos platform
102101 cmake_defines += [
103102 # required by OpenCV CMake toolchain file
104103 # https://github.com/opencv/opencv/blob/4223495e6cd67011f86b8ecd9be1fa105018f3b1/platforms/ios/cmake/Toolchains/common-ios-toolchain.cmake#L64-L66
@@ -213,19 +212,61 @@ def platform_arch_framework_build_dir(platform, arch):
213212 platform_fat_framework_dir .mkdir ()
214213
215214 # copy over files from arch-specific framework to fat framework
216- for framework_file_relative_path in [Path ("Headers" ), Path ("Info.plist" )]:
217- src = first_arch_framework_dir / framework_file_relative_path
218- dst = platform_fat_framework_dir / framework_file_relative_path
219- if src .is_dir ():
220- shutil .copytree (src , dst )
221- else :
222- shutil .copy (src , dst )
223-
224- # combine arch-specific framework libraries
225- arch_libs = [str (framework_dir / "onnxruntime_extensions" ) for framework_dir in arch_framework_dirs ]
226- run (
227- * ([_lipo , "-create" , "-output" , str (platform_fat_framework_dir / "onnxruntime_extensions" )] + arch_libs )
228- )
215+ # macos requires different framework structure:
216+ # https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPFrameworks/Concepts/FrameworkAnatomy.html
217+ if platform == "macosx" or platform == "maccatalyst" :
218+ # Set up directory strcture
219+ dest_headers_dir = platform_fat_framework_dir / "Versions/A/Headers"
220+ dest_resources_dir = platform_fat_framework_dir / "Versions/A/Resources"
221+
222+ # Copy headers and Info.plist
223+ shutil .copytree (first_arch_framework_dir / Path ("Headers" ), dest_headers_dir )
224+ Path (dest_resources_dir ).mkdir (parents = True , exist_ok = True )
225+ shutil .copy (first_arch_framework_dir / Path ("Info.plist" ), dest_resources_dir / "Info.plist" )
226+
227+ # combine arch-specific framework libraries
228+ arch_libs = [str (framework_dir / "onnxruntime_extensions" ) for framework_dir in arch_framework_dirs ]
229+ run (
230+ * (
231+ [
232+ _lipo ,
233+ "-create" ,
234+ "-output" ,
235+ str (platform_fat_framework_dir / "Versions/A/onnxruntime_extensions" ),
236+ ]
237+ + arch_libs
238+ )
239+ )
240+
241+ # create Symbolic links
242+ Path (platform_fat_framework_dir / "Versions/Current" ).symlink_to ("A" , target_is_directory = True )
243+ Path (platform_fat_framework_dir / "Headers" ).symlink_to (
244+ "Versions/Current/Headers" , target_is_directory = True
245+ )
246+ Path (platform_fat_framework_dir / "Resources" ).symlink_to (
247+ "Versions/Current/Resources" , target_is_directory = True
248+ )
249+ Path (platform_fat_framework_dir / "onnxruntime_extensions" ).symlink_to (
250+ "Versions/Current/onnxruntime_extensions"
251+ )
252+
253+ else :
254+ for framework_file_relative_path in [Path ("Headers" ), Path ("Info.plist" )]:
255+ src = first_arch_framework_dir / framework_file_relative_path
256+ dst = platform_fat_framework_dir / framework_file_relative_path
257+ if src .is_dir ():
258+ shutil .copytree (src , dst )
259+ else :
260+ shutil .copy (src , dst )
261+
262+ # combine arch-specific framework libraries
263+ arch_libs = [str (framework_dir / "onnxruntime_extensions" ) for framework_dir in arch_framework_dirs ]
264+ run (
265+ * (
266+ [_lipo , "-create" , "-output" , str (platform_fat_framework_dir / "onnxruntime_extensions" )]
267+ + arch_libs
268+ )
269+ )
229270
230271 platform_fat_framework_dirs .append (platform_fat_framework_dir )
231272
@@ -241,7 +282,7 @@ def platform_arch_framework_build_dir(platform, arch):
241282 # copy public headers
242283 output_headers_dir = output_dir / "Headers"
243284 _rmtree_if_existing (output_headers_dir )
244- shutil .copytree (headers_dir , output_headers_dir )
285+ shutil .copytree (headers_dir , output_headers_dir , symlinks = True )
245286
246287 # merge framework_info.json per platform into xcframework_info.json in output_dir
247288 _merge_framework_info_files (framework_info_files_to_merge , Path (output_dir , "xcframework_info.json" ))
0 commit comments