Merge "Run "prebuilt_postdeps" mutator again"
diff --git a/android/fixture.go b/android/fixture.go
index 2c8997b..2085e43 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -564,7 +564,11 @@
 }
 
 func (f *fixtureFactory) Extend(preparers ...FixturePreparer) FixtureFactory {
-	all := append(f.preparers, dedupAndFlattenPreparers(f.preparers, preparers)...)
+	// Create a new slice to avoid accidentally sharing the preparers slice from this factory with
+	// the extending factories.
+	var all []*simpleFixturePreparer
+	all = append(all, f.preparers...)
+	all = append(all, dedupAndFlattenPreparers(f.preparers, preparers)...)
 	// Copy the existing factory.
 	extendedFactory := &fixtureFactory{}
 	*extendedFactory = *f
diff --git a/android/queryview.go b/android/queryview.go
index b940202..12d14df 100644
--- a/android/queryview.go
+++ b/android/queryview.go
@@ -66,16 +66,20 @@
 	bazelQueryView := ctx.Rule(pctx, "bazelQueryView",
 		blueprint.RuleParams{
 			Command: fmt.Sprintf(
-				"rm -rf ${outDir}/* && "+
-					"BUILDER=\"%s\" && "+
-					"cd $$(dirname \"$$BUILDER\") && "+
-					"ABSBUILDER=\"$$PWD/$$(basename \"$$BUILDER\")\" && "+
-					"cd / && "+
-					"env -i \"$$ABSBUILDER\" --bazel_queryview_dir ${outDir} \"%s\" && "+
-					"echo WORKSPACE: `cat %s` > ${outDir}/.queryview-depfile.d",
+				`rm -rf "${outDir}/"* && `+
+					`mkdir -p "${outDir}" && `+
+					`echo WORKSPACE: cat "%s" > "${outDir}/.queryview-depfile.d" && `+
+					`BUILDER="%s" && `+
+					`echo BUILDER=$$BUILDER && `+
+					`cd "$$(dirname "$$BUILDER")" && `+
+					`echo PWD=$$PWD && `+
+					`ABSBUILDER="$$PWD/$$(basename "$$BUILDER")" && `+
+					`echo ABSBUILDER=$$ABSBUILDER && `+
+					`cd / && `+
+					`env -i "$$ABSBUILDER" --bazel_queryview_dir "${outDir}" "%s"`,
+				moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile.
 				primaryBuilder.String(),
 				strings.Join(os.Args[1:], "\" \""),
-				moduleListFilePath.String(), // Use the contents of Android.bp.list as the depfile.
 			),
 			CommandDeps: []string{primaryBuilder.String()},
 			Description: fmt.Sprintf(
diff --git a/android/testing.go b/android/testing.go
index 042fa2b..4eb616a 100644
--- a/android/testing.go
+++ b/android/testing.go
@@ -378,9 +378,9 @@
 	ctx.singletons.registerAll(ctx.Context)
 
 	// Save the sorted components order away to make them easy to access while debugging.
-	ctx.preSingletonOrder = globalOrder.preSingletonOrder.namesInOrder
-	ctx.mutatorOrder = globalOrder.mutatorOrder.namesInOrder
-	ctx.singletonOrder = globalOrder.singletonOrder.namesInOrder
+	ctx.preSingletonOrder = componentsToNames(preSingletons)
+	ctx.mutatorOrder = componentsToNames(mutators)
+	ctx.singletonOrder = componentsToNames(singletons)
 }
 
 // RegisterForBazelConversion prepares a test context for bp2build conversion.
@@ -787,10 +787,6 @@
 		return "<nil path>"
 	}
 	p := path.String()
-	// Allow absolute paths to /dev/
-	if strings.HasPrefix(p, "/dev/") {
-		return p
-	}
 	if w, ok := path.(WritablePath); ok {
 		rel, err := filepath.Rel(w.buildDir(), p)
 		if err != nil {
diff --git a/apex/allowed_deps.txt b/apex/allowed_deps.txt
index 047a0f3..faa6569 100644
--- a/apex/allowed_deps.txt
+++ b/apex/allowed_deps.txt
@@ -163,6 +163,7 @@
 crtend_so(minSdkVersion:16)
 crtend_so(minSdkVersion:apex_inherit)
 datastallprotosnano(minSdkVersion:29)
+derive_classpath(minSdkVersion:30)
 derive_sdk(minSdkVersion:30)
 derive_sdk(minSdkVersion:current)
 derive_sdk_prefer32(minSdkVersion:30)
@@ -306,6 +307,7 @@
 libcutils(minSdkVersion:29)
 libcutils_headers(minSdkVersion:29)
 libcutils_sockets(minSdkVersion:29)
+libderive_classpath(minSdkVersion:30)
 libderive_sdk(minSdkVersion:30)
 libdiagnose_usb(minSdkVersion:(no version))
 libdl(minSdkVersion:(no version))
diff --git a/build_kzip.bash b/build_kzip.bash
index 9564723..a4659d4 100755
--- a/build_kzip.bash
+++ b/build_kzip.bash
@@ -16,6 +16,7 @@
 : ${BUILD_NUMBER:=$(uuidgen)}
 : ${KYTHE_JAVA_SOURCE_BATCH_SIZE:=500}
 : ${KYTHE_KZIP_ENCODING:=proto}
+: ${XREF_CORPUS:?should be set}
 export KYTHE_JAVA_SOURCE_BATCH_SIZE KYTHE_KZIP_ENCODING
 
 # The extraction might fail for some source files, so run with -k and then check that
@@ -29,11 +30,15 @@
 declare -r abspath_out=$(realpath "${out}")
 declare -r go_extractor=$(realpath prebuilts/build-tools/linux-x86/bin/go_extractor)
 declare -r go_root=$(realpath prebuilts/go/linux-x86)
-declare -r vnames_path=$(realpath build/soong/vnames.go.json)
 declare -r source_root=$PWD
+
+# TODO(asmundak): Until b/182183061 is fixed, default corpus has to be specified 
+# in the rules file. Generate this file on the fly with corpus value set from the
+# environment variable.
 for dir in blueprint soong; do
   (cd "build/$dir";
-   KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" --rules="${vnames_path}" \
+   KYTHE_ROOT_DIRECTORY="${source_root}" "$go_extractor" --goroot="$go_root" \
+   --rules=<(printf '[{"pattern": "(.*)","vname": {"path": "@1@", "corpus":"%s"}}]' "${XREF_CORPUS}") \
    --canonicalize_package_corpus --output "${abspath_out}/soong/build_${dir}.go.kzip" ./...
   )
 done
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index d022f49..4586f44 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -43,7 +43,7 @@
 	flag.StringVar(&delveListen, "delve_listen", "", "Delve port to listen on for debugging")
 	flag.StringVar(&delvePath, "delve_path", "", "Path to Delve. Only used if --delve_listen is set")
 	flag.StringVar(&docFile, "soong_docs", "", "build documentation file to output")
-	flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory")
+	flag.StringVar(&bazelQueryViewDir, "bazel_queryview_dir", "", "path to the bazel queryview directory relative to --top")
 }
 
 func newNameResolver(config android.Config) *android.NameResolver {
@@ -150,7 +150,8 @@
 	if bazelQueryViewDir != "" {
 		// Run the code-generation phase to convert BazelTargetModules to BUILD files.
 		codegenContext := bp2build.NewCodegenContext(configuration, *ctx, bp2build.QueryView)
-		if err := createBazelQueryView(codegenContext, bazelQueryViewDir); err != nil {
+		absoluteQueryViewDir := shared.JoinPath(topDir, bazelQueryViewDir)
+		if err := createBazelQueryView(codegenContext, absoluteQueryViewDir); err != nil {
 			fmt.Fprintf(os.Stderr, "%s", err)
 			os.Exit(1)
 		}
diff --git a/java/app.go b/java/app.go
index 2d918e9..0660aa6 100755
--- a/java/app.go
+++ b/java/app.go
@@ -1280,6 +1280,14 @@
 	outputFile := android.PathForModuleOut(ctx, "manifest_check", "AndroidManifest.xml")
 	statusFile := dexpreopt.UsesLibrariesStatusFile(ctx)
 
+	// Disable verify_uses_libraries check if dexpreopt is globally disabled. Without dexpreopt the
+	// check is not necessary, and although it is good to have, it is difficult to maintain on
+	// non-linux build platforms where dexpreopt is generally disabled (the check may fail due to
+	// various unrelated reasons, such as a failure to get manifest from an APK).
+	if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
+		return manifest
+	}
+
 	rule := android.NewRuleBuilder(pctx, ctx)
 	cmd := rule.Command().BuiltTool("manifest_check").
 		Flag("--enforce-uses-libraries").
@@ -1310,6 +1318,14 @@
 	outputFile := android.PathForModuleOut(ctx, "verify_uses_libraries", apk.Base())
 	statusFile := dexpreopt.UsesLibrariesStatusFile(ctx)
 
+	// Disable verify_uses_libraries check if dexpreopt is globally disabled. Without dexpreopt the
+	// check is not necessary, and although it is good to have, it is difficult to maintain on
+	// non-linux build platforms where dexpreopt is generally disabled (the check may fail due to
+	// various unrelated reasons, such as a failure to get manifest from an APK).
+	if dexpreopt.GetGlobalConfig(ctx).DisablePreopt {
+		return apk
+	}
+
 	rule := android.NewRuleBuilder(pctx, ctx)
 	aapt := ctx.Config().HostToolPath(ctx, "aapt")
 	rule.Command().
diff --git a/java/app_import.go b/java/app_import.go
index d69dd10..d38f63e 100644
--- a/java/app_import.go
+++ b/java/app_import.go
@@ -67,6 +67,9 @@
 	// module name in the form ":module". Should be empty if presigned or default_dev_cert is set.
 	Certificate *string
 
+	// Names of extra android_app_certificate modules to sign the apk with in the form ":module".
+	Additional_certificates []string
+
 	// Set this flag to true if the prebuilt apk is already signed. The certificate property must not
 	// be set for presigned modules.
 	Presigned *bool
@@ -156,6 +159,16 @@
 		ctx.AddDependency(ctx.Module(), certificateTag, cert)
 	}
 
+	for _, cert := range a.properties.Additional_certificates {
+		cert = android.SrcIsModule(cert)
+		if cert != "" {
+			ctx.AddDependency(ctx.Module(), certificateTag, cert)
+		} else {
+			ctx.PropertyErrorf("additional_certificates",
+				`must be names of android_app_certificate modules in the form ":module"`)
+		}
+	}
+
 	a.usesLibrary.deps(ctx, !a.isPrebuiltFrameworkRes())
 }
 
@@ -303,9 +316,6 @@
 		// If the certificate property is empty at this point, default_dev_cert must be set to true.
 		// Which makes processMainCert's behavior for the empty cert string WAI.
 		certificates = processMainCert(a.ModuleBase, String(a.properties.Certificate), certificates, ctx)
-		if len(certificates) != 1 {
-			ctx.ModuleErrorf("Unexpected number of certificates were extracted: %q", certificates)
-		}
 		a.certificate = certificates[0]
 		signed := android.PathForModuleOut(ctx, "signed", apkFilename)
 		var lineageFile android.Path
diff --git a/java/app_import_test.go b/java/app_import_test.go
index dc31d07..00406aa 100644
--- a/java/app_import_test.go
+++ b/java/app_import_test.go
@@ -109,16 +109,30 @@
 			name: "foo",
 			apk: "prebuilts/apk/app.apk",
 			certificate: "platform",
+			additional_certificates: [":additional_certificate"],
 			lineage: "lineage.bin",
 		}
+
+		android_app_certificate {
+			name: "additional_certificate",
+			certificate: "cert/additional_cert",
+		}
 	`)
 
 	variant := ctx.ModuleForTests("foo", "android_common")
 
-	// Check cert signing lineage flag.
 	signedApk := variant.Output("signed/foo.apk")
+	// Check certificates
+	certificatesFlag := signedApk.Args["certificates"]
+	expected := "build/make/target/product/security/platform.x509.pem " +
+		"build/make/target/product/security/platform.pk8 " +
+		"cert/additional_cert.x509.pem cert/additional_cert.pk8"
+	if expected != certificatesFlag {
+		t.Errorf("Incorrect certificates flags, expected: %q, got: %q", expected, certificatesFlag)
+	}
+	// Check cert signing lineage flag.
 	signingFlag := signedApk.Args["flags"]
-	expected := "--lineage lineage.bin"
+	expected = "--lineage lineage.bin"
 	if expected != signingFlag {
 		t.Errorf("Incorrect signing flags, expected: %q, got: %q", expected, signingFlag)
 	}
diff --git a/vnames.go.json b/vnames.go.json
deleted file mode 100644
index f8c6b7f..0000000
--- a/vnames.go.json
+++ /dev/null
@@ -1,8 +0,0 @@
-[
-    {
-        "pattern": "(.*)",
-        "vname": {
-            "path": "@1@"
-        }
-    }
-]