Merge "bpfix: remove empty HIDL libs"

This commit is contained in:
Treehugger Robot 2019-11-07 00:42:28 +00:00 committed by Gerrit Code Review
commit 8a70579037
2 changed files with 111 additions and 0 deletions

View File

@ -116,6 +116,10 @@ var fixSteps = []FixStep{
Name: "rewriteAndroidAppImport",
Fix: rewriteAndroidAppImport,
},
{
Name: "removeEmptyLibDependencies",
Fix: removeEmptyLibDependencies,
},
}
func NewFixRequest() FixRequest {
@ -650,6 +654,50 @@ func rewriteAndroidAppImport(f *Fixer) error {
return nil
}
// Removes library dependencies which are empty (and restricted from usage in Soong)
func removeEmptyLibDependencies(f *Fixer) error {
emptyLibraries := []string{
"libhidltransport",
"libhwbinder",
}
relevantFields := []string{
"export_shared_lib_headers",
"export_static_lib_headers",
"static_libs",
"whole_static_libs",
"shared_libs",
}
for _, def := range f.tree.Defs {
mod, ok := def.(*parser.Module)
if !ok {
continue
}
for _, field := range relevantFields {
listValue, ok := getLiteralListProperty(mod, field)
if !ok {
continue
}
newValues := []parser.Expression{}
for _, v := range listValue.Values {
stringValue, ok := v.(*parser.String)
if !ok {
return fmt.Errorf("Expecting string for %s.%s fields", mod.Type, field)
}
if inList(stringValue.Value, emptyLibraries) {
continue
}
newValues = append(newValues, stringValue)
}
if len(newValues) == 0 && len(listValue.Values) != 0 {
removeProperty(mod, field)
} else {
listValue.Values = newValues
}
}
}
return nil
}
// Converts the default source list property, 'srcs', to a single source property with a given name.
// "LOCAL_MODULE" reference is also resolved during the conversion process.
func convertToSingleSource(mod *parser.Module, srcPropertyName string) {
@ -1084,3 +1132,12 @@ func removeProperty(mod *parser.Module, propertyName string) {
}
mod.Properties = newList
}
func inList(s string, list []string) bool {
for _, v := range list {
if s == v {
return true
}
}
return false
}

View File

@ -833,3 +833,57 @@ func TestRewriteAndroidAppImport(t *testing.T) {
})
}
}
func TestRemoveEmptyLibDependencies(t *testing.T) {
tests := []struct {
name string
in string
out string
}{
{
name: "remove sole shared lib",
in: `
cc_library {
name: "foo",
shared_libs: ["libhwbinder"],
}
`,
out: `
cc_library {
name: "foo",
}
`,
},
{
name: "remove a shared lib",
in: `
cc_library {
name: "foo",
shared_libs: [
"libhwbinder",
"libfoo",
"libhidltransport",
],
}
`,
out: `
cc_library {
name: "foo",
shared_libs: [
"libfoo",
],
}
`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
runPass(t, test.in, test.out, func(fixer *Fixer) error {
return removeEmptyLibDependencies(fixer)
})
})
}
}