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:
./configure --prefix=/usr/local
make
make check
sudo make installThe 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 |
maintainer user (release tarball)
configure.ac ──autoreconf──► configure ─────► ./configure ──► config.status ──► Makefile, config.h
Makefile.am ──automake───► Makefile.in ────► │
make ◄───────┘A first project
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--installcopied the helper scripts automake needs (compile,install-sh,missing,depcomp) into the project. They are part of the release tarball too.foreigntells automake this is not a full GNU project, so it doesn't demandNEWS,README,AUTHORS,ChangeLogandCOPYING.-Wall -Werrorturn automake's warnings into errors, which keepsMakefile.amclean.subdir-objectsputssrc/main.onext 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_INITset thePACKAGE_*macros that ended up inconfig.h, and the program printed them.- The
makeoutput shows automake's generated rules:make all-am, thedepbase=…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:
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
./configurechecking 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:
./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") buildTest yourself
Which tools must be installed on the USER's machine to build a release tarball of an Autotools project?
- autoconf, automake and libtool
- A POSIX shell, make and a C compiler
- Python and Ninja
- M4
What does automake read and write?
configure.ac→configureMakefile.am→Makefile.inMakefile.in→Makefileconfig.h.in→config.h
What is config.status?
- A log of the configure run
- A script written by configure that records the results and generates
Makefileandconfig.hfrom their templates - The automake configuration
- A lock file