package.py 11 KB

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