blob: 3b5cd37f7b16b2737add8b2eecc089f94b7438ef [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001CHRPATH_BIN ?= "chrpath"
2PREPROCESS_RELOCATE_DIRS ?= ""
3
4def 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 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()))
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 Williamsc0f7c042017-02-23 20:41:17 -060042 bb.fatal("%s: chrpath command failed with exit code %d:\n%s%s" % (d.getVar('PN', True), p.returncode, out, err))
Patrick Williamsc124f4f2015-09-15 14:41:29 -050043
44def 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 Williamsc0f7c042017-02-23 20:41:17 -060048 out, err = p.communicate()
49 # If returned successfully, process stdout for results
Patrick Williamsc124f4f2015-09-15 14:41:29 -050050 if p.returncode != 0:
51 return
Patrick Williamsc0f7c042017-02-23 20:41:17 -060052 for l in out.split("\n"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050053 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 Williamsc0f7c042017-02-23 20:41:17 -060061 out, err = p.communicate()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050062
63def 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
111def 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