Skip to contentSkip to conversation

tests/runqueue: Set TOPDIR in test

PATCH New Richard Purdie 1 participant 3 messages Nov 13, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 tests/runqueue: Set TOPDIR in test

It was clear with testing that we're asuming bitbake sets TOPDIR correctly when running these tests. Remove that implict assumption and make it explicit.

---
 lib/bb/tests/runqueue.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
lib/bb/tests/runqueue.py +2 -1
index 5b6ada886a..2bf00dc62a 100644
@@ -29,9 +29,10 @@ class RunQueueTests(unittest.TestCase):
     def run_bitbakecmd(self, cmd, builddir, sstatevalid="", slowtasks="", extraenv=None, cleanup=False):
         env = os.environ.copy()
         env["BBPATH"] = os.path.realpath(os.path.join(os.path.dirname(__file__), "runqueue-tests"))
-        env["BB_ENV_EXTRAWHITE"] = "SSTATEVALID SLOWTASKS"
+        env["BB_ENV_EXTRAWHITE"] = "SSTATEVALID SLOWTASKS TOPDIR"
         env["SSTATEVALID"] = sstatevalid
         env["SLOWTASKS"] = slowtasks
+        env["TOPDIR"] = builddir
         if extraenv:
             for k in extraenv:
                 env[k] = extraenv[k]
Signature · 1 line
2.32.0
#1 lore raw

PATCH 2/3 cookerdata: Set TOPDIR explicitly and fix broke findTopdir()

TOPDIR is set internally deep within the parser to os.getcwd(). Rather than do that, set it explicitly if not set. Note that modern code will almost always have a bblayers.conf file which would have already set TOPDIR before this new code.

Also fix findTopdir since the conf/bitbake.conf codepath is just plain incorrect, it would find build metadata, not the current build directory that bitbake would use. Again, the use of bblayers.conf means hitting the fallback code was unlikely.

This change makes everything clear and explicit.

---
 lib/bb/cookerdata.py | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/bb/cookerdata.py +6 -6
index 592bc2968e..397b43dfa7 100644
@@ -210,7 +210,7 @@ def findConfigFile(configfile, data):
 
 #
 # We search for a conf/bblayers.conf under an entry in BBPATH or in cwd working
-# up to /. If that fails, we search for a conf/bitbake.conf in BBPATH.
+# up to /. If that fails, bitbake would fall back to cwd.
 #
 
 def findTopdir():
@@ -223,11 +223,8 @@ def findTopdir():
     layerconf = findConfigFile("bblayers.conf", d)
     if layerconf:
         return os.path.dirname(os.path.dirname(layerconf))
-    if bbpath:
-        bitbakeconf = bb.utils.which(bbpath, "conf/bitbake.conf")
-        if bitbakeconf:
-            return os.path.dirname(os.path.dirname(bitbakeconf))
-    return None
+
+    return os.path.abspath(os.getcwd())
 
 class CookerDataBuilder(object):
 
@@ -417,6 +414,9 @@ class CookerDataBuilder(object):
                         " invoked bitbake from the wrong directory?")
             raise SystemExit(msg)
 
+        if not data.getVar("TOPDIR"):
+            data.setVar("TOPDIR", os.path.abspath(os.getcwd()))
+
         data = parse_config_file(os.path.join("conf", "bitbake.conf"), data)
 
         # Parse files for loading *after* bitbake.conf and any includes
Signature · 1 line
2.32.0
#2 lore raw

PATCH 3/3 cache/ConfHandler: Drop TOPDIR/chdir manipulations

In reply to Richard Purdie

This code has been unchanged since 2006 apart from attempts to optimise performance by minimising chdir() calls.

There is no reason the modern bitbake parser should be changing directory all the time. We did have some path assumptions in the mists of time but those were resovled and the code is deterministic and doesn't depend on cwd now for parsing. We can therefore drop the changes in directory.

Also, TOPDIR is now being set by cookerdata in all cases so we don't need the fallbacks in this code (which was used to effectively initialise a value). We don't need to change TOPDIR when parsing a recipe, that makes no sense. If we stop all the other messing around, we don't need to expand TMPDIR either.

These changes have the potential to break some obscure use cases such as an anonymous function assuming the current working directory, or some case which depends on TOPDIR changing but I believe any such uses should be fixed at this point.

---
 lib/bb/cache.py                      | 29 ++++------------------------
 lib/bb/parse/parse_py/ConfHandler.py |  5 +----
 2 files changed, 5 insertions(+), 29 deletions(-)
lib/bb/cache.py +4 -25
index 4e08c100ab..fcb15796cc 100644
@@ -284,36 +284,15 @@ def parse_recipe(bb_data, bbfile, appends, mc=''):
     Parse a recipe
     """
 
-    chdir_back = False
-
     bb_data.setVar("__BBMULTICONFIG", mc)
 
-    # expand tmpdir to include this topdir
-    bb_data.setVar('TMPDIR', bb_data.getVar('TMPDIR') or "")
     bbfile_loc = os.path.abspath(os.path.dirname(bbfile))
-    oldpath = os.path.abspath(os.getcwd())
     bb.parse.cached_mtime_noerror(bbfile_loc)
 
-    # The ConfHandler first looks if there is a TOPDIR and if not
-    # then it would call getcwd().
-    # Previously, we chdir()ed to bbfile_loc, called the handler
-    # and finally chdir()ed back, a couple of thousand times. We now
-    # just fill in TOPDIR to point to bbfile_loc if there is no TOPDIR yet.
-    if not bb_data.getVar('TOPDIR', False):
-        chdir_back = True
-        bb_data.setVar('TOPDIR', bbfile_loc)
-    try:
-        if appends:
-            bb_data.setVar('__BBAPPEND', " ".join(appends))
-        bb_data = bb.parse.handle(bbfile, bb_data)
-        if chdir_back:
-            os.chdir(oldpath)
-        return bb_data
-    except:
-        if chdir_back:
-            os.chdir(oldpath)
-        raise
-
+    if appends:
+        bb_data.setVar('__BBAPPEND', " ".join(appends))
+    bb_data = bb.parse.handle(bbfile, bb_data)
+    return bb_data
 
 
 class NoCache(object):
lib/bb/parse/parse_py/ConfHandler.py +1 -4
index 0834fe3f9b..b895d5b5ef 100644
@@ -48,10 +48,7 @@ __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\-_+.]+)\]$" )
 
 def init(data):
-    topdir = data.getVar('TOPDIR', False)
-    if not topdir:
-        data.setVar('TOPDIR', os.getcwd())
-
+    return
 
 def supports(fn, d):
     return fn[-5:] == ".conf"
Signature · 1 line
2.32.0