Skip to content

Instantly share code, notes, and snippets.

@csuft
Created February 23, 2024 03:52
Show Gist options
  • Save csuft/9b62fa2cc2c55765b0d5701c8281b039 to your computer and use it in GitHub Desktop.
Save csuft/9b62fa2cc2c55765b0d5701c8281b039 to your computer and use it in GitHub Desktop.
make universal library for macOS
#!/usr/bin/env python3
import os
import subprocess
import shutil
import sys
def make_universal(arm64_path, x86_64_path, output_path):
# 遍历文件夹下所有文件
for root, dirs, files in os.walk(arm64_path):
for file in files:
arm64_file = os.path.join(root, file)
# 复制文件到目标文件夹
target_file_path = os.path.join(output_path, os.path.relpath(arm64_file, arm64_path))
target_folder_path = os.path.dirname(target_file_path)
os.makedirs(target_folder_path, exist_ok=True) # 递归创建目标文件夹
shutil.copy2(arm64_file, target_file_path)
# 判断文件是否为符号链接
if os.path.islink(arm64_file):
continue # 跳过符号链接
# 获取文件扩展名
file_ext = os.path.splitext(arm64_file)[1]
# 过滤 macOS 下的可执行文件、动态库和静态库
if file_ext in ['.dylib', '.a']:
x86_file = os.path.join(x86_64_path, os.path.relpath(arm64_file, arm64_path))
output_file = os.path.join(output_path, os.path.relpath(arm64_file, arm64_path))
run_lipo(arm64_file, x86_file, output_file)
def run_lipo(arm64_file, x86_64_file, dest_file):
# 构建 lipo 命令
lipo_command = f'lipo -create {arm64_file} {x86_64_file} -output {dest_file}'
# 运行 lipo 命令
try:
subprocess.run(lipo_command, shell=True, check=True)
print("merged library saved to", dest_file)
except subprocess.CalledProcessError as e:
print(f"Error merging library files: {e}")
if __name__ == '__main__':
if len(sys.argv) != 4:
print("usage: make_universal.py arm_directory x86_directory merged_directory")
sys.exit(1)
print(f"arm directory: {sys.argv[1]}, x86 directory: {sys.argv[2]}, output directory: {sys.argv[3]}")
if not os.path.exists(sys.argv[1]):
print("arm directory doesn't exist")
exit(-1)
if not os.path.exists(sys.argv[2]):
print("x86 directory doesn't exist")
exit(-1)
if not os.path.isdir(sys.argv[3]):
print("output directory doesn't exist, create it")
os.makedirs(sys.argv[3])
make_universal(sys.argv[1], sys.argv[2], sys.argv[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment