chrpath: Add support for relocating darwin binaries

On darwin, install_name_tool can be used to relocate binaries/libraries. This
adds support for adjusting them with relative paths rather than hardcoded ones.
The Linux code is factored out into a function but is otherwise unchanged.

(From OE-Core rev: ed5ace3437eb0f751172e6b93399639c94b89e59)

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Richard Purdie 2013-08-22 11:07:22 +00:00
parent 45de2c2b5a
commit 9f67e31ab2
1 changed files with 88 additions and 50 deletions

View File

@ -1,46 +1,14 @@
CHRPATH_BIN ?= "chrpath"
PREPROCESS_RELOCATE_DIRS ?= ""
def process_dir (directory, d):
def process_file_linux(cmd, fpath, basedir, tmpdir, d):
import subprocess as sub
import stat
cmd = d.expand('${CHRPATH_BIN}')
tmpdir = os.path.normpath(d.getVar('TMPDIR'))
basedir = os.path.normpath(d.expand('${base_prefix}'))
#bb.debug("Checking %s for binaries to process" % directory)
if not os.path.exists(directory):
return
dirs = os.listdir(directory)
for file in dirs:
fpath = directory + "/" + file
fpath = os.path.normpath(fpath)
if os.path.islink(fpath):
# Skip symlinks
continue
if os.path.isdir(fpath):
process_dir(fpath, d)
else:
#bb.note("Testing %s for relocatability" % fpath)
# We need read and write permissions for chrpath, if we don't have
# them then set them temporarily. Take a copy of the files
# permissions so that we can restore them afterwards.
perms = os.stat(fpath)[stat.ST_MODE]
if os.access(fpath, os.W_OK|os.R_OK):
perms = None
else:
# Temporarily make the file writeable so we can chrpath it
os.chmod(fpath, perms|stat.S_IRWXU)
p = sub.Popen([cmd, '-l', fpath],stdout=sub.PIPE,stderr=sub.PIPE)
err, out = p.communicate()
# If returned succesfully, process stderr for results
if p.returncode != 0:
continue
return
# Throw away everything other than the rpath list
curr_rpath = err.partition("RPATH=")[2]
@ -67,7 +35,7 @@ def process_dir (directory, d):
libpath = rpath.partition(tmpdir)[2].strip()
else:
new_rpaths.append(rpath.strip())
continue
return
base = "$ORIGIN"
while depth > 1:
base += "/.."
@ -84,6 +52,76 @@ def process_dir (directory, d):
bb.error("%s: chrpath command failed with exit code %d:\n%s%s" % (d.getVar('PN', True), p.returncode, out, err))
raise bb.build.FuncFailed
def process_file_darwin(cmd, fpath, basedir, tmpdir, d):
import subprocess as sub
p = sub.Popen([d.expand("${HOST_PREFIX}otool"), '-L', fpath],stdout=sub.PIPE,stderr=sub.PIPE)
err, out = p.communicate()
# If returned succesfully, process stderr for results
if p.returncode != 0:
return
for l in err.split("\n"):
if "(compatibility" not in l:
continue
rpath = l.partition("(compatibility")[0].strip()
if rpath.find(basedir) != -1:
depth = fpath.partition(basedir)[2].count('/')
libpath = rpath.partition(basedir)[2].strip()
else:
continue
base = "@loader_path"
while depth > 1:
base += "/.."
depth-=1
base = base + libpath
p = sub.Popen([d.expand("${HOST_PREFIX}install_name_tool"), '-change', rpath, base, fpath],stdout=sub.PIPE,stderr=sub.PIPE)
err, out = p.communicate()
def process_dir (directory, d):
import stat
cmd = d.expand('${CHRPATH_BIN}')
tmpdir = os.path.normpath(d.getVar('TMPDIR'))
basedir = os.path.normpath(d.expand('${base_prefix}'))
hostos = d.getVar("HOST_OS", True)
#bb.debug("Checking %s for binaries to process" % directory)
if not os.path.exists(directory):
return
if "linux" in hostos:
process_file = process_file_linux
elif "darwin" in hostos:
process_file = process_file_darwin
else:
# Relocations not supported
return
dirs = os.listdir(directory)
for file in dirs:
fpath = directory + "/" + file
fpath = os.path.normpath(fpath)
if os.path.islink(fpath):
# Skip symlinks
continue
if os.path.isdir(fpath):
process_dir(fpath, d)
else:
#bb.note("Testing %s for relocatability" % fpath)
# We need read and write permissions for chrpath, if we don't have
# them then set them temporarily. Take a copy of the files
# permissions so that we can restore them afterwards.
perms = os.stat(fpath)[stat.ST_MODE]
if os.access(fpath, os.W_OK|os.R_OK):
perms = None
else:
# Temporarily make the file writeable so we can chrpath it
os.chmod(fpath, perms|stat.S_IRWXU)
process_file(cmd, fpath, basedir, tmpdir, d)
if perms:
os.chmod(fpath, perms)