diff mbox series

[12/17] bblayers: Switch to use layerdata and new format layers

Message ID 20261130152847.3472849-12-richard.purdie@linuxfoundation.org
State New
Headers show
Series [01/17] uihelper: Fix exception when TaskStarted event handler breaks | expand

Commit Message

Richard Purdie Nov. 30, 2026, 3:28 p.m. UTC
Switch to use the new layerdata structure after the layer changes.

The output does change as layers no longer have priorities.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bblayers/action.py | 65 +++++++++++-------------------------------
 lib/bblayers/common.py | 20 +++++++++++--
 lib/bblayers/query.py  | 41 +++++++-------------------
 3 files changed, 44 insertions(+), 82 deletions(-)
diff mbox series

Patch

diff --git a/lib/bblayers/action.py b/lib/bblayers/action.py
index a14f19948e1..7ee326946fa 100644
--- a/lib/bblayers/action.py
+++ b/lib/bblayers/action.py
@@ -4,7 +4,6 @@ 
 # SPDX-License-Identifier: GPL-2.0-only
 #
 
-import fnmatch
 import logging
 import os
 import shutil
@@ -103,16 +102,17 @@  cleanup may still be necessary afterwards, in particular:
 * where non-recipe files (such as patches) are overwritten (the flatten
   command will show a warning for these)
 * where anything beyond the normal layer setup has been added to
