From c0647eb9ee8bb8bd3c0d4888095e0fb4eaa9f0d5 Mon Sep 17 00:00:00 2001 From: Steven Moreland Date: Fri, 1 Nov 2019 15:31:18 -0700 Subject: [PATCH] bpfix: remove empty HIDL libs libhidltransport/libhwbinder are empty and disallowed in Android.bp (motivation is ~4kb per empty library per process overhead). Bug: 135686713 Test: bpfix Change-Id: I964215ad35068465217af74c5ef1322b43476428 --- bpfix/bpfix/bpfix.go | 57 +++++++++++++++++++++++++++++++++++++++ bpfix/bpfix/bpfix_test.go | 54 +++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+) diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go index 9cba80a75..4633aa64d 100644 --- a/bpfix/bpfix/bpfix.go +++ b/bpfix/bpfix/bpfix.go @@ -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 +} diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go index 5e0b81754..032282f84 100644 --- a/bpfix/bpfix/bpfix_test.go +++ b/bpfix/bpfix/bpfix_test.go @@ -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) + }) + }) + } +}