Fix: git version build system integration (v2)
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 29 Jul 2014 22:36:51 +0000 (18:36 -0400)
committerDavid Goulet <dgoulet@efficios.com>
Wed, 30 Jul 2014 12:46:28 +0000 (08:46 -0400)
Fix:
- Don't overwrite version.h if the previous content matches.
- While we are there, if we notice the previous content matches,
  print a (cached) message to show that we do not overwrite the
  content.
- Introduce LTTNG_TOOLS_BUILD_GIT_SOURCE automake conditional rather
  than compile-time define to disable the feature. It ensures we
  do not invoke "git describe" when configure --disable-git-version
  has been requested.
- Use git describe rather than git describe --long --all. Based
  on the last tag, and adds the first numbers of git revision.
  Last tag is useful both to the developer and in a bug report,
  whereas the branch name returned by --long --all is meaningless
  in a bug report.
- We want to ship version.h.tmpl in the tarball (make dist), not
  version.h which is generated. Someone could very well do a
  git init on the extracted tarball and want to have git tracking
  support.
- Fix the git prefix " - " that is incorrectly printed in some
  situations, e.g. when GIT_SOURCE was active, but we are in
  a non-git tree (or git is not available).
- Fix incorrect handling of out of tree build. Invoke git describe from
  top_srcdir.

Changelog since v1:
- Add missing comma for lttng usage output.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: David Goulet <dgoulet@efficios.com>
configure.ac
include/Makefile.am
include/version.h.tmpl
src/bin/lttng/commands/version.c
src/bin/lttng/lttng.c

