TExcelTAD
Home › Meson, Autotools & xmake › Autotools: The Pipeline

Autotools: The Pipeline

The GNU Build System, usually called Autotools, is the oldest tool in this course and still the one behind the classic install instructions every Unix user knows:

bash
./configure --prefix=/usr/local
make
make check
sudo make install

The key idea, from the early 1990s, is that the user needs nothing but a POSIX shell and make. All the clever work happens on the maintainer's machine: the maintainer runs Autotools to generate a portable configure shell script and Makefile.in templates, and ships those generated files in the release tarball. The user's machine never needs autoconf or automake installed.

The pieces

Tool Input Output
autoconf configure.ac (M4 macros + shell) configure, a large portable /bin/sh script
autoheader configure.ac config.h.in, the template for config.h
aclocal configure.ac + installed .m4 macro files aclocal.m4, the third-party macros configure.ac uses
automake Makefile.am (declarations) Makefile.in (a full GNU-standards Makefile template)
libtool (libtoolize) LT_INIT in configure.ac ltmain.sh, then a libtool script that builds shared libraries portably
autoreconf — runs all of the above, in the right order

And on the user's side:

Step Reads Writes
./configure Makefile.in, config.h.in config.status, config.log, then (by running config.status) Makefile and config.h
make Makefile the program
text
 maintainer                                     user (release tarball)
 configure.ac ──autoreconf──► configure ─────► ./configure ──► config.status ──► Makefile, config.h
 Makefile.am  ──automake───► Makefile.in ────►                                        │
                                                                         make ◄───────┘

A first project

bash · try it yourself
mkdir -p hello/src && cd hello
cat > configure.ac <<'AC'
AC_INIT([hello], [1.0.0], [bugs@example.com])
AM_INIT_AUTOMAKE([foreign -Wall -Werror subdir-objects])
AC_PROG_CC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
cat > Makefile.am <<'AM'
bin_PROGRAMS = hello
hello_SOURCES = src/main.c
AM
cat > src/main.c <<'SRC'
#include "config.h"
#include <stdio.h>
int main(void) { printf("%s %s, report bugs to %s\n", PACKAGE_NAME, PACKAGE_VERSION, PACKAGE_BUGREPORT); return 0; }
SRC
echo "### maintainer: generate the build system"
autoreconf --install
ls
echo "### user: configure and build"
./configure --quiet
make -j1
./hello
### maintainer: generate the build system
configure.ac:3: installing './compile'
configure.ac:2: installing './install-sh'
configure.ac:2: installing './missing'
Makefile.am: installing './depcomp'
Makefile.am
Makefile.in
aclocal.m4
autom4te.cache
compile
config.h.in
configure
configure.ac
depcomp
install-sh
missing
src
### user: configure and build
make  all-am
make[1]: Entering directory '/home/learner/hello'
depbase=`echo src/main.o | sed 's|[^/]*$|.deps/&|;s|\.o$||'`;\
gcc -DHAVE_CONFIG_H -I.     -g -O2 -MT src/main.o -MD -MP -MF $depbase.Tpo -c -o src/main.o src/main.c &&\
mv -f $depbase.Tpo $depbase.Po
gcc  -g -O2   -o hello src/main.o
make[1]: Leaving directory '/home/learner/hello'
hello 1.0.0, report bugs to bugs@example.com

Reading the session:

  • autoreconf --install (short: -i) ran aclocal, autoconf, autoheader and automake, and --install copied the helper scripts automake needs (compile, install-sh, missing, depcomp) into the project. They are part of the release tarball too.
  • foreign tells automake this is not a full GNU project, so it doesn't demand NEWS, README, AUTHORS, ChangeLog and COPYING. -Wall -Werror turn automake's warnings into errors, which keeps Makefile.am clean. subdir-objects puts src/main.o next to its source instead of in the top directory; without it, automake 1.16 warns (an error here, thanks to -Werror) that the default will change in a future version. Use it in every new project.
  • AC_INIT set the PACKAGE_* macros that ended up in config.h, and the program printed them.
  • The make output shows automake's generated rules: make all-am, the depbase=… dependency-tracking dance (-MT -MD -MP -MF), then the link. The Makefile.am lesson turns on quieter "silent rules".

What ./configure does

configure is a shell script that runs feature tests: for each check it writes a tiny C program, tries to compile/link/run it, and records the answer. Without --quiet you see every test; even this minimal project runs about twenty:

bash · try it yourself
mkdir -p hello && cd hello
cat > configure.ac <<'AC'
AC_INIT([hello], [1.0.0])
AM_INIT_AUTOMAKE([foreign])
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
AC
printf 'bin_PROGRAMS = hello\nhello_SOURCES = main.c\n' > Makefile.am
echo 'int main(void) { return 0; }' > main.c
autoreconf -i 2> /dev/null
./configure
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for a race-free mkdir -p... /usr/bin/mkdir -p
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking whether make supports nested variables... yes
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether the compiler supports GNU C... yes
checking whether gcc accepts -g... yes
checking for gcc option to enable C11 features... none needed
checking whether gcc understands -c and -o together... yes
checking whether make supports the include directive... yes (GNU style)
checking dependency style of gcc... gcc3
checking that generated files are newer than configure... done
configure: creating ./config.status
config.status: creating Makefile
config.status: executing depfiles commands

The last lines are config.status: configure stores all its results in that script and then runs it to instantiate the templates. Re-running ./config.status regenerates Makefile and config.h without repeating the tests, and the generated Makefile does it for you when a template changes (it even re-runs automake and autoconf if Makefile.am or configure.ac changed, through the missing helper).

The user's view: --help and standard variables

Every configure script accepts the same standard interface, which is a large part of Autotools' value:

bash
./configure --help                       # every option, including the project's own
./configure --prefix=/opt/hello          # installation prefix (default /usr/local)
./configure CC=clang CFLAGS="-O2 -g"     # compiler and flags, remembered by config.status
./configure --host=aarch64-linux-gnu     # cross-compile (needs aarch64-linux-gnu-gcc on PATH)
mkdir build && cd build && ../configure  # out-of-tree ("VPATH") build

Test yourself

Which tools must be installed on the USER's machine to build a release tarball of an Autotools project?

  1. autoconf, automake and libtool
  2. A POSIX shell, make and a C compiler
  3. Python and Ninja
  4. M4

What does automake read and write?

  1. configure.ac → configure
  2. Makefile.am → Makefile.in
  3. Makefile.in → Makefile
  4. config.h.in → config.h

What is config.status?

  1. A log of the configure run
  2. A script written by configure that records the results and generates Makefile and config.h from their templates
  3. The automake configuration
  4. A lock file