diff mbox series

[16/17] ast/cookerdata: Allow addfragments to be called more than once

Message ID 20261130152847.3472849-16-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
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.

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 .../bitbake-user-manual-metadata.rst           |  3 +++
 lib/bb/cookerdata.py                           | 18 ++++++++++++++++++
 lib/bb/parse/ast.py                            | 10 +++++-----
 3 files changed, 26 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 3bea91e2387..7d769682450 100644
--- a/lib/bb/cookerdata.py
+++ b/lib/bb/cookerdata.py
@@ -228,6 +228,22 @@  def findTopdir():
 
     return os.path.abspath(os.getcwd())
 
+
+def check_fragment_recommendations(d):
+    """
+    Warn about recommended fragments which aren't enabled or opted out.
+    """
+    enabled = d.getVar("_BB_FRAGMENTS_ENABLED")
+    if enabled is None:
+        return
+    enabled = set(enabled.split())
+    layerdata = d.getVar("layerdata", expand=False) or {}
+    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 (wanted & enabled):
+            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
@@ -663,6 +679,8 @@  class CookerDataBuilder(object):
         for p in postfiles:
             data = parse_config_file(p, data)
 
+        check_fragment_recommendations(data)
+
         # Handle any INHERITs and inherit the base class
         bbclasses  = ["base"] + (data.getVar('INHERIT') or "").split()
         for bbclass in bbclasses:
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