Remove global state on module suffixes from vendor and recovery snapshots

Propgate the Android.mk suffix from source modules into the snapshot so
that it can be used for the prebuilt modules.

Bug: 177098205
Test: vendor_snapshot_test.go
Change-Id: Iea151dc91395f714fbcad1df3a6fd0874e5455d9
diff --git a/cc/snapshot_prebuilt.go b/cc/snapshot_prebuilt.go
index c0b8d5d..ffaed8e 100644
--- a/cc/snapshot_prebuilt.go
+++ b/cc/snapshot_prebuilt.go
@@ -19,12 +19,10 @@
 
 import (
 	"strings"
-	"sync"
 
 	"android/soong/android"
 
 	"github.com/google/blueprint"
-	"github.com/google/blueprint/proptools"
 )
 
 // Defines the specifics of different images to which the snapshot process is applicable, e.g.,
@@ -59,15 +57,6 @@
 	// exclude_from_recovery_snapshot properties.
 	excludeFromSnapshot(m *Module) bool
 
-	// Returns mutex used for mutual exclusion when updating the snapshot maps.
-	getMutex() *sync.Mutex
-
-	// For a given arch, a maps of which modules are included in this image.
-	suffixModules(config android.Config) map[string]bool
-
-	// Whether to add a given module to the suffix map.
-	shouldBeAddedToSuffixModules(m *Module) bool
-
 	// Returns true if the build is using a snapshot for this image.
 	isUsingSnapshot(cfg android.DeviceConfig) bool
 
@@ -131,29 +120,6 @@
 	return m.ExcludeFromVendorSnapshot()
 }
 
-func (vendorSnapshotImage) getMutex() *sync.Mutex {
-	return &vendorSnapshotsLock
-}
-
-func (vendorSnapshotImage) suffixModules(config android.Config) map[string]bool {
-	return vendorSuffixModules(config)
-}
-
-func (vendorSnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
-	// vendor suffix should be added to snapshots if the source module isn't vendor: true.
-	if module.SocSpecific() {
-		return false
-	}
-
-	// But we can't just check SocSpecific() since we already passed the image mutator.
-	// Check ramdisk and recovery to see if we are real "vendor: true" module.
-	ramdiskAvailable := module.InRamdisk() && !module.OnlyInRamdisk()
-	vendorRamdiskAvailable := module.InVendorRamdisk() && !module.OnlyInVendorRamdisk()
-	recoveryAvailable := module.InRecovery() && !module.OnlyInRecovery()
-
-	return !ramdiskAvailable && !recoveryAvailable && !vendorRamdiskAvailable
-}
-
 func (vendorSnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
 	vndkVersion := cfg.VndkVersion()
 	return vndkVersion != "current" && vndkVersion != ""
@@ -219,18 +185,6 @@
 	return m.ExcludeFromRecoverySnapshot()
 }
 
-func (recoverySnapshotImage) getMutex() *sync.Mutex {
-	return &recoverySnapshotsLock
-}
-
-func (recoverySnapshotImage) suffixModules(config android.Config) map[string]bool {
-	return recoverySuffixModules(config)
-}
-
-func (recoverySnapshotImage) shouldBeAddedToSuffixModules(module *Module) bool {
-	return proptools.BoolDefault(module.Properties.Recovery_available, false)
-}
-
 func (recoverySnapshotImage) isUsingSnapshot(cfg android.DeviceConfig) bool {
 	recoverySnapshotVersion := cfg.RecoverySnapshotVersion()
 	return recoverySnapshotVersion != "current" && recoverySnapshotVersion != ""
@@ -269,16 +223,6 @@
 	snapshotObjectSuffix = "_object."
 )
 
-var (
-	vendorSnapshotsLock    sync.Mutex
-	vendorSuffixModulesKey = android.NewOnceKey("vendorSuffixModules")
-)
-
-var (
-	recoverySnapshotsLock    sync.Mutex
-	recoverySuffixModulesKey = android.NewOnceKey("recoverySuffixModules")
-)
-
 type SnapshotProperties struct {
 	Header_libs []string `android:"arch_variant"`
 	Static_libs []string `android:"arch_variant"`
@@ -411,27 +355,6 @@
 	return snapshot
 }
 
-// vendorSuffixModules holds names of modules whose vendor variants should have the vendor suffix.
-// This is determined by source modules, and then this will be used when exporting snapshot modules
-// to Makefile.
-//
-// For example, if libbase has "vendor_available: true", the name of core variant will be "libbase"
-// while the name of vendor variant will be "libbase.vendor". In such cases, the vendor snapshot of
-// "libbase" should be exported with the name "libbase.vendor".
-//
-// Refer to VendorSnapshotSourceMutator and makeLibName which use this.
-func vendorSuffixModules(config android.Config) map[string]bool {
-	return config.Once(vendorSuffixModulesKey, func() interface{} {
-		return make(map[string]bool)
-	}).(map[string]bool)
-}
-
-func recoverySuffixModules(config android.Config) map[string]bool {
-	return config.Once(recoverySuffixModulesKey, func() interface{} {
-		return make(map[string]bool)
-	}).(map[string]bool)
-}
-
 type baseSnapshotDecoratorProperties struct {
 	// snapshot version.
 	Version string
@@ -439,6 +362,9 @@
 	// Target arch name of the snapshot (e.g. 'arm64' for variant 'aosp_arm64')
 	Target_arch string
 
+	// Suffix to be added to the module name when exporting to Android.mk, e.g. ".vendor".
+	Androidmk_suffix string
+
 	// Suffix to be added to the module name, e.g., vendor_shared,
 	// recovery_shared, etc.
 	ModuleSuffix string `blueprint:"mutated"`
@@ -489,6 +415,10 @@
 	return true
 }
 
