[build] Support gRPC golang bindings in proto_library().

This CL adds a new parameter named 'generate_go_grpc' to the
'proto_library()' template, which is enabled to indicate that a
gRPC go stub, instead of a regular one, should be generated by
the template call.

This will call 'protoc-gen-go-grpc' instead of 'protoc-gen-go'
and will add a suffix of '_grpb.pb.go', instead of '.pb.go' to
the generated source file.

Bug: 72810
Change-Id: I0ae9595ce0ade7611764c1bec294fdcdb8faa9b2
diff --git a/proto_library.gni b/proto_library.gni
index fcb9a27..28cafdd 100644
--- a/proto_library.gni
+++ b/proto_library.gni
@@ -30,6 +30,11 @@
 #
 #   generate_go (optional, default false)
 #       Generate Go protobuf stubs.
+#       Mutually exclusive with generate_go_grpc.
+#
+#   generate_go_grpc (optional, default false)
+#       Generate a Go gRPC protobuf stub, instead of a regular one.
+#       Mutually exclusive with generate_go.
 #
 #   cc_generator_options (optional)
 #       List of extra flags passed to the protocol compiler.  If you need to
@@ -120,11 +125,13 @@
     generate_descriptor_set = false
   }
 
-  if (defined(invoker.generate_go)) {
-    generate_go = invoker.generate_go
-  } else {
-    generate_go = false
-  }
+  generate_go = defined(invoker.generate_go) && invoker.generate_go
+  generate_go_grpc =
+      defined(invoker.generate_go_grpc) && invoker.generate_go_grpc
+
+  assert(!generate_go || !generate_go_grpc,
+         "Only one of generate_go or generate_go_grpc can be enabled!")
+
   if (defined(invoker.generator_plugin_label)) {
     # Straightforward way to get the name of executable doesn't work because
     # |root_out_dir| and |root_build_dir| may differ in cross-compilation and
@@ -188,7 +195,7 @@
     py_out_dir = "$root_out_dir/pyproto/" + proto_out_dir
     rel_py_out_dir = rebase_path(py_out_dir, root_build_dir)
   }
-  if (generate_go) {
+  if (generate_go || generate_go_grpc) {
     go_out_dir = "$root_gen_dir/go-proto-gen/src/" + proto_out_dir
     rel_go_out_dir = rebase_path(go_out_dir, root_build_dir)
   }
@@ -220,6 +227,9 @@
     if (generate_go) {
       protogens += [ "$go_out_dir/${proto_path}.pb.go" ]
     }
+    if (generate_go_grpc) {
+      protogens += [ "$go_out_dir/${proto_path}_grpc.pb.go" ]
+    }
     if (generate_with_plugin) {
       foreach(suffix, generator_plugin_suffixes) {
         protogens += [ "$cc_out_dir/${proto_path}${suffix}" ]
@@ -247,10 +257,14 @@
     protoc_label = "//third_party/protobuf:protoc($host_toolchain)"
     protoc_path = get_label_info(protoc_label, "root_out_dir") + "/protoc"
 
-    if (generate_go) {
-      protoc_gen_go_label = "//third_party/golibs/google.golang.org/protobuf/cmd/protoc-gen-go($host_toolchain)"
-      protoc_gen_go_path =
-          get_label_info(protoc_gen_go_label, "root_out_dir") + "/protoc-gen-go"
+    if (generate_go || generate_go_grpc) {
+      if (generate_go_grpc) {
+        protoc_gen_go_label = "//third_party/golibs/google.golang.org/grpc/cmd/protoc-gen-go-grpc($host_toolchain)"
+      } else {
+        protoc_gen_go_label = "//third_party/golibs/google.golang.org/protobuf/cmd/protoc-gen-go($host_toolchain)"
+      }
+      protoc_gen_go_path = get_label_info(protoc_gen_go_label, "root_out_dir") +
+                           "/" + get_label_info(protoc_gen_go_label, "name")
     }
 
     # Depfile information.
@@ -307,7 +321,7 @@
       ]
     }
 
-    if (generate_go) {
+    if (generate_go || generate_go_grpc) {
       args += [
         "--plugin",
         rebase_path(protoc_gen_go_path, root_build_dir),
@@ -356,7 +370,7 @@
       protoc_output_info_target,
     ]
 
-    if (generate_go) {
+    if (generate_go || generate_go_grpc) {
       deps += [ protoc_gen_go_label ]
       inputs += [ protoc_gen_go_path ]
     }