Convert sdk package to ModuleProxy

sdk.GenerateAndroidBuildActions was collecting dependency info in
collectMembers in the OSType-specific variants, and then retrieving
it from the CommonOS variant.  collectMembers is already using
WalkDepsProxy, change it to be run in the CommonOS variant and then
walk into the OSType-specific variants one at a time, collecting
the necessary info directly into the CommonOS variant where it will
be used.

Bug: 377723687
Test: sdk_test.go
Change-Id: I8002e649d1ca4949ee6a64c337c9e3c0c2e2d72a
diff --git a/android/arch.go b/android/arch.go
index 397de06..318ab45 100644
--- a/android/arch.go
+++ b/android/arch.go
@@ -556,13 +556,11 @@
 // module referenced in the supplied context. An empty list is returned if there
 // are no enabled variants or the supplied context is not for an CommonOS
 // variant.
-func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []Module {
-	var variants []Module
-	mctx.VisitDirectDeps(func(m Module) {
+func GetOsSpecificVariantsOfCommonOSVariant(mctx BaseModuleContext) []ModuleProxy {
+	var variants []ModuleProxy
+	mctx.VisitDirectDepsProxy(func(m ModuleProxy) {
 		if mctx.OtherModuleDependencyTag(m) == commonOsToOsSpecificVariantTag {
-			if m.Enabled(mctx) {
-				variants = append(variants, m)
-			}
+			variants = append(variants, m)
 		}
 	})
 	return variants
diff --git a/sdk/sdk.go b/sdk/sdk.go
index 46f7d27..11dbd2a 100644
--- a/sdk/sdk.go
+++ b/sdk/sdk.go
@@ -57,18 +57,6 @@
 	// list properties.
 	dynamicMemberTraitListProperties interface{}
 
-	// Information about the OsType specific member variants depended upon by this variant.
-	//
-	// Set by OsType specific variants in the collectMembers() method and used by the
-	// CommonOS variant when building the snapshot. That work is all done on separate
-	// calls to the sdk.GenerateAndroidBuildActions method which is guaranteed to be
-	// called for the OsType specific variants before the CommonOS variant (because
-	// the latter depends on the former).
-	memberVariantDeps []sdkMemberVariantDep
-
-	// The multilib variants that are used by this sdk variant.
-	multilibUsages multilibUsage
-
 	properties sdkProperties
 
 	snapshotFile android.OptionalPath
@@ -162,30 +150,33 @@
 	// This method is guaranteed to be called on OsType specific variants before it is called
 	// on their corresponding CommonOS variant.
 	if !s.IsCommonOSVariant() {
-		// Update the OsType specific sdk variant with information about its members.
-		s.collectMembers(ctx)
+		if s.snapshotFile.Valid() || s.infoFile.Valid() {
+			panic(fmt.Errorf("Snapshot (%q) and info file (%q) should not be set for sdk CommonOSVariant.",
+				s.snapshotFile, s.infoFile))
+		}
 	} else {
 		// Get the OsType specific variants on which the CommonOS depends.
 		osSpecificVariants := android.GetOsSpecificVariantsOfCommonOSVariant(ctx)
-		var sdkVariants []*sdk
+		var memberVariantDeps []sdkMemberVariantDep
 		for _, m := range osSpecificVariants {
-			if sdkVariant, ok := m.(*sdk); ok {
-				sdkVariants = append(sdkVariants, sdkVariant)
-			}
+			// Update the OsType specific sdk variant with information about its members.
+			memberVariantDeps = append(memberVariantDeps, s.collectMembers(ctx, m)...)
 		}
 
 		// Generate the snapshot from the member info.
-		s.buildSnapshot(ctx, sdkVariants)
-	}
+		s.buildSnapshot(ctx, memberVariantDeps)
 
-	if s.snapshotFile.Valid() != s.infoFile.Valid() {
-		panic(fmt.Sprintf("Snapshot (%q) and info file (%q) should both be set or neither should be set.", s.snapshotFile, s.infoFile))
-	}
+		if !s.snapshotFile.Valid() {
+			panic(fmt.Errorf("snapshotFile should be set."))
+		}
+		if !s.infoFile.Valid() {
+			panic(fmt.Errorf("infoFile should be set"))
+		}
 
-	if s.snapshotFile.Valid() {
 		ctx.SetOutputFiles([]android.Path{s.snapshotFile.Path()}, "")
 		ctx.SetOutputFiles([]android.Path{s.snapshotFile.Path(), s.infoFile.Path()}, android.DefaultDistTag)
 	}
+
 	moduleInfoJSON := ctx.ModuleInfoJSON()
 	moduleInfoJSON.Class = []string{"FAKE"}
 	moduleInfoJSON.SystemSharedLibs = []string{"none"}
diff --git a/sdk/update.go b/sdk/update.go
index 6845982..82a49d4 100644
--- a/sdk/update.go
+++ b/sdk/update.go
@@ -111,11 +111,16 @@
 
 // Collect all the members.
 //
-// Updates the sdk module with a list of sdkMemberVariantDep instances and details as to which
-// multilibs (32/64/both) are used by this sdk variant.
-func (s *sdk) collectMembers(ctx android.ModuleContext) {
-	s.multilibUsages = multilibNone
+// Returns a list of sdkMemberVariantDep instances for the given OSType-specific variant of the SDK.
+func (s *sdk) collectMembers(ctx android.ModuleContext, osSpecificSdk android.ModuleProxy) []sdkMemberVariantDep {
+	var memberVariantDeps []sdkMemberVariantDep
 	ctx.WalkDepsProxy(func(child, parent android.ModuleProxy) bool {
+		if parent == ctx.ModuleProxy() {
+			// Only recurse into the requested os-specific variant of this common OS sdk module.
+			return child == osSpecificSdk
+		}
+
+		// This is a subdependency of the requested OSType-specific variant
 		tag := ctx.OtherModuleDependencyTag(child)
 		if memberTag, ok := tag.(android.SdkMemberDependencyTag); ok {
 			memberType := memberTag.SdkMemberType(ctx, child)
@@ -132,7 +137,6 @@
 
 			// Keep track of which multilib variants are used by the sdk.
 			commonInfo := android.OtherModulePointerProviderOrDefault(ctx, child, android.CommonModuleInfoProvider)
-			s.multilibUsages = s.multilibUsages.addArchType(commonInfo.Target.Arch.ArchType)
 
 			exportedComponentsInfo, _ := android.OtherModuleProvider(ctx, child, android.ExportedComponentsInfoProvider)
 
@@ -146,11 +150,11 @@
 				export:                 export,
 				exportedComponentsInfo: exportedComponentsInfo,
 			}
-			if !android.EqualModules(parent, ctx.Module()) {
+			if !android.EqualModules(parent, osSpecificSdk) {
 				container := parent
 				vd.container = &container
 			}
-			s.memberVariantDeps = append(s.memberVariantDeps, vd)
+			memberVariantDeps = append(memberVariantDeps, vd)
 
 			// Recurse down into the member's dependencies as it may have dependencies that need to be
 			// automatically added to the sdk.
@@ -159,6 +163,8 @@
 
 		return false
 	})
+
+	return memberVariantDeps
 }
 
 // A denylist of modules whose host variants will be removed from the generated snapshots above the ApiLevel
@@ -299,7 +305,7 @@
 
 // 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) {
+func (s *sdk) buildSnapshot(ctx android.ModuleContext, memberVariantDeps []sdkMemberVariantDep) {
 
 	targetBuildRelease := s.targetBuildRelease(ctx)
 	targetApiLevel, err := android.ApiLevelFromUser(ctx, targetBuildRelease.name)
@@ -309,10 +315,6 @@
 
 	// Aggregate all the sdkMemberVariantDep instances from all the sdk variants.
 	hasLicenses := false
-	var memberVariantDeps []sdkMemberVariantDep
-	for _, sdkVariant := range sdkVariants {
-		memberVariantDeps = append(memberVariantDeps, sdkVariant.memberVariantDeps...)
-	}
 
 	// Filter out any sdkMemberVariantDep that is a component of another.
 	memberVariantDeps = filterOutComponents(ctx, memberVariantDeps)