bitbake: parse/ast: add support for 'built-in' fragments

When reviewing proposed fragments to add settings for DISTRO and MACHINE,
RP noted that such fragments only add clutter and overhead, and there's
no need to maintain them as separate files.

Rather when bitbake sees 'fragmentvar/fragmentvalue' it can expand that into
FRAGMENTVAR = "fragmentvalue".

To achieve that, 'addfragments' directive is extended with a parameter
that sets the name of the variable that holds definitions of such
built-in fragments, for example like this:

"machine:MACHINE distro:DISTRO"

Then each enabled fragment name is matched against these definitions and the
respective variable is set, e.g. 'machine/qemuarm' would match
'machine:MACHINE' and result in MACHINE set to 'qemuarm'.

This happens before any fragment files are looked up on disk,
and no such lookup happens if there was a match, which should prevent
possible misuse of the feature. So the builtin fragment definition
is also an allowlist for them.

Please also see the patches for oe-core that show an application of the feature.

(Bitbake rev: 3b9d7bea915dc7f10e845854f1dae325743f9456)

Signed-off-by: Alexander Kanavin <alex@linutronix.de>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
This commit is contained in:
Alexander Kanavin
2025-06-18 11:20:53 +02:00
committed by Richard Purdie
parent be8123d8df
commit 941ac7d256
3 changed files with 32 additions and 5 deletions

View File

@@ -343,11 +343,12 @@ class InheritDeferredNode(AstNode):
bb.parse.BBHandler.inherit_defer(*self.inherit, data)
class AddFragmentsNode(AstNode):
def __init__(self, filename, lineno, fragments_path_prefix, fragments_variable, flagged_variables_list_variable):
def __init__(self, filename, lineno, fragments_path_prefix, fragments_variable, flagged_variables_list_variable, builtin_fragments_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
self.builtin_fragments_variable = builtin_fragments_variable
def eval(self, data):
# No need to use mark_dependency since we would only match a fragment
@@ -360,13 +361,23 @@ class AddFragmentsNode(AstNode):
return candidate_fragment_path
return None
def check_and_set_builtin_fragment(fragment, data, builtin_fragments):
prefix, value = fragment.split('/', 1)
if prefix in builtin_fragments.keys():
data.setVar(builtin_fragments[prefix], value)
return True
return False
fragments = data.getVar(self.fragments_variable)
layers = data.getVar('BBLAYERS')
flagged_variables = data.getVar(self.flagged_variables_list_variable).split()
builtin_fragments = {f[0]:f[1] for f in [f.split(':') for f in data.getVar(self.builtin_fragments_variable).split()] }
if not fragments:
return
for f in fragments.split():
if check_and_set_builtin_fragment(f, data, builtin_fragments):
continue
layerid, fragment_name = f.split('/', 1)
full_fragment_name = data.expand("{}/{}.conf".format(self.fragments_path_prefix, fragment_name))
fragment_path = find_fragment(layers, layerid, full_fragment_name)
@@ -430,7 +441,8 @@ def handleAddFragments(statements, filename, lineno, m):
fragments_path_prefix = m.group(1)
fragments_variable = m.group(2)
flagged_variables_list_variable = m.group(3)
statements.append(AddFragmentsNode(filename, lineno, fragments_path_prefix, fragments_variable, flagged_variables_list_variable))
builtin_fragments_variable = m.group(4)
statements.append(AddFragmentsNode(filename, lineno, fragments_path_prefix, fragments_variable, flagged_variables_list_variable, builtin_fragments_variable))
def runAnonFuncs(d):
code = []

View File

@@ -48,7 +48,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+(.+)\s+(.+)" )
__addfragments_regexp__ = re.compile(r"addfragments\s+(.+)\s+(.+)\s+(.+)\s+(.+)" )
def init(data):
return