create, but dont register, bp2build_deps mutator

This is the bulk of the "allowlist v2" feature. It will disable bp2build
generation for modules which have transitive dependencies without a
bazel build definition.

This CL includes this mutator, but doesn't register it as a bp2build
mutator (outside of a few unit tests). This allows us to easily iterate
on completion of this feature and ensure there are no launch blockers
before we finalize the change in AOSP.

Bug: 285631638
Test: Unit tests
Change-Id: Ifb0a079c409ca19b02cafa3fab2efa0d3deebc50
diff --git a/android/bazel.go b/android/bazel.go
index 5df12f0..e307b18 100644
--- a/android/bazel.go
+++ b/android/bazel.go
@@ -625,7 +625,18 @@
 	ctx.BottomUp("bp2build_conversion", bp2buildConversionMutator).Parallel()
 }
 
+func registerBp2buildDepsMutator(ctx RegisterMutatorsContext) {
+	ctx.BottomUp("bp2build_deps", bp2buildDepsMutator).Parallel()
+}
+
 func bp2buildConversionMutator(ctx BottomUpMutatorContext) {
+	// If an existing BUILD file in the module directory has a target defined
+	// with this same name as this module, assume that this is an existing
+	// definition for this target.
+	if ctx.Config().HasBazelBuildTargetInSource(ctx.ModuleDir(), ctx.ModuleName()) {
+		ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE, ctx.ModuleName())
+		return
+	}
 	bModule, ok := ctx.Module().(Bazelable)
 	if !ok {
 		ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_TYPE_UNSUPPORTED, "")
@@ -659,6 +670,10 @@
 		panic(fmt.Errorf("illegal bp2build invariant: module '%s' was neither converted nor marked unconvertible", ctx.ModuleName()))
 	}
 
+	// If an existing BUILD file in the module directory has a target defined
+	// with the same name as any target generated by this module, assume that this
+	// is an existing definition for this target. (These generated target names
+	// may be different than the module name, as checked at the beginning of this function!)
 	for _, targetInfo := range ctx.Module().base().Bp2buildTargets() {
 		if ctx.Config().HasBazelBuildTargetInSource(targetInfo.TargetPackage(), targetInfo.TargetName()) {
 			// Defer to the BUILD target. Generating an additional target would
@@ -669,6 +684,27 @@
 	}
 }
 
+// TODO: b/285631638 - Add this as a new mutator to the bp2build conversion mutators.
+// Currently, this only exists to prepare test coverage for the launch of this feature.
+func bp2buildDepsMutator(ctx BottomUpMutatorContext) {
+	if ctx.Module().base().GetUnconvertedReason() != nil {
+		return
+	}
+
+	if len(ctx.Module().GetMissingBp2buildDeps()) > 0 {
+		exampleDep := ctx.Module().GetMissingBp2buildDeps()[0]
+		ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNCONVERTED_DEP, exampleDep)
+	}
+
+	ctx.VisitDirectDeps(func(dep Module) {
+		if dep.base().GetUnconvertedReason() != nil &&
+			dep.base().GetUnconvertedReason().ReasonType != int(bp2build_metrics_proto.UnconvertedReasonType_DEFINED_IN_BUILD_FILE) &&
+			ctx.OtherModuleDependencyTag(dep) == Bp2buildDepTag {
+			ctx.MarkBp2buildUnconvertible(bp2build_metrics_proto.UnconvertedReasonType_UNCONVERTED_DEP, dep.Name())
+		}
+	})
+}
+
 // GetMainClassInManifest scans the manifest file specified in filepath and returns
 // the value of attribute Main-Class in the manifest file if it exists, or returns error.
 // WARNING: this is for bp2build converters of java_* modules only.