Add --bazel-mode and --bazel-mode-dev

This allows "bazel mixed builds prod mode", in additional to reworking
the mechanism in which mixed builds dev mode is enabled.

As a followup, CI scripts will be migrated to use the new flags, as
USE_BAZEL_ANALYSIS=1 is deprecated.

Test: Manually ran --bazel-mode with an allowlist verifying that the
module alone was enabled
Test: Manually verified --bazel-mode and --bazel-mode-dev cause a build
failure

Change-Id: If0d34360e60452f428b05828f4ec7596b7cb619a
diff --git a/android/allowlists/allowlists.go b/android/allowlists/allowlists.go
index 5c79fa2..60b3503 100644
--- a/android/allowlists/allowlists.go
+++ b/android/allowlists/allowlists.go
@@ -595,4 +595,9 @@
 		"prebuilt_platform-robolectric-4.5.1-prebuilt",
 		"prebuilt_currysrc_org.eclipse",
 	}
+
+	ProdMixedBuildsEnabledList = []string{
+		// This list left intentionally empty for now. Add specific module names
+		// to have them built by Bazel in Prod Mixed Builds mode.
+	}
 )
diff --git a/android/bazel.go b/android/bazel.go
index 71eb036..3ae3d6f 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -219,16 +219,16 @@
 	// in the synthetic Bazel workspace.
 	keepExistingBuildFile map[string]bool
 
-	// Per-module allowlist to always opt modules in of both bp2build and mixed builds.
-	// These modules are usually in directories with many other modules that are not ready for
-	// conversion.
+	// Per-module allowlist to always opt modules into both bp2build and Bazel Dev Mode mixed
+	// builds. These modules are usually in directories with many other modules that are not ready
+	// for conversion.
 	//
 	// A module can either be in this list or its directory allowlisted entirely
 	// in bp2buildDefaultConfig, but not both at the same time.
 	moduleAlwaysConvert map[string]bool
 
-	// Per-module-type allowlist to always opt modules in to both bp2build and mixed builds
-	// when they have the same type as one listed.
+	// Per-module-type allowlist to always opt modules in to both bp2build and
+	// Bazel Dev Mode mixed builds when they have the same type as one listed.
 	moduleTypeAlwaysConvert map[string]bool
 
 	// Per-module denylist to always opt modules out of bp2build conversion.
diff --git a/android/bazel_handler.go b/android/bazel_handler.go
index d87f988..93b6779 100644
--- a/android/bazel_handler.go
+++ b/android/bazel_handler.go
@@ -343,7 +343,30 @@
 }
 
 func NewBazelContext(c *config) (BazelContext, error) {
-	if !c.IsMixedBuildsEnabled() {
+	var modulesDefaultToBazel bool
+	disabledModules := map[string]bool{}
+	enabledModules := map[string]bool{}
+
+	switch c.BuildMode {
+	case BazelProdMode:
+		modulesDefaultToBazel = false
+
+		for _, enabledProdModule := range allowlists.ProdMixedBuildsEnabledList {
+			enabledModules[enabledProdModule] = true
+		}
+	case BazelDevMode:
+		modulesDefaultToBazel = true
+
+		// Don't use partially-converted cc_library targets in mixed builds,
+		// since mixed builds would generally rely on both static and shared
+		// variants of a cc_library.
+		for staticOnlyModule, _ := range GetBp2BuildAllowList().ccLibraryStaticOnly {
+			disabledModules[staticOnlyModule] = true
+		}
+		for _, disabledDevModule := range allowlists.MixedBuildsDisabledList {
+			disabledModules[disabledDevModule] = true
+		}
+	default:
 		return noopBazelContext{}, nil
 	}
 
@@ -352,24 +375,12 @@
 		return nil, err
 	}
 
-	// TODO(cparsons): Use a different allowlist depending on prod vs. dev
-	// bazel mode.
-	disabledModules := map[string]bool{}
-	// Don't use partially-converted cc_library targets in mixed builds,
-	// since mixed builds would generally rely on both static and shared
-	// variants of a cc_library.
-	for staticOnlyModule, _ := range GetBp2BuildAllowList().ccLibraryStaticOnly {
-		disabledModules[staticOnlyModule] = true
-	}
-	for _, disabledDevModule := range allowlists.MixedBuildsDisabledList {
-		disabledModules[disabledDevModule] = true
-	}
-
 	return &bazelContext{
 		bazelRunner:           &builtinBazelRunner{},
 		paths:                 p,
 		requests:              make(map[cqueryKey]bool),
-		modulesDefaultToBazel: true,
+		modulesDefaultToBazel: modulesDefaultToBazel,
+		bazelEnabledModules:   enabledModules,
 		bazelDisabledModules:  disabledModules,
 	}, nil
 }
diff --git a/android/config.go b/android/config.go
index 5ca9420..222b142 100644
--- a/android/config.go
+++ b/android/config.go
@@ -96,7 +96,6 @@
 
 	// Use bazel during analysis of build modules from an allowlist carefully
 	// curated by the build team to be proven stable.
-	// TODO(cparsons): Implement this mode.
 	BazelProdMode
 )
 
@@ -481,14 +480,6 @@
 		config.AndroidFirstDeviceTarget = FirstTarget(config.Targets[Android], "lib64", "lib32")[0]
 	}
 
-	// Checking USE_BAZEL_ANALYSIS must be done here instead of in the caller, so
-	// that we can invoke IsEnvTrue (which also registers the env var as a
-	// dependency of the build).
-	// TODO(cparsons): Remove this hack once USE_BAZEL_ANALYSIS is removed.
-	if buildMode == AnalysisNoBazel && config.IsEnvTrue("USE_BAZEL_ANALYSIS") {
-		buildMode = BazelDevMode
-	}
-
 	config.BuildMode = buildMode
 	config.BazelContext, err = NewBazelContext(config)
 	config.bp2buildPackageConfig = GetBp2BuildAllowList()
@@ -678,9 +669,7 @@
 // DeviceProduct returns the current product target. There could be multiple of
 // these per device type.
 //
-// NOTE: Do not base conditional logic on this value. It may break product
-//
-//	inheritance.
+// NOTE: Do not base conditional logic on this value. It may break product inheritance.
 func (c *config) DeviceProduct() string {
 	return *c.productVariables.DeviceProduct
 }