public inbox for bitbake-devel@lists.openembedded.org
 help / color / mirror / Atom feed
From: Joshua Watt <jpewhacker@gmail.com>
To: bitbake-devel@lists.openembedded.org
Cc: Joshua Watt <jpewhacker@gmail.com>, Joshua Watt <JPEWhacker@gmail.com>
Subject: [bitbake-devel][PATCH v3] cookerdata: Include "originating" recipe name when parsing
Date: Fri, 20 Feb 2026 12:58:43 -0700	[thread overview]
Message-ID: <20260220195915.466314-1-JPEWhacker@gmail.com> (raw)
In-Reply-To: <20251219223620.2222861-1-JPEWhacker@gmail.com>

From: Joshua Watt <jpewhacker@gmail.com>

When a bbappend file is parsed, the FILE variable is set to the name of
the actual file being parsed (e.g. the name of the bbappend). Since
PN/PV etc. are derived from FILE, this means that they can be
misleading in the event that bbappend is using wildcards (e.g. PV might
have the value of "%" instead of the actual version number).

In order to allow bbappends to derived the actual information of the
recipe, capture the name of the original recipe being parsed as
__BB_RECIPE_FILE when parsing a new recipe. The value of this variable
doesn't change when parsing .bbappend or .inc file associated with the
recipe

Signed-off-by: Joshua Watt <JPEWhacker@gmail.com>
---
V3: Rename to __BB_RECIPE_FILE, add documentation and tests


 .../bitbake-user-manual-ref-variables.rst     |   4 +
 lib/bb/cookerdata.py                          |   2 +
 lib/bb/tests/parse-tests/classes/base.bbclass |   5 +
 .../classes/recipe-file-class.bbclass         |   2 +
 lib/bb/tests/parse-tests/conf/bitbake.conf    |  15 ++
 lib/bb/tests/parse.py                         | 132 +++++++++++++++++-
 6 files changed, 159 insertions(+), 1 deletion(-)
 create mode 100644 lib/bb/tests/parse-tests/classes/base.bbclass
 create mode 100644 lib/bb/tests/parse-tests/classes/recipe-file-class.bbclass
 create mode 100644 lib/bb/tests/parse-tests/conf/bitbake.conf

diff --git a/doc/bitbake-user-manual/bitbake-user-manual-ref-variables.rst b/doc/bitbake-user-manual/bitbake-user-manual-ref-variables.rst
index 3a01144ff..5940e6670 100644
--- a/doc/bitbake-user-manual/bitbake-user-manual-ref-variables.rst
+++ b/doc/bitbake-user-manual/bitbake-user-manual-ref-variables.rst
@@ -720,6 +720,10 @@ overview of their function and contents.
          pressure on the system. Monitor the varying value after ``Mem:`` above
          to set a sensible value.
 
+   :term:`__BB_RECIPE_FILE`
+      Specifies the name of the recipe file that is being parsed. This is the
+      same even if when evalated in a bbappend, include or bbclass file.
+
    :term:`BB_RUNFMT`
       Specifies the name of the executable script files (i.e. run files)
       saved into ``${``\ :term:`T`\ ``}``. By default, the
diff --git a/lib/bb/cookerdata.py b/lib/bb/cookerdata.py
index 22ac95eac..59f808c96 100644
--- a/lib/bb/cookerdata.py
+++ b/lib/bb/cookerdata.py
@@ -512,6 +512,8 @@ class CookerDataBuilder(object):
         bb_data.setVar("__BBMULTICONFIG", mc)
         bb_data.setVar("FILE_LAYERNAME", layername)
 
+        bb_data.setVar("__BB_RECIPE_FILE", bbfile)
+
         bbfile_loc = os.path.abspath(os.path.dirname(bbfile))
         bb.parse.cached_mtime_noerror(bbfile_loc)
 
