Skip to contentSkip to conversation

bitbake: parse: add support for flagged fragment variables

PATCH New Alexander Kanavin 1 participant 2 messages Dec 11, 2024

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 bitbake: parse: add support for flagged fragment variables

From: Alexander Kanavin <[email protected]>

It was suggested that using flags in fragment content to mark fragment-specific metadata (such as descriptions and dependencies) is prone to quiet regressions when fragments are renamed or moved, and it clutters the fragment content.

With this change allowed fragment metadata variables must be explicitly listed in a variable that is given to addfragment directive, and parser will add the flag containing the fragment name to them.

---
 lib/bb/parse/ast.py                  | 11 +++++++++--
 lib/bb/parse/parse_py/ConfHandler.py |  2 +-
 2 files changed, 10 insertions(+), 3 deletions(-)
lib/bb/parse/ast.py +9 -2
index 7abefab2c2e..03370180e35 100644
@@ -327,10 +327,11 @@ class InheritDeferredNode(AstNode):
         data.setVar('__BBDEFINHERITS', inherits)
 
 class AddFragmentsNode(AstNode):
-    def __init__(self, filename, lineno, fragments_path_prefix, fragments_variable):
+    def __init__(self, filename, lineno, fragments_path_prefix, fragments_variable, flagged_variables_list_variable):
         AstNode.__init__(self, filename, lineno)
         self.fragments_path_prefix = fragments_path_prefix
         self.fragments_variable = fragments_variable
+        self.flagged_variables_list_variable = flagged_variables_list_variable
 
     def eval(self, data):
         # No need to use mark_dependency since we would only match a fragment
@@ -345,6 +346,7 @@ class AddFragmentsNode(AstNode):
 
         fragments = data.getVar(self.fragments_variable)
         layers = data.getVar('BBLAYERS')
+        flagged_variables = data.getVar(self.flagged_variables_list_variable).split()
 
         if not fragments:
             return
@@ -354,6 +356,10 @@ class AddFragmentsNode(AstNode):
             fragment_path = find_fragment(layers, layerid, full_fragment_name)
             if fragment_path:
                 bb.parse.ConfHandler.include(self.filename, fragment_path, self.lineno, data, "include fragment")
+                for flagged_var in flagged_variables:
+                    val = data.getVar(flagged_var)
+                    data.setVarFlag(flagged_var, f, val)
+                    data.setVar(flagged_var, None)
             else:
                 bb.error("Could not find fragment {} in enabled layers: {}".format(f, layers))
 
@@ -404,7 +410,8 @@ def handleInheritDeferred(statements, filename, lineno, m):
 def handleAddFragments(statements, filename, lineno, m):
     fragments_path_prefix = m.group(1)
     fragments_variable = m.group(2)
-    statements.append(AddFragmentsNode(filename, lineno, fragments_path_prefix, fragments_variable))
+    flagged_variables_list_variable = m.group(3)
+    statements.append(AddFragmentsNode(filename, lineno, fragments_path_prefix, fragments_variable, flagged_variables_list_variable))
 
 def runAnonFuncs(d):
     code = []
lib/bb/parse/parse_py/ConfHandler.py +1 -1
index 35321dacfe1..d0711eda042 100644
@@ -47,7 +47,7 @@ __export_regexp__ = re.compile( r"export\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
 __unset_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)$" )
 __unset_flag_regexp__ = re.compile( r"unset\s+([a-zA-Z0-9\-_+.${}/~]+)\[([a-zA-Z0-9\-_+.][a-zA-Z0-9\-_+.@]+)\]$" )
 __addpylib_regexp__      = re.compile(r"addpylib\s+(.+)\s+(.+)" )
-__addfragments_regexp__  = re.compile(r"addfragments\s+(.+)\s+(.+)" )
+__addfragments_regexp__  = re.compile(r"addfragments\s+(.+)\s+(.+)\s+(.+)" )
 
 def init(data):
     return
Signature · 1 line
2.39.5
#1 lore raw

PATCH 2/2 bitbake-user-manual-metadata: add a section about 'addfragments`

From: Alexander Kanavin <[email protected]>

---
 .../bitbake-user-manual-metadata.rst          | 48 ++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
doc/bitbake-user-manual/bitbake-user-manual-metadata.rst +47 -1
index 40a0c6f02d9..75930fef829 100644
@@ -754,7 +754,9 @@ share the task.
 This section presents the mechanisms BitBake provides to allow you to
 share functionality between recipes. Specifically, the mechanisms
 include ``include``, ``inherit``, :term:`INHERIT`, and ``require``
-directives.
+directives. There is also a higher-level abstraction called
+``configuration fragments`` that is enabled with ``addfragments``
+directive.
 
 Locating Include and Class Files
 --------------------------------
@@ -962,6 +964,50 @@ the ``autotools`` and ``pkgconfig`` classes::
 
    INHERIT += "autotools pkgconfig"
 
+``addfragments`` Directive
+--------------------------
+
+This directive allows fine-tuning local configurations with configuration
+snippets contained in layers in a structured, controlled way. Typically it would
+go into ``bitbake.conf``, for example::
+
+   addfragments conf/fragments OE_FRAGMENTS OE_FRAGMENTS_METADATA_VARS
+
+``addfragments`` takes three parameters:
+
+-  path prefix for fragment files inside the layer file tree that bitbake
+uses to construct full paths to the fragment files
+
+-  name of variable that holds the list of enabled fragments in an
+active build
+
+-  name of variable that contains a list of variable names containing
+fragment-specific metadata (such as descriptions)
+
+This allows listing enabled configuration fragments in ``OE_FRAGMENTS``
+variable like this::
+
+   OE_FRAGMENTS = "core/domain/somefragment core/someotherfragment anotherlayer/anotherdomain/anotherfragment"
+
+Fragment names listed in this variable must be prefixed by the layer name
+where a fragment file is located, defined by :term:`BBFILE_COLLECTIONS` in ``layer.conf``.
+
+The implementation then expands this list into
+:ref:`require <bitbake-user-manual/bitbake-user-manual-metadata:\`\`require\`\` directive>`
+directives with full paths to respective layers::
+
+   require /path/to/core-layer/conf/fragments/domain/somefragment.conf
+   require /path/to/core-layer/conf/fragments/someotherfragment.conf
+   require /path/to/another-layer/conf/fragments/anotherdomain/anotherfragment.conf
+
+The variable containing a list of fragment metadata variables could look like this::
+
+   OE_FRAGMENTS_METADATA_VARS = "BB_CONF_FRAGMENT_SUMMARY BB_CONF_FRAGMENT_DESCRIPTION"
+
+The implementation will add a flag containing the fragment name to each of those variables
+when parsing fragments, so that the variables are namespaced by fragment name, and do not override
+each other when several fragments are enabled.
+
 Functions
 =========
 
Signature · 1 line
2.39.5