Convert apex transition tag to dependency on apex

When the apex mutator moves to using an info struct instead of a simple
string it becomes difficult to add a dependency directly on the apex
variation of a module, as that would require constructing a matching
ApexInfo to locate it.  Simplify adding the dependency by adding it
on the apex instead, and then traversing from the apex to the desired
module inside the apex.

This causes a few changes in behaviors that require updating tests to
match.

When AlwaysUsePrebuiltSdks is set, the previous behavior was to use
the fragment and its dependencies from the source module for the
platform bootclasspath.  Since the fragment is now found by traversing
from the apex, and the prebuilt apex is preferred, the fragment from
the prebuilt will now be found instead.

prebuilt_bootclasspath lists fragments in apexes that it depends on.
Previously it would depend on the apex variant of the fragment, which
then allowed the fragment to be replaced by the variant from an
override apex that set apex_name.  Now it always depends on the original
apex to find the fragment.

Test: go test ./...
Bug: 372543712
Change-Id: I1a92ff461d4706cf68a0b8b37e53dea412efb8bf
diff --git a/java/bootclasspath.go b/java/bootclasspath.go
index 856f439..5500926 100644
--- a/java/bootclasspath.go
+++ b/java/bootclasspath.go
@@ -15,6 +15,8 @@
 package java
 
 import (
+	"fmt"
+
 	"android/soong/android"
 
 	"github.com/google/blueprint"
@@ -52,25 +54,76 @@
 	tag := bootclasspathDependencyTag{
 		typ: tagType,
 	}
-	if !android.IsConfiguredJarForPlatform(apex) {
-		tag.apex = apex
+	target := ctx.Module().Target()
+	if android.IsConfiguredJarForPlatform(apex) {
+		// Platform variant, add a direct dependency.
+		ctx.AddFarVariationDependencies(target.Variations(), tag, name)
+	} else {
+		// A module in an apex.  Dependencies can't be added directly onto an apex variation, as that would
+		// require constructing a full ApexInfo configuration, which can't be predicted here.  Add a dependency
+		// on the apex instead, and annotate the dependency tag with the desired module in the apex.
+		tag.moduleInApex = name
+		ctx.AddFarVariationDependencies(target.Variations(), tag, apex)
 	}
 
-	target := ctx.Module().Target()
-
-	ctx.AddFarVariationDependencies(target.Variations(), tag, name)
 }
 
 // gatherApexModulePairDepsWithTag returns the list of dependencies with the supplied tag that was
 // added by addDependencyOntoApexModulePair.
 func gatherApexModulePairDepsWithTag(ctx android.BaseModuleContext, tagType bootclasspathDependencyTagType) []android.Module {
 	var modules []android.Module
+
+	type moduleInApex struct {
+		module string
+		apex   string
+	}
+
+	var modulesInApexes []moduleInApex
+
 	ctx.VisitDirectDeps(func(module android.Module) {
 		t := ctx.OtherModuleDependencyTag(module)
 		if bcpTag, ok := t.(bootclasspathDependencyTag); ok && bcpTag.typ == tagType {
-			modules = append(modules, module)
+			if bcpTag.moduleInApex != "" {
+				modulesInApexes = append(modulesInApexes, moduleInApex{bcpTag.moduleInApex, ctx.OtherModuleName(module)})
+			} else {
+				modules = append(modules, module)
+			}
 		}
 	})
+
+	for _, moduleInApex := range modulesInApexes {
+		var found android.Module
+		ctx.WalkDeps(func(child, parent android.Module) bool {
+			t := ctx.OtherModuleDependencyTag(child)
+			if parent == ctx.Module() {
+				if bcpTag, ok := t.(bootclasspathDependencyTag); ok && bcpTag.typ == tagType && ctx.OtherModuleName(child) == moduleInApex.apex {
+					// recurse into the apex
+					return true
+				}
+			} else if tagType != fragment && android.IsFragmentInApexTag(t) {
+				return true
+			} else if android.IsDontReplaceSourceWithPrebuiltTag(t) {
+				return false
+			} else if t == android.PrebuiltDepTag {
+				return false
+			} else if IsBootclasspathFragmentContentDepTag(t) {
+				return false
+			} else if android.RemoveOptionalPrebuiltPrefix(ctx.OtherModuleName(child)) == moduleInApex.module {
+				if found != nil && child != found {
+					panic(fmt.Errorf("found two conflicting modules %q in apex %q: %s and %s",
+						moduleInApex.module, moduleInApex.apex, found, child))
+				}
+				found = child
+			}
+			return false
+		})
+		if found != nil {
+			modules = append(modules, found)
+		} else if !ctx.Config().AllowMissingDependencies() {
+			ctx.ModuleErrorf("failed to find module %q in apex %q\n",
+				moduleInApex.module, moduleInApex.apex)
+		}
+	}
 	return modules
 }
 
@@ -111,7 +164,9 @@
 
 	typ bootclasspathDependencyTagType
 
-	apex string
+	// moduleInApex is set to the name of the desired module when this dependency points
+	// to the apex that the modules is contained in.
+	moduleInApex string
 }
 
 type bootclasspathDependencyTagType int
@@ -130,8 +185,8 @@
 func (t bootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
 }
 
-func (t bootclasspathDependencyTag) ApexTransition() string {
-	return t.apex
+func (t bootclasspathDependencyTag) LicenseAnnotations() []android.LicenseAnnotation {
+	return []android.LicenseAnnotation{android.LicenseAnnotationSharedDependency}
 }
 
 // Dependencies that use the bootclasspathDependencyTag instances are only added after all the