2019-10-07 02:53:57 +02:00
|
|
|
#!/usr/bin/env python3
|
2008-11-24 21:29:47 +01:00
|
|
|
|
2008-11-25 02:10:28 +01:00
|
|
|
import re
|
|
|
|
import sys
|
|
|
|
import os
|
|
|
|
import shutil
|
2009-08-01 01:47:22 +02:00
|
|
|
import stat
|
2008-11-25 02:10:28 +01:00
|
|
|
|
|
|
|
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
|
2008-12-29 05:41:00 +01:00
|
|
|
goodlist = []
|
2012-05-13 02:58:01 +02:00
|
|
|
badlist = []
|
2010-01-12 14:00:24 +01:00
|
|
|
link_map = {}
|
2008-11-25 02:10:28 +01:00
|
|
|
|
|
|
|
def otool(cmdline):
|
|
|
|
pipe = os.popen("otool " + cmdline, 'r')
|
|
|
|
output = pipe.readlines()
|
|
|
|
pipe.close()
|
|
|
|
return output
|
|
|
|
|
|
|
|
def collectlibs(lib, masterlist, targetdir):
|
2010-01-12 14:00:24 +01:00
|
|
|
global goodlist, link_map
|
2009-06-02 02:16:45 +02:00
|
|
|
liblist = otool("-L '" + lib + "'")
|
2008-11-25 02:10:28 +01:00
|
|
|
locallist = []
|
2010-01-12 14:00:24 +01:00
|
|
|
|
2008-11-25 02:10:28 +01:00
|
|
|
for l in liblist:
|
|
|
|
lr = otool_libname_extract(l)
|
|
|
|
if not lr: continue
|
|
|
|
l = lr.group(1)
|
2012-05-13 02:58:01 +02:00
|
|
|
if is_bad_lib(l) and not l in badlist:
|
|
|
|
badlist.append(l)
|
2013-12-08 04:58:03 +01:00
|
|
|
if ((not is_sys_lib(l)) or is_bad_lib(l)) and not l in masterlist:
|
2008-11-25 02:10:28 +01:00
|
|
|
locallist.append(l)
|
2019-05-17 00:11:03 +02:00
|
|
|
print("found %s:" % l)
|
2009-08-01 01:47:22 +02:00
|
|
|
|
|
|
|
check = l
|
2010-01-12 14:00:24 +01:00
|
|
|
link_list = []
|
2009-08-01 01:47:22 +02:00
|
|
|
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)
|
2010-01-12 14:00:24 +01:00
|
|
|
if link_list:
|
|
|
|
for link in link_list:
|
|
|
|
link_map[link] = os.path.basename(check)
|
2009-08-01 01:47:22 +02:00
|
|
|
break
|
|
|
|
|
|
|
|
if os.path.islink(check):
|
2019-05-17 00:11:03 +02:00
|
|
|
print(" LINK %s" % check)
|
2010-01-12 14:00:24 +01:00
|
|
|
link_list.append(os.path.basename(check))
|
2009-08-01 01:47:22 +02:00
|
|
|
check = os.path.dirname(check) + "/" + os.readlink(check)
|
|
|
|
|
2008-12-29 05:41:00 +01:00
|
|
|
elif not l in goodlist and not l in masterlist:
|
|
|
|
goodlist.append(l)
|
2008-11-25 02:10:28 +01:00
|
|
|
masterlist.extend(locallist)
|
2010-01-12 14:00:24 +01:00
|
|
|
|
2008-11-25 02:10:28 +01:00
|
|
|
for l in locallist:
|
|
|
|
collectlibs(l, masterlist, targetdir)
|
|
|
|
|
2009-08-01 01:47:22 +02:00
|
|
|
exit;
|
2008-11-25 02:10:28 +01:00
|
|
|
binname = sys.argv[1]
|
|
|
|
targetdir = os.path.dirname(binname)
|
2019-05-17 00:11:03 +02:00
|
|
|
print("Searching for libraries in ", binname, "...")
|
2008-11-25 02:10:28 +01:00
|
|
|
libs = [binname]
|
|
|
|
collectlibs(sys.argv[1], libs, targetdir)
|
|
|
|
|
2010-01-12 14:00:24 +01:00
|
|
|
|
2019-05-17 00:11:03 +02:00
|
|
|
print()
|
|
|
|
print("System libraries used...")
|
2008-12-29 05:41:00 +01:00
|
|
|
goodlist.sort()
|
|
|
|
for l in goodlist:
|
2019-05-17 00:11:03 +02:00
|
|
|
print(l)
|
2008-12-29 05:41:00 +01:00
|
|
|
|
|
|
|
|
2019-05-17 00:11:03 +02:00
|
|
|
print()
|
|
|
|
print("Fixing library install names...")
|
2008-11-25 02:10:28 +01:00
|
|
|
in_tool_cmdline = "install_name_tool "
|
|
|
|
for lib in libs:
|
|
|
|
libbase = os.path.basename(lib)
|
2010-01-12 14:00:24 +01:00
|
|
|
if libbase in link_map:
|
|
|
|
libbase = link_map[libbase]
|
2009-06-02 02:16:45 +02:00
|
|
|
in_tool_cmdline = in_tool_cmdline + ("-change '%s' '@executable_path/%s' " % (lib, libbase))
|
2008-11-25 02:10:28 +01:00
|
|
|
for lib in libs:
|
|
|
|
libbase = os.path.basename(lib)
|
2010-01-12 14:00:24 +01:00
|
|
|
|
|
|
|
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))
|
2010-01-12 14:00:24 +01:00
|
|
|
else:
|
2019-05-17 00:11:03 +02:00
|
|
|
print("%s -> @executable_path/%s" % (lib, libbase))
|
2010-01-12 14:00:24 +01:00
|
|
|
|
2009-06-02 02:16:45 +02:00
|
|
|
os.system("%s -id '@executable_path/%s' '%s/%s'" % (in_tool_cmdline, libbase, targetdir, libbase))
|
2008-11-25 02:10:28 +01:00
|
|
|
sys.stdout.flush()
|
2008-12-29 05:41:00 +01:00
|
|
|
|
2012-05-13 02:58:01 +02:00
|
|
|
if badlist:
|
2019-05-17 00:11:03 +02:00
|
|
|
print()
|
|
|
|
print("WARNING: The following libraries have blacklisted paths:")
|
2012-05-13 02:58:01 +02:00
|
|
|
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.")
|
2010-01-12 14:00:24 +01:00
|
|
|
|
2019-05-17 00:11:03 +02:00
|
|
|
print()
|
|
|
|
print("All done!")
|