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