Add unit test for parsing build files in bp2build
This involves some minor changes to testing infrastructure.
Bug: 285631638
Fixes: 286545783
Test: unit test
Change-Id: If64ba29308d99e63a1cc5526feaf077f2cb14478
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index 989dd7f..6975677 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -21,7 +21,6 @@
"fmt"
"os"
"path/filepath"
- "regexp"
"strings"
"time"
@@ -737,43 +736,6 @@
return excluded
}
-// buildTargetsByPackage parses Bazel BUILD.bazel and BUILD files under
-// the workspace, and returns a map containing names of Bazel targets defined in
-// these BUILD files.
-// For example, maps "//foo/bar" to ["baz", "qux"] if `//foo/bar:{baz,qux}` exist.
-func buildTargetsByPackage(ctx *android.Context) map[string][]string {
- existingBazelFiles, err := getExistingBazelRelatedFiles(topDir)
- maybeQuit(err, "Error determining existing Bazel-related files")
-
- result := map[string][]string{}
-
- // Search for instances of `name = "$NAME"` (with arbitrary spacing).
- targetNameRegex := regexp.MustCompile(`(?m)^\s*name\s*=\s*\"([^\"]+)\"`)
-
- for _, path := range existingBazelFiles {
- if !ctx.Config().Bp2buildPackageConfig.ShouldKeepExistingBuildFileForDir(filepath.Dir(path)) {
- continue
- }
- fullPath := shared.JoinPath(topDir, path)
- sourceDir := filepath.Dir(path)
- fileInfo, err := os.Stat(fullPath)
- maybeQuit(err, "Error accessing Bazel file '%s'", fullPath)
-
- if !fileInfo.IsDir() &&
- (fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") {
- // Process this BUILD file.
- buildFileContent, err := os.ReadFile(fullPath)
- maybeQuit(err, "Error reading Bazel file '%s'", fullPath)
-
- matches := targetNameRegex.FindAllStringSubmatch(string(buildFileContent), -1)
- for _, match := range matches {
- result[sourceDir] = append(result[sourceDir], match[1])
- }
- }
- }
- return result
-}
-
// Run Soong in the bp2build mode. This creates a standalone context that registers
// an alternate pipeline of mutators and singletons specifically for generating
// Bazel BUILD files instead of Ninja files.
@@ -782,7 +744,11 @@
ctx.EventHandler.Do("bp2build", func() {
ctx.EventHandler.Do("read_build", func() {
- ctx.Config().SetBazelBuildFileTargets(buildTargetsByPackage(ctx))
+ existingBazelFiles, err := getExistingBazelRelatedFiles(topDir)
+ maybeQuit(err, "Error determining existing Bazel-related files")
+
+ err = ctx.ParseBuildFiles(topDir, existingBazelFiles)
+ maybeQuit(err, "Error parsing existing Bazel-related files")
})
// Propagate "allow misssing dependencies" bit. This is normally set in