source: Binaries/macRelink.py @ 4437

Last change on this file since 4437 was 4437, checked in by toby, 3 years ago

add fix for paths in libraries

  • Property svn:eol-style set to native
File size: 2.2 KB
RevLine 
[4436]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    for f in fileList:
48        cmd = ['otool','-L',f]
49        s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
50        out,err = MakeByte2str(s.communicate())
51        for i in out.split('\n')[1:]: 
52            if not i: continue
53            lib = i.split()[0]
54            if os.path.split(lib)[0] == "/usr/lib": # ignore items in system library (usually libSystem.B.dylib)
55                if "libSystem" not in lib: print("ignoring ",lib)
56                continue
57            if "@rpath" in lib: continue
58            if lib not in libs:
59                libs[lib] = []
60            libs[lib].append(f)
61    for key in libs:
62        newkey = os.path.join('@rpath',os.path.split(key)[1])
63        print('Fixing',key,'to',newkey)
64        for f in libs[key]:
65            print('\t',os.path.split(f)[1])
66            cmd = ["install_name_tool","-change",key,newkey,f]
67            s = subprocess.Popen(cmd,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
68            out,err = MakeByte2str(s.communicate())
Note: See TracBrowser for help on using the repository browser.