blob: ad3c3975a576c98e9c4ed55249f6c6c4bbd7e3c4 [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()))
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:
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 Bishop6e60e8b2018-02-01 10:27:11 -050047 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 -050048
49def 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 Williamsc0f7c042017-02-23 20:41:17 -060053 out, err = p.communicate()
54 # If returned successfully, process stdout for results
Patrick Williamsc124f4f2015-09-15 14:41:29 -050055 if p.returncode != 0:
56 return
Patrick Williamsc0f7c042017-02-23 20:41:17 -060057 for l in out.split("\n"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -050058 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 Williamsc0f7c042017-02-23 20:41:17 -060066 out, err = p.communicate()
Patrick Williamsc124f4f2015-09-15 14:41:29 -050067
68def 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 Bishop6e60e8b2018-02-01 10:27:11 -050075 hostos = d.getVar("HOST_OS")
Patrick Williamsc124f4f2015-09-15 14:41:29 -050076
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
116def 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