Refactor standard and rewrite properties to reduce duplication

Refactor calls to standardProperties and rewriteProperties into
translateSingleProperty that can be used for normal, suffix, and
target properties.

Change-Id: I04e060588d4feeba1da7802d68622fe9b20e2c8b
This commit is contained in:
Colin Cross
2015-07-07 12:22:51 -07:00
parent aee540a439
commit 3cc00f1fd8
2 changed files with 60 additions and 81 deletions

View File

@@ -25,6 +25,21 @@ type androidMkWriter struct {
path string path string
} }
type propAssignment struct {
name, assigner, value string
}
func (a propAssignment) assignmentWithSuffix(suffix string) string {
if suffix != "" {
a.name = a.name + "_" + suffix
}
return a.name + " " + a.assigner + " " + a.value
}
func (a propAssignment) assignment() string {
return a.assignmentWithSuffix("")
}
func (w *androidMkWriter) WriteString(s string) (int, error) { func (w *androidMkWriter) WriteString(s string) (int, error) {
return io.WriteString(w.Writer, s) return io.WriteString(w.Writer, s)
} }
@@ -131,19 +146,10 @@ func translateTargetConditionals(props []*bpparser.Property,
var scopedProps []string var scopedProps []string
for _, targetScopedProp := range target.Value.MapValue { for _, targetScopedProp := range target.Value.MapValue {
if mkProp, ok := standardProperties[targetScopedProp.Name.Name]; ok { if assignment, ok, err := translateSingleProperty(targetScopedProp); err != nil {
val, err := valueToString(targetScopedProp.Value) return nil, err
if err != nil { } else if ok {
return nil, err scopedProps = append(scopedProps, assignment.assignment())
}
scopedProps = append(scopedProps, fmt.Sprintf("%s += %s",
mkProp.string, val))
} else if rwProp, ok := rewriteProperties[targetScopedProp.Name.Name]; ok {
props, err := rwProp.f(rwProp.string, targetScopedProp, nil)
if err != nil {
return nil, err
}
scopedProps = append(scopedProps, props...)
} else if "disabled" == targetScopedProp.Name.Name { } else if "disabled" == targetScopedProp.Name.Name {
if targetScopedProp.Value.BoolValue { if targetScopedProp.Value.BoolValue {
disabledBuilds[target.Name.Name] = true disabledBuilds[target.Name.Name] = true
@@ -174,18 +180,10 @@ func translateSuffixProperties(suffixProps []*bpparser.Property,
for _, suffixProp := range suffixProps { for _, suffixProp := range suffixProps {
if suffix, ok := suffixMap[suffixProp.Name.Name]; ok { if suffix, ok := suffixMap[suffixProp.Name.Name]; ok {
for _, stdProp := range suffixProp.Value.MapValue { for _, stdProp := range suffixProp.Value.MapValue {
if mkProp, ok := standardProperties[stdProp.Name.Name]; ok { if assignment, ok, err := translateSingleProperty(stdProp); err != nil {
val, err := valueToString(stdProp.Value) return nil, err
if err != nil { } else if ok {
return nil, err computedProps = append(computedProps, assignment.assignmentWithSuffix(suffix))
}
computedProps = append(computedProps, fmt.Sprintf("%s_%s := %s", mkProp.string, suffix, val))
} else if rwProp, ok := rewriteProperties[stdProp.Name.Name]; ok {
props, err := rwProp.f(rwProp.string, stdProp, &suffix)
if err != nil {
return nil, err
}
computedProps = append(computedProps, props...)
} else { } else {
return nil, fmt.Errorf("Unsupported property %q", stdProp.Name.Name) return nil, fmt.Errorf("Unsupported property %q", stdProp.Name.Name)
} }
@@ -197,56 +195,45 @@ func translateSuffixProperties(suffixProps []*bpparser.Property,
return return
} }
func appendAssign(name string, prop *bpparser.Property, suffix *string) ([]string, error) { func translateSingleProperty(prop *bpparser.Property) (propAssignment, bool, error) {
if suffix != nil { var assignment propAssignment
name += "_" + *suffix if mkProp, ok := standardProperties[prop.Name.Name]; ok {
name := mkProp.string
val, err := valueToString(prop.Value)
if err != nil {
return propAssignment{}, false, err
}
assignment = propAssignment{name, ":=", val}
} else if rwProp, ok := rewriteProperties[prop.Name.Name]; ok {
val, err := valueToString(prop.Value)
if err != nil {
return propAssignment{}, false, err
}
assignment, err = rwProp.f(rwProp.string, prop, val)
if err != nil {
return propAssignment{}, false, err
}
} else {
// Unhandled, return false with no error to tell the caller to handle it
return propAssignment{}, false, nil
} }
val, err := valueToString(prop.Value) return assignment, true, nil
if err != nil {
return nil, err
}
return []string{
fmt.Sprintf("%s += %s", name, val),
}, nil
} }
func prependLocalPath(name string, prop *bpparser.Property, suffix *string) ([]string, error) { func appendAssign(name string, prop *bpparser.Property, val string) (propAssignment, error) {
if suffix != nil { return propAssignment{name, "+=", val}, nil
name += "_" + *suffix
}
val, err := valueToString(prop.Value)
if err != nil {
return nil, err
}
return []string{
fmt.Sprintf("%s += $(addprefix $(LOCAL_PATH)/,%s)", name, val),
}, nil
} }
func prependLocalModule(name string, prop *bpparser.Property, suffix *string) ([]string, error) { func prependLocalPath(name string, prop *bpparser.Property, val string) (propAssignment, error) {
if suffix != nil { return propAssignment{name, "+=", fmt.Sprintf("$(addprefix $(LOCAL_PATH)/,%s)", val)}, nil
name += "_" + *suffix
}
val, err := valueToString(prop.Value)
if err != nil {
return nil, err
}
return []string{
fmt.Sprintf("%s := $(LOCAL_MODULE)%s\n", name, val),
}, nil
} }
func versionScript(name string, prop *bpparser.Property, suffix *string) ([]string, error) { func prependLocalModule(name string, prop *bpparser.Property, val string) (propAssignment, error) {
if suffix != nil { return propAssignment{name, ":=", "$(LOCAL_MODULE)" + val}, nil
name += "_" + *suffix }
}
val, err := valueToString(prop.Value) func versionScript(name string, prop *bpparser.Property, val string) (propAssignment, error) {
if err != nil { return propAssignment{name, "+=", "-Wl,--version-script,$(LOCAL_PATH)/" + val}, nil
return nil, err
}
return []string{
fmt.Sprintf("%s += -Wl,--version-script,$(LOCAL_PATH)/%s\n", name, val),
}, nil
} }
func (w *androidMkWriter) writeModule(moduleRule string, props []string, func (w *androidMkWriter) writeModule(moduleRule string, props []string,
@@ -271,18 +258,10 @@ func (w *androidMkWriter) parsePropsAndWriteModule(module *Module) error {
standardProps := make([]string, 0, len(module.bpmod.Properties)) standardProps := make([]string, 0, len(module.bpmod.Properties))
disabledBuilds := make(map[string]bool) disabledBuilds := make(map[string]bool)
for _, prop := range module.bpmod.Properties { for _, prop := range module.bpmod.Properties {
if mkProp, ok := standardProperties[prop.Name.Name]; ok { if assignment, ok, err := translateSingleProperty(prop); err != nil {
val, err := valueToString(prop.Value) return err
if err != nil { } else if ok {
return err standardProps = append(standardProps, assignment.assignment())
}
standardProps = append(standardProps, fmt.Sprintf("%s := %s", mkProp.string, val))
} else if rwProp, ok := rewriteProperties[prop.Name.Name]; ok {
props, err := rwProp.f(rwProp.string, prop, nil)
if err != nil {
return err
}
standardProps = append(standardProps, props...)
} else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok { } else if suffixMap, ok := suffixProperties[prop.Name.Name]; ok {
props, err := translateSuffixProperties(prop.Value.MapValue, suffixMap) props, err := translateSuffixProperties(prop.Value.MapValue, suffixMap)
if err != nil { if err != nil {

View File

@@ -64,7 +64,7 @@ var standardProperties = map[string]struct {
var rewriteProperties = map[string]struct { var rewriteProperties = map[string]struct {
string string
f func(name string, prop *bpparser.Property, suffix *string) ([]string, error) f func(name string, prop *bpparser.Property, val string) (propAssignment, error)
}{ }{
"include_dirs": {"LOCAL_C_INCLUDES", appendAssign}, "include_dirs": {"LOCAL_C_INCLUDES", appendAssign},
"local_include_dirs": {"LOCAL_C_INCLUDES", prependLocalPath}, "local_include_dirs": {"LOCAL_C_INCLUDES", prependLocalPath},