Remove support for generating versioned snapshots

(cherry picked from commit b01ac4b6ba89e13cbb47e9d22619bf015270ee26)

Previously, the code for selecting specific versions of sdk snapshots
was removed (along with the uses_sdks property). That makes versioned
snapshots useless so this change removes all the code and tests that
generated those versioned snapshots.

Bug: 232546567
Test: m nothing
      packages/modules/common/build/mainline_modules_sdks.sh
Change-Id: Ib6d1b72bc8399fbb39075494ae37da92f4b28d03
Merged-In: Ib6d1b72bc8399fbb39075494ae37da92f4b28d03
diff --git a/sdk/update.go b/sdk/update.go
index 063597f..8e4f9d4 100644
--- a/sdk/update.go
+++ b/sdk/update.go
@@ -35,7 +35,7 @@
 // ========================================================
 //
 // SOONG_SDK_SNAPSHOT_PREFER
-//     By default every unversioned module in the generated snapshot has prefer: false. Building it
+//     By default every module in the generated snapshot has prefer: false. Building it
 //     with SOONG_SDK_SNAPSHOT_PREFER=true will force them to use prefer: true.
 //
 // SOONG_SDK_SNAPSHOT_USE_SOURCE_CONFIG_VAR
@@ -69,20 +69,6 @@
 //     maintainable solution has been implemented.
 //     TODO(b/174997203): Remove when no longer necessary.
 //
-// SOONG_SDK_SNAPSHOT_VERSION
-//     This provides control over the version of the generated snapshot.
-//
-//     SOONG_SDK_SNAPSHOT_VERSION=current will generate unversioned and versioned prebuilts and a
-//     versioned snapshot module. This is the default behavior. The zip file containing the
-//     generated snapshot will be <sdk-name>-current.zip.
-//
-//     SOONG_SDK_SNAPSHOT_VERSION=unversioned will generate unversioned prebuilts only and the zip
-//     file containing the generated snapshot will be <sdk-name>.zip.
-//
-//     SOONG_SDK_SNAPSHOT_VERSION=<number> will generate versioned prebuilts and a versioned
-//     snapshot module only. The zip file containing the generated snapshot will be
-//     <sdk-name>-<number>.zip.
-//
 // SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE
 //     This allows the target build release (i.e. the release version of the build within which
 //     the snapshot will be used) of the snapshot to be specified. If unspecified then it defaults
