[GN] support generating descriptor set

Bug: 61402
Change-Id: I094f53481f206fbacce735623d9f95301cb5c302
Reviewed-on: https://fuchsia-review.googlesource.com/c/third_party/protobuf/+/442717
Reviewed-by: Zach Bush <zmbush@google.com>
diff --git a/proto_library.gni b/proto_library.gni
index 82d8c75..b74e5fe 100644
--- a/proto_library.gni
+++ b/proto_library.gni
@@ -25,6 +25,9 @@
 #   generate_cc (optional, default true)
 #       Generate C++ protobuf stubs.
 #
+#   generate_descriptor_set (optional, default false)
+#       Generate a descriptor set file.
+#
 #   generate_go (optional, default false)
 #       Generate Go protobuf stubs.
 #
@@ -111,6 +114,12 @@
     generate_python = true
   }
 
+  if (defined(invoker.generate_descriptor_set)) {
+    generate_descriptor_set = invoker.generate_descriptor_set
+  } else {
+    generate_descriptor_set = false
+  }
+
   if (defined(invoker.generate_go)) {
     generate_go = invoker.generate_go
   } else {
@@ -187,6 +196,12 @@
 
   protos = rebase_path(invoker.sources, proto_in_dir)
   protogens = []
+  if (generate_descriptor_set) {
+    descriptor_set_out_out = "${target_out_dir}/${target_name}.desc.pb"
+    rel_descriptor_set_out_out =
+        rebase_path(descriptor_set_out_out, root_build_dir)
+    protogens += [ descriptor_set_out_out ]
+  }
 
   # List output files.
   foreach(proto, protos) {
@@ -268,6 +283,13 @@
       ]
     }
 
+    if (generate_descriptor_set) {
+      args += [
+        "--descriptor-set-out",
+        rel_descriptor_set_out_out,
+      ]
+    }
+
     if (generate_go) {
       args += [
         "--plugin",
diff --git a/protoc_wrapper.py b/protoc_wrapper.py
index d969e7e..56e14e7 100755
--- a/protoc_wrapper.py
+++ b/protoc_wrapper.py
@@ -90,6 +90,9 @@
   parser.add_argument("--import-dir", action="append", default=[],
                       help="Extra import directory for protos, can be repeated."
   )
+  parser.add_argument("--descriptor-set-out",
+                      help="Passed through to protoc as --descriptor_set_out")
+
   parser.add_argument("protos", nargs="+",
                       help="Input protobuf definition file(s).")
 
@@ -102,6 +105,9 @@
   headers = []
   VerifyProtoNames(protos)
 
+  if options.descriptor_set_out:
+    protoc_cmd += ["--descriptor_set_out", options.descriptor_set_out]
+
   if options.py_out_dir:
     protoc_cmd += ["--python_out", options.py_out_dir]