diff options
Diffstat (limited to 'meta/classes/chrpath.bbclass')
-rw-r--r-- | meta/classes/chrpath.bbclass | 135 |
1 files changed, 135 insertions, 0 deletions
diff --git a/meta/classes/chrpath.bbclass b/meta/classes/chrpath.bbclass new file mode 100644 index 0000000000..61a24b3f5a --- /dev/null +++ b/meta/classes/chrpath.bbclass | |||
@@ -0,0 +1,135 @@ | |||
1 | CHRPATH_BIN ?= "chrpath" | ||
2 | PREPROCESS_RELOCATE_DIRS ?= "" | ||
3 | |||
4 | def process_file_linux(cmd, fpath, basedir, tmpdir, d): | ||
5 | import subprocess as sub | ||
6 | |||
7 | p = sub.Popen([cmd, '-l', fpath],stdout=sub.PIPE,stderr=sub.PIPE) | ||
8 | err, out = p.communicate() | ||
9 | # If returned succesfully, process stderr for results | ||
10 | if p.returncode != 0: | ||
11 | return | ||
12 | |||
13 | # Throw away everything other than the rpath list | ||
14 | curr_rpath = err.partition("RPATH=")[2] | ||
15 | #bb.note("Current rpath for %s is %s" % (fpath, curr_rpath.strip())) | ||
16 | rpaths = curr_rpath.split(":") | ||
17 | new_rpaths = [] | ||
18 | for rpath in rpaths: | ||
19 | # If rpath is already dynamic copy it to new_rpath and continue | ||
20 | if rpath.find("$ORIGIN") != -1: | ||
21 | new_rpaths.append(rpath.strip()) | ||
22 | continue | ||
23 | rpath = os.path.normpath(rpath) | ||
24 | # If the rpath shares a root with base_prefix determine a new dynamic rpath from the | ||
25 | # base_prefix shared root | ||
26 | if rpath.find(basedir) != -1: | ||
27 | depth = fpath.partition(basedir)[2].count('/') | ||
28 | libpath = rpath.partition(basedir)[2].strip() | ||
29 | # otherwise (i.e. cross packages) determine a shared root based on the TMPDIR | ||
30 | # NOTE: This will not work reliably for cross packages, particularly in the case | ||
31 | # where your TMPDIR is a short path (i.e. /usr/poky) as chrpath cannot insert an | ||
32 | # rpath longer than that which is already set. | ||
33 | elif rpath.find(tmpdir) != -1: | ||
34 | depth = fpath.rpartition(tmpdir)[2].count('/') | ||
35 | libpath = rpath.partition(tmpdir)[2].strip() | ||
36 | else: | ||
37 | new_rpaths.append(rpath.strip()) | ||
38 | return | ||
39 | base = "$ORIGIN" | ||
40 | while depth > 1: | ||
41 | base += "/.." | ||
42 | depth-=1 | ||
43 | new_rpaths.append("%s%s" % (base, libpath)) | ||
44 | |||
45 | # if we have modified some rpaths call chrpath to update the binary | ||
46 | if len(new_rpaths): | ||
47 | args = ":".join(new_rpaths) | ||
48 | #bb.note("Setting rpath for %s to %s" %(fpath, args)) | ||
49 | p = sub.Popen([cmd, '-r', args, fpath],stdout=sub.PIPE,stderr=sub.PIPE) | ||
50 | out, err = p.communicate() | ||
51 | if p.returncode != 0: | ||
52 | bb.error("%s: chrpath command failed with exit code %d:\n%s%s" % (d.getVar('PN', True), p.returncode, out, err)) | ||
53 | raise bb.build.FuncFailed | ||
54 | |||
55 | def process_file_darwin(cmd, fpath, basedir, tmpdir, d): | ||
56 | import subprocess as sub | ||
57 | |||
58 | p = sub.Popen([d.expand("${HOST_PREFIX}otool"), '-L', fpath],stdout=sub.PIPE,stderr=sub.PIPE) | ||
59 | err, out = p.communicate() | ||
60 | # If returned succesfully, process stderr for results | ||
61 | if p.returncode != 0: | ||
62 | return | ||
63 | for l in err.split("\n"): | ||
64 | if "(compatibility" not in l: | ||
65 | continue | ||
66 | rpath = l.partition("(compatibility")[0].strip() | ||
67 | if rpath.find(basedir) != -1: | ||
68 | depth = fpath.partition(basedir)[2].count('/') | ||
69 | libpath = rpath.partition(basedir)[2].strip() | ||
70 | else: | ||
71 | continue | ||
72 | |||
73 | base = "@loader_path" | ||
74 | while depth > 1: | ||
75 | base += "/.." | ||
76 | depth-=1 | ||
77 | base = base + libpath | ||
78 | p = sub.Popen([d.expand("${HOST_PREFIX}install_name_tool"), '-change', rpath, base, fpath],stdout=sub.PIPE,stderr=sub.PIPE) | ||
79 | err, out = p.communicate() | ||
80 | |||
81 | def process_dir (directory, d): | ||
82 | import stat | ||
83 | |||
84 | cmd = d.expand('${CHRPATH_BIN}') | ||
85 | tmpdir = os.path.normpath(d.getVar('TMPDIR')) | ||
86 | basedir = os.path.normpath(d.expand('${base_prefix}')) | ||
87 | hostos = d.getVar("HOST_OS", True) | ||
88 | |||
89 | #bb.debug("Checking %s for binaries to process" % directory) | ||
90 | if not os.path.exists(directory): | ||
91 | return | ||
92 | |||
93 | if "linux" in hostos: | ||
94 | process_file = process_file_linux | ||
95 | elif "darwin" in hostos: | ||
96 | process_file = process_file_darwin | ||
97 | else: | ||
98 | # Relocations not supported | ||
99 | return | ||
100 | |||
101 | dirs = os.listdir(directory) | ||
102 | for file in dirs: | ||
103 | fpath = directory + "/" + file | ||
104 | fpath = os.path.normpath(fpath) | ||
105 | if os.path.islink(fpath): | ||
106 | # Skip symlinks | ||
107 | continue | ||
108 | |||
109 | if os.path.isdir(fpath): | ||
110 | process_dir(fpath, d) | ||
111 | else: | ||
112 | #bb.note("Testing %s for relocatability" % fpath) | ||
113 | |||
114 | # We need read and write permissions for chrpath, if we don't have | ||
115 | # them then set them temporarily. Take a copy of the files | ||
116 | # permissions so that we can restore them afterwards. | ||
117 | perms = os.stat(fpath)[stat.ST_MODE] | ||
118 | if os.access(fpath, os.W_OK|os.R_OK): | ||
119 | perms = None | ||
120 | else: | ||
121 | # Temporarily make the file writeable so we can chrpath it | ||
122 | os.chmod(fpath, perms|stat.S_IRWXU) | ||
123 | process_file(cmd, fpath, basedir, tmpdir, d) | ||
124 | |||
125 | if perms: | ||
126 | os.chmod(fpath, perms) | ||
127 | |||
128 | def rpath_replace (path, d): | ||
129 | bindirs = d.expand("${bindir} ${sbindir} ${base_sbindir} ${base_bindir} ${libdir} ${base_libdir} ${libexecdir} ${PREPROCESS_RELOCATE_DIRS}").split() | ||
130 | |||
131 | for bindir in bindirs: | ||
132 | #bb.note ("Processing directory " + bindir) | ||
133 | directory = path + "/" + bindir | ||
134 | process_dir (directory, d) | ||
135 | |||