Move proto compilation to RuleBuilder

Using blueprint.Rule for protoc commands was causing code duplication
because there was no good way to run the same protoc for cc, java and
python but then run custom source packaging steps for java and python.
Move most of the code into a common function that returns a
RuleBuilder, and then let java and python add their own commands at
the end of the rule.

Bug: 70706119
Test: All Soong tests
Test: m checkbuild
Change-Id: Ic692136775d273bcc4f4de99620ab4878667c83a
diff --git a/cc/proto.go b/cc/proto.go
index ce8a30e..5a96ae7 100644
--- a/cc/proto.go
+++ b/cc/proto.go
@@ -15,47 +15,26 @@
 package cc
 
 import (
-	"strings"
-
-	"github.com/google/blueprint"
 	"github.com/google/blueprint/pathtools"
 
 	"android/soong/android"
 )
 
-func init() {
-	pctx.HostBinToolVariable("protocCmd", "aprotoc")
-	pctx.HostBinToolVariable("depFixCmd", "dep_fixer")
-}
-
-var (
-	proto = pctx.AndroidStaticRule("protoc",
-		blueprint.RuleParams{
-			Command: "$protocCmd $protoOut=$protoOutParams:$outDir --dependency_out=$out.d -I $protoBase $protoFlags $in && " +
-				`$depFixCmd $out.d`,
-			CommandDeps: []string{"$protocCmd", "$depFixCmd"},
-			Depfile:     "${out}.d",
-			Deps:        blueprint.DepsGCC,
-		}, "protoFlags", "protoOut", "protoOutParams", "protoBase", "outDir")
-)
-
 // genProto creates a rule to convert a .proto file to generated .pb.cc and .pb.h files and returns
 // the paths to the generated files.
-func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (ccFile, headerFile android.WritablePath) {
+func genProto(ctx android.ModuleContext, protoFile android.Path, flags builderFlags) (cc, header android.WritablePath) {
+	var ccFile, headerFile android.ModuleGenPath
 
 	srcSuffix := ".cc"
 	if flags.protoC {
 		srcSuffix = ".c"
 	}
 
-	var protoBase string
-	if flags.protoRoot {
-		protoBase = "."
+	if flags.proto.CanonicalPathFromRoot {
 		ccFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb"+srcSuffix)
 		headerFile = android.GenPathWithExt(ctx, "proto", protoFile, "pb.h")
 	} else {
 		rel := protoFile.Rel()
-		protoBase = strings.TrimSuffix(protoFile.String(), rel)
 		ccFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb"+srcSuffix))
 		headerFile = android.PathForModuleGen(ctx, "proto", pathtools.ReplaceExtension(rel, "pb.h"))
 	}
@@ -63,27 +42,19 @@
 	protoDeps := flags.protoDeps
 	if flags.protoOptionsFile {
 		optionsFile := pathtools.ReplaceExtension(protoFile.String(), "options")
-		optionsPath := android.ExistentPathForSource(ctx, optionsFile)
-		if optionsPath.Valid() {
-			protoDeps = append(android.Paths{optionsPath.Path()}, protoDeps...)
-		}
+		optionsPath := android.PathForSource(ctx, optionsFile)
+		protoDeps = append(android.Paths{optionsPath}, protoDeps...)
 	}
 
-	ctx.Build(pctx, android.BuildParams{
-		Rule:           proto,
-		Description:    "protoc " + protoFile.Rel(),
-		Output:         ccFile,
-		ImplicitOutput: headerFile,
-		Input:          protoFile,
-		Implicits:      protoDeps,
-		Args: map[string]string{
-			"outDir":         android.ProtoDir(ctx).String(),
-			"protoFlags":     flags.protoFlags,
-			"protoOut":       flags.protoOutTypeFlag,
-			"protoOutParams": flags.protoOutParams,
-			"protoBase":      protoBase,
-		},
-	})
+	outDir := flags.proto.Dir
+	depFile := ccFile.ReplaceExtension(ctx, "d")
+	outputs := android.WritablePaths{ccFile, headerFile}
+
+	rule := android.NewRuleBuilder()
+
+	android.ProtoRule(ctx, rule, protoFile, flags.proto, protoDeps, outDir, depFile, outputs)
+
+	rule.Build(pctx, ctx, "protoc_"+protoFile.Rel(), "protoc "+protoFile.Rel())
 
 	return ccFile, headerFile
 }
@@ -143,13 +114,11 @@
 func protoFlags(ctx ModuleContext, flags Flags, p *android.ProtoProperties) Flags {
 	flags.CFlags = append(flags.CFlags, "-DGOOGLE_PROTOBUF_NO_RTTI")
 
-	flags.ProtoRoot = android.ProtoCanonicalPathFromRoot(ctx, p)
-	if flags.ProtoRoot {
-		flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoSubDir(ctx).String())
+	flags.proto = android.GetProtoFlags(ctx, p)
+	if flags.proto.CanonicalPathFromRoot {
+		flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.SubDir.String())
 	}
-	flags.GlobalFlags = append(flags.GlobalFlags, "-I"+android.ProtoDir(ctx).String())
-
-	flags.protoFlags = android.ProtoFlags(ctx, p)
+	flags.GlobalFlags = append(flags.GlobalFlags, "-I"+flags.proto.Dir.String())
 
 	var plugin string
 
@@ -157,18 +126,18 @@
 	case "nanopb-c", "nanopb-c-enable_malloc", "nanopb-c-16bit", "nanopb-c-enable_malloc-16bit", "nanopb-c-32bit", "nanopb-c-enable_malloc-32bit":
 		flags.protoC = true
 		flags.protoOptionsFile = true
-		flags.protoOutTypeFlag = "--nanopb_out"
+		flags.proto.OutTypeFlag = "--nanopb_out"
 		plugin = "protoc-gen-nanopb"
 	case "full":
-		flags.protoOutTypeFlag = "--cpp_out"
+		flags.proto.OutTypeFlag = "--cpp_out"
 	case "lite":
-		flags.protoOutTypeFlag = "--cpp_out"
-		flags.protoOutParams = append(flags.protoOutParams, "lite")
+		flags.proto.OutTypeFlag = "--cpp_out"
+		flags.proto.OutParams = append(flags.proto.OutParams, "lite")
 	case "":
 		// TODO(b/119714316): this should be equivalent to "lite" in
 		// order to match protoDeps, but some modules are depending on
 		// this behavior
-		flags.protoOutTypeFlag = "--cpp_out"
+		flags.proto.OutTypeFlag = "--cpp_out"
 	default:
 		ctx.PropertyErrorf("proto.type", "unknown proto type %q",
 			String(p.Proto.Type))
@@ -177,7 +146,7 @@
 	if plugin != "" {
 		path := ctx.Config().HostToolPath(ctx, plugin)
 		flags.protoDeps = append(flags.protoDeps, path)
-		flags.protoFlags = append(flags.protoFlags, "--plugin="+path.String())
+		flags.proto.Flags = append(flags.proto.Flags, "--plugin="+path.String())
 	}
 
 	return flags