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)
}
diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go
index 89acbe9..b9b250a 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -412,7 +412,7 @@
config := android.TestConfig(buildDir, nil, testCase.bp, nil)
ctx := android.NewTestContext(config)
ctx.RegisterModuleType("custom", customModuleFactory)
- ctx.RegisterBp2BuildMutator("custom_starlark", customBp2BuildMutatorFromStarlark)
+ ctx.RegisterBp2BuildMutator("custom", customBp2BuildMutatorFromStarlark)
ctx.RegisterForBazelConversion()
_, errs := ctx.ParseFileList(dir, []string{"Android.bp"})
@@ -1144,7 +1144,7 @@
}
}
-func TestAllowlistingBp2buildTargets(t *testing.T) {
+func TestAllowlistingBp2buildTargetsExplicitly(t *testing.T) {
testCases := []struct {
moduleTypeUnderTest string
moduleTypeUnderTestFactory android.ModuleFactory
@@ -1222,6 +1222,124 @@
}
}
+func TestAllowlistingBp2buildTargetsWithConfig(t *testing.T) {
+ testCases := []struct {
+ moduleTypeUnderTest string
+ moduleTypeUnderTestFactory android.ModuleFactory
+ moduleTypeUnderTestBp2BuildMutator bp2buildMutator
+ expectedCount map[string]int
+ description string
+ bp2buildConfig android.Bp2BuildConfig
+ checkDir string
+ fs map[string]string
+ }{
+ {
+ description: "test bp2build config package and subpackages config",
+ moduleTypeUnderTest: "filegroup",
+ moduleTypeUnderTestFactory: android.FileGroupFactory,
+ moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
+ expectedCount: map[string]int{
+ "migrated": 1,
+ "migrated/but_not_really": 0,
+ "migrated/but_not_really/but_really": 1,
+ "not_migrated": 0,
+ "also_not_migrated": 0,
+ },
+ bp2buildConfig: android.Bp2BuildConfig{
+ "migrated": android.Bp2BuildDefaultTrueRecursively,
+ "migrated/but_not_really": android.Bp2BuildDefaultFalse,
+ "not_migrated": android.Bp2BuildDefaultFalse,
+ },
+ fs: map[string]string{
+ "migrated/Android.bp": `filegroup { name: "a" }`,
+ "migrated/but_not_really/Android.bp": `filegroup { name: "b" }`,
+ "migrated/but_not_really/but_really/Android.bp": `filegroup { name: "c" }`,
+ "not_migrated/Android.bp": `filegroup { name: "d" }`,
+ "also_not_migrated/Android.bp": `filegroup { name: "e" }`,
+ },
+ },
+ {
+ description: "test bp2build config opt-in and opt-out",
+ moduleTypeUnderTest: "filegroup",
+ moduleTypeUnderTestFactory: android.FileGroupFactory,
+ moduleTypeUnderTestBp2BuildMutator: android.FilegroupBp2Build,
+ expectedCount: map[string]int{
+ "package-opt-in": 2,
+ "package-opt-in/subpackage": 0,
+ "package-opt-out": 1,
+ "package-opt-out/subpackage": 0,
+ },
+ bp2buildConfig: android.Bp2BuildConfig{
+ "package-opt-in": android.Bp2BuildDefaultFalse,
+ "package-opt-out": android.Bp2BuildDefaultTrueRecursively,
+ },
+ fs: map[string]string{
+ "package-opt-in/Android.bp": `
+filegroup { name: "opt-in-a" }
+filegroup { name: "opt-in-b", bazel_module: { bp2build_available: true } }
+filegroup { name: "opt-in-c", bazel_module: { bp2build_available: true } }
+`,
+
+ "package-opt-in/subpackage/Android.bp": `
+filegroup { name: "opt-in-d" } // parent package not configured to DefaultTrueRecursively
+`,
+
+ "package-opt-out/Android.bp": `
+filegroup { name: "opt-out-a" }
+filegroup { name: "opt-out-b", bazel_module: { bp2build_available: false } }
+filegroup { name: "opt-out-c", bazel_module: { bp2build_available: false } }
+`,
+
+ "package-opt-out/subpackage/Android.bp": `
+filegroup { name: "opt-out-g", bazel_module: { bp2build_available: false } }
+filegroup { name: "opt-out-h", bazel_module: { bp2build_available: false } }
+`,
+ },
+ },
+ }
+
+ dir := "."
+ for _, testCase := range testCases {
+ fs := make(map[string][]byte)
+ toParse := []string{
+ "Android.bp",
+ }
+ for f, content := range testCase.fs {
+ if strings.HasSuffix(f, "Android.bp") {
+ toParse = append(toParse, f)
+ }
+ fs[f] = []byte(content)
+ }
+ config := android.TestConfig(buildDir, nil, "", fs)
+ ctx := android.NewTestContext(config)
+ ctx.RegisterModuleType(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestFactory)
+ ctx.RegisterBp2BuildMutator(testCase.moduleTypeUnderTest, testCase.moduleTypeUnderTestBp2BuildMutator)
+ ctx.RegisterBp2BuildConfig(testCase.bp2buildConfig)
+ ctx.RegisterForBazelConversion()
+
+ _, errs := ctx.ParseFileList(dir, toParse)
+ android.FailIfErrored(t, errs)
+ _, errs = ctx.ResolveDependencies(config)
+ android.FailIfErrored(t, errs)
+
+ codegenCtx := NewCodegenContext(config, *ctx.Context, Bp2Build)
+
+ // For each directory, test that the expected number of generated targets is correct.
+ for dir, expectedCount := range testCase.expectedCount {
+ bazelTargets := generateBazelTargetsForDir(codegenCtx, dir)
+ if actualCount := len(bazelTargets); actualCount != expectedCount {
+ t.Fatalf(
+ "%s: Expected %d bazel target for %s package, got %d",
+ testCase.description,
+ expectedCount,
+ dir,
+ actualCount)
+ }
+
+ }
+ }
+}
+
func TestCombineBuildFilesBp2buildTargets(t *testing.T) {
testCases := []struct {
description string
diff --git a/bp2build/conversion.go b/bp2build/conversion.go
index 7877bb8..787222d 100644
--- a/bp2build/conversion.go
+++ b/bp2build/conversion.go
@@ -21,14 +21,15 @@
mode CodegenMode) []BazelFile {
files := make([]BazelFile, 0, len(ruleShims)+len(buildToTargets)+numAdditionalFiles)
- // Write top level files: WORKSPACE and BUILD. These files are empty.
+ // Write top level files: WORKSPACE. These files are empty.
files = append(files, newFile("", "WORKSPACE", ""))
- // Used to denote that the top level directory is a package.
- files = append(files, newFile("", GeneratedBuildFileName, ""))
-
- files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
if mode == QueryView {
+ // Used to denote that the top level directory is a package.
+ files = append(files, newFile("", GeneratedBuildFileName, ""))
+
+ files = append(files, newFile(bazelRulesSubDir, GeneratedBuildFileName, ""))
+
// These files are only used for queryview.
files = append(files, newFile(bazelRulesSubDir, "providers.bzl", providersBzl))
diff --git a/bp2build/conversion_test.go b/bp2build/conversion_test.go
index ec5f27e..a115ddc 100644
--- a/bp2build/conversion_test.go
+++ b/bp2build/conversion_test.go
@@ -89,16 +89,8 @@
expectedFilePaths := []filepath{
{
dir: "",
- basename: "BUILD",
- },
- {
- dir: "",
basename: "WORKSPACE",
},
- {
- dir: bazelRulesSubDir,
- basename: "BUILD",
- },
}
assertFilecountsAreEqual(t, files, expectedFilePaths)
diff --git a/bp2build/testing.go b/bp2build/testing.go
index a15a4a5..ede8044 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -126,7 +126,7 @@
func customBp2BuildMutator(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*customModule); ok {
- if !m.ConvertWithBp2build() {
+ if !m.ConvertWithBp2build(ctx) {
return
}
@@ -147,7 +147,7 @@
// module to target.
func customBp2BuildMutatorFromStarlark(ctx android.TopDownMutatorContext) {
if m, ok := ctx.Module().(*customModule); ok {
- if !m.ConvertWithBp2build() {
+ if !m.ConvertWithBp2build(ctx) {
return
}