[GN] don't define a static_library if it's not needed

For callers that only need Go code, the static_library is not needed.

Bug: 61402
Change-Id: I5091a4054b40d7c917cbe6a86db815dcb808de49
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/protobuf/+/442587
Reviewed-by: Zach Bush <zmbush@google.com>
diff --git a/proto_library.gni b/proto_library.gni
index 29ef1b3..82d8c75 100644
--- a/proto_library.gni
+++ b/proto_library.gni
@@ -214,11 +214,10 @@
   }
 
   action_name = "${target_name}_gen"
-  source_set_name = target_name
 
   # Generate protobuf stubs.
   action(action_name) {
-    visibility = [ ":$source_set_name" ]
+    visibility = [ ":*" ]
     script = "//third_party/protobuf/protoc_wrapper.py"
     sources = proto_sources
     outputs = get_path_info(protogens, "abspath")
@@ -340,42 +339,54 @@
   }
 
   # Build generated protobuf stubs as static libary.
-  static_library(target_name) {
-    forward_variables_from(invoker,
-                           [
-                             "defines",
-                             "deps",
-                             "testonly",
-                             "visibility",
-                           ])
-    sources = []
-    foreach(source, get_target_outputs(":$action_name")) {
-      extension = get_path_info(source, "extension")
-      if (extension == "h" || extension == "cc") {
-        sources += [ source ]
+  _static_lib_deps = []
+  if (generate_cc || generate_python || generate_with_plugin) {
+    _static_lib_name = "${target_name}_static_lib"
+    _static_lib_deps = [ ":${_static_lib_name}" ]
+    static_library(_static_lib_name) {
+      forward_variables_from(invoker,
+                             [
+                               "defines",
+                               "deps",
+                               "testonly",
+                               "visibility",
+                             ])
+      output_name = target_name
+      sources = []
+      foreach(source, get_target_outputs(":${action_name}")) {
+        extension = get_path_info(source, "extension")
+        if (extension == "h" || extension == "cc") {
+          sources += [ source ]
+        }
       }
-    }
 
-    if (defined(invoker.extra_configs)) {
-      configs += invoker.extra_configs
-    }
+      if (defined(invoker.extra_configs)) {
+        configs += invoker.extra_configs
+      }
 
-    configs += [ "//third_party/protobuf:protobuf_warnings" ]
-    public_configs = [ "//third_party/protobuf:using_proto" ]
-    public_deps = [ ":${action_name}" ]
+      configs += [ "//third_party/protobuf:protobuf_warnings" ]
+      public_configs = [ "//third_party/protobuf:using_proto" ]
 
-    if (generate_cc || generate_with_plugin) {
+      if (!defined(deps)) {
+        deps = []
+      }
+      deps += [ ":${action_name}" ]
+
       # If using built-in cc generator, the resulting headers reference headers
       # within protobuf_lite. Hence, dependencies require those headers too.
       # If using generator plugin, extra deps should be resolved by the invoker.
       if (generate_cc) {
         if (defined(invoker.use_protobuf_full) &&
             invoker.use_protobuf_full == true) {
-          public_deps += [ "//third_party/protobuf:protobuf_full" ]
+          public_deps = [ "//third_party/protobuf:protobuf_full" ]
         } else {
-          public_deps += [ "//third_party/protobuf:protobuf_lite" ]
+          public_deps = [ "//third_party/protobuf:protobuf_lite" ]
         }
       }
     }
   }
+
+  group(target_name) {
+    public_deps = [ ":${action_name}" ] + _static_lib_deps
+  }
 }