index bd49df7ba12a20cc13a0a1f2e72b6d8d711ae77b..a8e04f5ab5cd39b44ba4edaf5be4a5e4629d3f39 100644 (file)
@@ -262,11 +262,11 @@ AC_CHECK_LIB([c], [open_memstream],
 AC_ARG_ENABLE([git-version],
               [AC_HELP_STRING([--disable-git-version],
                               [Do not use the git version for the build])],
-              [disable_git_version=yes], [disable_git_version=no]
+              [have_git_version=$enableval], [have_git_version=yes]
 )
-if test "x${disable_git_version:-no}" = xno; then
-    AC_DEFINE_UNQUOTED([GIT_SOURCE], 1, [Disable git version.])
-fi
+
+AM_CONDITIONAL([LTTNG_TOOLS_BUILD_GIT_SOURCE],
+       [test "x${have_git_version}" = "xyes"])
 
 # For Python
 # SWIG version needed or newer:
index 767b05b5d99ff38128713925a7dbd71e12579bd7..4b59e72d8589d28af21e7b4a531539e0fdcbc050 100644 (file)
@@ -1,3 +1,9 @@
+if LTTNG_TOOLS_BUILD_GIT_SOURCE
+GIT_DESCRIBE_CMD = (cd $(top_srcdir); git describe)
+else
+GIT_DESCRIBE_CMD = /bin/true
+endif
+
 ##
 ## The version.h file must be verified and generated or updated if the
 ## git commit id (called git version here) changed since the last build
 version.h:
        ##
        ## We first create variables for the current git version and
-       ## the locations of the version.h and version.h.tmpl files
+       ## the locations of the version.h and version.h.tmpl files.
        ##
-       @echo -n "Generating version.h ... "
+       @echo -n "Generating version.h... "
        @(version_h_tmpl="$(top_srcdir)/include/version.h.tmpl"; \
        if [ -f "$${version_h_tmpl}" ]; then \
                version_h="$(top_builddir)/include/version.h"; \
                ##
-               ## We check the git version format we will use depending on
-               ## whether or not we are in the master branch or on a tag
+               ## Check whether we are in a git repo.
                ##
-               git_branch="$$(git describe --all 2>/dev/null)"; \
-               if [ -z "$${git_branch}" ]; then \
-                       git_version=""; \
+               git_describe="$$($(GIT_DESCRIBE_CMD) 2>/dev/null)"; \
+               if [ $$? -eq 0 ]; then \
+                       git_version="$${git_describe}"; \
                else \
-                       git_describe="$$(git describe)"; \
-                       if [ "$${git_branch}" == "$${git_describe}" ]; then \
-                               git_version="$${git_describe}"; \
-                       else \
-                               git_version="$$(git describe --long --all)"; \
-                       fi; \
+                       git_version=""; \
                fi; \
                ##
                ## If the version.h file doesn't exist or is not up to date,
-               ## We replace it by the version.h.tmpl file
+               ## We replace it by the version.h.tmpl file.
                ##
                if [ ! -e "$${version_h}" ] || \
                        [ "$${version_h_tmpl}" -nt "$${version_h}" ]; then \
                        cp "$${version_h_tmpl}" "$${version_h}"; \
                fi; \
-               if [ -n "$${git_version}" ]; then \
-                       ##
-                       ## We remove the leading "v" for the version number
-                       ##
-                       git_version="$$(echo "$${git_version}" | sed -r "s/^v([0-9])/\1/")"; \
-                       ##
-                       ## If we have a git version, we verify that it isn't the same
-                       ## as the one currently in the file (if there is one), as we
-                       ## don't want to update the file if it is already up to date
-                       ##
-                       if [ $$(grep -cE "^#define GIT_VERSION_SED \"?$${git_version}\"?$$" "$${version_h}") -eq 0 ]; then \
-                               sed -i "s'^#define GIT_VERSION_SED.*$$'#define GIT_VERSION \"$${git_version}\"'" "$${version_h}"; \
-                       fi; \
+               echo -n "git version: \"$${git_version}\""; \
+               ##
+               ## We verify that git_version isn't the same as the one
+               ## currently in the file (if there is one), as we don't
+               ## want to update the file if it is already up to date.
+               ##
+               version_match='^#define GIT_VERSION.*'; \
+               old_version=$$(grep "$${version_match}" "$${version_h}"); \
+               new_version="#define GIT_VERSION        \"$${git_version}\""; \
+               if [ x"$${old_version}" != x"$${new_version}" ]; then \
+                       sed -i "s'$${version_match}'$${new_version}'" "$${version_h}"; \
+               else \
+                       echo -n " (cached)"; \
                fi; \
+               echo -n "... "; \
        fi)
        @echo "ok"
 
@@ -58,6 +59,9 @@ version.h:
 ##
 .PHONY: version.h
 
+nodist_noinst_HEADERS = \
+       version.h
+
 lttnginclude_HEADERS = \
        lttng/health.h \
        lttng/lttng.h \
@@ -71,7 +75,7 @@ lttnginclude_HEADERS = \
        lttng/snapshot.h \
        lttng/save.h \
        lttng/load.h \
-       version.h
+       version.h.tmpl
 
 noinst_HEADERS = \
        lttng/snapshot-internal.h \
index e2ce531d4a24ff5a80d32fbf2a51f9128691fb8b..d3f33024a937d82850fcae2f184c9ee4654a9222 100644 (file)
@@ -1,3 +1,6 @@
+#ifndef VERSION_H
+#define VERSION_H
+
 /*
  * Copyright (C) 2013-2014 - RaphaĆ«l Beamonte <raphael.beamonte@gmail.com>
  *
 
 #include <config.h>
 
-#ifndef VERSION_H
-#define VERSION_H
-
-#ifdef GIT_SOURCE
-#define GIT_VERSION_PREFIX " - "
-#define GIT_VERSION_SED ""
-#else
-#define GIT_VERSION_PREFIX ""
-#define GIT_VERSION ""
-#endif
+#define GIT_VERSION    ""
 
 #endif /* VERSION_H */
index 4e46d724f10941400be812e7582aa4d60750a869..f4d89631e7e5a13dfc1a42a6e5aa5d7767d3bacc 100644 (file)
@@ -164,7 +164,8 @@ int cmd_version(int argc, const char **argv)
        if (lttng_opt_mi) {
                ret = print_mi();
        } else {
-               MSG("lttng version " VERSION " - " VERSION_NAME GIT_VERSION_PREFIX GIT_VERSION);
+               MSG("lttng version " VERSION " - " VERSION_NAME "%s",
+                       GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
                MSG("\n" VERSION_DESCRIPTION "\n");
                MSG("Web site: http://lttng.org");
                MSG("\n%s", lttng_license);
index 41232bef9e6e4a2efdc40f90ec9b70dff69ea76c..2df8b8740225afc831c5769779473c0db5140f51 100644 (file)
@@ -90,7 +90,8 @@ static struct cmd_struct commands[] =  {
 
 static void usage(FILE *ofp)
 {
-       fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME" - " GIT_VERSION "\n\n");
+       fprintf(ofp, "LTTng Trace Control " VERSION " - " VERSION_NAME "%s\n\n",
+               GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
        fprintf(ofp, "usage: lttng [OPTIONS] <COMMAND> [<ARGS>]\n");
        fprintf(ofp, "\n");
        fprintf(ofp, "Options:\n");
@@ -134,8 +135,9 @@ static void usage(FILE *ofp)
 
 static void version(FILE *ofp)
 {
-       fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME" - " GIT_VERSION "\n",
-                       progname);
+       fprintf(ofp, "%s (LTTng Trace Control) " VERSION" - " VERSION_NAME "%s\n",
+                       progname,
+                       GIT_VERSION[0] == '\0' ? "" : " - " GIT_VERSION);
 }
 
 /*
This page took 0.031296 seconds and 5 git commands to generate.