Merge "Add -sha256 to robolectric zip"
diff --git a/android/bazel_handler.go b/android/bazel_handler.go
index 4b1a8f4..d5ccfca 100644
--- a/android/bazel_handler.go
+++ b/android/bazel_handler.go
@@ -176,8 +176,6 @@
 	// (for example, that it is MixedBuildBuildable).
 	IsModuleNameAllowed(moduleName string, withinApex bool) bool
 
-	IsModuleDclaAllowed(moduleName string) bool
-
 	// Returns the bazel output base (the root directory for all bazel intermediate outputs).
 	OutputBase() string
 
@@ -316,10 +314,6 @@
 	return true
 }
 
-func (m MockBazelContext) IsModuleDclaAllowed(_ string) bool {
-	return true
-}
-
 func (m MockBazelContext) OutputBase() string { return m.OutputBaseDir }
 
 func (m MockBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
@@ -461,10 +455,6 @@
 	return false
 }
 
-func (n noopBazelContext) IsModuleDclaAllowed(_ string) bool {
-	return false
-}
-
 func (m noopBazelContext) BuildStatementsToRegister() []*bazel.BuildStatement {
 	return []*bazel.BuildStatement{}
 }
@@ -598,17 +588,13 @@
 	if context.bazelEnabledModules[moduleName] {
 		return true
 	}
-	if withinApex && context.IsModuleDclaAllowed(moduleName) {
+	if withinApex && context.bazelDclaEnabledModules[moduleName] {
 		return true
 	}
 
 	return false
 }
 
-func (context *mixedBuildBazelContext) IsModuleDclaAllowed(moduleName string) bool {
-	return context.bazelDclaEnabledModules[moduleName]
-}
-
 func pwdPrefix() string {
 	// Darwin doesn't have /proc
 	if runtime.GOOS != "darwin" {
diff --git a/android/fixture.go b/android/fixture.go
index 03abb7f..dbc3bc5 100644
--- a/android/fixture.go
+++ b/android/fixture.go
@@ -275,15 +275,6 @@
 	})
 }
 
-// Sync the mock filesystem with the current config, then modify the context,
-// This allows context modification that requires filesystem access.
-func FixtureModifyContextWithMockFs(mutator func(ctx *TestContext)) FixturePreparer {
-	return newSimpleFixturePreparer(func(f *fixture) {
-		f.config.mockFileSystem("", f.mockFS)
-		mutator(f.ctx)
-	})
-}
-
 func FixtureRegisterWithContext(registeringFunc func(ctx RegistrationContext)) FixturePreparer {
 	return FixtureModifyContext(func(ctx *TestContext) { registeringFunc(ctx) })
 }
diff --git a/android/register.go b/android/register.go
index 5d277e1..64b0207 100644
--- a/android/register.go
+++ b/android/register.go
@@ -15,13 +15,9 @@
 package android
 
 import (
-	"bufio"
 	"fmt"
-	"path/filepath"
 	"reflect"
-	"regexp"
 
-	"android/soong/shared"
 	"github.com/google/blueprint"
 )
 
@@ -201,49 +197,6 @@
 	RegisterMutatorsForBazelConversion(ctx, bp2buildPreArchMutators)
 }
 
-func (c *Context) ParseBuildFiles(topDir string, existingBazelFiles []string) error {
-	result := map[string][]string{}
-
-	// Search for instances of `name = "$NAME"` (with arbitrary spacing).
-	targetNameRegex := regexp.MustCompile(`(?m)^\s*name\s*=\s*\"([^\"]+)\"`)
-
-	parseBuildFile := func(path string) error {
-		fullPath := shared.JoinPath(topDir, path)
-		sourceDir := filepath.Dir(path)
-
-		fileInfo, err := c.Config().fs.Stat(fullPath)
-		if err != nil {
-			return fmt.Errorf("Error accessing Bazel file '%s': %s", path, err)
-		}
-		if !fileInfo.IsDir() &&
-			(fileInfo.Name() == "BUILD" || fileInfo.Name() == "BUILD.bazel") {
-			f, err := c.Config().fs.Open(fullPath)
-			if err != nil {
-				return fmt.Errorf("Error reading Bazel file '%s': %s", path, err)
-			}
-			defer f.Close()
-			scanner := bufio.NewScanner(f)
-			for scanner.Scan() {
-				line := scanner.Text()
-				matches := targetNameRegex.FindAllStringSubmatch(line, -1)
-				for _, match := range matches {
-					result[sourceDir] = append(result[sourceDir], match[1])
-				}
-			}
-		}
-		return nil
-	}
-
-	for _, path := range existingBazelFiles {
-		err := parseBuildFile(path)
-		if err != nil {
-			return err
-		}
-	}
-	c.Config().SetBazelBuildFileTargets(result)
-	return nil
-}
-
 // RegisterForApiBazelConversion is similar to RegisterForBazelConversion except that
 // it only generates API targets in the generated  workspace
 func (ctx *Context) RegisterForApiBazelConversion() {
diff --git a/bp2build/build_conversion_test.go b/bp2build/build_conversion_test.go
index 1a35743..e127fd5 100644
--- a/bp2build/build_conversion_test.go
+++ b/bp2build/build_conversion_test.go
@@ -1946,36 +1946,3 @@
 	actual, _ := prettyPrintAttribute(lla, 0)
 	android.AssertStringEquals(t, "Print the common value if all keys in an axis have the same value", `[":libfoo.impl"]`, actual)
 }
