Parameterize the sdk member processing

Extracts the type specific functionality into the SdkMemberType
interface which has to be implemented by each module type that can
be added as a member of the sdk. It provides functionality to add
the required dependencies for the module type, check to see if a
resolved module is the correct instance and build the snapshot.

The latter was previously part of SdkAware but was moved because
it has to be able to process multiple SdkAware variants so delegating
it to a single instance did not make sense.

The custom code for handling each member type specific property,
e.g. java_libs, has been replaced with common code that processes
a list of sdkMemberListProperty struct which associates the
property (name and getter) with the SdkMemberType and a special
DependencyTag which is passed to the SdkMemberType when it has to add
dependencies.

The DependencyTag contains a reference to the appropriate
sdkMemberListProperty which allows the resolved dependencies to be
grouped by type.

Previously, the dependency collection methods would ignore a module if
it was an unsupported type because they did not have a way of
determining which property it was initially listed in. That meant it
was possible to add say a droidstubs module to the java_libs property
(and because they had the same variants) it would work as if it was
added to the stubs_sources property. Or alternatively, a module of an
unsupported type could be added to any property and it would just be
ignored.

However, the DependencyTag provides information about which property
a resolved module was referenced in and so it can detect when the
resolved module is of the wrong type and report an error. That check
identified a bug in one of the tests where the sdk referenced a
java_import module (which is not allowed in an sdk) instead of a
java_library module (which is allowed). That test was fixed as part
of this.

A list of sdkMemberListProperty structs defines the member properties
supported by the sdk and are processed in order to ensure consistent
behaviour.

The resolved dependencies are grouped by type and each group is then
processed in defined order. Within each type dependencies are grouped
by name and encapsulated behind an SdkMember interface which includes
the name and the list of variants.

The Droidstubs and java.Library types can only support one variant and
will fail if given more.

The processing for the native_shared_libs property has been moved into
the cc/library.go file so the sdk package code should now have no type
specific information in it apart from what is if the list of
sdkMemberListProperty structs.

Bug: 143678475
Test: m conscrypt-module-sdk
Change-Id: I10203594d33dbf53441f655aff124f9ab3538d87
diff --git a/sdk/sdk.go b/sdk/sdk.go
index 18b0040..80dd088 100644
--- a/sdk/sdk.go
+++ b/sdk/sdk.go
@@ -27,6 +27,7 @@
 	// registered before mutators in this package. See RegisterPostDepsMutators for more details.
 	_ "android/soong/apex"
 	"android/soong/cc"
+	"android/soong/java"
 )
 
 func init() {
@@ -37,6 +38,14 @@
 	android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
 	android.PreDepsMutators(RegisterPreDepsMutators)
 	android.PostDepsMutators(RegisterPostDepsMutators)
+
+	// Populate the dependency tags for each member list property.  This needs to
+	// be done here to break an initialization cycle.
+	for _, memberListProperty := range sdkMemberListProperties {
+		memberListProperty.dependencyTag = &sdkMemberDependencyTag{
+			memberListProperty: memberListProperty,
+		}
+	}
 }
 
 type sdk struct {
@@ -62,6 +71,45 @@
 	Snapshot bool `blueprint:"mutated"`
 }
 
+type sdkMemberDependencyTag struct {
+	blueprint.BaseDependencyTag
+	memberListProperty *sdkMemberListProperty
+}
+
+// Contains information about the sdk properties that list sdk members, e.g.
+// Java_libs.
+type sdkMemberListProperty struct {
+	// the name of the property as used in a .bp file
+	name string
+
+	// getter for the list of member names
+	getter func(properties *sdkProperties) []string
+
+	// the type of member referenced in the list
+	memberType android.SdkMemberType
+
+	// the dependency tag used for items in this list.
+	dependencyTag *sdkMemberDependencyTag
+}
+
+var sdkMemberListProperties = []*sdkMemberListProperty{
+	{
+		name:       "java_libs",
+		getter:     func(properties *sdkProperties) []string { return properties.Java_libs },
+		memberType: java.LibrarySdkMemberType,
+	},
+	{
+		name:       "stubs_sources",
+		getter:     func(properties *sdkProperties) []string { return properties.Stubs_sources },
+		memberType: java.DroidStubsSdkMemberType,
+	},
+	{
+		name:       "native_shared_libs",
+		getter:     func(properties *sdkProperties) []string { return properties.Native_shared_libs },
+		memberType: cc.LibrarySdkMemberType,
+	},
+}
+
 // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
 // which Mainline modules like APEX can choose to build with.
 func ModuleFactory() android.Module {
@@ -145,10 +193,6 @@
 	blueprint.BaseDependencyTag
 }
 
