perfetto-ui: Increase correctness of build

Adding typescript files to inputs means that after removing a typescript
file the build fails until gn gen is rerun with an error like:

ninja: error: '../../ui/src/foo.ts', needed by 'obj/ui/controller/index.js', missing and no known rule to make it

Using depfile is the more correct thing here since depfiles mentioning
files that do not exist is not an error. Neither this CL (nor the
previous state) address the situation of adding a new typescript file
- this doesn't error but changes to the new file won't automatically
cause rebuilds until gn gen is run. This is because we generate the
dependency list too early, at the gn time instead of at buildtime.
The real fix for is to have tsc (or a wrapper) create the depfile.

Change-Id: I784e9623fd13491a08ecc9ddd65826d17800e22c
diff --git a/gn/standalone/glob.py b/gn/standalone/glob.py
index da4a253..eafcbc4 100755
--- a/gn/standalone/glob.py
+++ b/gn/standalone/glob.py
@@ -21,7 +21,7 @@
 output of the build but just cause spurious re-runs (e.g. as input section of
 an "action" target).
 """
-
+from __future__ import print_function 
 import argparse
 import fnmatch
 import os
@@ -31,27 +31,38 @@
   parser = argparse.ArgumentParser()
   parser.add_argument('--filter', default=[], action='append')
   parser.add_argument('--exclude', default=[], action='append')
+  parser.add_argument('--deps', default=None)
+  parser.add_argument('--output', default=None)
   parser.add_argument('--root', required=True)
   args = parser.parse_args()
 
+  fout = open(args.output, 'w') if args.output else sys.stdout
+  def writepath(path):
+    if args.deps:
+      path = '\t' + path
+    print(path, file=fout)
+
   root = args.root
   if not root.endswith('/'):
     root += '/'
   if not os.path.exists(root):
     return 0
+
+  if args.deps:
+    print(args.deps + ':', file=fout)
   for pardir, dirs, files in os.walk(root, topdown=True):
-    assert(pardir.startswith(root))
+    assert pardir.startswith(root)
     relpar = pardir[len(root):]
     dirs[:] = [d for d in dirs if os.path.join(relpar, d) not in args.exclude]
     for fname in files:
-      fpath = os.path.join(relpar, fname)
+      fpath = os.path.join(pardir, fname)
       match = len(args.filter) == 0
       for filter in args.filter:
         if fnmatch.fnmatch(fpath, filter):
           match = True
           break
       if match:
-        print fpath
+        writepath(fpath)
 
 if __name__ == '__main__':
   sys.exit(main())
diff --git a/ui/BUILD.gn b/ui/BUILD.gn
index 297464d..077f7b4 100644
--- a/ui/BUILD.gn
+++ b/ui/BUILD.gn
@@ -45,6 +45,7 @@
                            [
                              "inputs",
                              "outputs",
+                             "depfile",
                            ])
     deps = [
       ":node_modules",
@@ -207,39 +208,34 @@
 # | TypeScript: transpiles all *.ts into .js                                   |
 # +----------------------------------------------------------------------------+
 
-# Builds all .ts sources in the repo that are reachable from |sources|.
+# Builds all .ts sources in the repo under |src|.
 node_bin("transpile_all_ts") {
-  sources = [
-    "src/controller/index.ts",
-    "src/engine/index.ts",
-    "src/frontend/index.ts",
-  ]
   deps = [
     ":dist_symlink",
     ":protos_to_ts",
     ":wasm_gen",
   ]
-  inputs = sources + [ "tsconfig.json" ]
+  inputs = [
+    "tsconfig.json",
+  ]
   outputs = [
     "$target_out_dir/frontend/index.js",
     "$target_out_dir/engine/index.js",
     "$target_out_dir/controller/index.js",
   ]
 
-  # Find all *.ts files and pass them as input triggers. This does NOT affect
-  # which files will get built. This defines only the set of files that, when
-  # changes, will re-trigger this rule in ninja. The files that get transpiled
-  # are the transitive dependencies of |sources|.
-  all_ts_files = exec_script("../gn/standalone/glob.py",
-                             [
-                               "--root=" + rebase_path(".", root_build_dir),
-                               "--filter=*.ts",
-                               "--exclude=node_modules",
-                               "--exclude=dist",
-                             ],
-                             "list lines",
-                             [ "." ])
-  inputs += all_ts_files
+  depfile = root_out_dir + "/tsc.d"
+  exec_script("../gn/standalone/glob.py",
+              [
+                "--root=" + rebase_path(".", root_build_dir),
+                "--filter=*.ts",
+                "--exclude=node_modules",
+                "--exclude=dist",
+                "--deps=obj/ui/frontend/index.js",
+                "--output=" + rebase_path(depfile),
+              ],
+              "")
+
   node_cmd = "tsc"
   args = [
     "--project",