Add DTC release version information.

Adopted the version information and implementation
from of the Linux Kernel Makefiles.

Signed-off-by: Jon Loeliger <jdl@jdl.com>
diff --git a/Makefile b/Makefile
index 6b278b9..4740bff 100644
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,49 @@
+#
+# Device Tree Compiler
+#
+
+#
+# Version information will be constructed in this order:
+# EXTRAVERSION might be "-rc", for example.
+# LOCAL_VERSION is likely from command line.
+# CONFIG_LOCALVERSION from some future config system.
+#
+VERSION = 1
+PATCHLEVEL = 0
+SUBLEVEL = 0
+EXTRAVERSION =
+LOCAL_VERSION =
+CONFIG_LOCALVERSION = 
+
+DTC_VERSION = $(VERSION).$(PATCHLEVEL).$(SUBLEVEL)$(EXTRAVERSION)
+VERSION_FILE = version_gen.h
+
+CONFIG_SHELL := $(shell if [ -x "$$BASH" ]; then echo $$BASH; \
+	  else if [ -x /bin/bash ]; then echo /bin/bash; \
+	  else echo sh; fi ; fi)
+
+nullstring :=
+space	:= $(nullstring) # end of line
+
+localver_config = $(subst $(space),, $(string) \
+			      $(patsubst "%",%,$(CONFIG_LOCALVERSION)))
+
+localver_cmd = $(subst $(space),, $(string) \
+			      $(patsubst "%",%,$(LOCALVERSION)))
+
+localver_scm = $(shell $(CONFIG_SHELL) ./scripts/setlocalversion)
+localver_full  = $(localver_config)$(localver_cmd)$(localver_scm)
+
+dtc_version = $(DTC_VERSION)$(localver_full)
+
+#
+# Contents of the generated version file.
+#
+define filechk_version
+	(echo "#define DTC_VERSION \"DTC $(dtc_version)\""; )
+endef
+
+
 CPPFLAGS = -I libfdt
 CFLAGS = -Wall -g
 LDFLAGS = -Llibfdt
@@ -28,11 +74,14 @@
 
 all: dtc ftdump libfdt tests
 
+
 STD_CLEANFILES = *~ *.o *.d *.a *.i *.s core a.out
+GEN_CLEANFILES = $(VERSION_FILE)
 
 clean: libfdt_clean tests_clean
 	@$(VECHO) CLEAN
 	rm -f $(STD_CLEANFILES)
+	rm -f $(GEN_CLEANFILES)
 	rm -f *.tab.[ch] lex.yy.c *.output vgcore.*
 	rm -f $(BIN)
 
@@ -82,6 +131,9 @@
 	@$(VECHO) ---- Expect 2 s/r and 2 r/r. ----
 	$(BISON) -d $<
 
+$(VERSION_FILE): Makefile FORCE
+	$(call filechk,version)
+
 lex.yy.c: dtc-lexer.l
 	@$(VECHO) LEX $@
 	$(LEX) $<
@@ -123,3 +175,18 @@
 	$(INSTALL) -d $(DESTDIR)$(BINDIR)
 	$(INSTALL) -m 755 dtc $(DESTDIR)$(BINDIR)
 	$(INSTALL) -m 755 ftdump $(DESTDIR)$(BINDIR)
+
+define filechk
+	set -e;					\
+	echo '	CHK $@';			\
+	mkdir -p $(dir $@);			\
+	$(filechk_$(1)) < $< > $@.tmp;		\
+	if [ -r $@ ] && cmp -s $@ $@.tmp; then	\
+		rm -f $@.tmp;			\
+	else					\
+		echo '	UPD $@';		\
+		mv -f $@.tmp $@;		\
+	fi;
+endef
+
+FORCE:
diff --git a/dtc.c b/dtc.c
index 5767834..ecd9fe5 100644
--- a/dtc.c
+++ b/dtc.c
@@ -21,6 +21,8 @@
 #include "dtc.h"
 #include "srcpos.h"
 
+#include "version_gen.h"
+
 /*
  * Command line options
  */
@@ -99,6 +101,8 @@
 	fprintf(stderr, "\t\tSet the physical boot cpu\n");
 	fprintf(stderr, "\t-f\n");
 	fprintf(stderr, "\t\tForce - try to produce output even if the input tree has errors\n");
+	fprintf(stderr, "\t-v\n");
+	fprintf(stderr, "\t\tPrint DTC version and exit\n");
 	exit(2);
 }
 
@@ -120,7 +124,7 @@
 	reservenum = 0;
 	minsize    = 0;
 
-	while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:fqb:")) != EOF) {
+	while ((opt = getopt(argc, argv, "hI:O:o:V:R:S:fqb:v")) != EOF) {
 		switch (opt) {
 		case 'I':
 			inform = optarg;
@@ -149,6 +153,9 @@
 		case 'b':
 			boot_cpuid_phys = strtol(optarg, NULL, 0);
 			break;
+		case 'v':
+		    printf("Version: %s\n", DTC_VERSION);
+		    exit(0);
 		case 'h':
 		default:
 			usage();
diff --git a/scripts/setlocalversion b/scripts/setlocalversion
new file mode 100755
index 0000000..82e4993
--- /dev/null
+++ b/scripts/setlocalversion
@@ -0,0 +1,22 @@
+#!/bin/sh
+# Print additional version information for non-release trees.
+
+usage() {
+	echo "Usage: $0 [srctree]" >&2
+	exit 1
+}
+
+cd "${1:-.}" || usage
+
+# Check for git and a git repo.
+if head=`git rev-parse --verify HEAD 2>/dev/null`; then
+	# Do we have an untagged version?
+	if git name-rev --tags HEAD | grep -E '^HEAD[[:space:]]+(.*~[0-9]*|undefined)$' > /dev/null; then
+		printf '%s%s' -g `echo "$head" | cut -c1-8`
+	fi
+
+	# Are there uncommitted changes?
+	if git diff-index HEAD | read dummy; then
+		printf '%s' -dirty
+	fi
+fi