Extract general bootclasspath related code into java/bootclasspath.go

The platform_bootclasspath and bootclasspath_fragment modules provide
different capabilities but are related and so have some common
functionality. This change moves some platform_bootclasspath code that
will be of use for bootclasspath_fragment in future into a separate
file.

Bug: 177892522
Test: m nothing
Change-Id: I827b85e33d16155fcc920d553100c9e99267dc4e
diff --git a/java/platform_bootclasspath.go b/java/platform_bootclasspath.go
index 568f5e4..38ce985 100644
--- a/java/platform_bootclasspath.go
+++ b/java/platform_bootclasspath.go
@@ -19,8 +19,6 @@
 
 	"android/soong/android"
 	"android/soong/dexpreopt"
-	"github.com/google/blueprint"
-	"github.com/google/blueprint/proptools"
 )
 
 func init() {
@@ -35,27 +33,8 @@
 	})
 }
 
-type platformBootclasspathDependencyTag struct {
-	blueprint.BaseDependencyTag
-
-	name string
-}
-
-// Avoid having to make platform bootclasspath content visible to the platform bootclasspath.
-//
-// This is a temporary workaround to make it easier to migrate to platform bootclasspath with proper
-// dependencies.
-// TODO(b/177892522): Remove this and add needed visibility.
-func (t platformBootclasspathDependencyTag) ExcludeFromVisibilityEnforcement() {
-}
-
 // The tag used for the dependency between the platform bootclasspath and any configured boot jars.
-var platformBootclasspathModuleDepTag = platformBootclasspathDependencyTag{name: "module"}
-
-// The tag used for the dependency between the platform bootclasspath and bootclasspath_fragments.
-var platformBootclasspathFragmentDepTag = platformBootclasspathDependencyTag{name: "fragment"}
-
-var _ android.ExcludeFromVisibilityEnforcementTag = platformBootclasspathDependencyTag{}
+var platformBootclasspathModuleDepTag = bootclasspathDependencyTag{name: "module"}
 
 type platformBootclasspathModule struct {
 	android.ModuleBase
@@ -83,22 +62,8 @@
 	hiddenAPIMetadataCSV android.OutputPath
 }
 
-// ApexVariantReference specifies a particular apex variant of a module.
-type ApexVariantReference struct {
-	// The name of the module apex variant, i.e. the apex containing the module variant.
-	//
-	// If this is not specified then it defaults to "platform" which will cause a dependency to be
-	// added to the module's platform variant.
-	Apex *string
-
-	// The name of the module.
-	Module *string
-}
-
 type platformBootclasspathProperties struct {
-	// The names of the bootclasspath_fragment modules that form part of this
-	// platform_bootclasspath.
-	Fragments []ApexVariantReference
+	BootclasspathFragmentsDepsProperties
 
 	Hidden_api HiddenAPIFlagFileProperties
 }
@@ -178,74 +143,7 @@
 		addDependenciesOntoBootImageModules(ctx, updatableModules)
 
 		// Add dependencies on all the fragments.
-		addDependencyOntoApexVariants(ctx, "fragments", p.properties.Fragments, platformBootclasspathFragmentDepTag)
-	}
-}
-
-func addDependencyOntoApexVariants(ctx android.BottomUpMutatorContext, propertyName string, refs []ApexVariantReference, tag blueprint.DependencyTag) {
-	for i, ref := range refs {
-		apex := proptools.StringDefault(ref.Apex, "platform")
-
-		if ref.Module == nil {
-			ctx.PropertyErrorf(propertyName, "missing module name at position %d", i)
-			continue
-		}
-		name := proptools.String(ref.Module)
-
-		addDependencyOntoApexModulePair(ctx, apex, name, tag)
-	}
-}
-
-func addDependencyOntoApexModulePair(ctx android.BottomUpMutatorContext, apex string, name string, tag blueprint.DependencyTag) {
-	var variations []blueprint.Variation
-	if apex != "platform" {
-		// Pick the correct apex variant.
-		variations = []blueprint.Variation{
-			{Mutator: "apex", Variation: apex},
-		}
-	}
-
-	addedDep := false
-	if ctx.OtherModuleDependencyVariantExists(variations, name) {
-		ctx.AddFarVariationDependencies(variations, tag, name)
-		addedDep = true
-	}
-
-	// Add a dependency on the prebuilt module if it exists.
-	prebuiltName := android.PrebuiltNameFromSource(name)
-	if ctx.OtherModuleDependencyVariantExists(variations, prebuiltName) {
-		ctx.AddVariationDependencies(variations, tag, prebuiltName)
-		addedDep = true
-	}
-
-	// If no appropriate variant existing for this, so no dependency could be added, then it is an
-	// error, unless missing dependencies are allowed. The simplest way to handle that is to add a
-	// dependency that will not be satisfied and the default behavior will handle it.
-	if !addedDep {
-		// Add dependency on the unprefixed (i.e. source or renamed prebuilt) module which we know does
-		// not exist. The resulting error message will contain useful information about the available
-		// variants.
-		reportMissingVariationDependency(ctx, variations, name)
-
-		// Add dependency on the missing prefixed prebuilt variant too if a module with that name exists
-		// so that information about its available variants will be reported too.
-		if ctx.OtherModuleExists(prebuiltName) {
-			reportMissingVariationDependency(ctx, variations, prebuiltName)
-		}
-	}
-}
-
-// reportMissingVariationDependency intentionally adds a dependency on a missing variation in order
-// to generate an appropriate error message with information about the available variations.
-func reportMissingVariationDependency(ctx android.BottomUpMutatorContext, variations []blueprint.Variation, name string) {
-	modules := ctx.AddFarVariationDependencies(variations, nil, name)
-	if len(modules) != 1 {
-		panic(fmt.Errorf("Internal Error: expected one module, found %d", len(modules)))
-		return
-	}
-	if modules[0] != nil {
-		panic(fmt.Errorf("Internal Error: expected module to be missing but was found: %q", modules[0]))
-		return
+		p.properties.BootclasspathFragmentsDepsProperties.addDependenciesOntoFragments(ctx)
 	}
 }
 
@@ -265,7 +163,7 @@
 		tag := ctx.OtherModuleDependencyTag(module)
 		if tag == platformBootclasspathModuleDepTag {
 			b.configuredModules = append(b.configuredModules, module)
-		} else if tag == platformBootclasspathFragmentDepTag {
+		} else if tag == bootclasspathFragmentDepTag {
 			b.fragments = append(b.fragments, module)
 		}
 	})