diff --git a/lib/bb/tests/parse-tests/classes/base.bbclass b/lib/bb/tests/parse-tests/classes/base.bbclass
new file mode 100644
index 000000000..db8898e12
--- /dev/null
+++ b/lib/bb/tests/parse-tests/classes/base.bbclass
@@ -0,0 +1,5 @@
+# At least one task is required for bitbake to parse
+do_fetch() {
+    :
+}
+addtask do_fetch
diff --git a/lib/bb/tests/parse-tests/classes/recipe-file-class.bbclass b/lib/bb/tests/parse-tests/classes/recipe-file-class.bbclass
new file mode 100644
index 000000000..4682dc6c3
--- /dev/null
+++ b/lib/bb/tests/parse-tests/classes/recipe-file-class.bbclass
@@ -0,0 +1,2 @@
+BBCLASS_RECIPE_FILE := "${@os.path.basename(d.getVar('__BB_RECIPE_FILE'))}"
+BBCLASS_FILE := "${@os.path.basename(d.getVar('FILE'))}"
diff --git a/lib/bb/tests/parse-tests/conf/bitbake.conf b/lib/bb/tests/parse-tests/conf/bitbake.conf
new file mode 100644
index 000000000..e03625061
--- /dev/null
+++ b/lib/bb/tests/parse-tests/conf/bitbake.conf
@@ -0,0 +1,15 @@
+CACHE = "${TOPDIR}/cache"
+THISDIR = "${@os.path.dirname(d.getVar('FILE'))}"
+COREBASE := "${@os.path.normpath(os.path.dirname(d.getVar('FILE')+'/../../'))}"
+EXTRA_BBFILES ?= ""
+BBFILES = "${COREBASE}/recipes/*.bb ${COREBASE}/recipes/*.bbappend ${EXTRA_BBFILES}"
+PROVIDES = "${PN}"
+PN = "${@bb.parse.vars_from_file(d.getVar('FILE', False),d)[0]}"
+PF = "${BB_CURRENT_MC}:${PN}"
+export PATH
+TMPDIR ??= "${TOPDIR}"
+STAMP = "${TMPDIR}/stamps/${PN}"
+T = "${TMPDIR}/workdir/${PN}/temp"
+BB_NUMBER_THREADS = "4"
+
+BB_BASEHASH_IGNORE_VARS = "BB_CURRENT_MC BB_HASHSERVE TMPDIR TOPDIR SLOWTASKS SSTATEVALID FILE BB_CURRENTTASK"
diff --git a/lib/bb/tests/parse.py b/lib/bb/tests/parse.py
index d3867ece9..6ac2137e0 100644
--- a/lib/bb/tests/parse.py
+++ b/lib/bb/tests/parse.py
@@ -11,6 +11,8 @@ import tempfile
 import logging
 import bb
 import os
+import subprocess
+import textwrap
 
 logger = logging.getLogger('BitBake.TestParse')
 
