From ff868661e2e2031212a1a74fbc10f2a72f8ddb36 Mon Sep 17 00:00:00 2001
From: Ron <ron@debian.org>
Date: Mon, 13 May 2013 20:32:13 +0930
Subject: [PATCH] Fully automate version updating

This one meets or exceeds the following requirements:

 - Version is checked/updated for every build action when in the git repo.
   Does not require the user to re- ./configure to get the correct version.

 - Version is not updated automatically when using exported tarball source.
   Avoids accidentally getting a wrong version from some other git repo in
   a parent directory of the source, and allows setting the correct version
   for distro package exports.

 - Automatic updating can be manually suppressed.
   For developers doing lots of change/rebuild cycles they don't plan to
   release, when they don't want a full rebuild triggered for every commit,
   and again for every change made immediately after a commit.
   The version will still always be updated if they do a `make dist`.

 - Does not require any manual updating of versions in the mainline git
   repo for each release aside from normal tagging.  The version is
   recorded in one file only, that is automatically generated and will
   never need to be committed.

 - Does not require gnu-make features for the autoconf builds.

It does not currently:

 - Keep a checksum of every source file in tarball releases to mangle the
   version if people modify the tarball source.  Responsible people can
   manually update the version easily though in such cases.
---
 .gitattibutes              |  4 +++
 .gitignore                 |  1 +
 Makefile.am                | 37 +++++++++++++++++++++-
 configure.ac               | 21 +++++++++---
 doc/Doxyfile.in            |  4 +--
 doc/Makefile               | 16 +++++++---
 doc/git-version.sh         | 13 --------
 opusfile-uninstalled.pc.in |  2 +-
 opusfile.pc.in             |  2 +-
 opusurl-uninstalled.pc.in  |  2 +-
 opusurl.pc.in              |  2 +-
 update_version             | 65 ++++++++++++++++++++++++++++++++++++++
 12 files changed, 141 insertions(+), 28 deletions(-)
 create mode 100644 .gitattibutes
 delete mode 100755 doc/git-version.sh
 create mode 100755 update_version

diff --git a/.gitattibutes b/.gitattibutes
new file mode 100644
index 0000000..2415029
--- /dev/null
+++ b/.gitattibutes
@@ -0,0 +1,4 @@
+.gitignore		export-ignore
+.gitattributes		export-ignore
+
+update_version		export-ignore
diff --git a/.gitignore b/.gitignore
index 80c72f6..19a7a22 100644
--- a/.gitignore
+++ b/.gitignore
@@ -35,6 +35,7 @@ opusfile.pc
 opusfile-uninstalled.pc
 opusurl.pc
 opusurl-uninstalled.pc
+package_version
 unix/objs
 unix/opusfile_example
 unix/seeking_example
diff --git a/Makefile.am b/Makefile.am
index 983e3f0..9cd0633 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -45,7 +45,6 @@ EXTRA_DIST = \
  opusfile-uninstalled.pc.in \
  opusurl-uninstalled.pc.in \
  doc/Doxyfile.in \
- doc/git-version.sh \
  doc/opus_logo.svg \
  doc/Makefile \
  unix/Makefile
@@ -91,4 +90,40 @@ uninstall-local:
 
 endif
 
+# We check this every time make is run, with configure.ac being touched to
+# trigger an update of the build system files if update_version changes the
+# current PACKAGE_VERSION (or if package_version was modified manually by a
+# user with either AUTO_UPDATE=no or no update_version script present - the
+# latter being the normal case for tarball releases).
+#
+# We can't just add the package_version file to CONFIGURE_DEPENDENCIES since
+# simply running autoconf will not actually regenerate configure for us when
+# the content of that file changes (due to autoconf dependency checking not
+# knowing about that without us creating yet another file for it to include).
+#
+# The MAKECMDGOALS check is a gnu-make'ism, but will degrade 'gracefully' for
+# makes that don't support it.  The only loss of functionality is not forcing
+# an update of package_version for `make dist` if AUTO_UPDATE=no, but that is
+# unlikely to be a real problem for any real user.
+$(top_srcdir)/configure.ac: force
+	@case "$(MAKECMDGOALS)" in \
+	    dist-hook)                             exit 0       ;; \
+	    dist-* | dist | distcheck | distclean) _arg=release ;; \
+	esac; \
+	if ! $(top_srcdir)/update_version $$_arg 2> /dev/null; then \
+	    if [ ! -e $(top_srcdir)/package_version ]; then \
+		echo 'PACKAGE_VERSION="unknown"' > $(top_srcdir)/package_version; \
+	    fi; \
+	    . $(top_srcdir)/package_version || exit 1; \
+	    [ "$(PACKAGE_VERSION)" != "$$PACKAGE_VERSION" ] || exit 0; \
+	fi; \
+	touch $@
+
+force:
+
+# Create a minimal package_version file when make dist is run.
+dist-hook:
+	echo 'PACKAGE_VERSION="$(PACKAGE_VERSION)"' > $(top_distdir)/package_version
+
+
 .PHONY: opusfile install-opusfile docs install-docs
