Search code examples
autotoolsautoconfm4

Prevent "#define PACKAGE_VERSION" from appearing in config.h


The package version (see AC_INIT) of my medium-sized autotools project changes quite often. When the package version changes, I suffer from unnecessary rebuild of the whole project, because PACKAGE_VERSION and friends in config.h has changed and config.h is included in all source files.

The version is actually needed in just one source file and I'd like to put these definitions in a separate generated header and include it in this source file.

In file general.m4 belonging to autoconf I see the following:

m4_define([_AC_INIT_PREPARE],
...
AC_DEFINE_UNQUOTED([PACKAGE_VERSION], ["$PACKAGE_VERSION"],
           [Define to the version of this package.])dnl
AC_DEFINE_UNQUOTED([PACKAGE_STRING], ["$PACKAGE_STRING"],
           [Define to the full name and version of this package.])dnl

In file init.m4 belonging to automake:

AC_DEFUN([AM_INIT_AUTOMAKE],
...
 AC_DEFINE_UNQUOTED([VERSION], ["$VERSION"], [Version number of package])])dnl

I'd like to undo the effects of all three. Currently, my configure.ac has these lines:

AC_INIT(pkgname, [1.2.3])
AC_CONFIG_HEADERS(config.h)
AM_INIT_AUTOMAKE([1.11 parallel-tests -Wno-portability tar-ustar])

Solution

  • You can prevent some of those symbols from being defined by specifying no-define among the Automake options in AM_INIT_AUTOMAKE, but that does not fully solve your problem because PACKAGE_VERSION itself will not be affected. It is defined by AC_INIT, and to the best of my knowledge, Autoconf does not provide a mechanism to suppress that.

    Supposing, then, that you don't want to adjust your versioning practices (it's very strange that your version number changes so frequently that this is an issue), and that you reject the notion that it's a reasonable policy to perform a clean build whenever a version number change occurs, my best recommendation is to patch the configure script after (re)generating it. You'll need to sort out what changes need to be made, but once you do, I expect you'll be able to write a tiny sed- or awk-based script to do it whenever needed.