gn: fix incremental build of descriptor when changing transitive deps

This CL makes it so that GN is aware of the transitive deps of a proto
descriptor rule so that it rebuilds the descriptor when the non-root
files are touched.

We need to wrap protoc because of a bug in protoc where the depfile is
not in the right output format when generating descriptors.

Change-Id: Ib0fb95351849f0ca802e87ea54329430ec6b1ffd
diff --git a/gn/standalone/proto_library.gni b/gn/standalone/proto_library.gni
index 3c73926..f79ae07 100644
--- a/gn/standalone/proto_library.gni
+++ b/gn/standalone/proto_library.gni
@@ -125,9 +125,12 @@
     sources = proto_sources
     outputs = get_path_info(protogens, "abspath")
 
+    protoc_script = "//gn/standalone/protoc.py"
     protoc_label = "//gn:protoc($host_toolchain)"
     protoc_path = get_label_info(protoc_label, "root_out_dir") + "/protoc"
     args = [
+      "./" + rebase_path(protoc_script, root_build_dir),
+
       # Path should be rebased because |root_build_dir| for current toolchain
       # may be different from |root_out_dir| of protoc built on host toolchain.
       "./" + rebase_path(protoc_path, root_build_dir),
@@ -141,10 +144,14 @@
       ]
     }
     if (generate_descriptor != "") {
+      depfile = "$target_gen_dir/" +
+                rebase_path("$generate_descriptor.d", root_gen_dir)
       args += [
         "--include_imports",
         "--descriptor_set_out",
         rebase_path("$root_gen_dir/" + generate_descriptor, root_build_dir),
+        "--dependency_out",
+        rebase_path("$root_gen_dir/$generate_descriptor.d", root_build_dir),
       ]
     }
 
diff --git a/gn/standalone/protoc.py b/gn/standalone/protoc.py
new file mode 100755
index 0000000..7f83bef
--- /dev/null
+++ b/gn/standalone/protoc.py
@@ -0,0 +1,54 @@
+#!/usr/bin/env python
+# Copyright (C) 2019 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+"""Script to wrap protoc execution.
+
+This script exists to work-around the bad depfile generation by protoc when
+generating descriptors."""
+
+from __future__ import print_function
+import argparse
+import os
+import sys
+import subprocess
+import tempfile
+
+def main():
+  parser = argparse.ArgumentParser()
+  parser.add_argument('--descriptor_set_out', default=None)
+  parser.add_argument('--dependency_out', default=None)
+  parser.add_argument('protoc')
+  args, remaining = parser.parse_known_args()
+
+  if args.dependency_out and args.descriptor_set_out:
+    with tempfile.NamedTemporaryFile() as t:
+      custom = [
+        '--descriptor_set_out',
+        args.descriptor_set_out,
+        '--dependency_out',
+        t.name
+      ]
+      subprocess.call([args.protoc] + custom + remaining)
+
+      dependency_data = t.read()
+
+    with open(args.dependency_out, 'w') as f:
+      f.write(args.descriptor_set_out + ":")
+      f.write(dependency_data)
+  else:
+    subprocess.call(sys.argv[1:])
+
+if __name__ == '__main__':
+  sys.exit(main())
diff --git a/protos/perfetto/trace/BUILD.gn b/protos/perfetto/trace/BUILD.gn
index 175e64a..bbab12a 100644
--- a/protos/perfetto/trace/BUILD.gn
+++ b/protos/perfetto/trace/BUILD.gn
@@ -82,7 +82,9 @@
 
 if (perfetto_build_standalone) {
   proto_descriptor("descriptor") {
-    sources = proto_sources
+    sources = [
+      "trace.proto",
+    ]
     generate_descriptor = "$perfetto_root_path/protos/trace/trace.descriptor"
     proto_in_dir = "$perfetto_root_path/protos"
     proto_out_dir = "$perfetto_root_path/protos"
diff --git a/tools/gen_android_bp b/tools/gen_android_bp
index 183a914..f2f388b 100755
--- a/tools/gen_android_bp
+++ b/tools/gen_android_bp
@@ -460,7 +460,7 @@
     # We only support genrules which call protoc (with or without a plugin) to
     # turn .proto files into header and source files.
     args = target['args']
-    if not args[0].endswith('/protoc'):
+    if '/protoc' not in args[0]:
         raise Error('Unsupported action in target %s: %s' % (target_name,
                                                              target['args']))
     parser = ThrowingArgumentParser('Action in target %s (%s)' %
@@ -472,7 +472,7 @@
     parser.add_argument('--descriptor_set_out')
     parser.add_argument('--include_imports', action='store_true')
     parser.add_argument('protos', nargs=argparse.REMAINDER)
-    args = parser.parse_args(args[1:])
+    args = parser.parse_args(args[2:])
 
     # Depending on whether we are using the default protoc C++ generator or the
     # protozero plugin, the output dir is passed as:
diff --git a/tools/gen_bazel b/tools/gen_bazel
index 6f7d5ca..c84da3d 100755
--- a/tools/gen_bazel
+++ b/tools/gen_bazel
@@ -417,7 +417,7 @@
 
         if target.type == 'cc_library' or target.type == 'cc_binary':
           target.srcs.update(dep_target.outs)
-      elif args[0].endswith('/protoc'):
+      elif '/protoc' in args[0]:
         result = self.create_proto_target(dep_name);
         if result is None:
           return
@@ -568,7 +568,7 @@
     target_desc = self.desc[gn_target_name]
     if target_desc['type'] == 'action':
       args = target_desc['args']
-      if args[0].endswith('/protoc'):
+      if '/protoc' in args[0]:
         return self.create_proto_target(gn_target_name)
       else:
         raise Error('Unsupported action in target %s: %s' % (gn_target_name,