diff mbox series

[v2,1/2] ast/cookerdata: Allow addfragments to be called more than once

Message ID 20261201165013.288011-1-richard.purdie@linuxfoundation.org
State New
Headers show
Series [v2,1/2] ast/cookerdata: Allow addfragments to be called more than once | expand

Commit Message

Richard Purdie Dec. 1, 2026, 4:50 p.m. UTC
Since it was implemented, one probably that has been tricky is that distros can't
enable fragments since the distro itself can be selected by a fragment.

Work around this by allowing the addfragments call to be called more than once.
Fragments are only applied the first time they are seen.

This means the recommends checks have to happen after bitbake.conf is parsed.

The checks for inclusion look at BBINCLUDED which means if files are included
directly by include/require, the checks also don't trigger.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 .../bitbake-user-manual-metadata.rst          |  3 ++
 lib/bb/cookerdata.py                          | 30 +++++++++++++++++++
 lib/bb/parse/ast.py                           | 10 +++----
 3 files changed, 38 insertions(+), 5 deletions(-)
diff mbox series

Patch

diff --git a/doc/bitbake-user-manual/bitbake-user-manual-metadata.rst b/doc/bitbake-user-manual/bitbake-user-manual-metadata.rst
index 3fe4489aebb..b52b5de03d8 100644
--- a/doc/bitbake-user-manual/bitbake-user-manual-metadata.rst
+++ b/doc/bitbake-user-manual/bitbake-user-manual-metadata.rst
@@ -1052,6 +1052,9 @@  bitbake will treat that as direct value assignment in its configuration::
 
    SOMEVARIABLE = "somevalue"
 
+``addfragments`` can be called more than once with the same arguments.
+Fragments that have already been applied are not applied again.
+
 Locating Include Files
 ----------------------
 
diff --git a/lib/bb/cookerdata.py b/lib/bb/cookerdata.py
index 3006c461ec7..bfde5af0681 100644
--- a/lib/bb/cookerdata.py
+++ b/lib/bb/cookerdata.py
@@ -228,6 +228,34 @@  def findTopdir():
 
     return os.path.abspath(os.getcwd())
 
+
+def check_fragment_recommendations(d):
+    """
+    Warn about recommended fragments which aren't enabled or opted out.
+
+    A fragment "<layer>/<name>" counts as enabled if <name>.conf from that
+    layer appears in BBINCLUDED. BBINCLUDED also lists files which were only
+    searched for along BBPATH, so the file has to exist too.
+    """
+    if d.getVar("_BB_SKIP_FRAGMENTS") == "1":
+        return
+    layerdata = d.getVar("layerdata", expand=False) or {}
+    included = [f for f in (d.getVar("BBINCLUDED") or "").split() if f.endswith(".conf")]
+
+    def is_enabled(fragment):
+        layer, _, name = fragment.partition("/")
+        if layer not in layerdata or not name:
+            return False
+        layerdir = os.path.abspath(layerdata[layer].path) + os.sep
+        suffix = os.sep + name + ".conf"
+        return any(f.startswith(layerdir) and f.endswith(suffix) and os.path.exists(f) for f in included)
+
+    optedout = set((d.getVar("LAYER_FRAGMENTS_OPTED_OUT") or "").split())
+    for layer in layerdata:
+        wanted = layerdata[layer].fragment_recommends_any - optedout
+        if wanted and not any(is_enabled(f) for f in wanted):
+            bb.warn("Layer %s recommends one of the fragments %s to be enabled but none are" % (layer, str(wanted)))
+
 @dataclasses.dataclass(repr=False)
 class Layer:
     path: str
@@ -680,6 +708,8 @@  class CookerDataBuilder(object):
 
         data.setVar('BBINCLUDED',bb.parse.get_file_depends(data))
 
+        check_fragment_recommendations(data)
+
         return data, layerdata
 
     @staticmethod
diff --git a/lib/bb/parse/ast.py b/lib/bb/parse/ast.py
index c26632b8b63..ffba5d5b960 100644
--- a/lib/bb/parse/ast.py
+++ b/lib/bb/parse/ast.py
@@ -394,11 +394,11 @@  class AddFragmentsNode(AstNode):
         builtin_fragments = {f[0]:f[1] for f in [f.split(':') for f in data.getVar(self.builtin_fragments_variable).split()] }
 
         layerdata = data.getVar("layerdata", expand=False)
-        optedout = set((data.getVar("LAYER_FRAGMENTS_OPTED_OUT") or "").split())
-        for layer in layerdata:
-            wanted = layerdata[layer].fragment_recommends_any - optedout
-            if wanted and not (wanted & set(fragments)):
-                bb.warn("Layer %s recommends one of the fragments %s to be enabled but none are" % (layer, str(wanted)))
+
+        # addfragments can be called more than once, only apply fragments once
+        enabled = (data.getVar("_BB_FRAGMENTS_ENABLED") or "").split()
+        fragments = [f for f in fragments if f not in enabled]
+        data.setVar("_BB_FRAGMENTS_ENABLED", " ".join(enabled + fragments))
 
         if not fragments:
             return