Add bazel-built modules as deps on the system image

These bazel-built modules will be installed into the system image
as part of the bazel rule, rather than going through the make staging
directory.

Bug: 297269187
Test: m bazel_sandwich
Change-Id: I96c6e58f8e0898b2ad92cb7069745ca2059a39f8
diff --git a/bp2build/testing.go b/bp2build/testing.go
index b2804f9..c978164 100644
--- a/bp2build/testing.go
+++ b/bp2build/testing.go
@@ -28,6 +28,7 @@
 	"testing"
 
 	"android/soong/ui/metrics/bp2build_metrics_proto"
+
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
@@ -124,26 +125,28 @@
 	// be merged with the generated BUILD file. This allows custom BUILD targets
 	// to be used in tests, or use BUILD files to draw package boundaries.
 	KeepBuildFileForDirs []string
-}
 
-func RunBp2BuildTestCaseExtraContext(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), modifyContext func(ctx *android.TestContext), tc Bp2buildTestCase) {
-	t.Helper()
-	preparers := []android.FixturePreparer{
-		android.FixtureRegisterWithContext(registerModuleTypes),
-	}
-	if modifyContext != nil {
-		preparers = append(preparers, android.FixtureModifyContext(modifyContext))
-	}
-	preparers = append(preparers, SetBp2BuildTestRunner)
-	bp2buildSetup := android.GroupFixturePreparers(
-		preparers...,
-	)
-	runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
+	// An extra FixturePreparer to use when running the test. If you need multiple extra
+	// FixturePreparers, use android.GroupFixturePreparers()
+	ExtraFixturePreparer android.FixturePreparer
+
+	// If bp2build_product_config.go should run as part of the test.
+	RunBp2buildProductConfig bool
 }
 
 func RunBp2BuildTestCase(t *testing.T, registerModuleTypes func(ctx android.RegistrationContext), tc Bp2buildTestCase) {
 	t.Helper()
-	RunBp2BuildTestCaseExtraContext(t, registerModuleTypes, nil, tc)
+	preparers := []android.FixturePreparer{
+		android.FixtureRegisterWithContext(registerModuleTypes),
+	}
+	if tc.ExtraFixturePreparer != nil {
+		preparers = append(preparers, tc.ExtraFixturePreparer)
+	}
+	preparers = append(preparers, android.FixtureSetTestRunner(&bazelTestRunner{generateProductConfigTargets: tc.RunBp2buildProductConfig}))
+	bp2buildSetup := android.GroupFixturePreparers(
+		preparers...,
+	)
+	runBp2BuildTestCaseWithSetup(t, bp2buildSetup, tc)
 }
 
 func runBp2BuildTestCaseWithSetup(t *testing.T, extraPreparer android.FixturePreparer, tc Bp2buildTestCase) {
@@ -247,11 +250,10 @@
 	result.CompareAllBazelTargets(t, tc, expectedTargets, true)
 }
 
-// SetBp2BuildTestRunner customizes the test fixture mechanism to run tests in Bp2Build mode.
-var SetBp2BuildTestRunner = android.FixtureSetTestRunner(&bazelTestRunner{})
-
 // bazelTestRunner customizes the test fixture mechanism to run tests of the bp2build build mode.
-type bazelTestRunner struct{}
+type bazelTestRunner struct {
+	generateProductConfigTargets bool
+}
 
 func (b *bazelTestRunner) FinalPreparer(result *android.TestResult) android.CustomTestResult {
 	ctx := result.TestContext
@@ -274,6 +276,16 @@
 	if bazelResult.CollateErrs(errs) {
 		return
 	}
+	if b.generateProductConfigTargets {
+		productConfig, err := createProductConfigFiles(codegenCtx, res.moduleNameToPartition, res.metrics.convertedModulePathMap)
+		if err != nil {
+			bazelResult.CollateErrs([]error{err})
+			return
+		}
+		for k, v := range productConfig.bp2buildTargets {
+			res.buildFileToTargets[k] = append(res.buildFileToTargets[k], v...)
+		}
+	}
 
 	// Store additional data for access by tests.
 	bazelResult.conversionResults = res