2008-12-29 02:13:55 +01:00
|
|
|
#!/usr/bin/env python
|
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)
|
2009-08-01 01:47:22 +02:00
|
|
|
print "found %s:" % l
|
|
|
|
|
|
|
|
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))
|
|
|
|
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):
|
2010-01-12 14:00:24 +01:00
|
|
|
print " LINK %s" % check
|
|
|
|
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)
|
|
|
|
print "Searching for libraries in ", binname, "..."
|
|
|
|
libs = [binname]
|
|
|
|
collectlibs(sys.argv[1], libs, targetdir)
|
|
|
|
|
2010-01-12 14:00:24 +01:00
|
|
|
|
2008-12-29 05:41:00 +01:00
|
|
|
print
|
|
|
|
print "System libraries used..."
|
|
|
|
goodlist.sort()
|
|
|
|
for l in goodlist:
|
|
|
|
print l
|
|
|
|
|
|
|
|
|
|
|
|
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]
|
|
|
|
print "%s -> @executable_path/%s (REMAPPED)" % (lib, libbase)
|
|
|
|
else:
|
|
|
|
print "%s -> @executable_path/%s" % (lib, libbase)
|
|
|
|
|
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:
|
|
|
|
print
|
|
|
|
print "WARNING: The following libraries have blacklisted paths:"
|
|
|
|
for lib in sorted(badlist):
|
|
|
|
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
|
|
|
|
2008-11-25 02:10:28 +01:00
|
|
|
print
|
|
|
|
print "All done!"
|