bp2build: add allowlist for package-level conversions.

This CL adds the support for specifying lists of directories in
build/soong/android/bazel.go, which are then written into
out/soong/bp2build/MANIFEST. Using this configuration,
modules/directories can either default to bp2build_available: true or
false, while still retaining the ability to opt-in or out at the module level.

It also ensures that ConvertWithBp2Build returns true iff the module
type has a registered bp2build converter.

Test: go tests
Test: demo.sh full
Test: TreeHugger presubmits for bp2build and mixed builds.

Change-Id: I0e0f6f4b1b2ec045f2f1c338f7084defc5d23a55
diff --git a/bp2build/bp2build.go b/bp2build/bp2build.go
index 97a5137..007d6d8 100644
--- a/bp2build/bp2build.go
+++ b/bp2build/bp2build.go
@@ -18,44 +18,48 @@
 	"android/soong/android"
 	"fmt"
 	"os"
+	"strings"
 )
 
-// The Bazel bp2build code generator is responsible for writing .bzl files that are equivalent to
-// Android.bp files that are capable of being built with Bazel.
+// Codegen is the backend of bp2build. The code generator is responsible for
+// writing .bzl files that are equivalent to Android.bp files that are capable
+// of being built with Bazel.
 func Codegen(ctx *CodegenContext) CodegenMetrics {
 	outputDir := android.PathForOutput(ctx, "bp2build")
 	android.RemoveAllOutputDir(outputDir)
 
-	ruleShims := CreateRuleShims(android.ModuleTypeFactories())
-
 	buildToTargets, metrics := GenerateBazelTargets(ctx)
 
-	filesToWrite := CreateBazelFiles(ruleShims, buildToTargets, ctx.mode)
+	filesToWrite := CreateBazelFiles(nil, buildToTargets, ctx.mode)
+
+	generatedBuildFiles := []string{}
 	for _, f := range filesToWrite {
-		if err := writeFile(outputDir, ctx, f); err != nil {
+		p := getOrCreateOutputDir(outputDir, ctx, f.Dir).Join(ctx, f.Basename)
+		if err := writeFile(ctx, p, f.Contents); err != nil {
 			fmt.Errorf("Failed to write %q (dir %q) due to %q", f.Basename, f.Dir, err)
 		}
+		// if these generated files are modified, regenerate on next run.
+		generatedBuildFiles = append(generatedBuildFiles, p.String())
 	}
 
+	// The MANIFEST file contains the full list of files generated by bp2build, excluding itself.
+	// Its purpose is for downstream tools to understand the set of files converted by bp2build.
+	manifestFile := outputDir.Join(ctx, "MANIFEST")
+	writeFile(ctx, manifestFile, strings.Join(generatedBuildFiles, "\n"))
+	generatedBuildFiles = append(generatedBuildFiles, manifestFile.String())
+
 	return metrics
 }
 
-func writeFile(outputDir android.OutputPath, ctx android.PathContext, f BazelFile) error {
-	return writeReadOnlyFile(ctx, getOutputPath(outputDir, ctx, f.Dir), f.Basename, f.Contents)
+// Get the output directory and create it if it doesn't exist.
+func getOrCreateOutputDir(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
+	dirPath := outputDir.Join(ctx, dir)
+	android.CreateOutputDirIfNonexistent(dirPath, os.ModePerm)
+	return dirPath
 }
 
-func getOutputPath(outputDir android.OutputPath, ctx android.PathContext, dir string) android.OutputPath {
-	return outputDir.Join(ctx, dir)
-}
-
-// The auto-conversion directory should be read-only, sufficient for bazel query. The files
-// are not intended to be edited by end users.
-func writeReadOnlyFile(ctx android.PathContext, dir android.OutputPath, baseName, content string) error {
-	android.CreateOutputDirIfNonexistent(dir, os.ModePerm)
-	pathToFile := dir.Join(ctx, baseName)
-
-	// 0444 is read-only
-	err := android.WriteFileToOutputDir(pathToFile, []byte(content), 0444)
-
-	return err
+func writeFile(ctx android.PathContext, pathToFile android.OutputPath, content string) error {
+	// These files are made editable to allow users to modify and iterate on them
+	// in the source tree.
+	return android.WriteFileToOutputDir(pathToFile, []byte(content), 0644)
 }