(THRIFT-40) Add -version switch to the compiler to show the Thrift version

The version is determined by configure.ac. After this version is shown the
revision. For subversion, it is simply the current revision as given by
`svnversion`. For git, the latest svn revision that HEAD includes is shown,
followed by the offset of HEAD from that commit, followed by a truncated sha1
for HEAD. If the offset is 0, the offset and sha1 are omitted.


git-svn-id: https://svn.apache.org/repos/asf/incubator/thrift/trunk@672900 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/compiler/cpp/Makefile.am b/compiler/cpp/Makefile.am
index d7c7085..2dcdb5e 100644
--- a/compiler/cpp/Makefile.am
+++ b/compiler/cpp/Makefile.am
@@ -1,4 +1,5 @@
 AM_YFLAGS = -d
+BUILT_SOURCES =
 
 bin_PROGRAMS = thrift
 
@@ -80,3 +81,31 @@
 
 clean-local:
 	$(RM) thriftl.cc thrifty.cc thrifty.h
+
+src/main.cc: version.h
+
+# Adding this to BUILT_SOURCES will cause version.h to be
+# regenerated on every "make all" or "make check", which is
+# necessary because it changes whenever we "svn up" or similar.
+# Ideally, we would like this to be regenerated whenever the
+# compiler is rebuilt, but every way we could think of to do
+# that caused unnecessary rebuilds of the compiler.
+BUILT_SOURCES += regen_version_h
+
+THRIFT_VERSION=$(shell /bin/sh $(top_srcdir)/print_version.sh -v)
+THRIFT_REVISION=$(shell /bin/sh $(top_srcdir)/print_version.sh -r)
+
+regen_version_h:
+	@echo -n "Regenerating version.h... "
+	@TMPFILE=`mktemp ./version_h.tmp_XXXXXX` ; \
+		echo "// AUTOGENERATED, DO NOT EDIT" > $$TMPFILE ; \
+		echo '#define THRIFT_VERSION "$(THRIFT_VERSION)"\n' >> $$TMPFILE ; \
+		echo '#define THRIFT_REVISION "$(THRIFT_REVISION)"\n' >> $$TMPFILE ; \
+		if cmp $$TMPFILE version.h >/dev/null ; \
+		then \
+			rm -f $$TMPFILE ; \
+			echo "No changes." ; \
+		else \
+			mv $$TMPFILE version.h ; \
+			echo "Updated." ; \
+		fi