Merge "Rename variables with "-" in androidmk"
This commit is contained in:
@@ -34,6 +34,7 @@ type bpFile struct {
|
||||
defs []bpparser.Definition
|
||||
localAssignments map[string]*bpparser.Property
|
||||
globalAssignments map[string]*bpparser.Expression
|
||||
variableRenames map[string]string
|
||||
scope mkparser.Scope
|
||||
module *bpparser.Module
|
||||
|
||||
@@ -43,6 +44,10 @@ type bpFile struct {
|
||||
inModule bool
|
||||
}
|
||||
|
||||
var invalidVariableStringToReplacement = map[string]string{
|
||||
"-": "_dash_",
|
||||
}
|
||||
|
||||
func (f *bpFile) insertComment(s string) {
|
||||
f.comments = append(f.comments, &bpparser.CommentGroup{
|
||||
Comments: []*bpparser.Comment{
|
||||
@@ -120,6 +125,7 @@ func ConvertFile(filename string, buffer *bytes.Buffer) (string, []error) {
|
||||
scope: androidScope(),
|
||||
localAssignments: make(map[string]*bpparser.Property),
|
||||
globalAssignments: make(map[string]*bpparser.Expression),
|
||||
variableRenames: make(map[string]string),
|
||||
}
|
||||
|
||||
var conds []*conditional
|
||||
@@ -224,6 +230,25 @@ func ConvertFile(filename string, buffer *bytes.Buffer) (string, []error) {
|
||||
return string(out), errs
|
||||
}
|
||||
|
||||
func renameVariableWithInvalidCharacters(name string) string {
|
||||
renamed := ""
|
||||
for invalid, replacement := range invalidVariableStringToReplacement {
|
||||
if strings.Contains(name, invalid) {
|
||||
renamed = strings.ReplaceAll(name, invalid, replacement)
|
||||
}
|
||||
}
|
||||
|
||||
return renamed
|
||||
}
|
||||
|
||||
func invalidVariableStrings() string {
|
||||
invalidStrings := make([]string, 0, len(invalidVariableStringToReplacement))
|
||||
for s := range invalidVariableStringToReplacement {
|
||||
invalidStrings = append(invalidStrings, "\""+s+"\"")
|
||||
}
|
||||
return strings.Join(invalidStrings, ", ")
|
||||
}
|
||||
|
||||
func handleAssignment(file *bpFile, assignment *mkparser.Assignment, c *conditional) {
|
||||
if !assignment.Name.Const() {
|
||||
file.errorf(assignment, "unsupported non-const variable name")
|
||||
@@ -238,6 +263,12 @@ func handleAssignment(file *bpFile, assignment *mkparser.Assignment, c *conditio
|
||||
name := assignment.Name.Value(nil)
|
||||
prefix := ""
|
||||
|
||||
if newName := renameVariableWithInvalidCharacters(name); newName != "" {
|
||||
file.warnf("Variable names cannot contain: %s. Renamed \"%s\" to \"%s\"", invalidVariableStrings(), name, newName)
|
||||
file.variableRenames[name] = newName
|
||||
name = newName
|
||||
}
|
||||
|
||||
if strings.HasPrefix(name, "LOCAL_") {
|
||||
for _, x := range propertyPrefixes {
|
||||
if strings.HasSuffix(name, "_"+x.mk) {
|
||||
@@ -341,11 +372,11 @@ func makeVariableToBlueprint(file *bpFile, val *mkparser.MakeString,
|
||||
var err error
|
||||
switch typ {
|
||||
case bpparser.ListType:
|
||||
exp, err = makeToListExpression(val, file.scope)
|
||||
exp, err = makeToListExpression(val, file)
|
||||
case bpparser.StringType:
|
||||
exp, err = makeToStringExpression(val, file.scope)
|
||||
exp, err = makeToStringExpression(val, file)
|
||||
case bpparser.BoolType:
|
||||
exp, err = makeToBoolExpression(val)
|
||||
exp, err = makeToBoolExpression(val, file)
|
||||
default:
|
||||
panic("unknown type")
|
||||
}
|
||||
@@ -358,7 +389,6 @@ func makeVariableToBlueprint(file *bpFile, val *mkparser.MakeString,
|
||||
}
|
||||
|
||||
func setVariable(file *bpFile, plusequals bool, prefix, name string, value bpparser.Expression, local bool) error {
|
||||
|
||||
if prefix != "" {
|
||||
name = prefix + "." + name
|
||||
}
|
||||
|
@@ -1364,6 +1364,28 @@ android_test_import {
|
||||
preprocessed: true,
|
||||
test_suites: ["cts"],
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
desc: "dashed_variable gets renamed",
|
||||
in: `
|
||||
include $(CLEAR_VARS)
|
||||
|
||||
dashed-variable:= a.cpp
|
||||
|
||||
LOCAL_MODULE:= test
|
||||
LOCAL_SRC_FILES:= $(dashed-variable)
|
||||
include $(BUILD_EXECUTABLE)
|
||||
`,
|
||||
expected: `
|
||||
|
||||
// ANDROIDMK TRANSLATION WARNING: Variable names cannot contain: "-". Renamed "dashed-variable" to "dashed_dash_variable"
|
||||
dashed_dash_variable = ["a.cpp"]
|
||||
cc_binary {
|
||||
|
||||
name: "test",
|
||||
srcs: dashed_dash_variable,
|
||||
}
|
||||
`,
|
||||
},
|
||||
{
|
||||
|
@@ -60,8 +60,7 @@ func addValues(val1, val2 bpparser.Expression) (bpparser.Expression, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func makeToStringExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bpparser.Expression, error) {
|
||||
|
||||
func makeToStringExpression(ms *mkparser.MakeString, file *bpFile) (bpparser.Expression, error) {
|
||||
var val bpparser.Expression
|
||||
var err error
|
||||
|
||||
@@ -70,18 +69,18 @@ func makeToStringExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bppa
|
||||
}
|
||||
|
||||
for i, s := range ms.Strings[1:] {
|
||||
if ret, ok := ms.Variables[i].EvalFunction(scope); ok {
|
||||
if ret, ok := ms.Variables[i].EvalFunction(file.scope); ok {
|
||||
if len(ret) > 1 {
|
||||
return nil, fmt.Errorf("Unexpected list value %s", ms.Dump())
|
||||
}
|
||||
val, err = addValues(val, stringToStringValue(ret[0]))
|
||||
} else {
|
||||
name := ms.Variables[i].Name
|
||||
if !name.Const() {
|
||||
return nil, fmt.Errorf("Unsupported non-const variable name %s", name.Dump())
|
||||
name, err := extractVariableName(ms.Variables[i].Name, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
tmp := &bpparser.Variable{
|
||||
Name: name.Value(nil),
|
||||
Name: name,
|
||||
Value: &bpparser.String{},
|
||||
}
|
||||
|
||||
@@ -125,8 +124,7 @@ func stringToListValue(s string) bpparser.Expression {
|
||||
|
||||
}
|
||||
|
||||
func makeToListExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bpparser.Expression, error) {
|
||||
|
||||
func makeToListExpression(ms *mkparser.MakeString, file *bpFile) (bpparser.Expression, error) {
|
||||
fields := ms.Split(" \t")
|
||||
|
||||
var listOfListValues []bpparser.Expression
|
||||
@@ -135,14 +133,14 @@ func makeToListExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bppars
|
||||
|
||||
for _, f := range fields {
|
||||
if len(f.Variables) == 1 && f.Strings[0] == "" && f.Strings[1] == "" {
|
||||
if ret, ok := f.Variables[0].EvalFunction(scope); ok {
|
||||
if ret, ok := f.Variables[0].EvalFunction(file.scope); ok {
|
||||
listValue.Values = append(listValue.Values, stringListToStringValueList(ret)...)
|
||||
} else {
|
||||
// Variable by itself, variable is probably a list
|
||||
if !f.Variables[0].Name.Const() {
|
||||
return nil, fmt.Errorf("unsupported non-const variable name")
|
||||
name, err := extractVariableName(f.Variables[0].Name, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if f.Variables[0].Name.Value(nil) == "TOP" {
|
||||
if name == "TOP" {
|
||||
listValue.Values = append(listValue.Values, &bpparser.String{
|
||||
Value: ".",
|
||||
})
|
||||
@@ -151,14 +149,14 @@ func makeToListExpression(ms *mkparser.MakeString, scope mkparser.Scope) (bppars
|
||||
listOfListValues = append(listOfListValues, listValue)
|
||||
}
|
||||
listOfListValues = append(listOfListValues, &bpparser.Variable{
|
||||
Name: f.Variables[0].Name.Value(nil),
|
||||
Name: name,
|
||||
Value: &bpparser.List{},
|
||||
})
|
||||
listValue = &bpparser.List{}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
s, err := makeToStringExpression(f, scope)
|
||||
s, err := makeToStringExpression(f, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -208,15 +206,15 @@ func stringToBoolValue(s string) (bpparser.Expression, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func makeToBoolExpression(ms *mkparser.MakeString) (bpparser.Expression, error) {
|
||||
func makeToBoolExpression(ms *mkparser.MakeString, file *bpFile) (bpparser.Expression, error) {
|
||||
if !ms.Const() {
|
||||
if len(ms.Variables) == 1 && ms.Strings[0] == "" && ms.Strings[1] == "" {
|
||||
name := ms.Variables[0].Name
|
||||
if !name.Const() {
|
||||
return nil, fmt.Errorf("unsupported non-const variable name")
|
||||
name, err := extractVariableName(ms.Variables[0].Name, file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &bpparser.Variable{
|
||||
Name: name.Value(nil),
|
||||
Name: name,
|
||||
Value: &bpparser.Bool{},
|
||||
}, nil
|
||||
} else {
|
||||
@@ -226,3 +224,17 @@ func makeToBoolExpression(ms *mkparser.MakeString) (bpparser.Expression, error)
|
||||
|
||||
return stringToBoolValue(ms.Value(nil))
|
||||
}
|
||||
|
||||
func extractVariableName(name *mkparser.MakeString, file *bpFile) (string, error) {
|
||||
if !name.Const() {
|
||||
return "", fmt.Errorf("Unsupported non-const variable name %s", name.Dump())
|
||||
}
|
||||
|
||||
variableName := name.Value(nil)
|
||||
|
||||
if newName, ok := file.variableRenames[variableName]; ok {
|
||||
variableName = newName
|
||||
}
|
||||
|
||||
return variableName, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user