Field Notes: Autotools
Autotools has thirty years of accumulated advice, much of it outdated. These are the notes that still matter for a project maintained today.
1. Never commit generated files; always ship them in the tarball
The generated files are configure, Makefile.in, aclocal.m4, config.h.in, the build-aux/ helper scripts and the libtool/m4 files that libtoolize copies. Rule of thumb:
| Where | Generated files? | Why |
|---|---|---|
| git | no | they are huge, differ between autoconf/automake versions, cause merge conflicts, and hide the real change in reviews; worse, a generated configure in git can be edited by hand and silently drift from configure.ac |
release tarball (make dist) |
yes | the whole point of Autotools: users build with only sh and make |
The 2024 xz-utils backdoor made this rule a security issue: the malicious code was in a modified build-to-host.m4 that existed only in the release tarballs, not in git, where far fewer people looked. Two lessons for maintainers and packagers: generate tarballs reproducibly from a tagged commit (in CI, with make distcheck), and distributions increasingly regenerate the build system (autoreconf -fi) from the git sources instead of trusting a tarball's configure.
When should you commit configure? Only in a separate release branch or tag that exists precisely to hold tarball contents, for users who build from git without autotools (some projects do this for embedded or Windows users). Even then, generate it in CI, never by hand.
A .gitignore for an Autotools project:
/aclocal.m4
/autom4te.cache/
/build-aux/
/config.h
/config.h.in
/config.log
/config.status
/configure
/configure~
/libtool
/m4/libtool.m4
/m4/lt*.m4
Makefile
Makefile.in
.deps/
.libs/
*.o
*.lo
*.la
stamp-h12. AC_CONFIG_MACRO_DIRS([m4]) and ACLOCAL_AMFLAGS
Third-party macros (libtool.m4, pkg.m4, autoconf-archive's ax_*.m4, your own) must be found by aclocal. Declare a project macro directory, so tools copy macros there and aclocal searches it:
AC_CONFIG_MACRO_DIRS([m4]) # configure.ac (autoconf 2.70+; AC_CONFIG_MACRO_DIR for one dir on older)ACLOCAL_AMFLAGS = -I m4 # top-level Makefile.am, for automatic re-runs by older automakeWithout it, libtoolize puts its files in the top directory and asks you to fix it:
mkdir -p nomacro && cd nomacro
cat > configure.ac <<'AC'
AC_INIT([nomacro], [1.0])
AM_INIT_AUTOMAKE([foreign])
AM_PROG_AR
LT_INIT
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
printf 'lib_LTLIBRARIES = libx.la\nlibx_la_SOURCES = x.c\n' > Makefile.am
echo 'int x(void) { return 0; }' > x.c
autoreconf -fi 2>&1 | grep libtoolizelibtoolize: putting auxiliary files in '.'.
libtoolize: copying file './ltmain.sh'
libtoolize: Consider adding 'AC_CONFIG_MACRO_DIRS([m4])' to configure.ac,
libtoolize: and rerunning libtoolize and aclocal.
libtoolize: Consider adding '-I m4' to ACLOCAL_AMFLAGS in Makefile.am.
Copy autoconf-archive macros you use into m4/ too, so building from git doesn't require the archive to be installed.
3. Make the maintainer workflow one command
- Provide an
autogen.shthat is justexec autoreconf -fiv(plus fetching gnulib if you use it). Don't put./configurein it: people want to pass their own options. - Use
AM_INIT_AUTOMAKE([foreign -Wall -Werror subdir-objects])andAM_SILENT_RULES([yes]): warnings are bugs, and quiet builds make real warnings visible (make V=1for full commands). - Run
make distcheckin CI on every merge, withAM_DISTCHECK_CONFIGURE_FLAGSenabling all optional features. It catches missing files, srcdir/builddir bugs and uninstall leftovers before a release does.
4. Prefer pkg-config over hand-written library checks
AC_CHECK_LIB([z], [deflate]) hard-codes the library name and can't know about extra flags or transitive dependencies. When a library ships a .pc file (most do), use PKG_CHECK_MODULES([ZLIB], [zlib >= 1.2.11]): it gets the right -I, -L and -l flags, checks the version, and lets users override with ZLIB_CFLAGS/ZLIB_LIBS or PKG_CONFIG_PATH. Install .pc files the portable way, which also avoids a classic automake error:
mkdir -p pc && cd pc
cat > configure.ac <<'AC'
AC_INIT([pc], [1.0])
AM_INIT_AUTOMAKE([foreign -Wall -Werror])
AC_CONFIG_FILES([Makefile pc.pc])
AC_OUTPUT
AC
printf 'prefix=@prefix@\nName: pc\nDescription: demo\nVersion: @VERSION@\n' > pc.pc.in
echo 'pkgconfig_DATA = pc.pc' > Makefile.am
echo "--- without a directory"
autoreconf -i 2>&1 | grep error
echo "--- with PKG_INSTALLDIR (from pkg.m4)"
sed -i 's/^AC_CONFIG_FILES/PKG_INSTALLDIR\nAC_CONFIG_FILES/' configure.ac
autoreconf -i 2>&1 | grep error || echo "no errors"
./configure --quiet --prefix=/usr && grep '^pkgconfigdir =' Makefile--- without a directory
Makefile.am:1: error: 'pkgconfig_DATA' is used but 'pkgconfigdir' is undefined
autoreconf: error: automake failed with exit status: 1
--- with PKG_INSTALLDIR (from pkg.m4)
no errors
pkgconfigdir = ${libdir}/pkgconfig
PKG_INSTALLDIR defines pkgconfigdir as $(libdir)/pkgconfig and adds a --with-pkgconfigdir option for distributions with unusual layouts.
5. Cross-compiling: --host, not CC alone
./configure --host=aarch64-linux-gnu is how Autotools cross-compiles: it looks for aarch64-linux-gnu-gcc, -ar, -strip on PATH, sets cross_compiling=yes, and makes tests that need to run a program fail with "cannot run test program while cross compiling" unless a cache variable or config.site provides the answer. Setting only CC=aarch64-linux-gnu-gcc without --host leaves configure believing it builds natively, and run-tests produce wrong answers or errors.
Test yourself
Should a generated configure script be committed to git?
- Yes, always
- No: generate it with
autoreconf(in CI for releases) and ship it only in release tarballs - Only
configure, notMakefile.in - Only on Windows
automake fails with 'pkgconfig_DATA' is used but 'pkgconfigdir' is undefined. The cleanest fix?
- Rename to
data_DATA - Add
PKG_INSTALLDIRtoconfigure.ac(or definepkgconfigdir = $(libdir)/pkgconfiginMakefile.am) - Remove the
.pcfile - Pass
--prefix
What does AC_CONFIG_MACRO_DIRS([m4]) change?
- Where object files go
- Where libtoolize/aclocal put and find
.m4macro files, so third-party macros live inside the project - Where
configureis written - Nothing on modern autoconf