Add Code Metadata rule to soong/testing.

This Cl adds a new rule to Soong to generate code ownership metadata. Also, this CL adds a provider in the Java SDK library to provide generated source files to the Code_metadata rule. Will add providers to other libraries in the future changes.

Bug: 296873595
Change-Id: Ic2e43aa9b161231fea4416d1f0d36b778361d7c5
diff --git a/testing/all_code_metadata.go b/testing/all_code_metadata.go
new file mode 100644
index 0000000..16d7aae
--- /dev/null
+++ b/testing/all_code_metadata.go
@@ -0,0 +1,51 @@
+package testing
+
+import (
+	"android/soong/android"
+)
+
+const fileContainingCodeMetadataFilePaths = "all_code_metadata_paths.rsp"
+const allCodeMetadataFile = "all_code_metadata.pb"
+
+func AllCodeMetadataFactory() android.Singleton {
+	return &allCodeMetadataSingleton{}
+}
+
+type allCodeMetadataSingleton struct {
+	// Path where the collected metadata is stored after successful validation.
+	outputPath android.OutputPath
+}
+
+func (this *allCodeMetadataSingleton) GenerateBuildActions(ctx android.SingletonContext) {
+	var intermediateMetadataPaths android.Paths
+
+	ctx.VisitAllModules(
+		func(module android.Module) {
+			if !ctx.ModuleHasProvider(module, CodeMetadataProviderKey) {
+				return
+			}
+			intermediateMetadataPaths = append(
+				intermediateMetadataPaths, ctx.ModuleProvider(
+					module, CodeMetadataProviderKey,
+				).(CodeMetadataProviderData).IntermediatePath,
+			)
+		},
+	)
+
+	rspFile := android.PathForOutput(ctx, fileContainingCodeMetadataFilePaths)
+	this.outputPath = android.PathForOutput(ctx, ownershipDirectory, allCodeMetadataFile)
+
+	rule := android.NewRuleBuilder(pctx, ctx)
+	cmd := rule.Command().
+		BuiltTool("metadata").
+		FlagWithArg("-rule ", "code_metadata").
+		FlagWithRspFileInputList("-inputFile ", rspFile, intermediateMetadataPaths)
+	cmd.FlagWithOutput("-outputFile ", this.outputPath)
+	rule.Build("all_code_metadata_rule", "Generate all code metadata")
+
+	ctx.Phony("all_code_metadata", this.outputPath)
+}
+
+func (this *allCodeMetadataSingleton) MakeVars(ctx android.MakeVarsContext) {
+	ctx.DistForGoal("code_metadata", this.outputPath)
+}