-
-func TestAlreadyPresentBuildTarget(t *testing.T) {
-	bp := `
-	custom {
-		name: "foo",
-	}
-	custom {
-		name: "bar",
-	}
-	`
-	alreadyPresentBuildFile :=
-		MakeBazelTarget(
-			"custom",
-			"foo",
-			AttrNameToString{},
-		)
-	expectedBazelTargets := []string{
-		MakeBazelTarget(
-			"custom",
-			"bar",
-			AttrNameToString{},
-		),
-	}
-	registerCustomModule := func(ctx android.RegistrationContext) {
-		ctx.RegisterModuleType("custom", customModuleFactoryHostAndDevice)
-	}
-	RunBp2BuildTestCase(t, registerCustomModule, Bp2buildTestCase{
-		AlreadyExistingBuildContents: alreadyPresentBuildFile,
-		Blueprint:                    bp,
-		ExpectedBazelTargets:         expectedBazelTargets,
-		Description:                  "Not duplicating work for an already-present BUILD target.",
-	})
-}
diff --git a/bp2build/testing.go b/bp2build/testing.go
index 89ef07b..140b214 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -21,7 +21,6 @@
 
 import (
 	"fmt"
-	"path/filepath"
 	"sort"
 	"strings"
 	"testing"
@@ -83,12 +82,7 @@
 	// ExpectedBazelTargets compares the BazelTargets generated in `Dir` (if not empty).
 	// Otherwise, it checks the BazelTargets generated by `Blueprint` in the root directory.
 	ExpectedBazelTargets []string
-	// AlreadyExistingBuildContents, if non-empty, simulates an already-present source BUILD file
-	// in the directory under test. The BUILD file has the given contents. This BUILD file
-	// will also be treated as "BUILD file to keep" by the simulated bp2build environment.
-	AlreadyExistingBuildContents string
-
-	Filesystem map[string]string
+	Filesystem           map[string]string
 	// Dir sets the directory which will be compared against the targets in ExpectedBazelTargets.
 	// This should used in conjunction with the Filesystem property to check for targets
 	// generated from a directory that is not the root.
@@ -125,22 +119,11 @@
 
 func runBp2BuildTestCaseWithSetup(t *testing.T, extraPreparer android.FixturePreparer, tc Bp2buildTestCase) {
 	t.Helper()
-	checkDir := "."
-	if tc.Dir != "" {
-		checkDir = tc.Dir
-	}
-	keepExistingBuildDirs := tc.KeepBuildFileForDirs
-	buildFilesToParse := []string{}
+	dir := "."
 	filesystem := make(map[string][]byte)
 	for f, content := range tc.Filesystem {
 		filesystem[f] = []byte(content)
 	}
-	if len(tc.AlreadyExistingBuildContents) > 0 {
-		buildFilePath := filepath.Join(checkDir, "BUILD")
-		filesystem[buildFilePath] = []byte(tc.AlreadyExistingBuildContents)
-		keepExistingBuildDirs = append(keepExistingBuildDirs, checkDir)
-		buildFilesToParse = append(buildFilesToParse, buildFilePath)
-	}
 
 	preparers := []android.FixturePreparer{
 		extraPreparer,
@@ -149,7 +132,7 @@
 		android.FixtureRegisterWithContext(func(ctx android.RegistrationContext) {
 			ctx.RegisterModuleType(tc.ModuleTypeUnderTest, tc.ModuleTypeUnderTestFactory)
 		}),
-		android.FixtureModifyContextWithMockFs(func(ctx *android.TestContext) {
+		android.FixtureModifyContext(func(ctx *android.TestContext) {
 			// A default configuration for tests to not have to specify bp2build_available on top level
 			// targets.
 			bp2buildConfig := android.NewBp2BuildAllowlist().SetDefaultConfig(
@@ -157,7 +140,7 @@
 					android.Bp2BuildTopLevel: allowlists.Bp2BuildDefaultTrueRecursively,
 				},
 			)
-			for _, f := range keepExistingBuildDirs {
+			for _, f := range tc.KeepBuildFileForDirs {
 				bp2buildConfig.SetKeepExistingBuildFile(map[string]bool{
 					f: /*recursive=*/ false,
 				})
@@ -167,10 +150,6 @@
 			// from cloning modules to their original state after mutators run. This
 			// would lose some data intentionally set by these mutators.
 			ctx.SkipCloneModulesAfterMutators = true
-			err := ctx.ParseBuildFiles(".", buildFilesToParse)
-			if err != nil {
-				t.Errorf("error parsing build files in test setup: %s", err)
-			}
 		}),
 		android.FixtureModifyEnv(func(env map[string]string) {
 			if tc.UnconvertedDepsMode == errorModulesUnconvertedDeps {
@@ -189,6 +168,10 @@
 		return
 	}
 
+	checkDir := dir
+	if tc.Dir != "" {
+		checkDir = tc.Dir
+	}
 	expectedTargets := map[string][]string{
 		checkDir: tc.ExpectedBazelTargets,
 	}
diff --git a/cc/cc.go b/cc/cc.go
index 52a30d7..6087970 100644
--- a/cc/cc.go
+++ b/cc/cc.go
@@ -1997,9 +1997,6 @@
 func GetApexConfigKey(ctx android.BaseModuleContext) *android.ApexConfigKey {
 	apexInfo := ctx.Provider(android.ApexInfoProvider).(android.ApexInfo)
 	if !apexInfo.IsForPlatform() {
-		if !ctx.Config().BazelContext.IsModuleDclaAllowed(ctx.Module().Name()) {
-			return nil
-		}
 		apexKey := android.ApexConfigKey{
 			WithinApex:     true,
 			ApexSdkVersion: findApexSdkVersion(ctx, apexInfo).String(),
diff --git a/cc/lto.go b/cc/lto.go
index 878c21f..547ebff 100644
--- a/cc/lto.go
+++ b/cc/lto.go
@@ -109,7 +109,7 @@
 
 		// If the module does not have a profile, be conservative and limit cross TU inline
 		// limit to 5 LLVM IR instructions, to balance binary size increase and performance.
-		if !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
+		if !ctx.Darwin() && !ctx.isPgoCompile() && !ctx.isAfdoCompile() {
 			flags.Local.LdFlags = append(flags.Local.LdFlags,
 				"-Wl,-plugin-opt,-import-instr-limit=5")
 		}
diff --git a/cmd/soong_build/main.go b/cmd/soong_build/main.go
index bf5af2c..22d64a2 100644
--- a/cmd/soong_build/main.go
+++ b/cmd/soong_build/main.go
@@ -21,6 +21,7 @@
 	"fmt"
 	"os"
 	"path/filepath"
+	"regexp"
 	"strings"
 	"time"
 
@@ -724,22 +725,47 @@
 
 		// FIXME: 'frameworks/compile/slang' has a filegroup error due to an escaping issue
 		"frameworks/compile/slang",
-
-		// FIXME(b/260809113): 'prebuilts/clang/host/linux-x86/clang-dev' is a tool-generated symlink
-		// directory that contains a BUILD file. The bazel files finder code doesn't traverse into symlink dirs,
-		// and hence is not aware of this BUILD file and exclude it accordingly during symlink forest generation
-		// when checking against keepExistingBuildFiles allowlist.
-		//
-		// This is necessary because globs in //prebuilts/clang/host/linux-x86/BUILD
-		// currently assume no subpackages (keepExistingBuildFile is not recursive for that directory).
-		//
-		// This is a bandaid until we the symlink forest logic can intelligently exclude BUILD files found in
-		// source symlink dirs according to the keepExistingBuildFile allowlist.
-		"prebuilts/clang/host/linux-x86/clang-dev",
 	)
 	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.
@@ -748,11 +774,7 @@
 	ctx.EventHandler.Do("bp2build", func() {
 
 		ctx.EventHandler.Do("read_build", func() {
-			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")
+			ctx.Config().SetBazelBuildFileTargets(buildTargetsByPackage(ctx))
 		})
 
 		// Propagate "allow misssing dependencies" bit. This is normally set in