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