Removing PowerLine since we weren't using it and don't have plans to.
[debian/gnuradio] / dtools / bin / tweak-cell-for-cross-compiling
1 #!/usr/bin/env python
2
3 """
4 This should only be run on a native Cell machine.  E.g., ps3, qs21, etc.
5
6 It makes a few modifications to the file system and some pseudo shared libs
7 so that cross compiling from a build machine over NFS works.  The changes
8 do not harm local compilation.
9
10 We create a symlink from /mnt/cell-root that points to /
11 This allows the local and build machine to access the root filesystem
12 using a common name, /mnt/cell-root.  This is required because
13 configure hardcodes absolute paths into the generated Makefiles.
14
15 There are some .so files that aren't really shared libraries, but rather are
16 ascii linker scripts containing instructions to the linker.  Most of them
17 contain a GROUP directive that includes hard-coded paths relative to /.
18 We modify those files such that the hard-coded paths are relative to /mnt/cell-root
19 instead of /.  This allows them to work locally and while cross compiling over NFS.
20
21 E.g., /usr/lib/libc.so originally contains:
22
23     /* GNU ld script
24        Use the shared library, but some functions are only in
25        the static library, so try that secondarily.  */
26     OUTPUT_FORMAT(elf32-powerpc)
27     GROUP ( /lib/libc.so.6 /usr/lib/libc_nonshared.a  AS_NEEDED ( /lib/ld.so.1 ) )
28
29 We modify it such that it reads:
30
31     /* GNU ld script
32        Use the shared library, but some functions are only in
33        the static library, so try that secondarily.  */
34     OUTPUT_FORMAT(elf32-powerpc)
35     GROUP ( /mnt/cell-root/lib/libc.so.6 /mnt/cell-root/usr/lib/libc_nonshared.a  AS_NEEDED ( /mnt/cell-root/lib/ld.so.1 ) )
36
37 We backup <foo>.so to <foo>.so.original
38
39 """
40
41 import os
42 import os.path
43 import sys
44 import shutil
45 import re
46
47 cell_root_path = '/mnt/cell-root'
48
49 def ensure_cell():
50     s = open('/proc/cpuinfo','r').read()
51     if s.find('Cell Broadband Engine') == -1:
52         sys.stderr.write('This program should only be run on Cell machines.\n')
53         raise SystemExit, 1
54
55 def make_symlinks():
56     create_symlink_if_reqd(cell_root_path, "..")
57     create_symlink_if_reqd("/opt/cell/toolchain", "../../usr")
58     create_symlink_if_reqd("/opt/cell/sysroot", "../..")
59
60 def symlink_exists_and_is_ok(path, contents):
61     return (os.path.islink(path) and os.readlink(path) == contents)
62
63 def create_symlink_if_reqd(path, contents):
64     if symlink_exists_and_is_ok(path, contents):
65         return
66
67     if os.path.islink(path):
68         # Is a symlink but points wrong place
69         os.remove(path)
70         os.symlink(contents, path)
71         return
72
73     if os.path.isdir(path):
74         # if it's empty we'll remove it and create the link
75         try:
76             os.rmdir(path)
77         except:
78             # directory wasn't empty
79             sys.stderr.write("There's already something at %s.\n" % (path,))
80             sys.stderr.write("Please remove it or move it out of the way and try again.\n")
81             raise SystemExit, 1
82         os.symlink(contents, path)
83         return
84         
85     if os.path.exists(path):
86         # There's something here, return an error
87         sys.stderr.write("There's already something at %s.\n" % (path,))
88         sys.stderr.write("Please remove it or move it out of the way and try again.\n")
89         raise SystemExit, 1
90
91     # nothing there; go ahead and create the symlink
92     os.symlink(contents, path)
93
94
95 def find_ascii_shared_libs():
96     cmd = "find /lib /lib64 /usr/lib /usr/lib64 -name '*.so' -type f -print 2>/dev/null | xargs file | grep -v ELF | cut -d: -f 1"
97     pipe = os.popen(cmd, 'r')
98     filenames = pipe.read().splitlines()
99     return filenames
100
101
102 def make_backup_copy(src):
103     dst = src + '.original'
104     if not os.path.exists(dst):
105         shutil.copy2(src, dst)
106
107
108 def edit_file(name):
109     def replace_group_body(mo):
110         pat = ' /(?!' + cell_root_path[1:] + ')'  # negative lookahead assertion
111         new = re.sub(pat, ' ' + cell_root_path + '/', mo.group(2))
112         return mo.group(1) + new + mo.group(3)
113         
114     f = open(name,'r')
115     s = f.read()
116     f.close()
117     
118     pat = re.compile(r'^( *GROUP *\()(.*)(\) *)$', re.M)
119     t = pat.sub(replace_group_body, s)
120
121     f = open(name,'w')
122     f.write(t)
123     f.close()
124
125
126 def edit_ascii_shared_libs():
127     print "Please be patient, this takes awhile..."
128     filenames = find_ascii_shared_libs()
129     for f in filenames:
130         make_backup_copy(f)
131         edit_file(f)
132
133
134 def main():
135     ensure_cell()
136     make_symlinks()
137     edit_ascii_shared_libs()
138
139
140 if __name__ == '__main__':
141     main()
142     
143