Create scripts to update and freeze a module SDK

`m <sdk_name>` generates two scripts each of which is use to update the
current snapshot of the SDK and to freeze ToT as a new version,
respectively. Executing the scripts will copy necessary files (stub
libraries, AIDL files, etc.) along with Android.bp into the ./<apiver>
directory under the directory where the sdk is defined.

This change also introduces a new module type 'sdk_snapshot' that
represents a snapshot of an SDK. It will be auto-generated by the above
scripts, so developers are not expected to write this manually.

The module type 'sdk' is now used to simply specify the list of modules
that an SDK has.

Finally, this change changes the version separator from '#' to '@'
because '#' confuses Make.

Bug: 138182343
Test: m

Change-Id: Ifcbc3a39a2f6ad5b4f4b200ba55a1ce3281498cf
diff --git a/sdk/sdk.go b/sdk/sdk.go
index fcb3fb7..d122cda 100644
--- a/sdk/sdk.go
+++ b/sdk/sdk.go
@@ -15,6 +15,9 @@
 package sdk
 
 import (
+	"fmt"
+	"strconv"
+
 	"github.com/google/blueprint"
 
 	"android/soong/android"
@@ -25,6 +28,7 @@
 
 func init() {
 	android.RegisterModuleType("sdk", ModuleFactory)
+	android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
 	android.PreDepsMutators(RegisterPreDepsMutators)
 	android.PostDepsMutators(RegisterPostDepsMutators)
 }
@@ -34,12 +38,18 @@
 	android.DefaultableModuleBase
 
 	properties sdkProperties
+
+	updateScript android.OutputPath
+	freezeScript android.OutputPath
 }
 
 type sdkProperties struct {
-	// The list of java_import modules that provide Java stubs for this SDK
-	Java_libs          []string
+	// The list of java libraries in this SDK
+	Java_libs []string
+	// The list of native libraries in this SDK
 	Native_shared_libs []string
+
+	Snapshot bool `blueprint:"mutated"`
 }
 
 // sdk defines an SDK which is a logical group of modules (e.g. native libs, headers, java libs, etc.)
@@ -52,8 +62,44 @@
 	return s
 }
 
+// sdk_snapshot is a versioned snapshot of an SDK. This is an auto-generated module.
+func SnapshotModuleFactory() android.Module {
+	s := ModuleFactory()
+	s.(*sdk).properties.Snapshot = true
+	return s
+}
+
+func (s *sdk) snapshot() bool {
+	return s.properties.Snapshot
+}
+
+func (s *sdk) frozenVersions(ctx android.BaseModuleContext) []string {
+	if s.snapshot() {
+		panic(fmt.Errorf("frozenVersions() called for sdk_snapshot %q", ctx.ModuleName()))
+	}
+	versions := []string{}
+	ctx.WalkDeps(func(child android.Module, parent android.Module) bool {
+		depTag := ctx.OtherModuleDependencyTag(child)
+		if depTag == sdkMemberDepTag {
+			return true
+		}
+		if versionedDepTag, ok := depTag.(sdkMemberVesionedDepTag); ok {
+			v := versionedDepTag.version
+			if v != "current" && !android.InList(v, versions) {
+				versions = append(versions, versionedDepTag.version)
+			}
+		}
+		return false
+	})
+	return android.SortedUniqueStrings(versions)
+}
+
 func (s *sdk) GenerateAndroidBuildActions(ctx android.ModuleContext) {
-	// TODO(jiyong): add build rules for creating stubs from members of this SDK
+	s.buildSnapshotGenerationScripts(ctx)
+}
+
+func (s *sdk) AndroidMkEntries() android.AndroidMkEntries {
+	return s.androidMkEntriesForScript()
 }
 
 // RegisterPreDepsMutators registers pre-deps mutators to support modules implementing SdkAware
@@ -112,8 +158,21 @@
 
 // Step 2: record that dependencies of SDK modules are members of the SDK modules
 func memberDepsMutator(mctx android.TopDownMutatorContext) {
-	if _, ok := mctx.Module().(*sdk); ok {
+	if s, ok := mctx.Module().(*sdk); ok {
 		mySdkRef := android.ParseSdkRef(mctx, mctx.ModuleName(), "name")
+		if s.snapshot() && mySdkRef.Unversioned() {
+			mctx.PropertyErrorf("name", "sdk_snapshot should be named as <name>@<version>. "+
+				"Did you manually modify Android.bp?")
+		}
+		if !s.snapshot() && !mySdkRef.Unversioned() {
+			mctx.PropertyErrorf("name", "sdk shouldn't be named as <name>@<version>.")
+		}
+		if mySdkRef.Version != "" && mySdkRef.Version != "current" {
+			if _, err := strconv.Atoi(mySdkRef.Version); err != nil {
+				mctx.PropertyErrorf("name", "version %q is neither a number nor \"current\"", mySdkRef.Version)
+			}
+		}
+
 		mctx.VisitDirectDeps(func(child android.Module) {
 			if member, ok := child.(android.SdkAware); ok {
 				member.MakeMemberOf(mySdkRef)
@@ -122,7 +181,7 @@
 	}
 }
 
-// Step 3: create dependencies from the in-development version of an SDK member to frozen versions
+// Step 3: create dependencies from the unversioned SDK member to snapshot versions
 // of the same member. By having these dependencies, they are mutated for multiple Mainline modules
 // (apex and apk), each of which might want different sdks to be built with. For example, if both
 // apex A and B are referencing libfoo which is a member of sdk 'mysdk', the two APEXes can be
@@ -130,7 +189,7 @@
 // using.
 func memberInterVersionMutator(mctx android.BottomUpMutatorContext) {
 	if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
-		if !m.ContainingSdk().IsCurrentVersion() {
+		if !m.ContainingSdk().Unversioned() {
 			memberName := m.MemberName()
 			tag := sdkMemberVesionedDepTag{member: memberName, version: m.ContainingSdk().Version}
 			mctx.AddReverseDependency(mctx.Module(), tag, memberName)
@@ -159,7 +218,7 @@
 // versioned module is used instead of the un-versioned (in-development) module libfoo
 func sdkDepsReplaceMutator(mctx android.BottomUpMutatorContext) {
 	if m, ok := mctx.Module().(android.SdkAware); ok && m.IsInAnySdk() {
-		if sdk := m.ContainingSdk(); !sdk.IsCurrentVersion() {
+		if sdk := m.ContainingSdk(); !sdk.Unversioned() {
 			if m.RequiredSdks().Contains(sdk) {
 				// Note that this replacement is done only for the modules that have the same
 				// variations as the current module. Since current module is already mutated for