Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 1 | CHRPATH_BIN ?= "chrpath" |
| 2 | PREPROCESS_RELOCATE_DIRS ?= "" |
| 3 | |
| 4 | def process_file_linux(cmd, fpath, rootdir, baseprefix, tmpdir, d): |
| 5 | import subprocess as sub |
| 6 | |
| 7 | p = sub.Popen([cmd, '-l', fpath],stdout=sub.PIPE,stderr=sub.PIPE) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 8 | out, err = p.communicate() |
| 9 | # If returned successfully, process stdout for results |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 10 | if p.returncode != 0: |
| 11 | return |
| 12 | |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 13 | out = out.decode('utf-8') |
| 14 | |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 15 | # Handle RUNPATH as well as RPATH |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 16 | out = out.replace("RUNPATH=","RPATH=") |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 17 | # Throw away everything other than the rpath list |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 18 | curr_rpath = out.partition("RPATH=")[2] |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 19 | #bb.note("Current rpath for %s is %s" % (fpath, curr_rpath.strip())) |
| 20 | rpaths = curr_rpath.split(":") |
| 21 | new_rpaths = [] |
| 22 | modified = False |
| 23 | for rpath in rpaths: |
| 24 | # If rpath is already dynamic copy it to new_rpath and continue |
| 25 | if rpath.find("$ORIGIN") != -1: |
| 26 | new_rpaths.append(rpath.strip()) |
| 27 | continue |
| 28 | rpath = os.path.normpath(rpath) |
| 29 | if baseprefix not in rpath and tmpdir not in rpath: |
| 30 | new_rpaths.append(rpath.strip()) |
| 31 | continue |
| 32 | new_rpaths.append("$ORIGIN/" + os.path.relpath(rpath.strip(), os.path.dirname(fpath.replace(rootdir, "/")))) |
| 33 | modified = True |
| 34 | |
| 35 | # if we have modified some rpaths call chrpath to update the binary |
| 36 | if modified: |
| 37 | args = ":".join(new_rpaths) |
| 38 | #bb.note("Setting rpath for %s to %s" %(fpath, args)) |
| 39 | p = sub.Popen([cmd, '-r', args, fpath],stdout=sub.PIPE,stderr=sub.PIPE) |
| 40 | out, err = p.communicate() |
| 41 | if p.returncode != 0: |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 42 | bb.fatal("%s: chrpath command failed with exit code %d:\n%s%s" % (d.getVar('PN', True), p.returncode, out, err)) |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 43 | |
| 44 | def process_file_darwin(cmd, fpath, rootdir, baseprefix, tmpdir, d): |
| 45 | import subprocess as sub |
| 46 | |
| 47 | p = sub.Popen([d.expand("${HOST_PREFIX}otool"), '-L', fpath],stdout=sub.PIPE,stderr=sub.PIPE) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 48 | out, err = p.communicate() |
| 49 | # If returned successfully, process stdout for results |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 50 | if p.returncode != 0: |
| 51 | return |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 52 | for l in out.split("\n"): |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 53 | if "(compatibility" not in l: |
| 54 | continue |
| 55 | rpath = l.partition("(compatibility")[0].strip() |
| 56 | if baseprefix not in rpath: |
| 57 | continue |
| 58 | |
| 59 | newpath = "@loader_path/" + os.path.relpath(rpath, os.path.dirname(fpath.replace(rootdir, "/"))) |
| 60 | p = sub.Popen([d.expand("${HOST_PREFIX}install_name_tool"), '-change', rpath, newpath, fpath],stdout=sub.PIPE,stderr=sub.PIPE) |
Patrick Williams | c0f7c04 | 2017-02-23 20:41:17 -0600 | [diff] [blame^] | 61 | out, err = p.communicate() |
Patrick Williams | c124f4f | 2015-09-15 14:41:29 -0500 | [diff] [blame] | 62 | |
| 63 | def process_dir (rootdir, directory, d): |
| 64 | import stat |
| 65 | |
| 66 | rootdir = os.path.normpath(rootdir) |
| 67 | cmd = d.expand('${CHRPATH_BIN}') |
| 68 | tmpdir = os.path.normpath(d.getVar('TMPDIR', False)) |
| 69 | baseprefix = os.path.normpath(d.expand('${base_prefix}')) |
| 70 | hostos = d.getVar("HOST_OS", True) |
| 71 | |
| 72 | #bb.debug("Checking %s for binaries to process" % directory) |
| 73 | if not os.path.exists(directory): |
| 74 | return |
| 75 | |
| 76 | if "linux" in hostos: |
| 77 | process_file = process_file_linux |
| 78 | elif "darwin" in hostos: |
| 79 | process_file = process_file_darwin |
| 80 | else: |
| 81 | # Relocations not supported |
| 82 | return |
| 83 | |
| 84 | dirs = os.listdir(directory) |
| 85 | for file in dirs: |
| 86 | fpath = directory + "/" + file |
| 87 | fpath = os.path.normpath(fpath) |
| 88 | if os.path.islink(fpath): |
| 89 | # Skip symlinks |
| 90 | continue |
| 91 | |
| 92 | if os.path.isdir(fpath): |
| 93 | process_dir(rootdir, fpath, d) |
| 94 | else: |
| 95 | #bb.note("Testing %s for relocatability" % fpath) |
| 96 | |
| 97 | # We need read and write permissions for chrpath, if we don't have |
| 98 | # them then set them temporarily. Take a copy of the files |
| 99 | # permissions so that we can restore them afterwards. |
| 100 | perms = os.stat(fpath)[stat.ST_MODE] |
| 101 | if os.access(fpath, os.W_OK|os.R_OK): |
| 102 | perms = None |
| 103 | else: |
| 104 | # Temporarily make the file writeable so we can chrpath it |
| 105 | os.chmod(fpath, perms|stat.S_IRWXU) |
| 106 | process_file(cmd, fpath, rootdir, baseprefix, tmpdir, d) |
| 107 | |
| 108 | if perms: |
| 109 | os.chmod(fpath, perms) |
| 110 | |
| 111 | def rpath_replace (path, d): |
| 112 | bindirs = d.expand("${bindir} ${sbindir} ${base_sbindir} ${base_bindir} ${libdir} ${base_libdir} ${libexecdir} ${PREPROCESS_RELOCATE_DIRS}").split() |
| 113 | |
| 114 | for bindir in bindirs: |
| 115 | #bb.note ("Processing directory " + bindir) |
| 116 | directory = path + "/" + bindir |
| 117 | process_dir (path, directory, d) |
| 118 | |