build: provide git-head revision via githead.h
authorDavid Herrmann <dh.herrmann@googlemail.com>
Mon, 31 Dec 2012 15:40:51 +0000 (16:40 +0100)
committerDavid Herrmann <dh.herrmann@googlemail.com>
Wed, 2 Jan 2013 14:51:54 +0000 (15:51 +0100)
We need the git-revision for module-version checks so provide the
infrastructure now and print it in log_init().

Note that the git-describe string is distributed with the tarballs so
end-users will not have to generate it themself. But when building from
git, the revision will be automatically updated whenever something
changes.

Signed-off-by: David Herrmann <dh.herrmann@googlemail.com>
.gitignore
Makefile.am
src/genversion.sh [new file with mode: 0755]
src/log.c

index 0f45cfe..0c9b5b4 100644 (file)
@@ -30,6 +30,7 @@ genshader
 genunifont
 src/static_shaders.c
 src/genshader.c
+src/githead.h
 src/text_font_unifont_data.c
 docs/reference/*.txt
 docs/reference/*.bak
index 8ee3c54..4309e98 100644 (file)
@@ -93,6 +93,42 @@ SHL_REGISTER = \
        $(SHL_DLIST)
 
 #
+# GIT-HEAD helper
+# The file ./src/githead.h contains a constant BUILD_GIT_HEAD which is defined
+# to the string returned by "git describe". We need to adjust this string for
+# every build and correctly rebuild any sources that depend on it. Therefore,
+# you should use this file rarely as it causes rebuilds on every git-commit.
+#
+# We have a helper-script ./src/genversion.sh that takes as argument the header
+# file and creates it if necessary. It updates it only if the new git-describe
+# string is different to the old one. So the file is only modified on changes.
+# Hence, we can use it as normal dependency in this Makefile.
+# However, we need to run this script on _every_ "make" invocation before any
+# recipy is executed. To achieve this, we use $(shell ...) and assign it to a
+# "simply expanded" variable (:=) so the shell command is executed on
+# variable-declaration and not during expansion.
+#
+# Note that we must not clean ./src/githead.h ever! If we would, a distribution
+# tarball might delete that file and have no way to recreate it.
+# We could delete it on something like "make maintainerclean", but then again,
+# it seems unnecessary so lets simply not clean it at all.
+#
+# If the helper-script is executed in a directory that is not a git-repository
+# (like a distribution tarball) and githead.h exists, then it does nothing as it
+# expects githead.h to be correctly written by "make dist".
+# However, if githead.h does not exist, it will print a warning and write
+# "<unknown>" as git-revision.
+# This guarantees, that githead.h is always present and has the most correct
+# value that we can get under any conditions.
+#
+# The $(emptyvariable) expansion below is used for broken $(shell ...)
+# syntax-highlighting algorithms in many existing editors.
+#
+
+EXTRA_DIST += src/genversion.sh
+GITHEAD:=$(shell $(emptyvariable)"$(srcdir)/src/genversion.sh" "$(srcdir)/src/githead.h")
+
+#
 # libeloop
 # This library contains the whole event-loop implementation of kmscon. It is
 # compiled into a separate object to allow using it in several other programs.
@@ -372,6 +408,7 @@ kmscon_SOURCES = \
        $(SHL_TIMER) \
        $(SHL_HOOK) \
        $(SHL_REGISTER) \
+       src/githead.h \
        src/conf.h \
        src/conf.c \
        src/log.h \
diff --git a/src/genversion.sh b/src/genversion.sh
new file mode 100755 (executable)
index 0000000..4d49fa1
--- /dev/null
@@ -0,0 +1,62 @@
+#!/bin/sh
+
+#
+# Generate $1 with:
+#   #define BUILD_GIT_HEAD "<git-head-revision>"
+# But do not touch $1 if the git-revision is already up-to-date.
+#
+
+if test "x$1" = "x" ; then
+       echo "usage: ./genversion <file>"
+       exit 1
+fi
+
+#
+# Check whether this is a valid git repository.
+# Set ISGIT to 1=true or 0=false.
+#
+
+ISGIT=0
+REV=`git rev-parse --git-dir 2>/dev/null`
+if test "x$?" = "x0" ; then
+       ISGIT=1
+fi
+
+#
+# Check the old revision from $1.
+#
+
+if test -f "$1" ; then
+       OLDREV=`cat "$1"`
+else
+       if test $ISGIT = 0 ; then
+               echo "WARNING: version file $1 is missing"
+               echo "#define BUILD_GIT_HEAD \"unknown-revision\""
+               exit 0
+       fi
+
+       OLDREV=""
+fi
+
+#
+# Check new revision from "git describe". However, if this is no valid
+# git-repository, return success and do nothing.
+#
+
+if test $ISGIT = 0 ; then
+       exit 0
+fi
+
+NEWREV=`git describe`
+NEWREV="#define BUILD_GIT_HEAD \"$NEWREV\""
+
+#
+# Exit if the file is already up to date.
+# Otherwise, write the new revision into the file.
+#
+
+if test "x$OLDREV" = "x$NEWREV" ; then
+       exit 0
+fi
+
+echo "$NEWREV" >"$1"
index 583ddd0..724050d 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -17,6 +17,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include "githead.h"
 #include "log.h"
 
 /*
@@ -68,19 +69,6 @@ static void log__time(long long *sec, long long *usec)
 }
 
 /*
- * Build-Count
- * This is incremented for every build. ISO-C does not provide a method to do
- * this so we default to 1 if no external build-number is specified.
- */
-
-/* TODO: define LOG_BUILD as incremental build number */
-#ifdef LOG_BUILD
-static unsigned long log_build = LOG_BUILD;
-#else
-static unsigned long log_build = 1;
-#endif
-
-/*
  * Default Values
  * Several logging-parameters may be omitted by applications. To provide sane
  * default values we provide constants here.
@@ -512,6 +500,6 @@ void log_print_init(const char *appname)
        if (!appname)
                appname = "<unknown>";
        log_format(LOG_DEFAULT_CONF, NULL, LOG_NOTICE,
-                       "%s Build #%lu %s %s", appname,
-                       log_build, __DATE__, __TIME__);
+                  "%s Revision %s %s %s", appname,
+                  BUILD_GIT_HEAD, __DATE__, __TIME__);
 }