Convert $(words) to Starlark

Bug: 226974242
Test: go test
Change-Id: I8ab5943306586c6a993df470e7a2894dd7ff1619
This commit is contained in:
Cole Faust 2022-04-22 17:43:52 -07:00
parent 12097e3109
commit 94c4a9ac62
2 changed files with 45 additions and 7 deletions

View file

@ -116,6 +116,7 @@ var knownFunctions = map[string]interface {
"subst": &substCallParser{fname: "subst"},
"warning": &makeControlFuncParser{name: baseName + ".mkwarning"},
"word": &wordCallParser{},
"words": &wordsCallParser{},
"wildcard": &simpleCallParser{name: baseName + ".expand_wildcard", returnType: starlarkTypeList},
}
@ -1637,9 +1638,11 @@ func (p *wordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkpa
if len(words) != 2 {
return ctx.newBadExpr(node, "word function should have 2 arguments")
}
var index uint64 = 0
var index = 0
if words[0].Const() {
index, _ = strconv.ParseUint(strings.TrimSpace(words[0].Strings[0]), 10, 64)
if i, err := strconv.Atoi(strings.TrimSpace(words[0].Strings[0])); err == nil {
index = i
}
}
if index < 1 {
return ctx.newBadExpr(node, "word index should be constant positive integer")
@ -1647,13 +1650,40 @@ func (p *wordCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkpa
words[1].TrimLeftSpaces()
words[1].TrimRightSpaces()
array := ctx.parseMakeString(node, words[1])
if xBad, ok := array.(*badExpr); ok {
return xBad
if bad, ok := array.(*badExpr); ok {
return bad
}
if array.typ() != starlarkTypeList {
array = &callExpr{object: array, name: "split", returnType: starlarkTypeList}
array = &callExpr{
name: baseName + ".words",
args: []starlarkExpr{array},
returnType: starlarkTypeList,
}
}
return &indexExpr{array, &intLiteralExpr{index - 1}}
}
type wordsCallParser struct{}
func (p *wordsCallParser) parse(ctx *parseContext, node mkparser.Node, args *mkparser.MakeString) starlarkExpr {
args.TrimLeftSpaces()
args.TrimRightSpaces()
array := ctx.parseMakeString(node, args)
if bad, ok := array.(*badExpr); ok {
return bad
}
if array.typ() != starlarkTypeList {
array = &callExpr{
name: baseName + ".words",
args: []starlarkExpr{array},
returnType: starlarkTypeList,
}
}
return &callExpr{
name: "len",
args: []starlarkExpr{array},
returnType: starlarkTypeInt,
}
return &indexExpr{array, &intLiteralExpr{int(index - 1)}}
}
type firstOrLastwordCallParser struct {

View file

@ -808,6 +808,10 @@ def init(g, handle):
PRODUCT_COPY_FILES := $(addprefix pfx-,a b c)
PRODUCT_COPY_FILES := $(addsuffix .sff, a b c)
PRODUCT_NAME := $(word 1, $(subst ., ,$(TARGET_BOARD_PLATFORM)))
ifeq (1,$(words $(SOME_UNKNOWN_VARIABLE)))
endif
ifeq ($(words $(SOME_OTHER_VARIABLE)),$(SOME_INT_VARIABLE))
endif
$(info $(patsubst %.pub,$(PRODUCT_NAME)%,$(PRODUCT_ADB_KEYS)))
$(info $$(dir foo/bar): $(dir foo/bar))
$(info $(firstword $(PRODUCT_COPY_FILES)))
@ -830,7 +834,11 @@ def init(g, handle):
cfg = rblf.cfg(handle)
cfg["PRODUCT_COPY_FILES"] = rblf.addprefix("pfx-", "a b c")
cfg["PRODUCT_COPY_FILES"] = rblf.addsuffix(".sff", "a b c")
cfg["PRODUCT_NAME"] = ((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " ")).split()[0]
cfg["PRODUCT_NAME"] = rblf.words((g.get("TARGET_BOARD_PLATFORM", "")).replace(".", " "))[0]
if len(rblf.words(g.get("SOME_UNKNOWN_VARIABLE", ""))) == 1:
pass
if ("%d" % (len(rblf.words(g.get("SOME_OTHER_VARIABLE", ""))))) == g.get("SOME_INT_VARIABLE", ""):
pass
rblf.mkinfo("product.mk", rblf.mkpatsubst("%.pub", "%s%%" % cfg["PRODUCT_NAME"], g.get("PRODUCT_ADB_KEYS", "")))
rblf.mkinfo("product.mk", "$(dir foo/bar): %s" % rblf.dir("foo/bar"))
rblf.mkinfo("product.mk", cfg["PRODUCT_COPY_FILES"][0])