Allow comparing $(wildcard) results to non-empty values

Also remove the file existence functions because they
can just be wildcards instead.

Bug: 226974242
Test: go test
Change-Id: Icbf65c47af97a710580864e8b76e2697aba96dd8
This commit is contained in:
Cole Faust
2022-04-26 12:06:49 -07:00
parent 12097e3109
commit a99afdfc22
2 changed files with 18 additions and 20 deletions

View File

@@ -1068,6 +1068,18 @@ func (ctx *parseContext) parseCompare(cond *mkparser.Directive) starlarkExpr {
return otherOperand return otherOperand
} }
} }
if otherOperand.typ() == starlarkTypeList {
fields := strings.Fields(stringOperand)
elements := make([]starlarkExpr, len(fields))
for i, s := range fields {
elements[i] = &stringLiteralExpr{literal: s}
}
return &eqExpr{
left: otherOperand,
right: &listExpr{elements},
isEq: isEq,
}
}
if intOperand, err := strconv.Atoi(strings.TrimSpace(stringOperand)); err == nil && otherOperand.typ() == starlarkTypeInt { if intOperand, err := strconv.Atoi(strings.TrimSpace(stringOperand)); err == nil && otherOperand.typ() == starlarkTypeInt {
return &eqExpr{ return &eqExpr{
left: otherOperand, left: otherOperand,
@@ -1113,8 +1125,6 @@ func (ctx *parseContext) parseCompareSpecialCases(directive *mkparser.Directive,
switch call.name { switch call.name {
case baseName + ".filter": case baseName + ".filter":
return ctx.parseCompareFilterFuncResult(directive, call, value, isEq) return ctx.parseCompareFilterFuncResult(directive, call, value, isEq)
case baseName + ".expand_wildcard":
return ctx.parseCompareWildcardFuncResult(directive, call, value, !isEq), true
case baseName + ".findstring": case baseName + ".findstring":
return ctx.parseCheckFindstringFuncResult(directive, call, value, !isEq), true return ctx.parseCheckFindstringFuncResult(directive, call, value, !isEq), true
case baseName + ".strip": case baseName + ".strip":
@@ -1159,22 +1169,6 @@ func (ctx *parseContext) parseCompareFilterFuncResult(cond *mkparser.Directive,
} }
} }
func (ctx *parseContext) parseCompareWildcardFuncResult(directive *mkparser.Directive,
xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
if !isEmptyString(xValue) {
return ctx.newBadExpr(directive, "wildcard result can be compared only to empty: %s", xValue)
}
callFunc := baseName + ".file_wildcard_exists"
if s, ok := xCall.args[0].(*stringLiteralExpr); ok && !strings.ContainsAny(s.literal, "*?{[") {
callFunc = baseName + ".file_exists"
}
var cc starlarkExpr = &callExpr{name: callFunc, args: xCall.args, returnType: starlarkTypeBool}
if !negate {
cc = &notExpr{cc}
}
return cc
}
func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive, func (ctx *parseContext) parseCheckFindstringFuncResult(directive *mkparser.Directive,
xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr { xCall *callExpr, xValue starlarkExpr, negate bool) starlarkExpr {
if isEmptyString(xValue) { if isEmptyString(xValue) {

View File

@@ -568,14 +568,18 @@ ifeq (,$(wildcard foo.mk))
endif endif
ifneq (,$(wildcard foo*.mk)) ifneq (,$(wildcard foo*.mk))
endif endif
ifeq (foo1.mk foo2.mk barxyz.mk,$(wildcard foo*.mk bar*.mk))
endif
`, `,
expected: `load("//build/make/core:product_config.rbc", "rblf") expected: `load("//build/make/core:product_config.rbc", "rblf")
def init(g, handle): def init(g, handle):
cfg = rblf.cfg(handle) cfg = rblf.cfg(handle)
if not rblf.file_exists("foo.mk"): if not rblf.expand_wildcard("foo.mk"):
pass pass
if rblf.file_wildcard_exists("foo*.mk"): if rblf.expand_wildcard("foo*.mk"):
pass
if rblf.expand_wildcard("foo*.mk bar*.mk") == ["foo1.mk", "foo2.mk", "barxyz.mk"]:␤
pass pass
`, `,
}, },