Merge "Use static build rules in snapshot generation"
diff --git a/sdk/sdk.go b/sdk/sdk.go
index 431ace9..18b0040 100644
--- a/sdk/sdk.go
+++ b/sdk/sdk.go
@@ -31,6 +31,8 @@
 
 func init() {
 	pctx.Import("android/soong/android")
+	pctx.Import("android/soong/java/config")
+
 	android.RegisterModuleType("sdk", ModuleFactory)
 	android.RegisterModuleType("sdk_snapshot", SnapshotModuleFactory)
 	android.PreDepsMutators(RegisterPreDepsMutators)
diff --git a/sdk/sdk_test.go b/sdk/sdk_test.go
index 5435ef6..6c20659 100644
--- a/sdk/sdk_test.go
+++ b/sdk/sdk_test.go
@@ -27,7 +27,7 @@
 	"android/soong/java"
 )
 
-func testSdkContext(t *testing.T, bp string) (*android.TestContext, android.Config) {
+func testSdkContext(bp string) (*android.TestContext, android.Config) {
 	config := android.TestArchConfig(buildDir, nil)
 	ctx := android.NewTestArchContext()
 
@@ -114,7 +114,7 @@
 }
 
 func testSdk(t *testing.T, bp string) (*android.TestContext, android.Config) {
-	ctx, config := testSdkContext(t, bp)
+	ctx, config := testSdkContext(bp)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	android.FailIfErrored(t, errs)
 	_, errs = ctx.PrepareBuildActions(config)
@@ -124,7 +124,7 @@
 
 func testSdkError(t *testing.T, pattern, bp string) {
 	t.Helper()
-	ctx, config := testSdkContext(t, bp)
+	ctx, config := testSdkContext(bp)
 	_, errs := ctx.ParseFileList(".", []string{"Android.bp"})
 	if len(errs) > 0 {
 		android.FailIfNoMatchingErrors(t, pattern, errs)
@@ -147,7 +147,7 @@
 }
 
 func pathsToStrings(paths android.Paths) []string {
-	ret := []string{}
+	var ret []string
 	for _, p := range paths {
 		ret = append(ret, p.String())
 	}
@@ -554,14 +554,23 @@
 	var copySrcs []string
 	var copyDests []string
 	buildParams := sdk.BuildParamsForTests()
-	var zipBp android.BuildParams
+	var mergeZipInputs []string
+	var intermediateZip string
+	var outputZip string
 	for _, bp := range buildParams {
 		ruleString := bp.Rule.String()
-		if ruleString == "android/soong/android.Cp" {
+		if ruleString == android.Cp.String() {
 			copySrcs = append(copySrcs, bp.Input.String())
 			copyDests = append(copyDests, bp.Output.Rel()) // rooted at the snapshot root
-		} else if ruleString == "<local rule>:m.mysdk_android_common.snapshot" {
-			zipBp = bp
+		} else if ruleString == zipFiles.String() {
+			intermediateZip = bp.Output.String()
+		} else if ruleString == mergeZips.String() {
+			input := bp.Input.String()
+			if intermediateZip != input {
+				t.Errorf("Intermediate zip %s is not an input to merge_zips, %s is used instead", intermediateZip, input)
+			}
+			mergeZipInputs = bp.Inputs.Strings()
+			outputZip = bp.Output.String()
 		}
 	}
 
@@ -582,17 +591,14 @@
 	ensureListContains(t, copyDests, "java/myjavalib.jar")
 	ensureListContains(t, copyDests, "arm64/lib/mynativelib.so")
 
+	expectedOutputZip := filepath.Join(buildDir, ".intermediates/mysdk/android_common/mysdk-current.zip")
+	expectedRepackagedZip := filepath.Join(buildDir, ".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip")
+
 	// Ensure that the droidstubs .srcjar as repackaged into a temporary zip file
 	// and then merged together with the intermediate snapshot zip.
-	snapshotCreationInputs := zipBp.Implicits.Strings()
-	ensureListContains(t, snapshotCreationInputs,
-		filepath.Join(buildDir, ".intermediates/mysdk/android_common/tmp/java/myjavaapistubs_stubs_sources.zip"))
-	ensureListContains(t, snapshotCreationInputs,
-		filepath.Join(buildDir, ".intermediates/mysdk/android_common/mysdk-current.unmerged.zip"))
-	actual := zipBp.Output.String()
-	expected := filepath.Join(buildDir, ".intermediates/mysdk/android_common/mysdk-current.zip")
-	if actual != expected {
-		t.Errorf("Expected snapshot output to be %q but was %q", expected, actual)
+	ensureListContains(t, mergeZipInputs, expectedRepackagedZip)
+	if outputZip != expectedOutputZip {
+		t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip)
 	}
 }
 
@@ -749,14 +755,23 @@
 	var copySrcs []string
 	var copyDests []string
 	buildParams := sdk.BuildParamsForTests()
-	var zipBp android.BuildParams
+	var mergeZipInputs []string
+	var intermediateZip string
+	var outputZip string
 	for _, bp := range buildParams {
 		ruleString := bp.Rule.String()
-		if ruleString == "android/soong/android.Cp" {
+		if ruleString == android.Cp.String() {
 			copySrcs = append(copySrcs, bp.Input.String())
 			copyDests = append(copyDests, bp.Output.Rel()) // rooted at the snapshot root
-		} else if ruleString == "<local rule>:m.mysdk_linux_glibc_common.snapshot" {
-			zipBp = bp
+		} else if ruleString == zipFiles.String() {
+			intermediateZip = bp.Output.String()
+		} else if ruleString == mergeZips.String() {
+			input := bp.Input.String()
+			if intermediateZip != input {
+				t.Errorf("Intermediate zip %s is not an input to merge_zips, %s is used instead", intermediateZip, input)
+			}
+			mergeZipInputs = bp.Inputs.Strings()
+			outputZip = bp.Output.String()
 		}
 	}
 
@@ -777,17 +792,14 @@
 	ensureListContains(t, copyDests, "java/myjavalib.jar")
 	ensureListContains(t, copyDests, "x86_64/lib/mynativelib.so")
 
+	expectedOutputZip := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/mysdk-current.zip")
+	expectedRepackagedZip := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip")
+
 	// Ensure that the droidstubs .srcjar as repackaged into a temporary zip file
 	// and then merged together with the intermediate snapshot zip.
-	snapshotCreationInputs := zipBp.Implicits.Strings()
-	ensureListContains(t, snapshotCreationInputs,
-		filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/tmp/java/myjavaapistubs_stubs_sources.zip"))
-	ensureListContains(t, snapshotCreationInputs,
-		filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/mysdk-current.unmerged.zip"))
-	actual := zipBp.Output.String()
-	expected := filepath.Join(buildDir, ".intermediates/mysdk/linux_glibc_common/mysdk-current.zip")
-	if actual != expected {
-		t.Errorf("Expected snapshot output to be %q but was %q", expected, actual)
+	ensureListContains(t, mergeZipInputs, expectedRepackagedZip)
+	if outputZip != expectedOutputZip {
+		t.Errorf("Expected snapshot output to be %q but was %q", expectedOutputZip, outputZip)
 	}
 }
 
@@ -810,7 +822,7 @@
 }
 
 func tearDown() {
-	os.RemoveAll(buildDir)
+	_ = os.RemoveAll(buildDir)
 }
 
 func TestMain(m *testing.M) {
diff --git a/sdk/update.go b/sdk/update.go
index 8159d3b..63bb1b7 100644
--- a/sdk/update.go
+++ b/sdk/update.go
@@ -20,6 +20,7 @@
 	"reflect"
 	"strings"
 
+	"github.com/google/blueprint"
 	"github.com/google/blueprint/proptools"
 
 	"android/soong/android"
@@ -29,6 +30,36 @@
 
 var pctx = android.NewPackageContext("android/soong/sdk")
 
+var (
+	repackageZip = pctx.AndroidStaticRule("SnapshotRepackageZip",
+		blueprint.RuleParams{
+			Command: `${config.Zip2ZipCmd} -i $in -o $out "**/*:$destdir"`,
+			CommandDeps: []string{
+				"${config.Zip2ZipCmd}",
+			},
+		},
+		"destdir")
+
+	zipFiles = pctx.AndroidStaticRule("SnapshotZipFiles",
+		blueprint.RuleParams{
+			Command: `${config.SoongZipCmd} -C $basedir -l $out.rsp -o $out`,
+			CommandDeps: []string{
+				"${config.SoongZipCmd}",
+			},
+			Rspfile:        "$out.rsp",
+			RspfileContent: "$in",
+		},
+		"basedir")
+
+	mergeZips = pctx.AndroidStaticRule("SnapshotMergeZips",
+		blueprint.RuleParams{
+			Command: `${config.MergeZipsCmd} $out $in`,
+			CommandDeps: []string{
+				"${config.MergeZipsCmd}",
+			},
+		})
+)
+
 type generatedContents struct {
 	content     strings.Builder
 	indentLevel int
@@ -316,41 +347,39 @@
 
 	// zip them all
 	outputZipFile := android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.zip").OutputPath
-	outputRuleName := "snapshot"
 	outputDesc := "Building snapshot for " + ctx.ModuleName()
 
 	// If there are no zips to merge then generate the output zip directly.
 	// Otherwise, generate an intermediate zip file into which other zips can be
 	// merged.
 	var zipFile android.OutputPath
-	var ruleName string
 	var desc string
 	if len(builder.zipsToMerge) == 0 {
 		zipFile = outputZipFile
-		ruleName = outputRuleName
 		desc = outputDesc
 	} else {
 		zipFile = android.PathForModuleOut(ctx, ctx.ModuleName()+"-current.unmerged.zip").OutputPath
-		ruleName = "intermediate snapshot"
 		desc = "Building intermediate snapshot for " + ctx.ModuleName()
 	}
 
-	rb := android.NewRuleBuilder()
-	rb.Command().
-		BuiltTool(ctx, "soong_zip").
-		FlagWithArg("-C ", builder.snapshotDir.String()).
-		FlagWithRspFileInputList("-l ", filesToZip).
-		FlagWithOutput("-o ", zipFile)
-	rb.Build(pctx, ctx, ruleName, desc)
+	ctx.Build(pctx, android.BuildParams{
+		Description: desc,
+		Rule:        zipFiles,
+		Inputs:      filesToZip,
+		Output:      zipFile,
+		Args: map[string]string{
+			"basedir": builder.snapshotDir.String(),
+		},
+	})
 
 	if len(builder.zipsToMerge) != 0 {
-		rb := android.NewRuleBuilder()
-		rb.Command().
-			BuiltTool(ctx, "merge_zips").
-			Output(outputZipFile).
-			Input(zipFile).
-			Inputs(builder.zipsToMerge)
-		rb.Build(pctx, ctx, outputRuleName, outputDesc)
+		ctx.Build(pctx, android.BuildParams{
+			Description: outputDesc,
+			Rule:        mergeZips,
+			Input:       zipFile,
+			Inputs:      builder.zipsToMerge,
+			Output:      outputZipFile,
+		})
 	}
 
 	return outputZipFile
@@ -534,14 +563,16 @@
 	// Repackage the zip file so that the entries are in the destDir directory.
 	// This will allow the zip file to be merged into the snapshot.
 	tmpZipPath := android.PathForModuleOut(ctx, "tmp", destDir+".zip").OutputPath
-	rb := android.NewRuleBuilder()
-	rb.Command().
-		BuiltTool(ctx, "zip2zip").
-		FlagWithInput("-i ", zipPath).
-		FlagWithOutput("-o ", tmpZipPath).
-		Flag("**/*:" + destDir)
-	rb.Build(pctx, ctx, "repackaging "+destDir,
-		"Repackaging zip file "+destDir+" for snapshot "+ctx.ModuleName())
+
+	ctx.Build(pctx, android.BuildParams{
+		Description: "Repackaging zip file " + destDir + " for snapshot " + ctx.ModuleName(),
+		Rule:        repackageZip,
+		Input:       zipPath,
+		Output:      tmpZipPath,
+		Args: map[string]string{
+			"destdir": destDir,
+		},
+	})
 
 	// Add the repackaged zip file to the files to merge.
 	s.zipsToMerge = append(s.zipsToMerge, tmpZipPath)