"""Make a Zig macOS dylib loadable as an iOS arm64 dylib.""" import struct from pathlib import Path PAGE = 0x4000 MH_MAGIC_64 = 0xFEEDFACF LC_SEGMENT_64 = 0x19 LC_LOAD_DYLINKER = 0xE LC_BUILD_VERSION = 0x32 def patch_dylib(path: Path) -> None: data = bytearray(path.read_bytes()) if data[:4] != struct.pack(" next_vm: raise SystemExit(f"{name} cannot be aligned without overlapping the next segment") if aligned < filesize: raise SystemExit(f"{name} aligned size {aligned:#x} is smaller than file size {filesize:#x}") struct.pack_into(" {aligned:#x}") rebuilt = bytearray() removed_dylinker = False patched_platform = False for cmd, chunk in cmds: if cmd == LC_BUILD_VERSION: struct.pack_into("