Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | # Yocto Project layer check tool |
| 2 | # |
| 3 | # Copyright (C) 2017 Intel Corporation |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # |
| 5 | # SPDX-License-Identifier: MIT |
| 6 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 7 | |
| 8 | import os |
| 9 | import re |
| 10 | import subprocess |
| 11 | from enum import Enum |
| 12 | |
| 13 | import bb.tinfoil |
| 14 | |
| 15 | class LayerType(Enum): |
| 16 | BSP = 0 |
| 17 | DISTRO = 1 |
| 18 | SOFTWARE = 2 |
| 19 | ERROR_NO_LAYER_CONF = 98 |
| 20 | ERROR_BSP_DISTRO = 99 |
| 21 | |
| 22 | def _get_configurations(path): |
| 23 | configs = [] |
| 24 | |
| 25 | for f in os.listdir(path): |
| 26 | file_path = os.path.join(path, f) |
| 27 | if os.path.isfile(file_path) and f.endswith('.conf'): |
| 28 | configs.append(f[:-5]) # strip .conf |
| 29 | return configs |
| 30 | |
| 31 | def _get_layer_collections(layer_path, lconf=None, data=None): |
| 32 | import bb.parse |
| 33 | import bb.data |
| 34 | |
| 35 | if lconf is None: |
| 36 | lconf = os.path.join(layer_path, 'conf', 'layer.conf') |
| 37 | |
| 38 | if data is None: |
| 39 | ldata = bb.data.init() |
| 40 | bb.parse.init_parser(ldata) |
| 41 | else: |
| 42 | ldata = data.createCopy() |
| 43 | |
| 44 | ldata.setVar('LAYERDIR', layer_path) |
| 45 | try: |
| 46 | ldata = bb.parse.handle(lconf, ldata, include=True) |
Brad Bishop | 0011132 | 2018-04-01 22:23:53 -0400 | [diff] [blame] | 47 | except: |
| 48 | raise RuntimeError("Parsing of layer.conf from layer: %s failed" % layer_path) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 49 | ldata.expandVarref('LAYERDIR') |
| 50 | |
| 51 | collections = (ldata.getVar('BBFILE_COLLECTIONS') or '').split() |
| 52 | if not collections: |
| 53 | name = os.path.basename(layer_path) |
| 54 | collections = [name] |
| 55 | |
| 56 | collections = {c: {} for c in collections} |
| 57 | for name in collections: |
| 58 | priority = ldata.getVar('BBFILE_PRIORITY_%s' % name) |
| 59 | pattern = ldata.getVar('BBFILE_PATTERN_%s' % name) |
| 60 | depends = ldata.getVar('LAYERDEPENDS_%s' % name) |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 61 | compat = ldata.getVar('LAYERSERIES_COMPAT_%s' % name) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 62 | collections[name]['priority'] = priority |
| 63 | collections[name]['pattern'] = pattern |
| 64 | collections[name]['depends'] = depends |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 65 | collections[name]['compat'] = compat |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 66 | |
| 67 | return collections |
| 68 | |
| 69 | def _detect_layer(layer_path): |
| 70 | """ |
| 71 | Scans layer directory to detect what type of layer |
| 72 | is BSP, Distro or Software. |
| 73 | |
| 74 | Returns a dictionary with layer name, type and path. |
| 75 | """ |
| 76 | |
| 77 | layer = {} |
| 78 | layer_name = os.path.basename(layer_path) |
| 79 | |
| 80 | layer['name'] = layer_name |
| 81 | layer['path'] = layer_path |
| 82 | layer['conf'] = {} |
| 83 | |
| 84 | if not os.path.isfile(os.path.join(layer_path, 'conf', 'layer.conf')): |
| 85 | layer['type'] = LayerType.ERROR_NO_LAYER_CONF |
| 86 | return layer |
| 87 | |
| 88 | machine_conf = os.path.join(layer_path, 'conf', 'machine') |
| 89 | distro_conf = os.path.join(layer_path, 'conf', 'distro') |
| 90 | |
| 91 | is_bsp = False |
| 92 | is_distro = False |
| 93 | |
| 94 | if os.path.isdir(machine_conf): |
| 95 | machines = _get_configurations(machine_conf) |
| 96 | if machines: |
| 97 | is_bsp = True |
| 98 | |
| 99 | if os.path.isdir(distro_conf): |
| 100 | distros = _get_configurations(distro_conf) |
| 101 | if distros: |
| 102 | is_distro = True |
| 103 | |
| 104 | if is_bsp and is_distro: |
| 105 | layer['type'] = LayerType.ERROR_BSP_DISTRO |
| 106 | elif is_bsp: |
| 107 | layer['type'] = LayerType.BSP |
| 108 | layer['conf']['machines'] = machines |
| 109 | elif is_distro: |
| 110 | layer['type'] = LayerType.DISTRO |
| 111 | layer['conf']['distros'] = distros |
| 112 | else: |
| 113 | layer['type'] = LayerType.SOFTWARE |
| 114 | |
| 115 | layer['collections'] = _get_layer_collections(layer['path']) |
| 116 | |
| 117 | return layer |
| 118 | |
| 119 | def detect_layers(layer_directories, no_auto): |
| 120 | layers = [] |
| 121 | |
| 122 | for directory in layer_directories: |
| 123 | directory = os.path.realpath(directory) |
| 124 | if directory[-1] == '/': |
| 125 | directory = directory[0:-1] |
| 126 | |
| 127 | if no_auto: |
| 128 | conf_dir = os.path.join(directory, 'conf') |
| 129 | if os.path.isdir(conf_dir): |
| 130 | layer = _detect_layer(directory) |
| 131 | if layer: |
| 132 | layers.append(layer) |
| 133 | else: |
| 134 | for root, dirs, files in os.walk(directory): |
| 135 | dir_name = os.path.basename(root) |
| 136 | conf_dir = os.path.join(root, 'conf') |
| 137 | if os.path.isdir(conf_dir): |
| 138 | layer = _detect_layer(root) |
| 139 | if layer: |
| 140 | layers.append(layer) |
| 141 | |
| 142 | return layers |
| 143 | |
| 144 | def _find_layer_depends(depend, layers): |
| 145 | for layer in layers: |
Andrew Geissler | 1e34c2d | 2020-05-29 16:02:59 -0500 | [diff] [blame] | 146 | if 'collections' not in layer: |
| 147 | continue |
| 148 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 149 | for collection in layer['collections']: |
| 150 | if depend == collection: |
| 151 | return layer |
| 152 | return None |
| 153 | |
| 154 | def add_layer_dependencies(bblayersconf, layer, layers, logger): |
| 155 | def recurse_dependencies(depends, layer, layers, logger, ret = []): |
| 156 | logger.debug('Processing dependencies %s for layer %s.' % \ |
| 157 | (depends, layer['name'])) |
| 158 | |
| 159 | for depend in depends.split(): |
| 160 | # core (oe-core) is suppose to be provided |
| 161 | if depend == 'core': |
| 162 | continue |
| 163 | |
| 164 | layer_depend = _find_layer_depends(depend, layers) |
| 165 | if not layer_depend: |
| 166 | logger.error('Layer %s depends on %s and isn\'t found.' % \ |
| 167 | (layer['name'], depend)) |
| 168 | ret = None |
| 169 | continue |
| 170 | |
| 171 | # We keep processing, even if ret is None, this allows us to report |
| 172 | # multiple errors at once |
| 173 | if ret is not None and layer_depend not in ret: |
| 174 | ret.append(layer_depend) |
Brad Bishop | 004d499 | 2018-10-02 23:54:45 +0200 | [diff] [blame] | 175 | else: |
| 176 | # we might have processed this dependency already, in which case |
| 177 | # we should not do it again (avoid recursive loop) |
| 178 | continue |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 179 | |
| 180 | # Recursively process... |
| 181 | if 'collections' not in layer_depend: |
| 182 | continue |
| 183 | |
| 184 | for collection in layer_depend['collections']: |
| 185 | collect_deps = layer_depend['collections'][collection]['depends'] |
| 186 | if not collect_deps: |
| 187 | continue |
| 188 | ret = recurse_dependencies(collect_deps, layer_depend, layers, logger, ret) |
| 189 | |
| 190 | return ret |
| 191 | |
| 192 | layer_depends = [] |
| 193 | for collection in layer['collections']: |
| 194 | depends = layer['collections'][collection]['depends'] |
| 195 | if not depends: |
| 196 | continue |
| 197 | |
| 198 | layer_depends = recurse_dependencies(depends, layer, layers, logger, layer_depends) |
| 199 | |
| 200 | # Note: [] (empty) is allowed, None is not! |
| 201 | if layer_depends is None: |
| 202 | return False |
| 203 | else: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 204 | add_layers(bblayersconf, layer_depends, logger) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 205 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 206 | return True |
| 207 | |
| 208 | def add_layers(bblayersconf, layers, logger): |
| 209 | # Don't add a layer that is already present. |
| 210 | added = set() |
| 211 | output = check_command('Getting existing layers failed.', 'bitbake-layers show-layers').decode('utf-8') |
| 212 | for layer, path, pri in re.findall(r'^(\S+) +([^\n]*?) +(\d+)$', output, re.MULTILINE): |
| 213 | added.add(path) |
| 214 | |
| 215 | with open(bblayersconf, 'a+') as f: |
| 216 | for layer in layers: |
| 217 | logger.info('Adding layer %s' % layer['name']) |
| 218 | name = layer['name'] |
| 219 | path = layer['path'] |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 220 | if path in added: |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 221 | logger.info('%s is already in %s' % (name, bblayersconf)) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 222 | else: |
| 223 | added.add(path) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 224 | f.write("\nBBLAYERS += \"%s\"\n" % path) |
| 225 | return True |
| 226 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 227 | def check_command(error_msg, cmd, cwd=None): |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 228 | ''' |
| 229 | Run a command under a shell, capture stdout and stderr in a single stream, |
| 230 | throw an error when command returns non-zero exit code. Returns the output. |
| 231 | ''' |
| 232 | |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 233 | p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, cwd=cwd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 234 | output, _ = p.communicate() |
| 235 | if p.returncode: |
| 236 | msg = "%s\nCommand: %s\nOutput:\n%s" % (error_msg, cmd, output.decode('utf-8')) |
| 237 | raise RuntimeError(msg) |
| 238 | return output |
| 239 | |
| 240 | def get_signatures(builddir, failsafe=False, machine=None): |
| 241 | import re |
| 242 | |
| 243 | # some recipes needs to be excluded like meta-world-pkgdata |
| 244 | # because a layer can add recipes to a world build so signature |
| 245 | # will be change |
| 246 | exclude_recipes = ('meta-world-pkgdata',) |
| 247 | |
| 248 | sigs = {} |
| 249 | tune2tasks = {} |
| 250 | |
Brad Bishop | c68388fc | 2019-08-26 01:33:31 -0400 | [diff] [blame] | 251 | cmd = 'BB_ENV_EXTRAWHITE="$BB_ENV_EXTRAWHITE BB_SIGNATURE_HANDLER" BB_SIGNATURE_HANDLER="OEBasicHash" ' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 252 | if machine: |
| 253 | cmd += 'MACHINE=%s ' % machine |
| 254 | cmd += 'bitbake ' |
| 255 | if failsafe: |
| 256 | cmd += '-k ' |
| 257 | cmd += '-S none world' |
| 258 | sigs_file = os.path.join(builddir, 'locked-sigs.inc') |
| 259 | if os.path.exists(sigs_file): |
| 260 | os.unlink(sigs_file) |
| 261 | try: |
| 262 | check_command('Generating signatures failed. This might be due to some parse error and/or general layer incompatibilities.', |
Andrew Geissler | 99467da | 2019-02-25 18:54:23 -0600 | [diff] [blame] | 263 | cmd, builddir) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 264 | except RuntimeError as ex: |
| 265 | if failsafe and os.path.exists(sigs_file): |
| 266 | # Ignore the error here. Most likely some recipes active |
| 267 | # in a world build lack some dependencies. There is a |
| 268 | # separate test_machine_world_build which exposes the |
| 269 | # failure. |
| 270 | pass |
| 271 | else: |
| 272 | raise |
| 273 | |
| 274 | sig_regex = re.compile("^(?P<task>.*:.*):(?P<hash>.*) .$") |
| 275 | tune_regex = re.compile("(^|\s)SIGGEN_LOCKEDSIGS_t-(?P<tune>\S*)\s*=\s*") |
| 276 | current_tune = None |
| 277 | with open(sigs_file, 'r') as f: |
| 278 | for line in f.readlines(): |
| 279 | line = line.strip() |
| 280 | t = tune_regex.search(line) |
| 281 | if t: |
| 282 | current_tune = t.group('tune') |
| 283 | s = sig_regex.match(line) |
| 284 | if s: |
| 285 | exclude = False |
| 286 | for er in exclude_recipes: |
| 287 | (recipe, task) = s.group('task').split(':') |
| 288 | if er == recipe: |
| 289 | exclude = True |
| 290 | break |
| 291 | if exclude: |
| 292 | continue |
| 293 | |
| 294 | sigs[s.group('task')] = s.group('hash') |
| 295 | tune2tasks.setdefault(current_tune, []).append(s.group('task')) |
| 296 | |
| 297 | if not sigs: |
| 298 | raise RuntimeError('Can\'t load signatures from %s' % sigs_file) |
| 299 | |
| 300 | return (sigs, tune2tasks) |
| 301 | |
| 302 | def get_depgraph(targets=['world'], failsafe=False): |
| 303 | ''' |
| 304 | Returns the dependency graph for the given target(s). |
| 305 | The dependency graph is taken directly from DepTreeEvent. |
| 306 | ''' |
| 307 | depgraph = None |
| 308 | with bb.tinfoil.Tinfoil() as tinfoil: |
| 309 | tinfoil.prepare(config_only=False) |
| 310 | tinfoil.set_event_mask(['bb.event.NoProvider', 'bb.event.DepTreeGenerated', 'bb.command.CommandCompleted']) |
| 311 | if not tinfoil.run_command('generateDepTreeEvent', targets, 'do_build'): |
| 312 | raise RuntimeError('starting generateDepTreeEvent failed') |
| 313 | while True: |
| 314 | event = tinfoil.wait_event(timeout=1000) |
| 315 | if event: |
| 316 | if isinstance(event, bb.command.CommandFailed): |
| 317 | raise RuntimeError('Generating dependency information failed: %s' % event.error) |
| 318 | elif isinstance(event, bb.command.CommandCompleted): |
| 319 | break |
| 320 | elif isinstance(event, bb.event.NoProvider): |
| 321 | if failsafe: |
| 322 | # The event is informational, we will get information about the |
| 323 | # remaining dependencies eventually and thus can ignore this |
| 324 | # here like we do in get_signatures(), if desired. |
| 325 | continue |
| 326 | if event._reasons: |
| 327 | raise RuntimeError('Nothing provides %s: %s' % (event._item, event._reasons)) |
| 328 | else: |
| 329 | raise RuntimeError('Nothing provides %s.' % (event._item)) |
| 330 | elif isinstance(event, bb.event.DepTreeGenerated): |
| 331 | depgraph = event._depgraph |
| 332 | |
| 333 | if depgraph is None: |
| 334 | raise RuntimeError('Could not retrieve the depgraph.') |
| 335 | return depgraph |
| 336 | |
| 337 | def compare_signatures(old_sigs, curr_sigs): |
| 338 | ''' |
| 339 | Compares the result of two get_signatures() calls. Returns None if no |
| 340 | problems found, otherwise a string that can be used as additional |
| 341 | explanation in self.fail(). |
| 342 | ''' |
| 343 | # task -> (old signature, new signature) |
| 344 | sig_diff = {} |
| 345 | for task in old_sigs: |
| 346 | if task in curr_sigs and \ |
| 347 | old_sigs[task] != curr_sigs[task]: |
| 348 | sig_diff[task] = (old_sigs[task], curr_sigs[task]) |
| 349 | |
| 350 | if not sig_diff: |
| 351 | return None |
| 352 | |
| 353 | # Beware, depgraph uses task=<pn>.<taskname> whereas get_signatures() |
| 354 | # uses <pn>:<taskname>. Need to convert sometimes. The output follows |
| 355 | # the convention from get_signatures() because that seems closer to |
| 356 | # normal bitbake output. |
| 357 | def sig2graph(task): |
| 358 | pn, taskname = task.rsplit(':', 1) |
| 359 | return pn + '.' + taskname |
| 360 | def graph2sig(task): |
| 361 | pn, taskname = task.rsplit('.', 1) |
| 362 | return pn + ':' + taskname |
| 363 | depgraph = get_depgraph(failsafe=True) |
| 364 | depends = depgraph['tdepends'] |
| 365 | |
| 366 | # If a task A has a changed signature, but none of its |
| 367 | # dependencies, then we need to report it because it is |
| 368 | # the one which introduces a change. Any task depending on |
| 369 | # A (directly or indirectly) will also have a changed |
| 370 | # signature, but we don't need to report it. It might have |
| 371 | # its own changes, which will become apparent once the |
| 372 | # issues that we do report are fixed and the test gets run |
| 373 | # again. |
| 374 | sig_diff_filtered = [] |
| 375 | for task, (old_sig, new_sig) in sig_diff.items(): |
| 376 | deps_tainted = False |
| 377 | for dep in depends.get(sig2graph(task), ()): |
| 378 | if graph2sig(dep) in sig_diff: |
| 379 | deps_tainted = True |
| 380 | break |
| 381 | if not deps_tainted: |
| 382 | sig_diff_filtered.append((task, old_sig, new_sig)) |
| 383 | |
| 384 | msg = [] |
| 385 | msg.append('%d signatures changed, initial differences (first hash before, second after):' % |
| 386 | len(sig_diff)) |
| 387 | for diff in sorted(sig_diff_filtered): |
| 388 | recipe, taskname = diff[0].rsplit(':', 1) |
| 389 | cmd = 'bitbake-diffsigs --task %s %s --signature %s %s' % \ |
| 390 | (recipe, taskname, diff[1], diff[2]) |
| 391 | msg.append(' %s: %s -> %s' % diff) |
| 392 | msg.append(' %s' % cmd) |
| 393 | try: |
| 394 | output = check_command('Determining signature difference failed.', |
| 395 | cmd).decode('utf-8') |
| 396 | except RuntimeError as error: |
| 397 | output = str(error) |
| 398 | if output: |
| 399 | msg.extend([' ' + line for line in output.splitlines()]) |
| 400 | msg.append('') |
| 401 | return '\n'.join(msg) |