@@ -130,8 +116,7 @@
 )
 
 const (
-	soongSdkSnapshotVersionUnversioned = "unversioned"
-	soongSdkSnapshotVersionCurrent     = "current"
+	soongSdkSnapshotVersionCurrent = "current"
 )
 
 type generatedContents struct {
@@ -163,13 +148,13 @@
 // IndentedPrintf will add spaces to indent the line to the appropriate level before printing the
 // arguments.
 func (gc *generatedContents) IndentedPrintf(format string, args ...interface{}) {
-	fmt.Fprintf(&(gc.content), strings.Repeat("    ", gc.indentLevel)+format, args...)
+	_, _ = fmt.Fprintf(&(gc.content), strings.Repeat("    ", gc.indentLevel)+format, args...)
 }
 
 // UnindentedPrintf does not add spaces to indent the line to the appropriate level before printing
 // the arguments.
 func (gc *generatedContents) UnindentedPrintf(format string, args ...interface{}) {
-	fmt.Fprintf(&(gc.content), format, args...)
+	_, _ = fmt.Fprintf(&(gc.content), format, args...)
 }
 
 func (gf *generatedFile) build(pctx android.PackageContext, ctx android.BuilderContext, implicits android.Paths) {
@@ -347,12 +332,6 @@
 //         <arch>/lib/
 //            libFoo.so   : a stub library
 
-// A name that uniquely identifies a prebuilt SDK member for a version of SDK snapshot
-// This isn't visible to users, so could be changed in future.
-func versionedSdkMemberName(ctx android.ModuleContext, memberName string, version string) string {
-	return ctx.ModuleName() + "_" + memberName + string(android.SdkVersionSeparator) + version
-}
-
 // buildSnapshot is the main function in this source file. It creates rules to copy
 // the contents (header files, stub libraries, etc) into the zip file.
 func (s *sdk) buildSnapshot(ctx android.ModuleContext, sdkVariants []*sdk) {
@@ -404,20 +383,9 @@
 	}
 
 	config := ctx.Config()
-	version := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_VERSION", "current")
 
-	// Generate versioned modules in the snapshot unless an unversioned snapshot has been requested.
-	generateVersioned := version != soongSdkSnapshotVersionUnversioned
-
-	// Generate unversioned modules in the snapshot unless a numbered snapshot has been requested.
-	//
-	// Unversioned modules are not required in that case because the numbered version will be a
-	// finalized version of the snapshot that is intended to be kept separate from the
-	generateUnversioned := version == soongSdkSnapshotVersionUnversioned || version == soongSdkSnapshotVersionCurrent
-	snapshotFileSuffix := ""
-	if generateVersioned {
-		snapshotFileSuffix = "-" + version
-	}
+	// Always add -current to the end
+	snapshotFileSuffix := "-current"
 
 	currentBuildRelease := latestBuildRelease()
 	targetBuildReleaseEnv := config.GetenvWithDefault("SOONG_SDK_SNAPSHOT_TARGET_BUILD_RELEASE", currentBuildRelease.name)
@@ -430,7 +398,6 @@
 	builder := &snapshotBuilder{
 		ctx:                   ctx,
 		sdk:                   s,
-		version:               version,
 		snapshotDir:           snapshotDir.OutputPath,
 		copies:                make(map[string]string),
 		filesToZip:            []android.Path{bp.path},
@@ -480,38 +447,19 @@
 		s.createMemberSnapshot(memberCtx, member, prebuiltModule.(*bpModule))
 	}
 
-	// Create a transformer that will transform an unversioned module into a versioned module.
-	unversionedToVersionedTransformer := unversionedToVersionedTransformation{builder: builder}
-
-	// Create a transformer that will transform an unversioned module by replacing any references
+	// Create a transformer that will transform a module by replacing any references
 	// to internal members with a unique module name and setting prefer: false.
-	unversionedTransformer := unversionedTransformation{
+	snapshotTransformer := snapshotTransformation{
 		builder: builder,
 	}
 
-	for _, unversioned := range builder.prebuiltOrder {
+	for _, module := range builder.prebuiltOrder {
 		// Prune any empty property sets.
-		unversioned = unversioned.transform(pruneEmptySetTransformer{})
+		module = module.transform(pruneEmptySetTransformer{})
 
-		if generateVersioned {
-			// Copy the unversioned module so it can be modified to make it versioned.
-			versioned := unversioned.deepCopy()
-
-			// Transform the unversioned module into a versioned one.
-			versioned.transform(unversionedToVersionedTransformer)
-			bpFile.AddModule(versioned)
-		}
-
-		if generateUnversioned {
-			// Transform the unversioned module to make it suitable for use in the snapshot.
-			unversioned.transform(unversionedTransformer)
-			bpFile.AddModule(unversioned)
-		}
-	}
-
-	if generateVersioned {
-		// Add the sdk/module_exports_snapshot module to the bp file.
-		s.addSnapshotModule(ctx, builder, sdkVariants, memberVariantDeps)
+		// Transform the module module to make it suitable for use in the snapshot.
+		module.transform(snapshotTransformer)
+		bpFile.AddModule(module)
 	}
 
 	// generate Android.bp
@@ -698,7 +646,7 @@
 func filterOutComponents(ctx android.ModuleContext, deps []sdkMemberVariantDep) []sdkMemberVariantDep {
 	// Collate the set of components that all the modules added to the sdk provide.
 	components := map[string]*sdkMemberVariantDep{}
-	for i, _ := range deps {
+	for i := range deps {
 		dep := &deps[i]
 		for _, c := range dep.exportedComponentsInfo.Components {
 			components[c] = dep
@@ -733,81 +681,6 @@
 	return filtered
 }
 
-// addSnapshotModule adds the sdk_snapshot/module_exports_snapshot module to the builder.
-func (s *sdk) addSnapshotModule(ctx android.ModuleContext, builder *snapshotBuilder, sdkVariants []*sdk, memberVariantDeps []sdkMemberVariantDep) {
-	bpFile := builder.bpFile
-
-	snapshotName := ctx.ModuleName() + string(android.SdkVersionSeparator) + builder.version
-	var snapshotModuleType string
-	if s.properties.Module_exports {
-		snapshotModuleType = "module_exports_snapshot"
-	} else {
-		snapshotModuleType = "sdk_snapshot"
-	}
-	snapshotModule := bpFile.newModule(snapshotModuleType)
-	snapshotModule.AddProperty("name", snapshotName)
-
-	// Make sure that the snapshot has the same visibility as the sdk.
-	visibility := android.EffectiveVisibilityRules(ctx, s).Strings()
-	if len(visibility) != 0 {
-		snapshotModule.AddProperty("visibility", visibility)
-	}
-
-	addHostDeviceSupportedProperties(s.ModuleBase.DeviceSupported(), s.ModuleBase.HostSupported(), snapshotModule)
-
-	combinedPropertiesList := s.collateSnapshotModuleInfo(ctx, sdkVariants, memberVariantDeps)
-	commonCombinedProperties := s.optimizeSnapshotModuleProperties(ctx, combinedPropertiesList)
-
-	s.addSnapshotPropertiesToPropertySet(builder, snapshotModule, commonCombinedProperties)
-
-	targetPropertySet := snapshotModule.AddPropertySet("target")
-
-	// Create a mapping from osType to combined properties.
-	osTypeToCombinedProperties := map[android.OsType]*combinedSnapshotModuleProperties{}
-	for _, combined := range combinedPropertiesList {
-		osTypeToCombinedProperties[combined.sdkVariant.Os()] = combined
-	}
-
-	// Iterate over the os types in a fixed order.
-	for _, osType := range s.getPossibleOsTypes() {
-		if combined, ok := osTypeToCombinedProperties[osType]; ok {
-			osPropertySet := targetPropertySet.AddPropertySet(osType.Name)
-
-			s.addSnapshotPropertiesToPropertySet(builder, osPropertySet, combined)
-		}
-	}
-
-	// If host is supported and any member is host OS dependent then disable host
-	// by default, so that we can enable each host OS variant explicitly. This
-	// avoids problems with implicitly enabled OS variants when the snapshot is
-	// used, which might be different from this run (e.g. different build OS).
-	if s.HostSupported() {
-		var supportedHostTargets []string
-		for _, memberVariantDep := range memberVariantDeps {
-			if memberVariantDep.memberType.IsHostOsDependent() && memberVariantDep.variant.Target().Os.Class == android.Host {
-				targetString := memberVariantDep.variant.Target().Os.String() + "_" + memberVariantDep.variant.Target().Arch.ArchType.String()
-				if !android.InList(targetString, supportedHostTargets) {
-					supportedHostTargets = append(supportedHostTargets, targetString)
-				}
-			}
-		}
-		if len(supportedHostTargets) > 0 {
-			hostPropertySet := targetPropertySet.AddPropertySet("host")
-			hostPropertySet.AddProperty("enabled", false)
-		}
-		// Enable the <os>_<arch> variant explicitly when we've disabled it by default on host.
-		for _, hostTarget := range supportedHostTargets {
-			propertySet := targetPropertySet.AddPropertySet(hostTarget)
-			propertySet.AddProperty("enabled", true)
-		}
-	}
-
-	// Prune any empty property sets.
-	snapshotModule.transform(pruneEmptySetTransformer{})
-
-	bpFile.AddModule(snapshotModule)
-}
-
 // Check the syntax of the generated Android.bp file contents and if they are
 // invalid then log an error with the contents (tagged with line numbers) and the
 // errors that were found so that it is easy to see where the problem lies.
@@ -944,95 +817,34 @@
 	}
 }
 
-func (s *sdk) addSnapshotPropertiesToPropertySet(builder *snapshotBuilder, propertySet android.BpPropertySet, combined *combinedSnapshotModuleProperties) {
-	staticProperties := combined.staticProperties
-	multilib := staticProperties.Compile_multilib
-	if multilib != "" && multilib != "both" {
-		// Compile_multilib defaults to both so only needs to be set when it's specified and not both.
-		propertySet.AddProperty("compile_multilib", multilib)
-	}
-
-	dynamicMemberTypeListProperties := combined.dynamicProperties
-	for _, memberListProperty := range s.memberTypeListProperties() {
-		if memberListProperty.getter == nil {
-			continue
-		}
-		if !isMemberTypeSupportedByTargetBuildRelease(memberListProperty.memberType, builder.targetBuildRelease) {
-			continue
-		}
-		names := memberListProperty.getter(dynamicMemberTypeListProperties)
-		if len(names) > 0 {
-			propertySet.AddProperty(memberListProperty.propertyName(), builder.versionedSdkMemberNames(names, false))
-		}
-	}
-}
-
 type propertyTag struct {
 	name string
 }
 
 var _ android.BpPropertyTag = propertyTag{}
 
-// A BpPropertyTag to add to a property that contains references to other sdk members.
+// BpPropertyTag instances to add to a property that contains references to other sdk members.
 //
-// This will cause the references to be rewritten to a versioned reference in the version
-// specific instance of a snapshot module.
+// These will ensure that the referenced modules are available, if required.
 var requiredSdkMemberReferencePropertyTag = propertyTag{"requiredSdkMemberReferencePropertyTag"}
 var optionalSdkMemberReferencePropertyTag = propertyTag{"optionalSdkMemberReferencePropertyTag"}
 
-// A BpPropertyTag that indicates the property should only be present in the versioned
-// module.
-//
-// This will cause the property to be removed from the unversioned instance of a
-// snapshot module.
-var sdkVersionedOnlyPropertyTag = propertyTag{"sdkVersionedOnlyPropertyTag"}
-
-type unversionedToVersionedTransformation struct {
+type snapshotTransformation struct {
 	identityTransformation
 	builder *snapshotBuilder
 }
 
-func (t unversionedToVersionedTransformation) transformModule(module *bpModule) *bpModule {
-	// Use a versioned name for the module but remember the original name for the
-	// snapshot.
-	name := module.Name()
-	module.setProperty("name", t.builder.versionedSdkMemberName(name, true))
-	module.insertAfter("name", "sdk_member_name", name)
-	// Remove the prefer property if present as versioned modules never need marking with prefer.
-	module.removeProperty("prefer")
-	// Ditto for use_source_config_var
-	module.removeProperty("use_source_config_var")
-	return module
-}
-
-func (t unversionedToVersionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
-	if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
-		required := tag == requiredSdkMemberReferencePropertyTag
-		return t.builder.versionedSdkMemberNames(value.([]string), required), tag
-	} else {
-		return value, tag
-	}
-}
-
-type unversionedTransformation struct {
-	identityTransformation
-	builder *snapshotBuilder
-}
-
-func (t unversionedTransformation) transformModule(module *bpModule) *bpModule {
+func (t snapshotTransformation) transformModule(module *bpModule) *bpModule {
 	// If the module is an internal member then use a unique name for it.
 	name := module.Name()
-	module.setProperty("name", t.builder.unversionedSdkMemberName(name, true))
+	module.setProperty("name", t.builder.snapshotSdkMemberName(name, true))
 	return module
 }
 
-func (t unversionedTransformation) transformProperty(name string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
+func (t snapshotTransformation) transformProperty(_ string, value interface{}, tag android.BpPropertyTag) (interface{}, android.BpPropertyTag) {
 	if tag == requiredSdkMemberReferencePropertyTag || tag == optionalSdkMemberReferencePropertyTag {
 		required := tag == requiredSdkMemberReferencePropertyTag
-		return t.builder.unversionedSdkMemberNames(value.([]string), required), tag
-	} else if tag == sdkVersionedOnlyPropertyTag {
-		// The property is not allowed in the unversioned module so remove it.
-		return nil, nil
+		return t.builder.snapshotSdkMemberNames(value.([]string), required), tag
 	} else {
 		return value, tag
 	}
@@ -1044,7 +856,7 @@
 
 var _ bpTransformer = (*pruneEmptySetTransformer)(nil)
 
-func (t pruneEmptySetTransformer) transformPropertySetAfterContents(name string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
+func (t pruneEmptySetTransformer) transformPropertySetAfterContents(_ string, propertySet *bpPropertySet, tag android.BpPropertyTag) (*bpPropertySet, android.BpPropertyTag) {
 	if len(propertySet.properties) == 0 {
 		return nil, nil
 	} else {
@@ -1053,20 +865,12 @@
 }
 
 func generateBpContents(contents *generatedContents, bpFile *bpFile) {
-	generateFilteredBpContents(contents, bpFile, func(*bpModule) bool {
-		return true
-	})
-}
-
-func generateFilteredBpContents(contents *generatedContents, bpFile *bpFile, moduleFilter func(module *bpModule) bool) {
 	contents.IndentedPrintf("// This is auto-generated. DO NOT EDIT.\n")
 	for _, bpModule := range bpFile.order {
-		if moduleFilter(bpModule) {
-			contents.IndentedPrintf("\n")
-			contents.IndentedPrintf("%s {\n", bpModule.moduleType)
-			outputPropertySet(contents, bpModule.bpPropertySet)
-			contents.IndentedPrintf("}\n")
-		}
+		contents.IndentedPrintf("\n")
+		contents.IndentedPrintf("%s {\n", bpModule.moduleType)
+		outputPropertySet(contents, bpModule.bpPropertySet)
+		contents.IndentedPrintf("}\n")
 	}
 }
 
@@ -1202,36 +1006,10 @@
 	return s.builderForTests.infoContents
 }
 
-func (s *sdk) GetUnversionedAndroidBpContentsForTests() string {
-	contents := &generatedContents{}
-	generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool {
-		name := module.Name()
-		// Include modules that are either unversioned or have no name.
-		return !strings.Contains(name, "@")
-	})
-	return contents.content.String()
-}
-
-func (s *sdk) GetVersionedAndroidBpContentsForTests() string {
-	contents := &generatedContents{}
-	generateFilteredBpContents(contents, s.builderForTests.bpFile, func(module *bpModule) bool {
-		name := module.Name()
-		// Include modules that are either versioned or have no name.
-		return name == "" || strings.Contains(name, "@")
-	})
-	return contents.content.String()
-}
-
 type snapshotBuilder struct {
 	ctx android.ModuleContext
 	sdk *sdk
 
-	// The version of the generated snapshot.
-	//
-	// See the documentation of SOONG_SDK_SNAPSHOT_VERSION above for details of the valid values of
-	// this field.
-	version string
-
 	snapshotDir android.OutputPath
 	bpFile      *bpFile
 
@@ -1385,13 +1163,6 @@
 
 	addHostDeviceSupportedProperties(deviceSupported, hostSupported, m)
 
-	// Disable installation in the versioned module of those modules that are ever installable.
-	if installable, ok := variant.(interface{ EverInstallable() bool }); ok {
-		if installable.EverInstallable() {
-			m.AddPropertyWithTag("installable", false, sdkVersionedOnlyPropertyTag)
-		}
-	}
-
 	s.prebuiltModules[name] = m
 	s.prebuiltOrder = append(s.prebuiltOrder, m)
 	return m
@@ -1424,45 +1195,28 @@
 	return optionalSdkMemberReferencePropertyTag
 }
 
-// Get a versioned name appropriate for the SDK snapshot version being taken.
-func (s *snapshotBuilder) versionedSdkMemberName(unversionedName string, required bool) string {
-	if _, ok := s.allMembersByName[unversionedName]; !ok {
+// Get a name for sdk snapshot member. If the member is private then generate a snapshot specific
+// name. As part of the processing this checks to make sure that any required members are part of
+// the snapshot.
+func (s *snapshotBuilder) snapshotSdkMemberName(name string, required bool) string {
+	if _, ok := s.allMembersByName[name]; !ok {
 		if required {
-			s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
+			s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", name)
 		}
-		return unversionedName
-	}
-	return versionedSdkMemberName(s.ctx, unversionedName, s.version)
-}
-
-func (s *snapshotBuilder) versionedSdkMemberNames(members []string, required bool) []string {
-	var references []string = nil
-	for _, m := range members {
-		references = append(references, s.versionedSdkMemberName(m, required))
-	}
-	return references
-}
-
-// Get an internal name unique to the sdk.
-func (s *snapshotBuilder) unversionedSdkMemberName(unversionedName string, required bool) string {
-	if _, ok := s.allMembersByName[unversionedName]; !ok {
-		if required {
-			s.ctx.ModuleErrorf("Required member reference %s is not a member of the sdk", unversionedName)
-		}
-		return unversionedName
+		return name
 	}
 
-	if s.isInternalMember(unversionedName) {
-		return s.ctx.ModuleName() + "_" + unversionedName
+	if s.isInternalMember(name) {
+		return s.ctx.ModuleName() + "_" + name
 	} else {
-		return unversionedName
+		return name
 	}
 }
 
-func (s *snapshotBuilder) unversionedSdkMemberNames(members []string, required bool) []string {
+func (s *snapshotBuilder) snapshotSdkMemberNames(members []string, required bool) []string {
 	var references []string = nil
 	for _, m := range members {
-		references = append(references, s.unversionedSdkMemberName(m, required))
+		references = append(references, s.snapshotSdkMemberName(m, required))
 	}
 	return references
 }