Merge changes I52f88bfd,I4263b7d5

* changes:
  Fix usage of bytes.NewBuffer in bpfix
  Add a dependency fixer for proto deps
diff --git a/Android.bp b/Android.bp
index 4a06a11..7f4fbca 100644
--- a/Android.bp
+++ b/Android.bp
@@ -235,6 +235,7 @@
         "java/java_resources.go",
         "java/proto.go",
         "java/sdk_library.go",
+        "java/support_libraries.go",
         "java/system_modules.go",
     ],
     testSrcs: [
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index edf3d42..37e2427 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -576,6 +576,19 @@
 			}
 		`,
 	},
+	{
+		desc: "cc_library shared_libs",
+		in: `
+			include $(CLEAR_VARS)
+			LOCAL_SHARED_LIBRARIES := libfoo
+			include $(BUILD_SHARED_LIBRARY)
+		`,
+		expected: `
+			cc_library_shared {
+				shared_libs: ["libfoo"],
+			}
+		`,
+	},
 }
 
 func TestEndToEnd(t *testing.T) {
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index e4d4e34..a610a17 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -22,6 +22,7 @@
 	"fmt"
 	"io"
 	"path/filepath"
+	"strings"
 
 	"github.com/google/blueprint/parser"
 )
@@ -220,6 +221,10 @@
 			continue
 		}
 
+		if !strings.HasPrefix(mod.Type, "java_") && !strings.HasPrefix(mod.Type, "android_") {
+			continue
+		}
+
 		hasAndroidLibraries := hasNonEmptyLiteralListProperty(mod, "android_libs")
 		hasStaticAndroidLibraries := hasNonEmptyLiteralListProperty(mod, "android_static_libs")
 		hasResourceDirs := hasNonEmptyLiteralListProperty(mod, "resource_dirs")
@@ -384,7 +389,7 @@
 			continue
 		}
 		legacyList, ok := getLiteralListProperty(mod, legacyName)
-		if !ok {
+		if !ok || len(legacyList.Values) == 0 {
 			continue
 		}
 		canonicalList, ok := getLiteralListProperty(mod, canonicalName)
@@ -392,6 +397,10 @@
 			continue
 		}
 		filterExpressionList(legacyList, canonicalList)
+
+		if len(legacyList.Values) == 0 {
+			removeProperty(mod, legacyName)
+		}
 	}
 	return nil
 }
diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go
index 51708eb..8fed0a2 100644
--- a/bpfix/bpfix/bpfix_test.go
+++ b/bpfix/bpfix/bpfix_test.go
@@ -73,20 +73,24 @@
 
 	// lookup legacy property
 	mod := fixer.tree.Defs[0].(*parser.Module)
-	_, found := mod.GetProperty("local_include_dirs")
-	if !found {
-		t.Fatalf("failed to include key local_include_dirs in parse tree")
+
+	expectedResultString := fmt.Sprintf("%q", expectedResult)
+	if expectedResult == nil {
+		expectedResultString = "unset"
 	}
 
 	// check that the value for the legacy property was updated to the correct value
 	errorHeader := fmt.Sprintf("\nFailed to correctly simplify key 'local_include_dirs' in the presence of 'export_include_dirs.'\n"+
 		"original local_include_dirs: %q\n"+
 		"original export_include_dirs: %q\n"+
-		"expected result: %q\n"+
+		"expected result: %s\n"+
 		"actual result: ",
-		local_include_dirs, export_include_dirs, expectedResult)
-	result, ok := mod.GetProperty("local_include_dirs")
-	if !ok {
+		local_include_dirs, export_include_dirs, expectedResultString)
+	result, found := mod.GetProperty("local_include_dirs")
+	if !found {
+		if expectedResult == nil {
+			return
+		}
 		t.Fatal(errorHeader + "property not found")
 	}
 
@@ -95,6 +99,10 @@
 		t.Fatalf("%sproperty is not a list: %v", errorHeader, listResult)
 	}
 
+	if expectedResult == nil {
+		t.Fatalf("%sproperty exists: %v", errorHeader, listResult)
+	}
+
 	actualExpressions := listResult.Values
 	actualValues := make([]string, 0)
 	for _, expr := range actualExpressions {
@@ -109,7 +117,7 @@
 
 func TestSimplifyKnownVariablesDuplicatingEachOther(t *testing.T) {
 	// TODO use []Expression{} once buildTree above can support it (which is after b/38325146 is done)
-	implFilterListTest(t, []string{"include"}, []string{"include"}, []string{})
+	implFilterListTest(t, []string{"include"}, []string{"include"}, nil)
 	implFilterListTest(t, []string{"include1"}, []string{"include2"}, []string{"include1"})
 	implFilterListTest(t, []string{"include1", "include2", "include3", "include4"}, []string{"include2"},
 		[]string{"include1", "include3", "include4"})
diff --git a/java/sdk_library.go b/java/sdk_library.go
index 703401c..ee6998c 100644
--- a/java/sdk_library.go
+++ b/java/sdk_library.go
@@ -68,10 +68,9 @@
 //
 // TODO: these are big features that are currently missing
 // 1) check for API consistency
-// 2) install stubs libs as the dist artifacts
-// 3) ensuring that apps have appropriate <uses-library> tag
-// 4) disallowing linking to the runtime shared lib
-// 5) HTML generation
+// 2) ensuring that apps have appropriate <uses-library> tag
+// 3) disallowing linking to the runtime shared lib
+// 4) HTML generation
 
 func init() {
 	android.RegisterModuleType("java_sdk_library", sdkLibraryFactory)
@@ -155,15 +154,31 @@
 }
 
 func (module *sdkLibrary) AndroidMk() android.AndroidMkData {
-	// Create a phony module that installs the impl library, for the case when this lib is
-	// in PRODUCT_PACKAGES.
 	return android.AndroidMkData{
 		Custom: func(w io.Writer, name, prefix, moduleDir string, data android.AndroidMkData) {
+			// Create a phony module that installs the impl library, for the case when this lib is
+			// in PRODUCT_PACKAGES.
 			fmt.Fprintln(w, "\ninclude $(CLEAR_VARS)")
 			fmt.Fprintln(w, "LOCAL_PATH :=", moduleDir)
 			fmt.Fprintln(w, "LOCAL_MODULE :=", name)
 			fmt.Fprintln(w, "LOCAL_REQUIRED_MODULES := "+module.implName())
 			fmt.Fprintln(w, "include $(BUILD_PHONY_PACKAGE)")
+			// Create dist rules to install the stubs libs to the dist dir
+			if len(module.publicApiStubsPath) == 1 {
+				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
+					module.publicApiStubsPath.Strings()[0]+
+					":"+path.Join("apistubs", "public", module.BaseModuleName()+".jar")+")")
+			}
+			if len(module.systemApiStubsPath) == 1 {
+				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
+					module.systemApiStubsPath.Strings()[0]+
+					":"+path.Join("apistubs", "system", module.BaseModuleName()+".jar")+")")
+			}
+			if len(module.testApiStubsPath) == 1 {
+				fmt.Fprintln(w, "$(call dist-for-goals,sdk win_sdk,"+
+					module.testApiStubsPath.Strings()[0]+
+					":"+path.Join("apistubs", "test", module.BaseModuleName()+".jar")+")")
+			}
 		},
 	}
 }
diff --git a/java/support_libraries.go b/java/support_libraries.go
new file mode 100644
index 0000000..320afae
--- /dev/null
+++ b/java/support_libraries.go
@@ -0,0 +1,66 @@
+// Copyright 2018 Google Inc. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package java
+
+import (
+	"sort"
+	"strings"
+
+	"android/soong/android"
+)
+
+func init() {
+	android.RegisterMakeVarsProvider(pctx, supportLibrariesMakeVarsProvider)
+}
+
+func supportLibrariesMakeVarsProvider(ctx android.MakeVarsContext) {
+	var supportAars, supportJars []string
+
+	sctx := ctx.SingletonContext()
+	sctx.VisitAllModules(func(module android.Module) {
+		dir := sctx.ModuleDir(module)
+		switch {
+		case strings.HasPrefix(dir, "prebuilts/sdk/current/extras"),
+			dir == "prebuilts/sdk/current/androidx",
+			dir == "prebuilts/sdk/current/car",
+			dir == "prebuilts/sdk/current/optional",
+			dir == "prebuilts/sdk/current/support":
+			// Support library
+		default:
+			// Not a support library
+			return
+		}
+
+		name := sctx.ModuleName(module)
+		if strings.HasSuffix(name, "-nodeps") {
+			return
+		}
+
+		switch module.(type) {
+		case *AndroidLibrary, *AARImport:
+			supportAars = append(supportAars, name)
+		case *Library, *Import:
+			supportJars = append(supportJars, name)
+		default:
+			sctx.ModuleErrorf(module, "unknown module type %t", module)
+		}
+	})
+
+	sort.Strings(supportAars)
+	sort.Strings(supportJars)
+
+	ctx.Strict("SUPPORT_LIBRARIES_AARS", strings.Join(supportAars, " "))
+	ctx.Strict("SUPPORT_LIBRARIES_JARS", strings.Join(supportJars, " "))
+}