+func (p *baseSnapshotDecorator) snapshotAndroidMkSuffix() string {
+	return p.baseProperties.Androidmk_suffix
+}
+
 // Call this with a module suffix after creating a snapshot module, such as
 // vendorSnapshotSharedSuffix, recoverySnapshotBinarySuffix, etc.
 func (p *baseSnapshotDecorator) init(m *Module, snapshotSuffix, moduleSuffix string) {
@@ -552,7 +482,6 @@
 		// Library flags for cfi variant.
 		Cfi snapshotLibraryProperties `android:"arch_variant"`
 	}
-	androidMkSuffix string
 }
 
 func (p *snapshotLibraryDecorator) linkerFlags(ctx ModuleContext, flags Flags) Flags {
@@ -575,14 +504,6 @@
 // As snapshots are prebuilts, this just returns the prebuilt binary after doing things which are
 // done by normal library decorator, e.g. exporting flags.
 func (p *snapshotLibraryDecorator) link(ctx ModuleContext, flags Flags, deps PathDeps, objs Objects) android.Path {
-	m := ctx.Module().(*Module)
-
-	if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = vendorSuffix
-	} else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = recoverySuffix
-	}
-
 	if p.header() {
 		return p.libraryDecorator.link(ctx, flags, deps, objs)
 	}
@@ -774,8 +695,7 @@
 type snapshotBinaryDecorator struct {
 	baseSnapshotDecorator
 	*binaryDecorator
-	properties      snapshotBinaryProperties
-	androidMkSuffix string
+	properties snapshotBinaryProperties
 }
 
 func (p *snapshotBinaryDecorator) matchesWithDevice(config android.DeviceConfig) bool {
@@ -799,14 +719,6 @@
 	p.unstrippedOutputFile = in
 	binName := in.Base()
 
-	m := ctx.Module().(*Module)
-	if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = vendorSuffix
-	} else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = recoverySuffix
-
-	}
-
 	// use cpExecutable to make it executable
 	outputFile := android.PathForModuleOut(ctx, binName)
 	ctx.Build(pctx, android.BuildParams{
@@ -876,8 +788,7 @@
 type snapshotObjectLinker struct {
 	baseSnapshotDecorator
 	objectLinker
-	properties      vendorSnapshotObjectProperties
-	androidMkSuffix string
+	properties vendorSnapshotObjectProperties
 }
 
 func (p *snapshotObjectLinker) matchesWithDevice(config android.DeviceConfig) bool {
@@ -897,14 +808,6 @@
 		return nil
 	}
 
-	m := ctx.Module().(*Module)
-
-	if m.InVendor() && vendorSuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = vendorSuffix
-	} else if m.InRecovery() && recoverySuffixModules(ctx.Config())[m.BaseModuleName()] {
-		p.androidMkSuffix = recoverySuffix
-	}
-
 	return android.PathForModuleSrc(ctx, *p.properties.Src)
 }
 
@@ -950,44 +853,12 @@
 
 type snapshotInterface interface {
 	matchesWithDevice(config android.DeviceConfig) bool
+	isSnapshotPrebuilt() bool
+	version() string
+	snapshotAndroidMkSuffix() string
 }
 
 var _ snapshotInterface = (*vndkPrebuiltLibraryDecorator)(nil)
 var _ snapshotInterface = (*snapshotLibraryDecorator)(nil)
 var _ snapshotInterface = (*snapshotBinaryDecorator)(nil)
 var _ snapshotInterface = (*snapshotObjectLinker)(nil)
-
-//
-// Mutators that helps vendor snapshot modules override source modules.
-//
-
-// VendorSnapshotSourceMutator disables source modules which have corresponding snapshots.
-func VendorSnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
-	snapshotSourceMutator(ctx, vendorSnapshotImageSingleton)
-}
-
-func RecoverySnapshotSourceMutator(ctx android.BottomUpMutatorContext) {
-	snapshotSourceMutator(ctx, recoverySnapshotImageSingleton)
-}
-
-func snapshotSourceMutator(ctx android.BottomUpMutatorContext, image snapshotImage) {
-	if !ctx.Device() {
-		return
-	}
-	if !image.isUsingSnapshot(ctx.DeviceConfig()) {
-		return
-	}
-
-	module, ok := ctx.Module().(*Module)
-	if !ok {
-		return
-	}
-
-	if image.shouldBeAddedToSuffixModules(module) {
-		mutex := image.getMutex()
-		mutex.Lock()
-		defer mutex.Unlock()
-
-		image.suffixModules(ctx.Config())[ctx.ModuleName()] = true
-	}
-}