copy_buildsystem.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #
  2. # SPDX-License-Identifier: GPL-2.0-only
  3. #
  4. # This class should provide easy access to the different aspects of the
  5. # buildsystem such as layers, bitbake location, etc.
  6. #
  7. # SDK_LAYERS_EXCLUDE: Layers which will be excluded from SDK layers.
  8. # SDK_LAYERS_EXCLUDE_PATTERN: The simiar to SDK_LAYERS_EXCLUDE, this supports
  9. # python regular expression, use space as separator,
  10. # e.g.: ".*-downloads closed-.*"
  11. #
  12. import stat
  13. import shutil
  14. def _smart_copy(src, dest):
  15. import subprocess
  16. # smart_copy will choose the correct function depending on whether the
  17. # source is a file or a directory.
  18. mode = os.stat(src).st_mode
  19. if stat.S_ISDIR(mode):
  20. bb.utils.mkdirhier(dest)
  21. cmd = "tar --exclude='.git' --xattrs --xattrs-include='*' -chf - -C %s -p . \
  22. | tar --xattrs --xattrs-include='*' -xf - -C %s" % (src, dest)
  23. subprocess.check_output(cmd, shell=True, stderr=subprocess.STDOUT)
  24. else:
  25. shutil.copyfile(src, dest)
  26. shutil.copymode(src, dest)
  27. class BuildSystem(object):
  28. def __init__(self, context, d):
  29. self.d = d
  30. self.context = context
  31. self.layerdirs = [os.path.abspath(pth) for pth in d.getVar('BBLAYERS').split()]
  32. self.layers_exclude = (d.getVar('SDK_LAYERS_EXCLUDE') or "").split()
  33. self.layers_exclude_pattern = d.getVar('SDK_LAYERS_EXCLUDE_PATTERN')
  34. def copy_bitbake_and_layers(self, destdir, workspace_name=None):
  35. import re
  36. # Copy in all metadata layers + bitbake (as repositories)
  37. copied_corebase = None
  38. layers_copied = []
  39. bb.utils.mkdirhier(destdir)
  40. layers = list(self.layerdirs)
  41. corebase = os.path.abspath(self.d.getVar('COREBASE'))
  42. layers.append(corebase)
  43. # The bitbake build system uses the meta-skeleton layer as a layout
  44. # for common recipies, e.g: the recipetool script to create kernel recipies
  45. # Add the meta-skeleton layer to be included as part of the eSDK installation
  46. layers.append(os.path.join(corebase, 'meta-skeleton'))
  47. # Exclude layers
  48. for layer_exclude in self.layers_exclude:
  49. if layer_exclude in layers:
  50. bb.note('Excluded %s from sdk layers since it is in SDK_LAYERS_EXCLUDE' % layer_exclude)
  51. layers.remove(layer_exclude)
  52. if self.layers_exclude_pattern:
  53. layers_cp = layers[:]
  54. for pattern in self.layers_exclude_pattern.split():
  55. for layer in layers_cp:
  56. if re.match(pattern, layer):
  57. bb.note('Excluded %s from sdk layers since matched SDK_LAYERS_EXCLUDE_PATTERN' % layer)
  58. layers.remove(layer)
  59. workspace_newname = workspace_name
  60. if workspace_newname:
  61. layernames = [os.path.basename(layer) for layer in layers]
  62. extranum = 0
  63. while workspace_newname in layernames:
  64. extranum += 1
  65. workspace_newname = '%s-%d' % (workspace_name, extranum)
  66. corebase_files = self.d.getVar('COREBASE_FILES').split()
  67. corebase_files = [corebase + '/' +x for x in corebase_files]
  68. # Make sure bitbake goes in
  69. bitbake_dir = bb.__file__.rsplit('/', 3)[0]
  70. corebase_files.append(bitbake_dir)
  71. for layer in layers:
  72. layerconf = os.path.join(layer, 'conf', 'layer.conf')
  73. layernewname = os.path.basename(layer)
  74. workspace = False
  75. if os.path.exists(layerconf):
  76. with open(layerconf, 'r') as f:
  77. if f.readline().startswith("# ### workspace layer auto-generated by devtool ###"):
  78. if workspace_newname:
  79. layernewname = workspace_newname
  80. workspace = True
  81. else:
  82. bb.plain("NOTE: Excluding local workspace layer %s from %s" % (layer, self.context))
  83. continue
  84. # If the layer was already under corebase, leave it there
  85. # since layers such as meta have issues when moved.
  86. layerdestpath = destdir
  87. if corebase == os.path.dirname(layer):
  88. layerdestpath += '/' + os.path.basename(corebase)
  89. else:
  90. layer_relative = os.path.basename(corebase) + '/' + os.path.relpath(layer, corebase)
  91. if os.path.dirname(layer_relative) != layernewname:
  92. layerdestpath += '/' + os.path.dirname(layer_relative)
  93. layerdestpath += '/' + layernewname
  94. layer_relative = os.path.relpath(layerdestpath,
  95. destdir)
  96. # Treat corebase as special since it typically will contain
  97. # build directories or other custom items.
  98. if corebase == layer:
  99. copied_corebase = layer_relative
  100. bb.utils.mkdirhier(layerdestpath)
  101. for f in corebase_files:
  102. f_basename = os.path.basename(f)
  103. destname = os.path.join(layerdestpath, f_basename)
  104. _smart_copy(f, destname)
  105. else:
  106. layers_copied.append(layer_relative)
  107. if os.path.exists(os.path.join(layerdestpath, 'conf/layer.conf')):
  108. bb.note("Skipping layer %s, already handled" % layer)
  109. else:
  110. _smart_copy(layer, layerdestpath)
  111. if workspace:
  112. # Make some adjustments original workspace layer
  113. # Drop sources (recipe tasks will be locked, so we don't need them)
  114. srcdir = os.path.join(layerdestpath, 'sources')
  115. if os.path.isdir(srcdir):
  116. shutil.rmtree(srcdir)
  117. # Drop all bbappends except the one for the image the SDK is being built for
  118. # (because of externalsrc, the workspace bbappends will interfere with the
  119. # locked signatures if present, and we don't need them anyway)
  120. image_bbappend = os.path.splitext(os.path.basename(self.d.getVar('FILE')))[0] + '.bbappend'
  121. appenddir = os.path.join(layerdestpath, 'appends')
  122. if os.path.isdir(appenddir):
  123. for fn in os.listdir(appenddir):
  124. if fn == image_bbappend:
  125. continue
  126. else:
  127. os.remove(os.path.join(appenddir, fn))
  128. # Drop README
  129. readme = os.path.join(layerdestpath, 'README')
  130. if os.path.exists(readme):
  131. os.remove(readme)
  132. # Filter out comments in layer.conf and change layer name
  133. layerconf = os.path.join(layerdestpath, 'conf', 'layer.conf')
  134. with open(layerconf, 'r') as f:
  135. origlines = f.readlines()
  136. with open(layerconf, 'w') as f:
  137. for line in origlines:
  138. if line.startswith('#'):
  139. continue
  140. line = line.replace('workspacelayer', workspace_newname)
  141. f.write(line)
  142. # meta-skeleton layer is added as part of the build system
  143. # but not as a layer included in the build, therefore it is
  144. # not reported to the function caller.
  145. for layer in layers_copied:
  146. if layer.endswith('/meta-skeleton'):
  147. layers_copied.remove(layer)
  148. break
  149. return copied_corebase, layers_copied
  150. def generate_locked_sigs(sigfile, d):
  151. bb.utils.mkdirhier(os.path.dirname(sigfile))
  152. depd = d.getVar('BB_TASKDEPDATA', False)
  153. tasks = ['%s.%s' % (v[2], v[1]) for v in depd.values()]
  154. bb.parse.siggen.dump_lockedsigs(sigfile, tasks)
  155. def prune_lockedsigs(excluded_tasks, excluded_targets, lockedsigs, pruned_output):
  156. with open(lockedsigs, 'r') as infile:
  157. bb.utils.mkdirhier(os.path.dirname(pruned_output))
  158. with open(pruned_output, 'w') as f:
  159. invalue = False
  160. for line in infile:
  161. if invalue:
  162. if line.endswith('\\\n'):
  163. splitval = line.strip().split(':')
  164. if not splitval[1] in excluded_tasks and not splitval[0] in excluded_targets:
  165. f.write(line)
  166. else:
  167. f.write(line)
  168. invalue = False
  169. elif line.startswith('SIGGEN_LOCKEDSIGS'):
  170. invalue = True
  171. f.write(line)
  172. def merge_lockedsigs(copy_tasks, lockedsigs_main, lockedsigs_extra, merged_output, copy_output=None):
  173. merged = {}
  174. arch_order = []
  175. with open(lockedsigs_main, 'r') as f:
  176. invalue = None
  177. for line in f:
  178. if invalue:
  179. if line.endswith('\\\n'):
  180. merged[invalue].append(line)
  181. else:
  182. invalue = None
  183. elif line.startswith('SIGGEN_LOCKEDSIGS_t-'):
  184. invalue = line[18:].split('=', 1)[0].rstrip()
  185. merged[invalue] = []
  186. arch_order.append(invalue)
  187. with open(lockedsigs_extra, 'r') as f:
  188. invalue = None
  189. tocopy = {}
  190. for line in f:
  191. if invalue:
  192. if line.endswith('\\\n'):
  193. if not line in merged[invalue]:
  194. target, task = line.strip().split(':')[:2]
  195. if not copy_tasks or task in copy_tasks:
  196. tocopy[invalue].append(line)
  197. merged[invalue].append(line)
  198. else:
  199. invalue = None
  200. elif line.startswith('SIGGEN_LOCKEDSIGS_t-'):
  201. invalue = line[18:].split('=', 1)[0].rstrip()
  202. if not invalue in merged:
  203. merged[invalue] = []
  204. arch_order.append(invalue)
  205. tocopy[invalue] = []
  206. def write_sigs_file(fn, types, sigs):
  207. fulltypes = []
  208. bb.utils.mkdirhier(os.path.dirname(fn))
  209. with open(fn, 'w') as f:
  210. for typename in types:
  211. lines = sigs[typename]
  212. if lines:
  213. f.write('SIGGEN_LOCKEDSIGS_%s = "\\\n' % typename)
  214. for line in lines:
  215. f.write(line)
  216. f.write(' "\n')
  217. fulltypes.append(typename)
  218. f.write('SIGGEN_LOCKEDSIGS_TYPES = "%s"\n' % ' '.join(fulltypes))
  219. if copy_output:
  220. write_sigs_file(copy_output, list(tocopy.keys()), tocopy)
  221. if merged_output:
  222. write_sigs_file(merged_output, arch_order, merged)
  223. def create_locked_sstate_cache(lockedsigs, input_sstate_cache, output_sstate_cache, d, fixedlsbstring="", filterfile=None):
  224. import shutil
  225. bb.note('Generating sstate-cache...')
  226. nativelsbstring = d.getVar('NATIVELSBSTRING')
  227. bb.process.run("gen-lockedsig-cache %s %s %s %s %s" % (lockedsigs, input_sstate_cache, output_sstate_cache, nativelsbstring, filterfile or ''))
  228. if fixedlsbstring and nativelsbstring != fixedlsbstring:
  229. nativedir = output_sstate_cache + '/' + nativelsbstring
  230. if os.path.isdir(nativedir):
  231. destdir = os.path.join(output_sstate_cache, fixedlsbstring)
  232. for root, _, files in os.walk(nativedir):
  233. for fn in files:
  234. src = os.path.join(root, fn)
  235. dest = os.path.join(destdir, os.path.relpath(src, nativedir))
  236. if os.path.exists(dest):
  237. # Already exists, and it'll be the same file, so just delete it
  238. os.unlink(src)
  239. else:
  240. bb.utils.mkdirhier(os.path.dirname(dest))
  241. shutil.move(src, dest)
  242. def check_sstate_task_list(d, targets, filteroutfile, cmdprefix='', cwd=None, logfile=None):
  243. import subprocess
  244. bb.note('Generating sstate task list...')
  245. if not cwd:
  246. cwd = os.getcwd()
  247. if logfile:
  248. logparam = '-l %s' % logfile
  249. else:
  250. logparam = ''
  251. cmd = "%sBB_SETSCENE_ENFORCE=1 PSEUDO_DISABLED=1 oe-check-sstate %s -s -o %s %s" % (cmdprefix, targets, filteroutfile, logparam)
  252. env = dict(d.getVar('BB_ORIGENV', False))
  253. env.pop('BUILDDIR', '')
  254. env.pop('BBPATH', '')
  255. pathitems = env['PATH'].split(':')
  256. env['PATH'] = ':'.join([item for item in pathitems if not item.endswith('/bitbake/bin')])
  257. bb.process.run(cmd, stderr=subprocess.STDOUT, env=env, cwd=cwd, executable='/bin/bash')