aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Laplante via bitbake-devel <bitbake-devel@lists.openembedded.org>2020-01-17 14:39:54 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2020-01-19 13:30:58 +0000
commit076eb5453ca35b8b75b8270efb989d5208095b27 (patch)
treed6f1ab0ff984d001ef1434a3540c5a7a1eaaff31
parentb809a6812aa15a8a9af97bc382cc4b19571e6bfc (diff)
downloadbitbake-076eb5453ca35b8b75b8270efb989d5208095b27.tar.gz
bb.utils: add get_referenced_vars
Given a start expression, bb.utils.get_referenced_vars returns the referenced variable names in a quasi-BFS order (variables within the same level are ordered aribitrarily). For example, given an empty data store: bb.utils.get_referenced_vars("${A} ${B} ${d.getVar('C')}", d) returns either ["A", "B", "C"], ["A", "C", "B"], or another permutation. If we then set A = "${F} ${G}", then the same call will return a permutation of [A, B, C] concatenated with a permutation of [F, G]. This method is like a version of d.expandWithRefs().references that gives some insight into the depth of variable references. Signed-off-by: Chris Laplante <chris.laplante@agilent.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--lib/bb/utils.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/lib/bb/utils.py b/lib/bb/utils.py
index 06c8819d2..68ca4ef25 100644
--- a/lib/bb/utils.py
+++ b/lib/bb/utils.py
@@ -1025,6 +1025,43 @@ def filter(variable, checkvalues, d):
checkvalues = set(checkvalues)
return ' '.join(sorted(checkvalues & val))
+
+def get_referenced_vars(start_expr, d):
+ """
+ :return: names of vars referenced in start_expr (recursively), in quasi-BFS order (variables within the same level
+ are ordered arbitrarily)
+ """
+
+ seen = set()
+ ret = []
+
+ # The first entry in the queue is the unexpanded start expression
+ queue = collections.deque([start_expr])
+ # Subsequent entries will be variable names, so we need to track whether or not entry requires getVar
+ is_first = True
+
+ empty_data = bb.data.init()
+ while queue:
+ entry = queue.popleft()
+ if is_first:
+ # Entry is the start expression - no expansion needed
+ is_first = False
+ expression = entry
+ else:
+ # This is a variable name - need to get the value
+ expression = d.getVar(entry, False)
+ ret.append(entry)
+
+ # expandWithRefs is how we actually get the referenced variables in the expression. We call it using an empty
+ # data store because we only want the variables directly used in the expression. It returns a set, which is what
+ # dooms us to only ever be "quasi-BFS" rather than full BFS.
+ new_vars = empty_data.expandWithRefs(expression, None).references - set(seen)
+
+ queue.extend(new_vars)
+ seen.update(new_vars)
+ return ret
+
+
def cpu_count():
return multiprocessing.cpu_count()