diff --git a/configure.ac b/configure.ac
index e3a7ad3..2bed739 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1,11 +1,23 @@
 # autoconf source script for generating configure
 
-AC_INIT([opusfile], m4_esyscmd([doc/git-version.sh]))
+dnl The package_version file will be automatically synced to the git revision
+dnl by the update_version script when configured in the repository, but will
+dnl remain constant in tarball releases unless it is manually edited.
+m4_define([CURRENT_VERSION],
+          m4_esyscmd_s([ if test -e package_version || ./update_version; then
+                             . ./package_version
+                             printf "$PACKAGE_VERSION"
+                         else
+                             printf "unknown"
+                         fi ]))
+
+AC_INIT([opusfile],[CURRENT_VERSION],[opus@xiph.org])
+AC_CONFIG_SRCDIR([src/opusfile.c])
 
 AC_USE_SYSTEM_EXTENSIONS
 AC_SYS_LARGEFILE
 
-AM_INIT_AUTOMAKE([1.11 foreign])
+AM_INIT_AUTOMAKE([1.11 foreign no-define])
 AM_MAINTAINER_MODE([enable])
 LT_INIT
 
@@ -130,7 +142,7 @@ if test "$HAVE_DOXYGEN" != "yes" -o "$enable_doc" != "yes" ; then
 fi
 AM_CONDITIONAL(HAVE_DOXYGEN, [test $HAVE_DOXYGEN = yes])
 