-// For dependencies from an SDK module to its members
-// e.g. mysdk -> libfoo and libbar
-var sdkMemberDepTag dependencyTag
-
 // For dependencies from an in-development version of an SDK member to frozen versions of the same member
 // e.g. libfoo -> libfoo.mysdk.11 and libfoo.mysdk.12
 type sdkMemberVesionedDepTag struct {
@@ -160,22 +204,10 @@
 // Step 1: create dependencies from an SDK module to its members.
 func memberMutator(mctx android.BottomUpMutatorContext) {
 	if m, ok := mctx.Module().(*sdk); ok {
-		mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Java_libs...)
-		mctx.AddVariationDependencies(nil, sdkMemberDepTag, m.properties.Stubs_sources...)
-
-		targets := mctx.MultiTargets()
-		for _, target := range targets {
-			for _, lib := range m.properties.Native_shared_libs {
-				name, version := cc.StubsLibNameAndVersion(lib)
-				if version == "" {
-					version = cc.LatestStubsVersionFor(mctx.Config(), name)
-				}
-				mctx.AddFarVariationDependencies(append(target.Variations(), []blueprint.Variation{
-					{Mutator: "image", Variation: android.CoreVariation},
-					{Mutator: "link", Variation: "shared"},
-					{Mutator: "version", Variation: version},
-				}...), sdkMemberDepTag, name)
-			}
+		for _, memberListProperty := range sdkMemberListProperties {
+			names := memberListProperty.getter(&m.properties)
+			tag := memberListProperty.dependencyTag
+			memberListProperty.memberType.AddDependencies(mctx, tag, names)
 		}
 	}
 }
diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go
index 6c20659..2f0e598 100644
--- a/sdk/sdk_test.go
+++ b/sdk/sdk_test.go
@@ -158,7 +158,7 @@
 	ctx, _ := testSdk(t, `
 		sdk {
 			name: "mysdk",
-			java_libs: ["sdkmember"],
+			java_libs: ["myjavalib"],
 		}
 
 		sdk_snapshot {
diff --git a/sdk/update.go b/sdk/update.go
index 63bb1b7..602e0a4 100644
--- a/sdk/update.go
+++ b/sdk/update.go
@@ -16,7 +16,6 @@
 
 import (
 	"fmt"
-	"path/filepath"
 	"reflect"
 	"strings"
 
@@ -24,8 +23,6 @@
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
-	"android/soong/cc"
-	"android/soong/java"
 )
 
 var pctx = android.NewPackageContext("android/soong/sdk")
@@ -105,105 +102,46 @@
 	rb.Build(pctx, ctx, gf.path.Base(), "Build "+gf.path.Base())
 }
 
-func (s *sdk) javaLibs(ctx android.ModuleContext) []android.SdkAware {
-	result := []android.SdkAware{}
+// Collect all the members.
+//
+// The members are first grouped by type and then grouped by name. The order of
+// the types is the order they are referenced in sdkMemberListProperties. The
+// names are in order in which the dependencies were added.
+func collectMembers(ctx android.ModuleContext) []*sdkMember {
+	byType := make(map[android.SdkMemberType][]*sdkMember)
+	byName := make(map[string]*sdkMember)
+
 	ctx.VisitDirectDeps(func(m android.Module) {
-		if j, ok := m.(*java.Library); ok {
-			result = append(result, j)
-		}
-	})
-	return result
-}
+		tag := ctx.OtherModuleDependencyTag(m)
+		if memberTag, ok := tag.(*sdkMemberDependencyTag); ok {
+			memberListProperty := memberTag.memberListProperty
+			memberType := memberListProperty.memberType
 
-func (s *sdk) stubsSources(ctx android.ModuleContext) []android.SdkAware {
-	result := []android.SdkAware{}
-	ctx.VisitDirectDeps(func(m android.Module) {
-		if j, ok := m.(*java.Droidstubs); ok {
-			result = append(result, j)
-		}
-	})
-	return result
-}
-
-// archSpecificNativeLibInfo represents an arch-specific variant of a native lib
-type archSpecificNativeLibInfo struct {
-	name                      string
-	archType                  string
-	exportedIncludeDirs       android.Paths
-	exportedSystemIncludeDirs android.Paths
-	exportedFlags             []string
-	exportedDeps              android.Paths
-	outputFile                android.Path
-}
-
-func (lib *archSpecificNativeLibInfo) signature() string {
-	return fmt.Sprintf("%v %v %v %v",
-		lib.name,
-		lib.exportedIncludeDirs.Strings(),
-		lib.exportedSystemIncludeDirs.Strings(),
-		lib.exportedFlags)
-}
-
-// nativeLibInfo represents a collection of arch-specific modules having the same name
-type nativeLibInfo struct {
-	name         string
-	archVariants []archSpecificNativeLibInfo
-	// hasArchSpecificFlags is set to true if modules for each architecture all have the same
-	// include dirs, flags, etc, in which case only those of the first arch is selected.
-	hasArchSpecificFlags bool
-}
-
-// nativeMemberInfos collects all cc.Modules that are member of an SDK.
-func (s *sdk) nativeMemberInfos(ctx android.ModuleContext) []*nativeLibInfo {
-	infoMap := make(map[string]*nativeLibInfo)
-
-	// Collect cc.Modules
-	ctx.VisitDirectDeps(func(m android.Module) {
-		ccModule, ok := m.(*cc.Module)
-		if !ok {
-			return
-		}
-		depName := ctx.OtherModuleName(m)
-
-		if _, ok := infoMap[depName]; !ok {
-			infoMap[depName] = &nativeLibInfo{name: depName}
-		}
-
-		info := infoMap[depName]
-		info.archVariants = append(info.archVariants, archSpecificNativeLibInfo{
-			name:                      ccModule.BaseModuleName(),
-			archType:                  ccModule.Target().Arch.ArchType.String(),
-			exportedIncludeDirs:       ccModule.ExportedIncludeDirs(),
-			exportedSystemIncludeDirs: ccModule.ExportedSystemIncludeDirs(),
-			exportedFlags:             ccModule.ExportedFlags(),
-			exportedDeps:              ccModule.ExportedDeps(),
-			outputFile:                ccModule.OutputFile().Path(),
-		})
-	})
-
-	// Determine if include dirs and flags for each module are different across arch-specific
-	// modules or not. And set hasArchSpecificFlags accordingly
-	for _, info := range infoMap {
-		// by default, include paths and flags are assumed to be the same across arches
-		info.hasArchSpecificFlags = false
-		oldSignature := ""
-		for _, av := range info.archVariants {
-			newSignature := av.signature()
-			if oldSignature == "" {
-				oldSignature = newSignature
+			// Make sure that the resolved module is allowed in the member list property.
+			if !memberType.IsInstance(m) {
+				ctx.ModuleErrorf("module %q is not valid in property %s", ctx.OtherModuleName(m), memberListProperty.name)
 			}
-			if oldSignature != newSignature {
-				info.hasArchSpecificFlags = true
-				break
+
+			name := ctx.OtherModuleName(m)
+
+			member := byName[name]
+			if member == nil {
+				member = &sdkMember{memberType: memberType, name: name}
+				byName[name] = member
+				byType[memberType] = append(byType[memberType], member)
 			}
+
+			member.variants = append(member.variants, m.(android.SdkAware))
 		}
+	})
+
+	var members []*sdkMember
+	for _, memberListProperty := range sdkMemberListProperties {
+		membersOfType := byType[memberListProperty.memberType]
+		members = append(members, membersOfType...)
 	}
 
-	var list []*nativeLibInfo
-	for _, v := range infoMap {
-		list = append(list, v)
-	}
-	return list
+	return members
 }
 
 // SDK directory structure
@@ -224,44 +162,6 @@
 //         <arch>/lib/
 //            libFoo.so   : a stub library
 
-const (
-	nativeIncludeDir          = "include"
-	nativeGeneratedIncludeDir = "include_gen"
-	nativeStubDir             = "lib"
-	nativeStubFileSuffix      = ".so"
-)
-
-// path to the stub file of a native shared library. Relative to <sdk_root>/<api_dir>
-func nativeStubFilePathFor(lib archSpecificNativeLibInfo) string {
-	return filepath.Join(lib.archType,
-		nativeStubDir, lib.name+nativeStubFileSuffix)
-}
-
-// paths to the include dirs of a native shared library. Relative to <sdk_root>/<api_dir>
-func nativeIncludeDirPathsFor(ctx android.ModuleContext, lib archSpecificNativeLibInfo,
-	systemInclude bool, archSpecific bool) []string {
-	var result []string
-	var includeDirs []android.Path
-	if !systemInclude {
-		includeDirs = lib.exportedIncludeDirs
-	} else {
-		includeDirs = lib.exportedSystemIncludeDirs
-	}
-	for _, dir := range includeDirs {
-		var path string
-		if _, gen := dir.(android.WritablePath); gen {
-			path = filepath.Join(nativeGeneratedIncludeDir, lib.name)
-		} else {
-			path = filepath.Join(nativeIncludeDir, dir.String())
-		}
-		if archSpecific {
-			path = filepath.Join(lib.archType, path)
-		}
-		result = append(result, path)
-	}
-	return result
-}
-
 // 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 {
@@ -290,22 +190,8 @@
 	}
 	s.builderForTests = builder
 
-	// copy exported AIDL files and stub jar files
-	javaLibs := s.javaLibs(ctx)
-	for _, m := range javaLibs {
-		m.BuildSnapshot(ctx, builder)
-	}
-
-	// copy stubs sources
-	stubsSources := s.stubsSources(ctx)
-	for _, m := range stubsSources {
-		m.BuildSnapshot(ctx, builder)
-	}
-
-	// copy exported header files and stub *.so files
-	nativeLibInfos := s.nativeMemberInfos(ctx)
-	for _, info := range nativeLibInfos {
-		buildSharedNativeLibSnapshot(ctx, info, builder)
+	for _, member := range collectMembers(ctx) {
+		member.memberType.BuildSnapshot(ctx, builder, member)
 	}
 
 	for _, unversioned := range builder.prebuiltOrder {
@@ -326,14 +212,11 @@
 	snapshotModule := bpFile.newModule("sdk_snapshot")
 	snapshotModule.AddProperty("name", snapshotName)
 	addHostDeviceSupportedProperties(&s.ModuleBase, snapshotModule)
-	if len(s.properties.Java_libs) > 0 {
-		snapshotModule.AddProperty("java_libs", builder.versionedSdkMemberNames(s.properties.Java_libs))
-	}
-	if len(s.properties.Stubs_sources) > 0 {
-		snapshotModule.AddProperty("stubs_sources", builder.versionedSdkMemberNames(s.properties.Stubs_sources))
-	}
-	if len(s.properties.Native_shared_libs) > 0 {
-		snapshotModule.AddProperty("native_shared_libs", builder.versionedSdkMemberNames(s.properties.Native_shared_libs))
+	for _, memberListProperty := range sdkMemberListProperties {
+		names := memberListProperty.getter(&s.properties)
+		if len(names) > 0 {
+			snapshotModule.AddProperty(memberListProperty.name, builder.versionedSdkMemberNames(names))
+		}
 	}
 	bpFile.AddModule(snapshotModule)
 
@@ -442,98 +325,6 @@
 	return contents.content.String()
 }
 
-func buildSharedNativeLibSnapshot(ctx android.ModuleContext, info *nativeLibInfo, builder android.SnapshotBuilder) {
-	// a function for emitting include dirs
-	printExportedDirCopyCommandsForNativeLibs := func(lib archSpecificNativeLibInfo) {
-		includeDirs := lib.exportedIncludeDirs
-		includeDirs = append(includeDirs, lib.exportedSystemIncludeDirs...)
-		if len(includeDirs) == 0 {
-			return
-		}
-		for _, dir := range includeDirs {
-			if _, gen := dir.(android.WritablePath); gen {
-				// generated headers are copied via exportedDeps. See below.
-				continue
-			}
-			targetDir := nativeIncludeDir
-			if info.hasArchSpecificFlags {
-				targetDir = filepath.Join(lib.archType, targetDir)
-			}
-
-			// TODO(jiyong) copy headers having other suffixes
-			headers, _ := ctx.GlobWithDeps(dir.String()+"/**/*.h", nil)
-			for _, file := range headers {
-				src := android.PathForSource(ctx, file)
-				dest := filepath.Join(targetDir, file)
-				builder.CopyToSnapshot(src, dest)
-			}
-		}
-
-		genHeaders := lib.exportedDeps
-		for _, file := range genHeaders {
-			targetDir := nativeGeneratedIncludeDir
-			if info.hasArchSpecificFlags {
-				targetDir = filepath.Join(lib.archType, targetDir)
-			}
-			dest := filepath.Join(targetDir, lib.name, file.Rel())
-			builder.CopyToSnapshot(file, dest)
-		}
-	}
-
-	if !info.hasArchSpecificFlags {
-		printExportedDirCopyCommandsForNativeLibs(info.archVariants[0])
-	}
-
-	// for each architecture
-	for _, av := range info.archVariants {
-		builder.CopyToSnapshot(av.outputFile, nativeStubFilePathFor(av))
-
-		if info.hasArchSpecificFlags {
-			printExportedDirCopyCommandsForNativeLibs(av)
-		}
-	}
-
-	info.generatePrebuiltLibrary(ctx, builder)
-}
-
-func (info *nativeLibInfo) generatePrebuiltLibrary(ctx android.ModuleContext, builder android.SnapshotBuilder) {
-
-	// a function for emitting include dirs
-	addExportedDirsForNativeLibs := func(lib archSpecificNativeLibInfo, properties android.BpPropertySet, systemInclude bool) {
-		includeDirs := nativeIncludeDirPathsFor(ctx, lib, systemInclude, info.hasArchSpecificFlags)
-		if len(includeDirs) == 0 {
-			return
-		}
-		var propertyName string
-		if !systemInclude {
-			propertyName = "export_include_dirs"
-		} else {
-			propertyName = "export_system_include_dirs"
-		}
-		properties.AddProperty(propertyName, includeDirs)
-	}
-
-	pbm := builder.AddPrebuiltModule(info.name, "cc_prebuilt_library_shared")
-
-	if !info.hasArchSpecificFlags {
-		addExportedDirsForNativeLibs(info.archVariants[0], pbm, false /*systemInclude*/)
-		addExportedDirsForNativeLibs(info.archVariants[0], pbm, true /*systemInclude*/)
-	}
-
-	archProperties := pbm.AddPropertySet("arch")
-	for _, av := range info.archVariants {
-		archTypeProperties := archProperties.AddPropertySet(av.archType)
-		archTypeProperties.AddProperty("srcs", []string{nativeStubFilePathFor(av)})
-		if info.hasArchSpecificFlags {
-			// export_* properties are added inside the arch: {<arch>: {...}} block
-			addExportedDirsForNativeLibs(av, archTypeProperties, false /*systemInclude*/)
-			addExportedDirsForNativeLibs(av, archTypeProperties, true /*systemInclude*/)
-		}
-	}
-	pbm.AddProperty("stl", "none")
-	pbm.AddProperty("system_shared_libs", []string{})
-}
-
 type snapshotBuilder struct {
 	ctx         android.ModuleContext
 	sdk         *sdk
@@ -613,3 +404,19 @@
 	}
 	return references
 }
+
+var _ android.SdkMember = (*sdkMember)(nil)
+
+type sdkMember struct {
+	memberType android.SdkMemberType
+	name       string
+	variants   []android.SdkAware
+}
+
+func (m *sdkMember) Name() string {
+	return m.name
+}
+
+func (m *sdkMember) Variants() []android.SdkAware {
+	return m.variants
+}