6

How can I assign a dynamic, git-based version number to an autoconf project? Autoconf requires a static string argument to

AC_INIT([Title],[version],[name])

AC_INIT documentation says that one can use M4 to supply a shell-based version. M4 is beyond my ken. I'd like to version my software according to the results of this command

version=`git describe --abbrev=7 --dirty --always --tags`

This produces something like 4.6.6-alpha07-9-ga3e01a8.

I may not understand high level answers. I need a solution like "cut and paste this into your autoconf.ac and/or acinclude.m4".

Any help appreciated.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Charlie Zender
  • 5,929
  • 14
  • 19
  • Possible duplicate of [Bumping version numbers for new releases in associated files (documentation)](https://stackoverflow.com/questions/986521/bumping-version-numbers-for-new-releases-in-associated-files-documentation) – John Kugelman Mar 31 '19 at 15:22

2 Answers2

9

How about:

AC_INIT([Title], [m4_esyscmd_s([git describe --abbrev=7 --dirty --always --tags])])

should work for you.

ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • Thanks. It partially works, then causes configure to fail. Hard to figure out why. Still investigating... – Charlie Zender Apr 22 '17 at 23:24
  • 1
    @CharlieZender: Remove the `[]` around `m4_esyscmd`, and run its result thru `tr` to get rid of the trailing newline: `AC_INIT([Title], m4_esyscmd([git describe --abbrev=7 --dirty --always --tags | tr -d '\n']))`. – Chris R. Timmons Aug 01 '17 at 23:33
  • This works! Configure completes with the version string I wanted. Surprised more projects don't use Git-based version tags. Not sure I should mark this answer as correct unless/until the ldav1s answer is edited ... – Charlie Zender Aug 06 '17 at 03:04
  • `tr`'s behavior is not always reliable in this situation. See [here](https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Usual-Tools.html) for more info. – ldav1s Aug 07 '17 at 00:30
  • Prefer `echo -n` over `tr` to remove newlines and collapse any other whitespace into simple spaces. – gatopeich Sep 26 '21 at 08:47
  • According to [here](https://www.gnu.org/software/autoconf/manual/autoconf-2.69/html_node/Limitations-of-Builtins.html#Limitations-of-Builtins) `printf %s` is to be preferred over `echo -n`. The use of `m4_esyscmd_s` should render newline stripping techniques moot anyway. – ldav1s May 03 '22 at 16:07
6

Just running git describe in m4_esyscmd for the AC_INIT version still leaves a few things to be desired:

  • What version to use if you build a dist tarball? There is no git describe useful output to be had here at all.

  • What version to use if you have just committed a change? Do you update the configure version from git describe, or just continue to build with the existing version?

For my own packages (such as ndim-utils), I have solved these issues (and a few more) by

  • Having a special build-helpers/package-version script which determines the version to use from a version-stamp file if found, or git describe. configure.ac AC_INIT will m4_esyscmd that script.

  • Having a special build-helpers/package-version.mk to include from the top-level Makefile.am which generates a version-stamp file for dist tarballs, checks whether the current git describe output differs from what configure has stored, and a few other things.

  • Having a GNUmakefile.in which updates the version stored in configure internals from git describe when necessary.

And I probably have forgotten a few issues addressed in that solution.

I am not certain that those scripts are ready to just copy over to your project, but I wanted to mention here that there are a few more things to consider.

ndim
  • 35,870
  • 12
  • 47
  • 57