Makefile: autobuild dependency rules

This uses gcc to generate dependency rules for each `.o` file, based on the
corresponding `.c` file, into a new set of `.d` files (which are also defined
to depend on the same set of source files as their `.o` files).

Result:
* all objectfile dependencies are correctly calculated
* only the necessary dependencies are recalculated when a sourcefile is updated

Inspiration for the build rules:
* http://www.gnu.org/software/make/manual/make.html#Automatic-Prerequisites
* http://make.paulandlesley.org/autodep.html

Signed-off-by: Lars Hjemli <hjemli@gmail.com>
diff --git a/Makefile b/Makefile
index d1950cb..faf7b98 100644
--- a/Makefile
+++ b/Makefile
@@ -13,6 +13,13 @@
 -include cgit.conf
 
 
+#
+# Define a pattern rule for automatic dependency building
+#
+%.d: %.c
+	$(CC) $(CFLAGS) -MM $< | sed -e 's/\($*\)\.o:/\1.o $@:/g' >$@
+
+
 EXTLIBS = git/libgit.a git/xdiff/lib.a -lz -lcrypto
 OBJECTS =
 OBJECTS += cache.o
@@ -61,6 +68,8 @@
 
 $(OBJECTS): git/xdiff/lib.a git/libgit.a VERSION
 
+-include $(OBJECTS:.o=.d)
+
 git/xdiff/lib.a: | git
 
 git/libgit.a: | git
@@ -84,7 +93,7 @@
 	rm -f $(CGIT_SCRIPT_PATH)/cgit.png
 
 clean:
-	rm -f cgit VERSION *.o
+	rm -f cgit VERSION *.o *.d
 	cd git && $(MAKE) clean
 
 distclean: clean