fastjar: switch to BBCLASSEXTEND and clean up
[openembedded.git] / classes / relocatable.bbclass
blobeb5b9e62ed53868a873a7efdb7a2a7542b4de478
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                     depth = fpath.partition(basedir)[2].count('/')
59                     libpath = 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                     depth = fpath.rpartition(tmpdir)[2].count('/')
66                     libpath = rpath.partition(tmpdir)[2].strip()
68                 base = "$ORIGIN"
69                 while depth > 1:
70                     base += "/.."
71                     depth-=1
72                 new_rpaths.append("%s%s" % (base, libpath))
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                 sub.call([cmd, '-r', args, fpath])
80             if perms:
81                 os.chmod(fpath, perms)
83 def rpath_replace (path, d):
84     bindirs = bb.data.expand("${bindir} ${sbindir} ${base_sbindir} ${base_bindir} ${libdir} ${base_libdir} ${PREPROCESS_RELOCATE_DIRS}", d).split()
86     for bindir in bindirs:
87         #bb.note ("Processing directory " + bindir)
88         directory = path + "/" + bindir
89         process_dir (directory, d)
91 python relocatable_binaries_preprocess() {
92     rpath_replace(bb.data.expand('${SYSROOT_DESTDIR}', d), d)