From 076eb5453ca35b8b75b8270efb989d5208095b27 Mon Sep 17 00:00:00 2001 From: Chris Laplante via bitbake-devel Date: Fri, 17 Jan 2020 14:39:54 -0500 Subject: 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 Signed-off-by: Richard Purdie --- lib/bb/utils.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) 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() -- cgit 1.2.3-korg