Aegisub/tools/osx-fix-libs.py

104 lines
2.6 KiB
Python
Raw Normal View History

2019-10-07 02:53:57 +02:00
#!/usr/bin/env python3
import re
import sys
import os
import shutil
import stat
is_bad_lib = re.compile(r'(/usr/local|/opt)').match
is_sys_lib = re.compile(r'(/usr|/System)').match
otool_libname_extract = re.compile(r'\s+(/.*?)[\(\s:]').search
goodlist = []
badlist = []
link_map = {}
def otool(cmdline):
pipe = os.popen("otool " + cmdline, 'r')
output = pipe.readlines()
pipe.close()
return output
def collectlibs(lib, masterlist, targetdir):
global goodlist, link_map
liblist = otool("-L '" + lib + "'")
locallist = []
for l in liblist:
lr = otool_libname_extract(l)
if not lr: continue
l = lr.group(1)
if is_bad_lib(l) and not l in badlist:
badlist.append(l)
if ((not is_sys_lib(l)) or is_bad_lib(l)) and not l in masterlist:
locallist.append(l)
2019-05-17 00:11:03 +02:00
print("found %s:" % l)
check = l
link_list = []
while check:
if os.path.isfile(check) and not os.path.islink(check):
os.system("cp '%s' '%s'" % (check, targetdir))
2019-05-17 00:11:03 +02:00
print(" FILE %s ... copied to target" % check)
if link_list:
for link in link_list:
link_map[link] = os.path.basename(check)
break
if os.path.islink(check):
2019-05-17 00:11:03 +02:00
print(" LINK %s" % check)
link_list.append(os.path.basename(check))
check = os.path.dirname(check) + "/" + os.readlink(check)
elif not l in goodlist and not l in masterlist:
goodlist.append(l)
masterlist.extend(locallist)
for l in locallist:
collectlibs(l, masterlist, targetdir)
exit;
binname = sys.argv[1]
targetdir = os.path.dirname(binname)
2019-05-17 00:11:03 +02:00
print("Searching for libraries in ", binname, "...")
libs = [binname]
collectlibs(sys.argv[1], libs, targetdir)
2019-05-17 00:11:03 +02:00
print()
print("System libraries used...")
goodlist.sort()
for l in goodlist:
2019-05-17 00:11:03 +02:00
print(l)
2019-05-17 00:11:03 +02:00
print()
print("Fixing library install names...")
in_tool_cmdline = "install_name_tool "
for lib in libs:
libbase = os.path.basename(lib)
if libbase in link_map:
libbase = link_map[libbase]
in_tool_cmdline = in_tool_cmdline + ("-change '%s' '@executable_path/%s' " % (lib, libbase))
for lib in libs:
libbase = os.path.basename(lib)
if libbase in link_map:
libbase = link_map[libbase]
2019-05-17 00:11:03 +02:00
print("%s -> @executable_path/%s (REMAPPED)" % (lib, libbase))
else:
2019-05-17 00:11:03 +02:00
print("%s -> @executable_path/%s" % (lib, libbase))
os.system("%s -id '@executable_path/%s' '%s/%s'" % (in_tool_cmdline, libbase, targetdir, libbase))
sys.stdout.flush()
if badlist:
2019-05-17 00:11:03 +02:00
print()
print("WARNING: The following libraries have blacklisted paths:")
for lib in sorted(badlist):
2019-05-17 00:11:03 +02:00
print(lib)
print("These paths normally have files from a package manager, which means that end result may not work if copied to another machine.")
2019-05-17 00:11:03 +02:00
print()
print("All done!")