source: Binaries/macRelink.py @ 4996

Last change on this file since 4996 was 4727, checked in by toby, 4 years ago

show needed OSX libraries

  • Property svn:eol-style set to native
File size: 2.4 KB
Line 
1#!/usr/bin/env python
2'''
3*macRelink: Remove hardcoded library references*
4===================================================
5
6'''
7from __future__ import division, print_function
8import sys, os, glob, subprocess
9#os.path, stat, shutil, subprocess, plistlib
10def Usage():
11    print("\n\tUsage: python "+sys.argv[0]+" <binary-dir>\n")
12    sys.exit()
13   
14def MakeByte2str(arg):
15    '''Convert output from subprocess pipes (bytes) to str (unicode) in Python 3.
16    In Python 2: Leaves output alone (already str).
17    Leaves stuff of other types alone (including unicode in Py2)
18    Works recursively for string-like stuff in nested loops and tuples.
19
20    typical use::
21
22        out = MakeByte2str(out)
23
24    or::
25
26        out,err = MakeByte2str(s.communicate())
27   
28    '''
29    if isinstance(arg,str): return arg
30    if isinstance(arg,bytes): return arg.decode()
31    if isinstance(arg,list):
32        return [MakeByte2str(i) for i in arg]
33    if isinstance(arg,tuple):
34        return tuple([MakeByte2str(i) for i in arg])
35    return arg
36
37if __name__ == '__main__':
38    if len(sys.argv) == 2:
39        dirloc = os.path.abspath(sys.argv[1])
40    else:
41        Usage()
42        raise Exception
43
44    fileList = glob.glob(os.path.join(dirloc,'*.so'))
45
46    libs = {}
47    ignorelist = []
48    for f in fileList:
49        cmd = ['otool','-L',f]
50        s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
51        out,err = MakeByte2str(s.communicate())
52        for i in out.split('\n')[1:]: 
53            if not i: continue
54            lib = i.split()[0]
55            if os.path.split(lib)[0] == "/usr/lib": # ignore items in system library (usually libSystem.B.dylib)
56                if "libSystem" not in lib: print("ignoring ",lib)
57                continue
58            if "@rpath" in lib and lib not in ignorelist:
59                ignorelist.append(lib)
60                print("ignoring ",lib)
61                continue
62            if lib not in libs:
63                libs[lib] = []
64            libs[lib].append(f)
65    for key in libs:
66        newkey = os.path.join('@rpath',os.path.split(key)[1])
67        print('Fixing',key,'to',newkey)
68        for f in libs[key]:
69            print('\t',os.path.split(f)[1])
70            cmd = ["install_name_tool","-change",key,newkey,f]
71            s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
72            out,err = MakeByte2str(s.communicate())
Note: See TracBrowser for help on using the repository browser.