ast/cookerdata: Allow addfragments to be called more than once
PATCH v2 1/2 ast/cookerdata: Allow addfragments to be called more than once
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 <[email protected]>
--- .../bitbake-user-manual-metadata.rst | 3 ++ lib/bb/cookerdata.py | 30 +++++++++++++++++++ lib/bb/parse/ast.py | 10 +++---- 3 files changed, 38 insertions(+), 5 deletions(-)
doc/bitbake-user-manual/bitbake-user-manual-metadata.rst +3 -0
@@ -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 ----------------------
lib/bb/cookerdata.py +30 -0
@@ -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
lib/bb/parse/ast.py +5 -5
@@ -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
PATCH v2 2/2 tests/layers: Add fragments tests
Add some tests to ensure the addfragment behaviour we want is tested.
AI-Generated: Uses Claude (Opus 5.5)
Signed-off-by Richard Purdie <[email protected]>
--- lib/bb/tests/layers.py | 90 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 87 insertions(+), 3 deletions(-)
lib/bb/tests/layers.py +87 -3
@@ -19,9 +19,9 @@ import bb.utils class _LayerBuilder(bb.cookerdata.CookerDataBuilder): - def __init__(self, topdir): + def __init__(self, topdir, skip_fragments=True): cfg = types.SimpleNamespace(prefile=[], postfile=[], tracking=False, - skip_fragments=True, env={}) + skip_fragments=skip_fragments, env={}) super().__init__(cfg) self.topdir = topdir
@@ -30,6 +30,8 @@ class _LayerBuilder(bb.cookerdata.CookerDataBuilder): class LayerTestBase(unittest.TestCase): + skip_fragments = True + CORE_CONF = """\ LAYERNAME = "core" LAYERSERIES_CORENAMES = "series1 series2"
@@ -75,7 +77,7 @@ LAYER_PASSTHROUGH_VARS_PREFIXES = "PASSVAR" def parse(self): self.write("conf/bblayers.conf", 'BBLAYERS = "%s"\n%s' % (" ".join(self.layers), self.bblayers_extra)) - builder = _LayerBuilder(self.topdir) + builder = _LayerBuilder(self.topdir, self.skip_fragments) return builder.parseConfigurationFiles([], []) def parse_fails(self, *expected):
@@ -354,6 +356,88 @@ class LayerVarsConfTest(LayerTestBase): self.assertIsNone(dc[""].getVar("FROM_LAYERVARS")) +class FragmentsTest(LayerTestBase): + skip_fragments = False + + # Mirrors the way OE-Core uses addfragments: it is called once before the + # distro configuration (which a builtin fragment can select) and again + # afterwards so the distro can enable fragments too + BITBAKE_CONF = """\ +CACHE = "${TOPDIR}/cache" +FRAGS_METADATA_VARS = "FRAG_SUMMARY" +FRAGS_BUILTIN = "distro:DISTRO" +addfragments conf/fragments FRAGMENTS FRAGS_METADATA_VARS FRAGS_BUILTIN +include conf/distro/${DISTRO}.conf +addfragments conf/fragments FRAGMENTS FRAGS_METADATA_VARS FRAGS_BUILTIN +""" + + def setUp(self): + super().setUp() + self.write("core/conf/bitbake.conf", self.BITBAKE_CONF) + self.write("core/conf/fragments/a.conf", + 'FRAG_SUMMARY = "Fragment a"\nFRAG_A = "set"\nCOUNT += "a"\n') + self.write("core/conf/fragments/b.conf", + 'FRAG_SUMMARY = "Fragment b"\nFRAG_B = "set"\nCOUNT += "b"\n') + self.write("core/conf/distro/mydistro.conf", + 'DISTRO_SET = "1"\nFRAGMENTS += "core/a"\n') + + def test_distro_fragment_applied(self): + self.bblayers_extra = 'DISTRO = "mydistro"\n' + d, _ = self.parse() + self.assertEqual(d.getVar("DISTRO_SET"), "1") + self.assertEqual(d.getVar("FRAG_A"), "set") + self.assertEqual(d.getVarFlag("FRAG_SUMMARY", "core/a"), "Fragment a") + self.assertIsNone(d.getVar("FRAG_SUMMARY")) + + def test_fragments_applied_once(self): + # distro/mydistro and core/b are seen by both addfragments calls, and + # core/a is enabled by both the user and the distro + self.bblayers_extra = 'FRAGMENTS = "distro/mydistro core/b core/a"\n' + d, _ = self.parse() + self.assertEqual(d.getVar("DISTRO"), "mydistro") + self.assertEqual(d.getVar("DISTRO_SET"), "1") + self.assertEqual(d.getVar("COUNT").split(), ["b", "a"]) + + def test_missing_distro_fragment_fatal(self): + self.write("core/conf/distro/mydistro.conf", 'FRAGMENTS += "core/missing"\n') + self.bblayers_extra = 'DISTRO = "mydistro"\n' + with self.assertLogs("BitBake", level="ERROR") as logs: + with self.assertRaises(bb.BBHandledException): + self.parse() + self.assertIn("core/missing", "\n".join(logs.output)) + + def test_recommendation_satisfied_by_distro_fragment(self): + self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "core/a"\n') + self.bblayers_extra = 'DISTRO = "mydistro"\n' + with self.assertNoLogs("BitBake", level="WARNING"): + self.parse() + + def test_recommendation_needs_fragment_from_that_layer(self): + # core/a is enabled, but the recommendation is for other/a + self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "other/a"\n') + self.write("other/conf/fragments/a.conf", 'FRAG_OTHER_A = "set"\n') + self.bblayers_extra = 'DISTRO = "mydistro"\n' + with self.assertLogs("BitBake", level="WARNING") as logs: + self.parse() + self.assertTrue(any("recommends one of the fragments" in l for l in logs.output), logs.output) + + def test_recommendation_warned_once(self): + self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "core/b"\n') + self.bblayers_extra = 'DISTRO = "mydistro"\n' + with self.assertLogs("BitBake", level="WARNING") as logs: + self.parse() + warnings = [l for l in logs.output if "recommends one of the fragments" in l] + self.assertEqual(len(warnings), 1, logs.output) + + def test_recommendation_not_checked_when_skipping_fragments(self): + self.skip_fragments = True + self.simple_layer("other", 'LAYER_FRAGMENT_RECOMMENDS_ANY = "core/b"\n') + self.bblayers_extra = 'DISTRO = "mydistro"\n' + with self.assertNoLogs("BitBake", level="WARNING"): + d, _ = self.parse() + self.assertIsNone(d.getVar("FRAG_A")) + + class LayerVarsDefaultPreferenceTest(LayerTestBase): """ End-to-end check that DEFAULT_PREFERENCE set in a layer's