blob: 1e5c3aa8e1dcb3c0f06d5cf7298fa001894f0d79 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001def runstrip(arg):
2 # Function to strip a single file, called from split_and_strip_files below
3 # A working 'file' (one which works on the target architecture)
4 #
5 # The elftype is a bit pattern (explained in split_and_strip_files) to tell
6 # us what type of file we're processing...
7 # 4 - executable
8 # 8 - shared library
9 # 16 - kernel module
10
Patrick Williamsc0f7c042017-02-23 20:41:17 -060011 import stat, subprocess
Patrick Williamsc124f4f2015-09-15 14:41:29 -050012
13 (file, elftype, strip) = arg
14
15 newmode = None
16 if not os.access(file, os.W_OK) or os.access(file, os.R_OK):
17 origmode = os.stat(file)[stat.ST_MODE]
18 newmode = origmode | stat.S_IWRITE | stat.S_IREAD
19 os.chmod(file, newmode)
20
Brad Bishop6e60e8b2018-02-01 10:27:11 -050021 stripcmd = [strip]
Patrick Williamsc124f4f2015-09-15 14:41:29 -050022
23 # kernel module
24 if elftype & 16:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050025 stripcmd.extend(["--strip-debug", "--remove-section=.comment",
26 "--remove-section=.note", "--preserve-dates"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050027 # .so and shared library
28 elif ".so" in file and elftype & 8:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050029 stripcmd.extend(["--remove-section=.comment", "--remove-section=.note", "--strip-unneeded"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050030 # shared or executable:
31 elif elftype & 8 or elftype & 4:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050032 stripcmd.extend(["--remove-section=.comment", "--remove-section=.note"])
Patrick Williamsc124f4f2015-09-15 14:41:29 -050033
Brad Bishop6e60e8b2018-02-01 10:27:11 -050034 stripcmd.append(file)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050035 bb.debug(1, "runstrip: %s" % stripcmd)
36
37 try:
Brad Bishop6e60e8b2018-02-01 10:27:11 -050038 output = subprocess.check_output(stripcmd, stderr=subprocess.STDOUT)
Patrick Williamsc124f4f2015-09-15 14:41:29 -050039 except subprocess.CalledProcessError as e:
40 bb.error("runstrip: '%s' strip command failed with %s (%s)" % (stripcmd, e.returncode, e.output))
41
42 if newmode:
43 os.chmod(file, origmode)
44
45 return
46
47
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048def strip_execs(pn, dstdir, strip_cmd, libdir, base_libdir, qa_already_stripped=False):
49 """
50 Strip executable code (like executables, shared libraries) _in_place_
51 - Based on sysroot_strip in staging.bbclass
52 :param dstdir: directory in which to strip files
53 :param strip_cmd: Strip command (usually ${STRIP})
54 :param libdir: ${libdir} - strip .so files in this directory
55 :param base_libdir: ${base_libdir} - strip .so files in this directory
56 :param qa_already_stripped: Set to True if already-stripped' in ${INSANE_SKIP}
57 This is for proper logging and messages only.
58 """
59 import stat, errno, oe.path, oe.utils, mmap
60
61 # Detect .ko module by searching for "vermagic=" string
62 def is_kernel_module(path):
63 with open(path) as f:
64 return mmap.mmap(f.fileno(), 0, prot=mmap.PROT_READ).find(b"vermagic=") >= 0
65
66 # Return type (bits):
67 # 0 - not elf
68 # 1 - ELF
69 # 2 - stripped
70 # 4 - executable
71 # 8 - shared library
72 # 16 - kernel module
73 def is_elf(path):
74 exec_type = 0
75 ret, result = oe.utils.getstatusoutput(
76 "file \"%s\"" % path.replace("\"", "\\\""))
77
78 if ret:
79 bb.error("split_and_strip_files: 'file %s' failed" % path)
80 return exec_type
81
82 if "ELF" in result:
83 exec_type |= 1
84 if "not stripped" not in result:
85 exec_type |= 2
86 if "executable" in result:
87 exec_type |= 4
88 if "shared" in result:
89 exec_type |= 8
90 if "relocatable" in result and is_kernel_module(path):
91 exec_type |= 16
92 return exec_type
93
94 elffiles = {}
95 inodes = {}
96 libdir = os.path.abspath(dstdir + os.sep + libdir)
97 base_libdir = os.path.abspath(dstdir + os.sep + base_libdir)
98 exec_mask = stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH
99 #
100 # First lets figure out all of the files we may have to process
101 #
102 for root, dirs, files in os.walk(dstdir):
103 for f in files:
104 file = os.path.join(root, f)
105
106 try:
107 ltarget = oe.path.realpath(file, dstdir, False)
108 s = os.lstat(ltarget)
109 except OSError as e:
110 (err, strerror) = e.args
111 if err != errno.ENOENT:
112 raise
113 # Skip broken symlinks
114 continue
115 if not s:
116 continue
117 # Check its an excutable
118 if s[stat.ST_MODE] & exec_mask \
119 or ((file.startswith(libdir) or file.startswith(base_libdir)) and ".so" in f) \
120 or file.endswith('.ko'):
121 # If it's a symlink, and points to an ELF file, we capture the readlink target
122 if os.path.islink(file):
123 continue
124
125 # It's a file (or hardlink), not a link
126 # ...but is it ELF, and is it already stripped?
127 elf_file = is_elf(file)
128 if elf_file & 1:
129 if elf_file & 2:
130 if qa_already_stripped:
131 bb.note("Skipping file %s from %s for already-stripped QA test" % (file[len(dstdir):], pn))
132 else:
133 bb.warn("File '%s' from %s was already stripped, this will prevent future debugging!" % (file[len(dstdir):], pn))
134 continue
135
136 if s.st_ino in inodes:
137 os.unlink(file)
138 os.link(inodes[s.st_ino], file)
139 else:
140 # break hardlinks so that we do not strip the original.
141 inodes[s.st_ino] = file
142 bb.utils.copyfile(file, file)
143 elffiles[file] = elf_file
144
145 #
146 # Now strip them (in parallel)
147 #
148 sfiles = []
149 for file in elffiles:
150 elf_file = int(elffiles[file])
151 sfiles.append((file, elf_file, strip_cmd))
152
153 oe.utils.multiprocess_exec(sfiles, runstrip)
154
155
156
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500157def file_translate(file):
158 ft = file.replace("@", "@at@")
159 ft = ft.replace(" ", "@space@")
160 ft = ft.replace("\t", "@tab@")
161 ft = ft.replace("[", "@openbrace@")
162 ft = ft.replace("]", "@closebrace@")
163 ft = ft.replace("_", "@underscore@")
164 return ft
165
166def filedeprunner(arg):
167 import re, subprocess, shlex
168
169 (pkg, pkgfiles, rpmdeps, pkgdest) = arg
170 provides = {}
171 requires = {}
172
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500173 file_re = re.compile(r'\s+\d+\s(.*)')
174 dep_re = re.compile(r'\s+(\S)\s+(.*)')
175 r = re.compile(r'[<>=]+\s+\S*')
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500176
177 def process_deps(pipe, pkg, pkgdest, provides, requires):
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500178 file = None
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500179 for line in pipe.split("\n"):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500180
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500181 m = file_re.match(line)
182 if m:
183 file = m.group(1)
184 file = file.replace(pkgdest + "/" + pkg, "")
185 file = file_translate(file)
186 continue
187
188 m = dep_re.match(line)
189 if not m or not file:
190 continue
191
192 type, dep = m.groups()
193
194 if type == 'R':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500195 i = requires
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500196 elif type == 'P':
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500197 i = provides
198 else:
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500199 continue
200
201 if dep.startswith("python("):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500202 continue
203
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500204 # Ignore all perl(VMS::...) and perl(Mac::...) dependencies. These
205 # are typically used conditionally from the Perl code, but are
206 # generated as unconditional dependencies.
207 if dep.startswith('perl(VMS::') or dep.startswith('perl(Mac::'):
208 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500209
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500210 # Ignore perl dependencies on .pl files.
211 if dep.startswith('perl(') and dep.endswith('.pl)'):
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500212 continue
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500213
214 # Remove perl versions and perl module versions since they typically
215 # do not make sense when used as package versions.
216 if dep.startswith('perl') and r.search(dep):
217 dep = dep.split()[0]
218
219 # Put parentheses around any version specifications.
220 dep = r.sub(r'(\g<0>)',dep)
221
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500222 if file not in i:
223 i[file] = []
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500224 i[file].append(dep)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500225
226 return provides, requires
227
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500228 output = subprocess.check_output(shlex.split(rpmdeps) + pkgfiles, stderr=subprocess.STDOUT).decode("utf-8")
229 provides, requires = process_deps(output, pkg, pkgdest, provides, requires)
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500230
231 return (pkg, provides, requires)
232
233
234def read_shlib_providers(d):
235 import re
236
237 shlib_provider = {}
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500238 shlibs_dirs = d.getVar('SHLIBSDIRS').split()
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500239 list_re = re.compile('^(.*)\.list$')
240 # Go from least to most specific since the last one found wins
241 for dir in reversed(shlibs_dirs):
242 bb.debug(2, "Reading shlib providers in %s" % (dir))
243 if not os.path.exists(dir):
244 continue
245 for file in os.listdir(dir):
246 m = list_re.match(file)
247 if m:
248 dep_pkg = m.group(1)
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600249 try:
250 fd = open(os.path.join(dir, file))
251 except IOError:
252 # During a build unrelated shlib files may be deleted, so
253 # handle files disappearing between the listdirs and open.
254 continue
Patrick Williamsc124f4f2015-09-15 14:41:29 -0500255 lines = fd.readlines()
256 fd.close()
257 for l in lines:
258 s = l.strip().split(":")
259 if s[0] not in shlib_provider:
260 shlib_provider[s[0]] = {}
261 shlib_provider[s[0]][s[1]] = (dep_pkg, s[2])
262 return shlib_provider
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500263
264
265def npm_split_package_dirs(pkgdir):
266 """
267 Work out the packages fetched and unpacked by BitBake's npm fetcher
268 Returns a dict of packagename -> (relpath, package.json) ordered
269 such that it is suitable for use in PACKAGES and FILES
270 """
271 from collections import OrderedDict
272 import json
273 packages = {}
274 for root, dirs, files in os.walk(pkgdir):
275 if os.path.basename(root) == 'node_modules':
276 for dn in dirs:
277 relpth = os.path.relpath(os.path.join(root, dn), pkgdir)
278 pkgitems = ['${PN}']
279 for pathitem in relpth.split('/'):
280 if pathitem == 'node_modules':
281 continue
282 pkgitems.append(pathitem)
283 pkgname = '-'.join(pkgitems).replace('_', '-')
Brad Bishop6e60e8b2018-02-01 10:27:11 -0500284 pkgname = pkgname.replace('@', '')
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500285 pkgfile = os.path.join(root, dn, 'package.json')
286 data = None
287 if os.path.exists(pkgfile):
288 with open(pkgfile, 'r') as f:
289 data = json.loads(f.read())
Patrick Williamsc0f7c042017-02-23 20:41:17 -0600290 packages[pkgname] = (relpth, data)
Patrick Williamsd8c66bc2016-06-20 12:57:21 -0500291 # We want the main package for a module sorted *after* its subpackages
292 # (so that it doesn't otherwise steal the files for the subpackage), so
293 # this is a cheap way to do that whilst still having an otherwise
294 # alphabetical sort
295 return OrderedDict((key, packages[key]) for key in sorted(packages, key=lambda pkg: pkg + '~'))