From f36eb7be39140a31ae6c316ea03249c997a0286d Mon Sep 17 00:00:00 2001 From: Jason Cooper Date: Tue, 6 Aug 2013 12:29:56 +0000 Subject: [PATCH] version.h: dynamically create version number based on git When building the source from a checked out tag, eg v1.15, VERSION will equal v1.15. However, when building from anything other than a tagged version, you get 'v1.15-4-g50432d5-dirty' meaning I was 4 patches in front of v1.15, particularly '50432d5' was my current HEAD, and I had uncommited changes, '-dirty'. Very useful for folks submitting bug reports on versions they compiled themselves. Signed-off-by: Jason Cooper --- .gitignore | 1 + Makefile | 14 ++++++++------ common.h | 5 +---- genver.sh | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 10 deletions(-) create mode 100755 genver.sh diff --git a/.gitignore b/.gitignore index 6d53fe8..fe98397 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ sslh-fork sslh-select sslh.8.gz tags +version.h diff --git a/Makefile b/Makefile index 25ff087..f28c02a 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ # Configuration -VERSION="1.15" +VERSION=$(shell ./genver.sh -r) USELIBCONFIG=1 # Use libconfig? (necessary to use configuration files) USELIBWRAP= # Use libwrap? COV_TEST= # Perform test coverage? @@ -34,17 +34,19 @@ endif all: sslh $(MAN) echosrv .c.o: *.h - $(CC) $(CFLAGS) -D'VERSION=$(VERSION)' -c $< + $(CC) $(CFLAGS) -c $< +version.h: + ./genver.sh >version.h sslh: $(OBJS) sslh-fork sslh-select -sslh-fork: $(OBJS) sslh-fork.o Makefile common.h - $(CC) $(CFLAGS) -D'VERSION=$(VERSION)' -o sslh-fork sslh-fork.o $(OBJS) $(LIBS) +sslh-fork: $(OBJS) sslh-fork.o Makefile common.h version.h + $(CC) $(CFLAGS) -o sslh-fork sslh-fork.o $(OBJS) $(LIBS) #strip sslh-fork -sslh-select: $(OBJS) sslh-select.o Makefile common.h - $(CC) $(CFLAGS) -D'VERSION=$(VERSION)' -o sslh-select sslh-select.o $(OBJS) $(LIBS) +sslh-select: $(OBJS) sslh-select.o Makefile common.h version.h + $(CC) $(CFLAGS) -o sslh-select sslh-select.o $(OBJS) $(LIBS) #strip sslh-select echosrv: $(OBJS) echosrv.o diff --git a/common.h b/common.h index 81fc3e6..7d49b0f 100644 --- a/common.h +++ b/common.h @@ -27,10 +27,7 @@ #include #include #include - -#ifndef VERSION -#define VERSION "v?" -#endif +#include "version.h" #define CHECK_RES_DIE(res, str) \ if (res == -1) { \ diff --git a/genver.sh b/genver.sh new file mode 100755 index 0000000..bd06d86 --- /dev/null +++ b/genver.sh @@ -0,0 +1,33 @@ +#!/bin/sh + +if [ ${#} -eq 1 ] && [ "x$1" == "x-r" ]; then + # release text only + QUIET=1 +else + QUIET=0 +fi + +if head=`git rev-parse --verify HEAD 2>/dev/null`; then + + if [ $QUIET -ne 1 ]; then + printf "#ifndef _VERSION_H_ \n" + printf "#define _VERSION_H_ \n\n" + printf "#define VERSION \"" + fi + + # generate the version info based on the tag + (git describe --tags || git --describe || git describe --all --long) \ + 2>/dev/null | tr -d '\n' + + # Are there uncommitted changes? + git update-index --refresh --unmerged > /dev/null + if git diff-index --name-only HEAD | grep -v "^scripts/package" \ + | read dummy; then + printf '%s' -dirty + fi + + if [ $QUIET -ne 1 ]; then + printf "\"\n" + printf "\n#endif\n" + fi +fi