@@ -210,7 +212,7 @@ python () {
     #
     # Test based upon a real world data corruption issue. One
     # data store changing a variable poked through into a different data
-    # store. This test case replicates that issue where the value 'B' would 
+    # store. This test case replicates that issue where the value 'B' would
     # become unset/disappear.
     #
     def test_parse_classextend_contamination(self):
@@ -508,3 +510,131 @@ EXTRA_OECONF:append = " foobar"
             test_helper("require some3.conf", " foobar")
             test_helper("include_all some.conf", " bar foo")
             test_helper("include_all some3.conf", " foobar")
+
+    def test_file_variables(self):
+        # Tests the values of FILE and __BB_RECIPE_FILE in different
+        # combinations of bbappends, includes, and inherits
+
+        def write_file(path, data):
+            with open(path, "w") as f:
+                f.write(textwrap.dedent(data))
+
+        def run_bitbake(cmd, builddir, extraenv={}):
+            env = os.environ.copy()
+            env["BBPATH"] = os.path.realpath(os.path.join(os.path.dirname(__file__), "parse-tests"))
+            env["BB_ENV_PASSTHROUGH_ADDITIONS"] = "TOPDIR"
+            env["TOPDIR"] = builddir
+            for k, v in extraenv.items():
+                env[k] = v
+                env["BB_ENV_PASSTHROUGH_ADDITIONS"] = env["BB_ENV_PASSTHROUGH_ADDITIONS"] + " " + k
+            try:
+                return subprocess.check_output(cmd, env=env, stderr=subprocess.STDOUT, universal_newlines=True, cwd=builddir)
+            except subprocess.CalledProcessError as e:
+                self.fail("Command %s failed with %s" % (cmd, e.output))
+
+        with tempfile.TemporaryDirectory(prefix="parserecipes") as recipes, tempfile.TemporaryDirectory(prefix="parsetest") as builddir:
+            extraenv = {
+                "EXTRA_BBFILES": f"{recipes}/*.bb {recipes}/*.bbappend",
+            }
+
+            inc_path = f"{recipes}/recipe-file.inc"
+            bbappend_path = f"{recipes}/recipe-%.bbappend"
+            recipe_path = f"{recipes}/recipe-file1.bb"
+
+            # __BB_RECIPE_FILE should always be the name of .bb file, even
+            # when set in a bbappend. FILE is the name of the bbappend
+            write_file(recipe_path, "")
+            write_file(bbappend_path,
+                """\
+                BBAPPEND_RECIPE_FILE := "${@os.path.basename(d.getVar('__BB_RECIPE_FILE'))}"
+                BBAPPEND_FILE := "${@os.path.basename(d.getVar('FILE'))}"
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('BBAPPEND_FILE="recipe-%.bbappend"', output)
+            self.assertIn(f'BBAPPEND_RECIPE_FILE="recipe-file1.bb"', output)
+
+            # __BB_RECIPE_FILE should always be the name of .bb file, even when
+            # set in an include file. FILE is the name of the include
+            write_file(recipe_path,
+                """\
+                require recipe-file.inc
+                """
+            )
+            write_file(inc_path,
+                """\
+                INC_RECIPE_FILE := "${@os.path.basename(d.getVar('__BB_RECIPE_FILE'))}"
+                INC_FILE := "${@os.path.basename(d.getVar('FILE'))}"
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('INC_FILE="recipe-file.inc"', output)
+            self.assertIn(f'INC_RECIPE_FILE="recipe-file1.bb"', output)
+
+            # Test when the include file is included from a bbappend
+            write_file(recipe_path, "")
+            write_file(bbappend_path,
+                """\
+                require recipe-file.inc
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('INC_FILE="recipe-file.inc"', output)
+            self.assertIn(f'INC_RECIPE_FILE="recipe-file1.bb"', output)
+
+            # Test the variables in a bbclass when inherited directly in the
+            # recipe. Note that FILE still refers to the recipe in a bbclass
+            write_file(recipe_path,
+                """\
+                inherit recipe-file-class
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('BBCLASS_FILE="recipe-file1.bb"', output)
+            self.assertIn(f'BBCLASS_RECIPE_FILE="recipe-file1.bb"', output)
+
+            # Test the variables when the inherit is in a bbappend. In this
+            # case, FILE is the bbappend
+            write_file(recipe_path, "")
+            write_file(bbappend_path,
+                """\
+                inherit recipe-file-class
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('BBCLASS_FILE="recipe-%.bbappend"', output)
+            self.assertIn(f'BBCLASS_RECIPE_FILE="recipe-file1.bb"', output)
+
+            # Test the variables when the inherit is in a include. In this
+            # case, FILE is the include file
+            write_file(recipe_path,
+                """\
+                require recipe-file.inc
+                """
+            )
+            write_file(bbappend_path, "")
+            write_file(inc_path,
+                """\
+                inherit recipe-file-class
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('BBCLASS_FILE="recipe-file.inc"', output)
+            self.assertIn(f'BBCLASS_RECIPE_FILE="recipe-file1.bb"', output)
+
+            # Test the variables when the inherit is in a include included from
+            # a bbappend. In this case, FILE is the include file
+            write_file(recipe_path, "")
+            write_file(bbappend_path,
+                """\
+                require recipe-file.inc
+                """
+            )
+            write_file(inc_path,
+                """\
+                inherit recipe-file-class
+                """
+            )
+            output = run_bitbake(["bitbake", "-e", "recipe-file1"], builddir, extraenv).splitlines()
+            self.assertIn('BBCLASS_FILE="recipe-file.inc"', output)
+            self.assertIn(f'BBCLASS_RECIPE_FILE="recipe-file1.bb"', output)
-- 
2.53.0



      parent reply	other threads:[~2026-02-20 19:59 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-30 14:19 [bitbake-devel][RFC] cookerdata: Include "originating" recipe data when parsing Joshua Watt
2025-10-30 17:09 ` Peter Kjellerstedt
2025-10-30 17:13   ` Joshua Watt
2025-10-30 21:30 ` [bitbake-devel][PATCH] toaster: support bitbake-setup Reyna, David
     [not found] ` <18736230F0A57013.18972@lists.openembedded.org>
2025-11-12  8:27   ` [bitbake-devel][PATCH] toaster: remove dependency on poky distro Reyna, David
2025-12-19 22:36 ` [bitbake-devel][PATCH v2] cookerdata: Include "originating" recipe name when parsing Joshua Watt
2026-02-19 16:32   ` Richard Purdie
2026-02-19 16:39     ` Joshua Watt
2026-02-19 16:55       ` Richard Purdie
2026-02-19 16:57         ` Joshua Watt
2026-02-20 19:58   ` Joshua Watt [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260220195915.466314-1-JPEWhacker@gmail.com \
    --to=jpewhacker@gmail.com \
    --cc=bitbake-devel@lists.openembedded.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox