diff options
author | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-08-22 11:07:22 +0000 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2013-08-23 16:20:14 +0100 |
commit | 9f67e31ab2ad0a67b0a66f2d2de182cc92bdb8d4 (patch) | |
tree | bacda5592172424d5784941d704faeff43562ff3 /meta | |
parent | 45de2c2b5a98d2d353b439440e3439893de093de (diff) | |
download | poky-9f67e31ab2ad0a67b0a66f2d2de182cc92bdb8d4.tar.gz |
chrpath: Add support for relocating darwin binaries
On darwin, install_name_tool can be used to relocate binaries/libraries. This
adds support for adjusting them with relative paths rather than hardcoded ones.
The Linux code is factored out into a function but is otherwise unchanged.
(From OE-Core rev: ed5ace3437eb0f751172e6b93399639c94b89e59)
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta')
-rw-r--r-- | meta/classes/chrpath.bbclass | 138 |
1 files changed, 88 insertions, 50 deletions
diff --git a/meta/classes/chrpath.bbclass b/meta/classes/chrpath.bbclass index 0c7ab77a81..61a24b3f5a 100644 --- a/meta/classes/chrpath.bbclass +++ b/meta/classes/chrpath.bbclass | |||
@@ -1,18 +1,103 @@ | |||
1 | CHRPATH_BIN ?= "chrpath" | 1 | CHRPATH_BIN ?= "chrpath" |
2 | PREPROCESS_RELOCATE_DIRS ?= "" | 2 | PREPROCESS_RELOCATE_DIRS ?= "" |
3 | 3 | ||
4 | def process_dir (directory, d): | 4 | def process_file_linux(cmd, fpath, basedir, tmpdir, d): |
5 | import subprocess as sub | 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): | ||
6 | import stat | 82 | import stat |
7 | 83 | ||
8 | cmd = d.expand('${CHRPATH_BIN}') | 84 | cmd = d.expand('${CHRPATH_BIN}') |
9 | tmpdir = os.path.normpath(d.getVar('TMPDIR')) | 85 | tmpdir = os.path.normpath(d.getVar('TMPDIR')) |
10 | basedir = os.path.normpath(d.expand('${base_prefix}')) | 86 | basedir = os.path.normpath(d.expand('${base_prefix}')) |
87 | hostos = d.getVar("HOST_OS", True) | ||
11 | 88 | ||
12 | #bb.debug("Checking %s for binaries to process" % directory) | 89 | #bb.debug("Checking %s for binaries to process" % directory) |
13 | if not os.path.exists(directory): | 90 | if not os.path.exists(directory): |
14 | return | 91 | return |
15 | 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 | |||
16 | dirs = os.listdir(directory) | 101 | dirs = os.listdir(directory) |
17 | for file in dirs: | 102 | for file in dirs: |
18 | fpath = directory + "/" + file | 103 | fpath = directory + "/" + file |
@@ -35,55 +120,8 @@ def process_dir (directory, d): | |||
35 | else: | 120 | else: |
36 | # Temporarily make the file writeable so we can chrpath it | 121 | # Temporarily make the file writeable so we can chrpath it |
37 | os.chmod(fpath, perms|stat.S_IRWXU) | 122 | os.chmod(fpath, perms|stat.S_IRWXU) |
38 | 123 | process_file(cmd, fpath, basedir, tmpdir, d) | |
39 | p = sub.Popen([cmd, '-l', fpath],stdout=sub.PIPE,stderr=sub.PIPE) | 124 | |
40 | err, out = p.communicate() | ||
41 | # If returned succesfully, process stderr for results | ||
42 | if p.returncode != 0: | ||
43 | continue | ||
44 | |||
45 | # Throw away everything other than the rpath list | ||
46 | curr_rpath = err.partition("RPATH=")[2] | ||
47 | #bb.note("Current rpath for %s is %s" % (fpath, curr_rpath.strip())) | ||
48 | rpaths = curr_rpath.split(":") | ||
49 | new_rpaths = [] | ||
50 | for rpath in rpaths: | ||
51 | # If rpath is already dynamic copy it to new_rpath and continue | ||
52 | if rpath.find("$ORIGIN") != -1: | ||
53 | new_rpaths.append(rpath.strip()) | ||
54 | continue | ||
55 | rpath = os.path.normpath(rpath) | ||
56 | # If the rpath shares a root with base_prefix determine a new dynamic rpath from the | ||
57 | # base_prefix shared root | ||
58 | if rpath.find(basedir) != -1: | ||
59 | depth = fpath.partition(basedir)[2].count('/') | ||
60 | libpath = rpath.partition(basedir)[2].strip() | ||
61 | # otherwise (i.e. cross packages) determine a shared root based on the TMPDIR | ||
62 | # NOTE: This will not work reliably for cross packages, particularly in the case | ||
63 | # where your TMPDIR is a short path (i.e. /usr/poky) as chrpath cannot insert an | ||
64 | # rpath longer than that which is already set. | ||
65 | elif rpath.find(tmpdir) != -1: | ||
66 | depth = fpath.rpartition(tmpdir)[2].count('/') | ||
67 | libpath = rpath.partition(tmpdir)[2].strip() | ||
68 | else: | ||
69 | new_rpaths.append(rpath.strip()) | ||
70 | continue | ||
71 | base = "$ORIGIN" | ||
72 | while depth > 1: | ||
73 | base += "/.." | ||
74 | depth-=1 | ||
75 | new_rpaths.append("%s%s" % (base, libpath)) | ||
76 | |||
77 | # if we have modified some rpaths call chrpath to update the binary | ||
78 | if len(new_rpaths): | ||
79 | args = ":".join(new_rpaths) | ||
80 | #bb.note("Setting rpath for %s to %s" %(fpath, args)) | ||
81 | p = sub.Popen([cmd, '-r', args, fpath],stdout=sub.PIPE,stderr=sub.PIPE) | ||
82 | out, err = p.communicate() | ||
83 | if p.returncode != 0: | ||
84 | bb.error("%s: chrpath command failed with exit code %d:\n%s%s" % (d.getVar('PN', True), p.returncode, out, err)) | ||
85 | raise bb.build.FuncFailed | ||
86 | |||
87 | if perms: | 125 | if perms: |
88 | os.chmod(fpath, perms) | 126 | os.chmod(fpath, perms) |
89 | 127 | ||