-  layer.conf (only the lowest priority number layer's layer.conf is used)
+  layer.conf (only the first layer's layer.conf is used, so the
+  flattened layer takes that layer's LAYERNAME and LAYERDEPENDS)
 * overridden/appended items from bbappends will need to be tidied up
 * when the flattened layers do not have the same directory structure (the
   flatten command should show a warning when this will cause a problem)
 
 Warning: if you flatten several layers where another layer is intended to
-be used "inbetween" them (in layer priority order) such that recipes /
-bbappends in the layers interact, and then attempt to use the new output
-layer together with that other layer, you may no longer get the same
-build results (as the layer priority order has effectively changed).
+be used "inbetween" them such that recipes / bbappends in the layers
+interact, and then attempt to use the new output layer together with that
+other layer, you may no longer get the same build results (as the layer
+ordering has effectively changed).
 """
         if len(args.layer) == 1:
             logger.error('If you specify layers to flatten you must specify at least two')
@@ -212,53 +212,20 @@  build results (as the layer priority order has effectively changed).
                         bb.utils.copyfile(appendname, fdest)
                         first_append = fdest
 
-        # Get the regex for the first layer in our list (which is where the conf/layer.conf file will
-        # have come from)
-        first_regex = None
-        layerdir = layers[0]
-        for layername, pattern, regex, _ in self.tinfoil.cooker.bbfile_config_priorities:
-            if regex.match(os.path.join(layerdir, 'test')):
-                first_regex = regex
-                break
-
-        if first_regex:
-            # Find the BBFILES entries that match (which will have come from this conf/layer.conf file)
-            bbfiles = str(self.tinfoil.config_data.getVar('BBFILES')).split()
-            bbfiles_layer = []
-            for item in bbfiles:
-                if first_regex.match(item):
-                    newpath = os.path.join(outputdir, item[len(layerdir)+1:])
-                    bbfiles_layer.append(newpath)
-
-            if bbfiles_layer:
-                # Check that all important layer files match BBFILES
-                for root, dirs, files in os.walk(outputdir):
-                    for f1 in files:
-                        ext = os.path.splitext(f1)[1]
-                        if ext in ['.bb', '.bbappend']:
-                            f1full = os.sep.join([root, f1])
-                            entry_found = False
-                            for item in bbfiles_layer:
-                                if fnmatch.fnmatch(f1full, item):
-                                    entry_found = True
-                                    break
-                            if not entry_found:
-                                logger.warning("File %s does not match the flattened layer's BBFILES setting, you may need to edit conf/layer.conf or move the file elsewhere" % f1full)
+        # Bitbake only looks for recipes and bbappends within a layer's recipes*
+        # directories (and dynamic-recipes/<layername>/), check the flattened
+        # layer's files will still be found
+        for root, dirs, files in os.walk(outputdir):
+            for f1 in files:
+                if f1.endswith(('.bb', '.bbappend')):
+                    f1full = os.path.join(root, f1)
+                    topdir = os.path.relpath(f1full, outputdir).split(os.sep)[0]
+                    if not (topdir.startswith('recipes') or topdir == 'dynamic-recipes'):
+                        logger.warning("File %s is not within a recipes* directory of the flattened layer so will not be found, you may need to move it" % f1full)
 
         self.tinfoil.modified_files()
 
 
-    def get_file_layer(self, filename):
-        layerdir = self.get_file_layerdir(filename)
-        if layerdir:
-            return self.get_layer_name(layerdir)
-        else:
-            return '?'
-
-    def get_file_layerdir(self, filename):
-        layer = bb.utils.get_file_layer(filename, self.tinfoil.config_data)
-        return self.bbfile_collections.get(layer, None)
-
     def apply_append(self, appendname, recipename):
         with open(appendname, 'r') as appendfile:
             with open(recipename, 'a') as recipefile:
diff --git a/lib/bblayers/common.py b/lib/bblayers/common.py
index f7b9cee3717..6a97476aa2a 100644
--- a/lib/bblayers/common.py
+++ b/lib/bblayers/common.py
@@ -8,6 +8,8 @@  import argparse
 import logging
 import os
 
+import bb.utils
+
 logger = logging.getLogger('bitbake-layers')
 
 
@@ -15,12 +17,12 @@  class LayerPlugin():
     def __init__(self):
         self.tinfoil = None
         self.bblayers = []
+        self.layerdata = {}
 
     def tinfoil_init(self, tinfoil):
         self.tinfoil = tinfoil
         self.bblayers = (self.tinfoil.config_data.getVar('BBLAYERS') or "").split()
-        layerconfs = self.tinfoil.config_data.varhistory.get_variable_items_files('BBFILE_COLLECTIONS')
-        self.bbfile_collections = {layer: os.path.dirname(os.path.dirname(path)) for layer, path in layerconfs.items()}
+        self.layerdata = self.tinfoil.config_data.getVar('layerdata', False) or {}
 
     @staticmethod
     def add_command(subparsers, cmdname, function, parserecipes=True, *args, **kwargs):
@@ -37,3 +39,17 @@  class LayerPlugin():
 
     def get_layer_name(self, layerdir):
         return os.path.basename(layerdir.rstrip(os.sep))
+
+    def get_file_layerdir(self, filename):
+        """Return the directory of the configured layer containing filename, or None"""
+        layername = bb.utils.get_file_layer_from_layerdata(filename, self.layerdata)
+        if layername:
+            return self.layerdata[layername].path
+        return None
+
+    def get_file_layer(self, filename):
+        layerdir = self.get_file_layerdir(filename)
+        if layerdir:
+            return self.get_layer_name(layerdir)
+        else:
+            return '?'
diff --git a/lib/bblayers/query.py b/lib/bblayers/query.py
index 7b8d1afd719..385e965e9ca 100644
--- a/lib/bblayers/query.py
+++ b/lib/bblayers/query.py
@@ -25,16 +25,14 @@  def plugin_init(plugins):
 class QueryPlugin(LayerPlugin):
     def __init__(self):
         super(QueryPlugin, self).__init__()
-        self.collection_res = {}
 
     def do_show_layers(self, args):
         """show current configured layers."""
-        logger.plain("%s  %s  %s" % ("layer".ljust(20), "path".ljust(70), "priority"))
-        logger.plain('=' * 104)
-        for layer, _, regex, pri in self.tinfoil.cooker.bbfile_config_priorities:
-            layerdir = self.bbfile_collections.get(layer, None)
-            layername = layer
-            logger.plain("%s  %s  %s" % (layername.ljust(20), layerdir.ljust(70), pri))
+        logger.plain("%s  %s  %s  %s" % ("layer".ljust(20), "path".ljust(70), "version".ljust(7), "depends"))
+        logger.plain('=' * 113)
+        for layername, layer in self.layerdata.items():
+            depends = bb.utils.join_deps(layer.depends, commasep=False)
+            logger.plain("%s  %s  %s  %s" % (layername.ljust(20), layer.path.ljust(70), str(layer.version).ljust(7), depends))
 
     def version_str(self, pe, pv, pr = None):
         verstr = "%s" % pv
@@ -65,12 +63,9 @@  are overlayed will also be listed, with a " (skipped)" suffix.
                             classes[classfile].append(classdir)
 
         # Locating classes and other files is a bit more complicated than recipes -
-        # layer priority is not a factor; instead BitBake uses the first matching
-        # file in BBPATH, which is manipulated directly by each layer's
-        # conf/layer.conf in turn, thus the order of layers in bblayers.conf is a
-        # factor - however, each layer.conf is free to either prepend or append to
-        # BBPATH (or indeed do crazy stuff with it). Thus the order in BBPATH might
-        # not be exactly the order present in bblayers.conf either.
+        # BitBake uses the first matching file in BBPATH, which is constructed
+        # from the layer dependencies (layers come before the layers they depend
+        # upon) so the order may not match the order present in bblayers.conf.
         bbpath = str(self.tinfoil.config_data.getVar('BBPATH'))
         overlayed_class_found = False
         for (classfile, classdirs) in classes.items():
@@ -255,22 +250,6 @@  skipped recipes will also be listed, with a " (skipped)" suffix.
 
         return items_listed
 
-    def get_file_layer(self, filename):
-        layerdir = self.get_file_layerdir(filename)
-        if layerdir:
-            return self.get_layer_name(layerdir)
-        else:
-            return '?'
-
-    def get_collection_res(self):
-        if not self.collection_res:
-            self.collection_res = bb.utils.get_collection_res(self.tinfoil.config_data)
-        return self.collection_res
-
-    def get_file_layerdir(self, filename):
-        layer = bb.utils.get_file_layer(filename, self.tinfoil.config_data, self.get_collection_res())
-        return self.bbfile_collections.get(layer, None)
-
     def remove_layer_prefix(self, f):
         """Remove the layer_dir prefix, e.g., f = /path/to/layer_dir/foo/blah, the
            return value will be: layer_dir/foo/blah"""
@@ -527,12 +506,12 @@  NOTE: .bbappend files can impact the dependencies.
         self.add_command(sp, 'show-layers', self.do_show_layers, parserecipes=False)
 
         parser_show_overlayed = self.add_command(sp, 'show-overlayed', self.do_show_overlayed)
-        parser_show_overlayed.add_argument('-f', '--filenames', help='instead of the default formatting, list filenames of higher priority recipes with the ones they overlay indented underneath', action='store_true')
+        parser_show_overlayed.add_argument('-f', '--filenames', help='instead of the default formatting, list filenames of preferred recipes with the ones they overlay indented underneath', action='store_true')
         parser_show_overlayed.add_argument('-s', '--same-version', help='only list overlayed recipes where the version is the same', action='store_true')
         parser_show_overlayed.add_argument('--mc', help='use specified multiconfig', default='')
 
         parser_show_recipes = self.add_command(sp, 'show-recipes', self.do_show_recipes)
-        parser_show_recipes.add_argument('-f', '--filenames', help='instead of the default formatting, list filenames of higher priority recipes with the ones they overlay indented underneath', action='store_true')
+        parser_show_recipes.add_argument('-f', '--filenames', help='instead of the default formatting, list filenames of preferred recipes with the ones they overlay indented underneath', action='store_true')
         parser_show_recipes.add_argument('-r', '--recipes-only', help='instead of the default formatting, list recipes only', action='store_true')
         parser_show_recipes.add_argument('-m', '--multiple', help='only list where multiple recipes (in the same layer or different layers) exist for the same recipe name', action='store_true')
         parser_show_recipes.add_argument('-i', '--inherits', help='only list recipes that inherit the named class(es) - separate multiple classes using , (without spaces)', metavar='CLASS', default='')