#!/usr/bin/env python3 """Bundle non-system dylibs recursively, including optional font dependencies.""" import pathlib import shutil import subprocess import sys bundle = pathlib.Path(sys.argv[1]).resolve() executable = bundle / "Contents/MacOS/ava2_client" frameworks = bundle / "Contents/Frameworks" frameworks.mkdir(parents=True, exist_ok=True) pending = [executable] copied = {} while pending: binary = pending.pop() lines = subprocess.check_output(["otool", "-L", str(binary)], text=True).splitlines()[1:] for line in lines: library = line.strip().split(" (", 1)[0] if not library.startswith(("/opt/homebrew/", "/usr/local/")): continue source = pathlib.Path(library) target = frameworks / source.name # otool includes a dylib's own install ID; it is handled by -id below. if source.name == binary.name: continue if source.name not in copied: if target.exists(): target.chmod(target.stat().st_mode | 0o200) shutil.copy2(source, target) copied[source.name] = source.resolve() pending.append(target) elif copied[source.name] != source.resolve(): raise RuntimeError(f"Conflicting library names: {source.name}") subprocess.run(["install_name_tool", "-change", library, "@executable_path/../Frameworks/" + source.name, str(binary)], check=True) if binary != executable: subprocess.run(["install_name_tool", "-id", "@executable_path/../Frameworks/" + binary.name, str(binary)], check=True) for binary in [*frameworks.glob("*.dylib"), executable]: subprocess.run(["codesign", "-f", "-s", "-", str(binary)], check=True) subprocess.run(["codesign", "-f", "-s", "-", str(bundle)], check=True) subprocess.run(["codesign", "--verify", "--deep", str(bundle)], check=True)