Use dependency files generated by llvm-rs-cc

This was a regression since kati has been introduced. This CL
introduces include-depfile function to make it easier to write
Makefiles which work with both make and kati.

As ninja can handle only a single dependency file per a build
rule, now we merge multiple .d files generated by llvm-rs-cc
into a .d file.

Change-Id: Iaf64a8f0523ab98115837e6e06abd50f06620363
diff --git a/core/definitions.mk b/core/definitions.mk
index 916dfa2..78b607b 100644
--- a/core/definitions.mk
+++ b/core/definitions.mk
@@ -874,7 +874,7 @@
 endif
 
 ###########################################################
-## Commands for munging the dependency files GCC generates
+## Commands for munging the dependency files the compiler generates
 ###########################################################
 # $(1): the input .d file
 # $(2): the output .P file
@@ -890,6 +890,21 @@
 endef
 
 ###########################################################
+## Commands for including the dependency files the compiler generates
+###########################################################
+# $(1): the .P file
+# $(2): the main build target
+ifeq ($(BUILDING_WITH_NINJA),true)
+define include-depfile
+$(eval $(2) : .KATI_DEPFILE := $1)
+endef
+else
+define include-depfile
+$(eval -include $1)
+endef
+endif
+
+###########################################################
 ## Commands for running lex
 ###########################################################
 
@@ -954,6 +969,24 @@
 ## Commands to compile RenderScript to C++
 ###########################################################
 
+## Merge multiple .d files generated by llvm-rs-cc. This is necessary
+## because ninja can handle only a single depfile per build target.
+## We assume .d files start with two targets and their prerequisites
+## follow. The first line is for the stamp file and the second line is
+## for .bc file. There's no way to let ninja know dependencies to .bc
+## files, so we give up build targets for .bc files. As we write the
+## .stamp file as the target by ourselves, the sed script removes the
+## first two lines and append a backslash to the last line to
+## concatenate contents of multiple files.
+# $(1): .d files to be merged
+# $(2): merged .d file
+define _merge-renderscript-d
+$(hide) echo '$@: $(backslash)' > $2
+$(foreach d,$1, \
+  $(hide) sed '1d; 2d; s/\( \\\)\?$$/ \\/' $d >> $2$(newline))
+$(hide) echo >> $2
+endef
+
 define transform-renderscripts-to-cpp-and-bc
 @echo "RenderScript: $(PRIVATE_MODULE) <= $(PRIVATE_RS_SOURCE_FILES)"
 $(hide) rm -rf $(PRIVATE_RS_OUTPUT_DIR)
@@ -967,8 +1000,8 @@
   $(PRIVATE_RS_FLAGS) \
   $(addprefix -I , $(PRIVATE_RS_INCLUDES)) \
   $(PRIVATE_RS_SOURCE_FILES)
-  $(foreach d,$(PRIVATE_DEP_FILES),\
-    $(call transform-d-to-p-args,$(d),$(d:%.d=%.P))$(newline))
+$(call _merge-renderscript-d,$(PRIVATE_DEP_FILES),$@.d)
+$(call transform-d-to-p-args,$@.d,$@.P)
 $(hide) mkdir -p $(dir $@)
 $(hide) touch $@
 endef