blob: 2870c10d51599b8462a694453d31db5cee9b1428 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001CHRPATH_BIN ?= "chrpath"
2PREPROCESS_RELOCATE_DIRS ?= ""
3
Brad Bishopacc069e2019-09-13 06:48:36 -04004def process_file_linux(cmd, fpath, rootdir, baseprefix, tmpdir, d, break_hardlinks = False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -05005 import subprocess as sub
6
7 p = sub.Popen([cmd, '-l', fpath],stdout=sub.PIPE,stderr=sub.PIPE)
Patrick Williamsc0f7c042017-02-23 20:41:17 -06008 out, err = p.communicate()
9 # If returned successfully, process stdout for results
Patrick Williamsc124f4f2015-09-15 14:41:29 -050010 if p.returncode != 0:
11 return
12
Patrick Williamsc0f7c042017-02-23 20:41:17 -060013 out = out.decode('utf-8')
14
Patrick Williamsc124f4f2015-09-15 14:41:29 -050015 # Handle RUNPATH as well as RPATH
Patrick Williamsc0f7c042017-02-23 20:41:17 -060016 out = out.replace("RUNPATH=","RPATH=")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050017 # Throw away everything other than the rpath list
Patrick Williamsc0f7c042017-02-23 20:41:17 -060018 curr_rpath = out.partition("RPATH=")[2]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050019 #bb.note("Current rpath for %s is %s" % (fpath, curr_rpath.strip()))
Brad Bishop6e60e8b2018-02-01 10:27:11 -050020 rpaths = curr_rpath.strip().split(":")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050021 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 Bishop6e60e8b2018-02-01 10:27:11 -050026 new_rpaths.append(rpath)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 continue
28 rpath = os.path.normpath(rpath)
29 if baseprefix not in rpath and tmpdir not in rpath:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050030 # 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 Williamsc124f4f2015-09-15 14:41:29 -050036 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -050037 new_rpaths.append("$ORIGIN/" + os.path.relpath(rpath, os.path.dirname(fpath.replace(rootdir, "/"))))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050038 modified = True
39
40 # if we have modified some rpaths call chrpath to update the binary
41 if modified:
Brad Bishopacc069e2019-09-13 06:48:36 -040042 if break_hardlinks:
43 bb.utils.break_hardlinks(fpath)
44
Patrick Williamsc124f4f2015-09-15 14:41:29 -050045 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 Bishop6e60e8b2018-02-01 10:27:11 -050050 bb.fatal("%s: chrpath command failed with exit code %d:\n%s%s" % (d.getVar('PN'), p.returncode, out, err))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050051
Brad Bishopacc069e2019-09-13 06:48:36 -040052def process_file_darwin(cmd, fpath, rootdir, baseprefix, tmpdir, d, break_hardlinks = False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 import subprocess as sub
54
55 p = sub.Popen([d.expand("${HOST_PREFIX}otool"), '-L', fpath],stdout=sub.PIPE,stderr=sub.PIPE)
Patrick Williamsc0f7c042017-02-23 20:41:17 -060056 out, err = p.communicate()
57 # If returned successfully, process stdout for results
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 if p.returncode != 0:
59 return
Patrick Williamsc0f7c042017-02-23 20:41:17 -060060 for l in out.split("\n"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050061 if "(compatibility" not in l:
62 continue
63 rpath = l.partition("(compatibility")[0].strip()
64 if baseprefix not in rpath:
65 continue
66
Brad Bishopacc069e2019-09-13 06:48:36 -040067 if break_hardlinks:
68 bb.utils.break_hardlinks(fpath)
69
Patrick Williamsc124f4f2015-09-15 14:41:29 -050070 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 Williamsc0f7c042017-02-23 20:41:17 -060072 out, err = p.communicate()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050073
Brad Bishopacc069e2019-09-13 06:48:36 -040074def process_dir(rootdir, directory, d, break_hardlinks = False):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050075 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 Bishop6e60e8b2018-02-01 10:27:11 -050081 hostos = d.getVar("HOST_OS")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050082
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 Bishopacc069e2019-09-13 06:48:36 -0400104 process_dir(rootdir, fpath, d, break_hardlinks = break_hardlinks)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500105 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 Bishopacc069e2019-09-13 06:48:36 -0400117
118 process_file(cmd, fpath, rootdir, baseprefix, tmpdir, d, break_hardlinks = break_hardlinks)
119
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500120 if perms:
121 os.chmod(fpath, perms)
122
123def 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