Skip to content

Instantly share code, notes, and snippets.

@automactic
Created August 16, 2017 17:57
Show Gist options
  • Save automactic/230b0030cea4e85e000b43f17b150107 to your computer and use it in GitHub Desktop.
Save automactic/230b0030cea4e85e000b43f17b150107 to your computer and use it in GitHub Desktop.
import subprocess
import os
def change_install_name(lib_install_path: str):
def get_name_map(dir_path: str):
files = {}
for file in os.listdir(dir):
if file.endswith('.dylib') and not os.path.islink(dir_path+'/'+file):
files[file.split('.')[0]] = file
return files
dylibs = get_name_map(lib_install_path)
for dylib_name, file_name in dylibs.items():
subprocess.run(['install_name_tool', '-id', '@rpath/{}'.format(file_name), file_name], cwd=lib_install_path)
process = subprocess.run(['otool', '-L', file_name], cwd=lib_install_path, stdout=subprocess.PIPE)
dependencies = process.stdout.decode().split('\n')[2:-1]
dependencies = [d.strip() for d in dependencies]
for d in dependencies:
if not d.startswith('/usr/lib/'):
old_name = d.split(' ')[0]
dylib_name = old_name.split('/')[-1].split('.')[0]
new_name = '@rpath/{}'.format(dylibs[dylib_name])
subprocess.run(['install_name_tool', '-change', old_name, new_name, file_name], cwd=dir)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment