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