Merge "Switch to toybox hostname(1)."
diff --git a/androidmk/cmd/androidmk/android.go b/androidmk/cmd/androidmk/android.go
index a28cf49..71eb19b 100644
--- a/androidmk/cmd/androidmk/android.go
+++ b/androidmk/cmd/androidmk/android.go
@@ -803,6 +803,11 @@
 	"BUILD_HOST_JAVA_LIBRARY":        "java_library_host",
 	"BUILD_HOST_DALVIK_JAVA_LIBRARY": "java_library_host_dalvik",
 	"BUILD_PACKAGE":                  "android_app",
+
+	"BUILD_CTS_SUPPORT_PACKAGE":     "cts_support_package",     // will be rewritten to android_test by bpfix
+	"BUILD_CTS_PACKAGE":             "cts_package",             // will be rewritten to android_test by bpfix
+	"BUILD_CTS_TARGET_JAVA_LIBRARY": "cts_target_java_library", // will be rewritten to java_library by bpfix
+	"BUILD_CTS_HOST_JAVA_LIBRARY":   "cts_host_java_library",   // will be rewritten to java_library_host by bpfix
 }
 
 var prebuiltTypes = map[string]string{
diff --git a/androidmk/cmd/androidmk/androidmk_test.go b/androidmk/cmd/androidmk/androidmk_test.go
index c750f22..9874cb2 100644
--- a/androidmk/cmd/androidmk/androidmk_test.go
+++ b/androidmk/cmd/androidmk/androidmk_test.go
@@ -752,6 +752,61 @@
 }
 `,
 	},
+	{
+		desc: "BUILD_CTS_SUPPORT_PACKAGE",
+		in: `
+include $(CLEAR_VARS)
+LOCAL_PACKAGE_NAME := FooTest
+LOCAL_COMPATIBILITY_SUITE := cts
+include $(BUILD_CTS_SUPPORT_PACKAGE)
+`,
+		expected: `
+android_test {
+    name: "FooTest",
+    defaults: ["cts_support_defaults"],
+    test_suites: ["cts"],
+}
+`,
+	},
+	{
+		desc: "BUILD_CTS_PACKAGE",
+		in: `
+include $(CLEAR_VARS)
+LOCAL_PACKAGE_NAME := FooTest
+LOCAL_COMPATIBILITY_SUITE := cts
+include $(BUILD_CTS_PACKAGE)
+`,
+		expected: `
+android_test {
+    name: "FooTest",
+    defaults: ["cts_defaults"],
+    test_suites: ["cts"],
+}
+`,
+	},
+	{
+		desc: "BUILD_CTS_*_JAVA_LIBRARY",
+		in: `
+include $(CLEAR_VARS)
+LOCAL_MODULE := foolib
+include $(BUILD_CTS_TARGET_JAVA_LIBRARY)
+
+include $(CLEAR_VARS)
+LOCAL_MODULE := foolib-host
+include $(BUILD_CTS_HOST_JAVA_LIBRARY)
+`,
+		expected: `
+java_library {
+    name: "foolib",
+    defaults: ["cts_defaults"],
+}
+
+java_library_host {
+    name: "foolib-host",
+    defaults: ["cts_defaults"],
+}
+`,
+	},
 }
 
 func TestEndToEnd(t *testing.T) {
diff --git a/bpfix/bpfix/bpfix.go b/bpfix/bpfix/bpfix.go
index a4723fb..6a75517 100644
--- a/bpfix/bpfix/bpfix.go
+++ b/bpfix/bpfix/bpfix.go
@@ -63,6 +63,10 @@
 		fix:  rewriteIncorrectAndroidmkPrebuilts,
 	},
 	{
+		name: "rewriteCtsModuleTypes",
+		fix:  rewriteCtsModuleTypes,
+	},
+	{
 		name: "rewriteIncorrectAndroidmkAndroidLibraries",
 		fix:  rewriteIncorrectAndroidmkAndroidLibraries,
 	},
@@ -237,6 +241,52 @@
 	return nil
 }
 
+func rewriteCtsModuleTypes(f *Fixer) error {
+	for _, def := range f.tree.Defs {
+		mod, ok := def.(*parser.Module)
+		if !ok {
+			continue
+		}
+
+		if mod.Type != "cts_support_package" && mod.Type != "cts_package" &&
+			mod.Type != "cts_target_java_library" &&
+			mod.Type != "cts_host_java_library" {
+
+			continue
+		}
+
+		var defStr string
+		switch mod.Type {
+		case "cts_support_package":
+			mod.Type = "android_test"
+			defStr = "cts_support_defaults"
+		case "cts_package":
+			mod.Type = "android_test"
+			defStr = "cts_defaults"
+		case "cts_target_java_library":
+			mod.Type = "java_library"
+			defStr = "cts_defaults"
+		case "cts_host_java_library":
+			mod.Type = "java_library_host"
+			defStr = "cts_defaults"
+		}
+
+		defaults := &parser.Property{
+			Name: "defaults",
+			Value: &parser.List{
+				Values: []parser.Expression{
+					&parser.String{
+						Value: defStr,
+					},
+				},
+			},
+		}
+		mod.Properties = append(mod.Properties, defaults)
+	}
+
+	return nil
+}
+
 func rewriteIncorrectAndroidmkAndroidLibraries(f *Fixer) error {
 	for _, def := range f.tree.Defs {
 		mod, ok := def.(*parser.Module)
diff --git a/bpfix/bpfix/bpfix_test.go b/bpfix/bpfix/bpfix_test.go
index 0469faf..5224ee3 100644
--- a/bpfix/bpfix/bpfix_test.go
+++ b/bpfix/bpfix/bpfix_test.go
@@ -621,3 +621,74 @@
 		})
 	}
 }
+
+func TestRewriteCtsModuleTypes(t *testing.T) {
+	tests := []struct {
+		name string
+		in   string
+		out  string
+	}{
+		{
+			name: "cts_support_package",
+			in: `
+				cts_support_package {
+					name: "foo",
+				}
+			`,
+			out: `
+				android_test {
+					name: "foo",
+					defaults: ["cts_support_defaults"],
+				}
+			`,
+		},
+		{
+			name: "cts_package",
+			in: `
+				cts_package {
+					name: "foo",
+				}
+			`,
+			out: `
+				android_test {
+					name: "foo",
+					defaults: ["cts_defaults"],
+				}
+			`,
+		},
+		{
+			name: "cts_target_java_library",
+			in: `
+				cts_target_java_library {
+					name: "foo",
+				}
+			`,
+			out: `
+				java_library {
+					name: "foo",
+					defaults: ["cts_defaults"],
+				}
+			`,
+		},
+		{
+			name: "cts_host_java_library",
+			in: `
+				cts_host_java_library {
+					name: "foo",
+				}
+			`,
+			out: `
+				java_library_host {
+					name: "foo",
+					defaults: ["cts_defaults"],
+				}
+			`,
+		},
+	}
+
+	for _, test := range tests {
+		t.Run(test.name, func(t *testing.T) {
+			runPass(t, test.in, test.out, rewriteCtsModuleTypes)
+		})
+	}
+}
diff --git a/cc/cc.go b/cc/cc.go
index c0bd11e..1b5e8b1 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1652,17 +1652,14 @@
 		// Export the shared libs to Make.
 		switch depTag {
 		case sharedDepTag, sharedExportDepTag, lateSharedDepTag:
-			// Dependency to the stubs lib which is already included in an APEX
-			// is not added to the androidmk dependency
 			if dependentLibrary, ok := ccDep.linker.(*libraryDecorator); ok {
 				if dependentLibrary.buildStubs() && android.InAnyApex(depName) {
-					// Also add the dependency to the APEX(es) providing the library so that
+					// Add the dependency to the APEX(es) providing the library so that
 					// m <module> can trigger building the APEXes as well.
 					for _, an := range android.GetApexesForModule(depName) {
 						c.Properties.ApexesProvidingSharedLibs = append(
 							c.Properties.ApexesProvidingSharedLibs, an)
 					}
-					break
 				}
 			}
 
diff --git a/cc/linker.go b/cc/linker.go
index eb71268..cda392d 100644
--- a/cc/linker.go
+++ b/cc/linker.go
@@ -240,11 +240,11 @@
 			deps.LateStaticLibs = append(deps.LateStaticLibs, "libgcc")
 		}
 
-		var systemSharedLibs []string
-		if !ctx.useSdk() && !ctx.useVndk() {
-			systemSharedLibs = linker.Properties.System_shared_libs
-		}
+		systemSharedLibs := linker.Properties.System_shared_libs
 		if systemSharedLibs == nil {
+			// Provide a default system_shared_libs if it is unspecified. Note: If an
+			// empty list [] is specified, it implies that the module declines the
+			// default system_shared_libs.
 			systemSharedLibs = []string{"libc", "libm", "libdl"}
 		}