angstrom: prefer the git version of tslib
[openembedded.git] / classes / relocatable.bbclass
blob2af3a7a1c4ab81f2602af5b5e88b0dce65f7cef7
1 SYSROOT_PREPROCESS_FUNCS += "relocatable_binaries_preprocess"
3 CHRPATH_BIN ?= "chrpath"
4 PREPROCESS_RELOCATE_DIRS ?= ""
6 def process_dir (directory, d):
7     import subprocess as sub
8     import stat
10     cmd = bb.data.expand('${CHRPATH_BIN}', d)
11     tmpdir = bb.data.getVar('TMPDIR', d)
12     basedir = bb.data.expand('${base_prefix}', d)
14     #bb.debug("Checking %s for binaries to process" % directory)
15     if not os.path.exists(directory):
16         return
18     dirs = os.listdir(directory)
19     for file in dirs:
20         fpath = directory + "/" + file
21         if os.path.islink(fpath):
22             # Skip symlinks
23             continue
25         if os.path.isdir(fpath):
26             process_dir(fpath, d)
27         else:
28             #bb.note("Testing %s for relocatability" % fpath)
30             # We need read and write permissions for chrpath, if we don't have
31             # them then set them temporarily. Take a copy of the files
32             # permissions so that we can restore them afterwards.
33             perms = os.stat(fpath)[stat.ST_MODE]
34             if os.access(fpath, os.W_OK|os.R_OK):
35                     perms = None
36             else:
37                 # Temporarily make the file writeable so we can chrpath it
38                 os.chmod(fpath, perms|stat.S_IRWXU)
40             p = sub.Popen([cmd, '-l', fpath],stdout=sub.PIPE,stderr=sub.PIPE)
41             err, out = p.communicate()
42             # If returned succesfully, process stderr for results
43             if p.returncode != 0:
44                 continue
46             # Throw away everything other than the rpath list
47             curr_rpath = err.partition("RPATH=")[2]
48             #bb.note("Current rpath for %s is %s" % (fpath, curr_rpath.strip()))
49             rpaths = curr_rpath.split(":")
50             new_rpaths = []
51             for rpath in rpaths:
52                 # If rpath is already dynamic continue
53                 if rpath.find("$ORIGIN") != -1:
54                     continue
55                 # If the rpath shares a root with base_prefix determine a new dynamic rpath from the
56                 # base_prefix shared root
57                 if rpath.find(basedir) != -1:
58                     fdir = os.path.dirname(fpath.partition(basedir)[2])
59                     ldir = rpath.partition(basedir)[2].strip()
60                 # otherwise (i.e. cross packages) determine a shared root based on the TMPDIR
61                 # NOTE: This will not work reliably for cross packages, particularly in the case
62                 # where your TMPDIR is a short path (i.e. /usr/poky) as chrpath cannot insert an
63                 # rpath longer than that which is already set.
64                 else:
65                     fdir = os.path.dirname(fpath.rpartition(tmpdir)[2])
66                     ldir = rpath.partition(tmpdir)[2].strip()
68                 try:
69                     new_rpaths.append("$ORIGIN/%s" % oe.path.relative(fdir, ldir))
70                 except ValueError:
71                     # Some programs link in non-existant RPATH directories.
72                     continue
74             # if we have modified some rpaths call chrpath to update the binary
75             if len(new_rpaths):
76                 args = ":".join(new_rpaths)
77                 #bb.note("Setting rpath for %s to %s" %(fpath,args))
78                 oe_system(d, [cmd, '-r', args, fpath], shell=False,
79                           stdout=open("/dev/null", "a"))
81             if perms:
82                 os.chmod(fpath, perms)
84 def rpath_replace (path, d):
85     bindirs = bb.data.expand("${bindir} ${sbindir} ${base_sbindir} ${base_bindir} ${libdir} ${base_libdir} ${PREPROCESS_RELOCATE_DIRS}", d).split()
87     for bindir in bindirs:
88         #bb.note ("Processing directory " + bindir)
89         directory = path + "/" + bindir
90         process_dir (directory, d)
92 python relocatable_binaries_preprocess() {
93     rpath_replace(bb.data.expand('${SYSROOT_DESTDIR}', d), d)