Flatten foreach calls that produce 2d lists

In make, the result of $(foreach $(x),$(y),$(foreach $(z),(w),a))
is a regular list, but in Starlark it's a list of lists. Flatten
the results of foreach expressions where each element is a list
so that they're regular lists of strings again.

Bug: 226974242
Test: go test
Change-Id: I3210d409aba0d807a5890e341ab1e0c0478f5930
This commit is contained in:
Cole Faust
2022-05-05 11:45:04 -07:00
parent 2845464d2d
commit 72374fc628
2 changed files with 18 additions and 4 deletions

View File

@@ -1574,11 +1574,21 @@ func (p *foreachCallParser) parse(ctx *parseContext, node mkparser.Node, args *m
} }
} }
return &foreachExpr{ var result starlarkExpr = &foreachExpr{
varName: loopVarName, varName: loopVarName,
list: list, list: list,
action: action, action: action,
} }
if action.typ() == starlarkTypeList {
result = &callExpr{
name: baseName + ".flatten_2d_list",
args: []starlarkExpr{result},
returnType: starlarkTypeList,
}
}
return result
} }
func transformNode(node starlarkNode, transformer func(expr starlarkExpr) starlarkExpr) { func transformNode(node starlarkNode, transformer func(expr starlarkExpr) starlarkExpr) {

View File

@@ -579,7 +579,7 @@ def init(g, handle):
pass pass
if rblf.expand_wildcard("foo*.mk"): if rblf.expand_wildcard("foo*.mk"):
pass pass
if rblf.expand_wildcard("foo*.mk bar*.mk") == ["foo1.mk", "foo2.mk", "barxyz.mk"]: if rblf.expand_wildcard("foo*.mk bar*.mk") == ["foo1.mk", "foo2.mk", "barxyz.mk"]:
pass pass
`, `,
}, },
@@ -1363,6 +1363,8 @@ BOOT_KERNEL_MODULES_FILTER := $(foreach m,$(BOOT_KERNEL_MODULES),%/$(m))
BOOT_KERNEL_MODULES_LIST := foo.ko BOOT_KERNEL_MODULES_LIST := foo.ko
BOOT_KERNEL_MODULES_LIST += bar.ko BOOT_KERNEL_MODULES_LIST += bar.ko
BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m)) BOOT_KERNEL_MODULES_FILTER_2 := $(foreach m,$(BOOT_KERNEL_MODULES_LIST),%/$(m))
NESTED_LISTS := $(foreach m,$(SOME_VAR),$(BOOT_KERNEL_MODULES_LIST))
NESTED_LISTS_2 := $(foreach x,$(SOME_VAR),$(foreach y,$(x),prefix$(y)))
FOREACH_WITH_IF := $(foreach module,\ FOREACH_WITH_IF := $(foreach module,\
$(BOOT_KERNEL_MODULES_LIST),\ $(BOOT_KERNEL_MODULES_LIST),\
@@ -1382,6 +1384,8 @@ def init(g, handle):
g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"] g["BOOT_KERNEL_MODULES_LIST"] = ["foo.ko"]
g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"] g["BOOT_KERNEL_MODULES_LIST"] += ["bar.ko"]
g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]] g["BOOT_KERNEL_MODULES_FILTER_2"] = ["%%/%s" % m for m in g["BOOT_KERNEL_MODULES_LIST"]]
g["NESTED_LISTS"] = rblf.flatten_2d_list([g["BOOT_KERNEL_MODULES_LIST"] for m in rblf.words(g.get("SOME_VAR", ""))])
g["NESTED_LISTS_2"] = rblf.flatten_2d_list([["prefix%s" % y for y in rblf.words(x)] for x in rblf.words(g.get("SOME_VAR", ""))])
g["FOREACH_WITH_IF"] = [("" if rblf.filter(module, "foo.ko") else rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)) for module in g["BOOT_KERNEL_MODULES_LIST"]] g["FOREACH_WITH_IF"] = [("" if rblf.filter(module, "foo.ko") else rblf.mkerror("product.mk", "module \"%s\" has an error!" % module)) for module in g["BOOT_KERNEL_MODULES_LIST"]]
# Same as above, but not assigning it to a variable allows it to be converted to statements # Same as above, but not assigning it to a variable allows it to be converted to statements
for module in g["BOOT_KERNEL_MODULES_LIST"]: for module in g["BOOT_KERNEL_MODULES_LIST"]:
@@ -1574,10 +1578,10 @@ def init(g, handle):
for x in rblf.words(g.get("MY_LIST_VAR", "")): for x in rblf.words(g.get("MY_LIST_VAR", "")):
_entry = { _entry = {
"foo/font.mk": ("foo/font", _font_init), "foo/font.mk": ("foo/font", _font_init),
}.get("foo/%s.mk" % _x) }.get("foo/%s.mk" % x)
(_varmod, _varmod_init) = _entry if _entry else (None, None) (_varmod, _varmod_init) = _entry if _entry else (None, None)
if not _varmod_init: if not _varmod_init:
rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % _x)) rblf.mkerror("product.mk", "Cannot find %s" % ("foo/%s.mk" % x))
_varmod_init(g, handle) _varmod_init(g, handle)
`, `,
}, },