Add depfile for linux targets

Linux tree did not use to specify any sources and would therefore never
be rebuilt after first build. Add a script which generates a depfile for
the tree and use it for a target that linux targets depend on.

Test: make ; touch third_party/linux/README; make (should recompile); \
      make (should not recompile)
Change-Id: Id0a06be73de2ed31c52f1697ac00f7d45425b43b
diff --git a/build/linux/gen_depfile.py b/build/linux/gen_depfile.py
new file mode 100755
index 0000000..7290840
--- /dev/null
+++ b/build/linux/gen_depfile.py
@@ -0,0 +1,51 @@
+#!/usr/bin/env python
+#
+# Copyright 2019 The Hafnium Authors.
+#
+# 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
+#
+#     https://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.
+
+#!/usr/bin/env python
+"""Generate a depfile for a folder."""
+
+import argparse
+import os
+import sys
+
+def main():
+	parser = argparse.ArgumentParser()
+	parser.add_argument("root_dir", help="input directory")
+	parser.add_argument("stamp_file", help="stamp file to be touched")
+	parser.add_argument("dep_file", help="depfile to be written")
+	args = parser.parse_args()
+
+	# Compile list of all files in the folder, relative to `root_dir`.
+	sources = []
+	for root, _, files in os.walk(args.root_dir):
+		sources.extend([ os.path.join(root, f) for f in files ])
+	sources = sorted(sources)
+
+	# Write `dep_file` as a Makefile rule for `stamp_file`.
+	with open(args.dep_file, "w") as f:
+		f.write(args.stamp_file)
+		f.write(":")
+		for source in sources:
+			f.write(' ');
+			f.write(source)
+		f.write(os.linesep)
+
+	# Touch `stamp_file`.
+	with open(args.stamp_file, "w"):
+		pass
+
+if __name__ == "__main__":
+    sys.exit(main())
diff --git a/build/linux/linux.gni b/build/linux/linux.gni
index 0f096eb..b675813 100644
--- a/build/linux/linux.gni
+++ b/build/linux/linux.gni
@@ -12,8 +12,25 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+template("source_dir") {
+  action("${target_name}") {
+    depfile = "${target_out_dir}/${target_name}.d"
+    outputs = [
+      "$target_out_dir/${target_name}.script.stamp",
+    ]
+
+    script = "//build/linux/gen_depfile.py"
+    args = [
+      rebase_path(invoker.path, root_build_dir),
+      rebase_path(outputs[0], root_build_dir),
+      rebase_path(depfile, root_build_dir),
+    ]
+  }
+}
+
 template("linux_kernel") {
-  # TODO: target has no "sources"
+  source_target = "${target_name}__source"
+  defconfig_target = "${target_name}__defconfig"
 
   # Args to build/make.py to start the Linux build.
   shared_args = [
@@ -32,9 +49,15 @@
     "-j24",
   ]
 
+  # Subtarget which generates a depfile with all files in the Linux tree
+  # and gets invalidated if any of them change.
+  source_dir(source_target) {
+    path = invoker.kernel_dir
+  }
+
   # Subtarget which runs `defconfig` and `modules_prepare`. Used by targets
   # which do not require the whole kernel to have been built.
-  action("${target_name}__defconfig") {
+  action(defconfig_target) {
     script = "//build/make.py"
     args = shared_args + [
              "defconfig",
@@ -46,6 +69,9 @@
     outputs = [
       "${target_out_dir}/.config",
     ]
+    deps = [
+      ":${source_target}",
+    ]
   }
 
   action(target_name) {
@@ -60,7 +86,8 @@
       output_file,
     ]
     deps = [
-      ":${target_name}__defconfig",
+      ":${defconfig_target}",
+      ":${source_target}",
     ]
   }
 }