-AC_OUTPUT([
+AC_CONFIG_FILES([
   Makefile
   opusfile.pc
   opusurl.pc
@@ -138,10 +150,11 @@ AC_OUTPUT([
   opusurl-uninstalled.pc
   doc/Doxyfile
 ])
+AC_OUTPUT
 
 AC_MSG_NOTICE([
 ------------------------------------------------------------------------
-  $PACKAGE $VERSION: Automatic configuration OK.
+  $PACKAGE_NAME $PACKAGE_VERSION: Automatic configuration OK.
 
     Assertions ................... ${enable_assertions}
 
diff --git a/doc/Doxyfile.in b/doc/Doxyfile.in
index 5e30304..5f0f3f6 100644
--- a/doc/Doxyfile.in
+++ b/doc/Doxyfile.in
@@ -1,7 +1,7 @@
 # Process with doxygen to generate API documentation
 
-PROJECT_NAME           = @PACKAGE@
-PROJECT_NUMBER         = @VERSION@
+PROJECT_NAME           = @PACKAGE_NAME@
+PROJECT_NUMBER         = @PACKAGE_VERSION@
 PROJECT_BRIEF          = "Stand-alone decoder library for .opus files."
 INPUT                  = @top_srcdir@/include/opusfile.h
 OPTIMIZE_OUTPUT_FOR_C  = YES
diff --git a/doc/Makefile b/doc/Makefile
index 24e62ab..f0c5e42 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -1,5 +1,7 @@
 ## GNU makefile for opusfile documentation.
 
+-include ../package_version
+
 all: doxygen
 
 doxygen: Doxyfile ../include/opusfile.h
@@ -17,10 +19,16 @@ distclean: clean
 
 .PHONY: all clean distclean doxygen pdf
 
+../package_version:
+	@if [ -x ../update_version ]; then \
+		../update_version || true; \
+	elif [ ! -e $@ ]; then \
+		echo 'PACKAGE_VERSION="unknown"' > $@; \
+	fi
+
 # run autoconf-like replacements to finalize our config
-GIT_VERSION := $(shell ./git-version.sh)
-Doxyfile: Doxyfile.in Makefile
-	sed -e 's/@PACKAGE@/opusfile/' \
-	    -e 's/@VERSION@/$(GIT_VERSION)/' \
+Doxyfile: Doxyfile.in Makefile ../package_version
+	sed -e 's/@PACKAGE_NAME@/opusfile/' \
+	    -e 's/@PACKAGE_VERSION@/$(PACKAGE_VERSION)/' \
 	    -e 's/@top_srcdir@/../' \
 	  < $< > $@
diff --git a/doc/git-version.sh b/doc/git-version.sh
deleted file mode 100755
index 812219e..0000000
--- a/doc/git-version.sh
+++ /dev/null
@@ -1,13 +0,0 @@
-#!/bin/sh
-
-# script to build a version string
-
-GIT_VERSION=$(git describe --tags --match 'v*' --dirty 2> /dev/null)
-GIT_VERSION=$(echo ${GIT_VERSION} | sed 's/^v//')
-if test -z ${GIT_VERSION}; then
-  VERSION='unknown'
-else
-  VERSION=${GIT_VERSION}
-fi
-
-/bin/echo -n ${VERSION}
diff --git a/opusfile-uninstalled.pc.in b/opusfile-uninstalled.pc.in
index f953e2d..b5861a4 100644
--- a/opusfile-uninstalled.pc.in
+++ b/opusfile-uninstalled.pc.in
@@ -7,7 +7,7 @@ includedir=${pcfiledir}/@top_srcdir@/include
 
 Name: opusfile uninstalled
 Description: High-level Opus decoding library (not installed)
-Version: @VERSION@
+Version: @PACKAGE_VERSION@
 Requires.private: ogg >= 1.3 opus >= 1.0.1
 Conflicts:
 Libs: ${libdir}/libopusfile.la @lrintf_lib@
diff --git a/opusfile.pc.in b/opusfile.pc.in
index 8c315be..9622591 100644
--- a/opusfile.pc.in
+++ b/opusfile.pc.in
@@ -7,7 +7,7 @@ includedir=@includedir@
 
 Name: opusfile
 Description: High-level Opus decoding library
-Version: @VERSION@
+Version: @PACKAGE_VERSION@
 Requires.private: ogg >= 1.3 opus >= 1.0.1
 Conflicts:
 Libs: -L${libdir} -lopusfile
diff --git a/opusurl-uninstalled.pc.in b/opusurl-uninstalled.pc.in
index f3af6a1..f47786a 100644
--- a/opusurl-uninstalled.pc.in
+++ b/opusurl-uninstalled.pc.in
@@ -7,7 +7,7 @@ includedir=${pcfiledir}/@top_srcdir@/include
 
 Name: opusfile uninstalled
 Description: High-level Opus decoding library, URL support (not installed)
-Version: @VERSION@
+Version: @PACKAGE_VERSION@
 Requires: opusfile
 Requires.private: @openssl@
 Conflicts:
diff --git a/opusurl.pc.in b/opusurl.pc.in
index 27bd788..df63759 100644
--- a/opusurl.pc.in
+++ b/opusurl.pc.in
@@ -7,7 +7,7 @@ includedir=@includedir@
 
 Name: opusurl
 Description: High-level Opus decoding library, URL support
-Version: @VERSION@
+Version: @PACKAGE_VERSION@
 Requires: opusfile
 Requires.private: @openssl@
 Conflicts:
diff --git a/update_version b/update_version
new file mode 100755
index 0000000..a999991
--- /dev/null
+++ b/update_version
@@ -0,0 +1,65 @@
+#!/bin/bash
+
+# Creates and updates the package_version information used by configure.ac
+# (or other makefiles).  When run inside a git repository it will use the
+# version information that can be queried from it unless AUTO_UPDATE is set
+# to 'no'.  If no version is currently known it will be set to 'unknown'.
+#
+# If called with the argument 'release', the PACKAGE_VERSION will be updated
+# even if AUTO_UPDATE=no, but the value of AUTO_UPDATE shall be preserved.
+# This is used to force a version update whenever `make dist` is run.
+#
+# The exit status is 1 if package_version is not modified, else 0 is returned.
+#
+# This script should NOT be included in distributed tarballs, because if a
+# parent directory contains a git repository we do not want to accidentally
+# retrieve the version information from it instead.  Tarballs should ship
+# with only the package_version file.
+#
+# Ron <ron@debian.org>, 2012.
+
+SRCDIR=$(dirname $0)
+
+if [ -e "$SRCDIR/package_version" ]; then
+    . "$SRCDIR/package_version"
+fi
+
+if [ "$AUTO_UPDATE" = no ]; then
+    [ "$1" = release ] || exit 1
+else
+    AUTO_UPDATE=yes
+fi
+
+# We run `git status` before describe here to ensure that we don't get a false
+# -dirty from files that have been touched but are not actually altered in the
+# working dir.
+GIT_VERSION=$(cd "$SRCDIR" && git status > /dev/null 2>&1 \
+                           && git describe --tags --match 'v*' --dirty 2> /dev/null)
+GIT_VERSION=${GIT_VERSION#v}
+
+if [ -n "$GIT_VERSION" ]; then
+
+    [ "$GIT_VERSION" != "$PACKAGE_VERSION" ] || exit 1
+    PACKAGE_VERSION="$GIT_VERSION"
+
+elif [ -z "$PACKAGE_VERSION" ]; then
+    # No current package_version and no git ...
+    # We really shouldn't ever get here, because this script should only be
+    # included in the git repository, and should usually be export-ignored.
+    PACKAGE_VERSION="unknown"
+else
+    exit 1
+fi
+
+cat > "$SRCDIR/package_version" <<-EOF
+	# Automatically generated by update_version.
+	# This file may be sourced into a shell script or makefile.
+
+	# Set this to 'no' if you do not wish the version information
+	# to be checked and updated for every build.  Most people will
+	# never want to change this, it is an option for developers
+	# making frequent changes that they know will not be released.
+	AUTO_UPDATE=$AUTO_UPDATE
+
+	PACKAGE_VERSION="$PACKAGE_VERSION"
+EOF
-- 
GitLab