build: provide git-head revision via githead.h

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>
This commit is contained in:
David Herrmann 2012-12-31 16:40:51 +01:00
parent 7a9b85c306
commit ca446c691f
4 changed files with 103 additions and 15 deletions

1
.gitignore vendored
View 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

View File

@ -92,6 +92,42 @@ SHL_REGISTER = \
src/shl_register.h \
$(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
@ -372,6 +408,7 @@ kmscon_SOURCES = \
$(SHL_TIMER) \
$(SHL_HOOK) \
$(SHL_REGISTER) \
src/githead.h \
src/conf.h \
src/conf.c \
src/log.h \

62
src/genversion.sh Executable file
View File

@ -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"

View File

@ -17,6 +17,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "githead.h"
#include "log.h"
/*
@ -67,19 +68,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
@ -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__);
}