[PATCH v3 1/2] binman: Do not pollute source tree when build with `make O=...`

Andy Shevchenko andriy.shevchenko at linux.intel.com
Mon Dec 6 12:44:12 CET 2021


Importing libraries in Python caches the bytecode by default.
Since we run scripts in source tree it ignores the current directory
settings, which is $(srctree), and creates cache just in the middle
of the source tree. Move cache to the current directory.

Signed-off-by: Andy Shevchenko <andriy.shevchenko at linux.intel.com>
---
v3: avoided crash (Simon), preserved tree hierarchy
 tools/binman/main.py | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/tools/binman/main.py b/tools/binman/main.py
index 8c1e478d54ce..d19ded491af7 100755
--- a/tools/binman/main.py
+++ b/tools/binman/main.py
@@ -16,9 +16,20 @@ import sys
 import traceback
 import unittest
 
+# Get the absolute path to this file at run-time
+our_path = os.path.dirname(os.path.realpath(__file__))
+our1_path = os.path.dirname(our_path)
+our2_path = os.path.dirname(our1_path)
+
+#
+# Do not pollute source tree with cache files:
+# https://stackoverflow.com/a/60024195/2511795
+# https://bugs.python.org/issue33499
+#
+sys.pycache_prefix = os.path.relpath(our_path, os.environ.get('srctree', our2_path))
+
 # Bring in the patman and dtoc libraries (but don't override the first path
 # in PYTHONPATH)
-our_path = os.path.dirname(os.path.realpath(__file__))
 sys.path.insert(2, os.path.join(our_path, '..'))
 
 from patman import test_util
-- 
2.33.0



More information about the U-Boot mailing list