Tuesday, September 28, 2010

GNU Autotools - Autoconf, Automake tutorial

The GNU build system, also known as the Autotools, is a suite of programming tools designed to assist in making source-code packages portable to many Unix-like systems.

Here is a sample step by step example to build a standard GNU project from scratch:

1. README contains some very limited documentation for our little package.
[onkar@gnutools queue]$ cat README
This is a demonstration package for GNU Automake.
Type `info Automake' to read the Automake manual.



2. Makefile.am and src/Makefile.am contain Automake instructions for these two directories.
[onkar@gnutools queue]$ cat src/Makefile.am
bin_PROGRAMS = queue
queue_SOURCES = main.cpp Queue.cpp
noinst_HEADERS = Queue.h

[onkar@gnutools queue]$ cat Makefile.am
SUBDIRS = src
docdir = ${datadir}/doc/${PACKAGE}
dist_doc_DATA = README




3. configure.ac contains Autoconf instructions to create the configure script.

[onkar@gnutools queue]$ cat configure.ac
AC_INIT([queue], [1.0], [bug-automake@gnu.org])
AC_PROG_CXX
AM_INIT_AUTOMAKE([-Wall -Werror foreign])
AC_PROG_CC
AC_CONFIG_HEADERS([config.h])
AC_CONFIG_FILES([
 Makefile
 src/Makefile
])
AC_OUTPUT



4. autoreconf: Once you have these five files, it is time to run the Autotools to instantiate the build system. Do this using the autoreconf command as follows:
[onkar@gnutools queue]$ autoreconf --install



5. configure: You can see that autoreconf created four other files: configure, config.h.in, Makefile.in, and src/Makefile.in. The latter three files are templates that will be adapted to the system by configure under the names config.h, Makefile, and src/Makefile. Let's do this:
[onkar@gnutools queue]$ ./configure
checking for g++... g++
checking for C++ compiler default output file name... a.out
checking whether the C++ compiler works... yes
checking whether we are cross compiling... no
checking for suffix of executables...
checking for suffix of object files... o
checking whether we are using the GNU C++ compiler... yes
checking whether g++ accepts -g... yes
checking for a BSD-compatible install... /usr/bin/install -c
checking whether build environment is sane... yes
checking for gawk... gawk
checking whether make sets $(MAKE)... yes
checking for style of include used by make... GNU
checking dependency style of g++... gcc3
checking for gcc... gcc
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ANSI C... none needed
checking dependency style of gcc... gcc3
configure: creating ./config.status
config.status: creating Makefile
config.status: creating src/Makefile
config.status: creating config.h
config.status: config.h is unchanged
config.status: executing depfiles commands


6. make, make clean, make distcheck: You can see Makefile, src/Makefile, and config.h being created at the end after configure has probed the system. It is now possible to run all the targets we wish. For instance:
[onkar@gnutools queue]$ make
make  all-recursive
make[1]: Entering directory `/home/onkar/practice/cc/queue'
Making all in src
make[2]: Entering directory `/home/onkar/practice/cc/queue/src'
if g++ -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -MT main.o -MD -MP -MF ".deps/main.Tpo" -c -o main.o main.cpp; \
then mv -f ".deps/main.Tpo" ".deps/main.Po"; else rm -f ".deps/main.Tpo"; exit 1; fi
if g++ -DHAVE_CONFIG_H -I. -I. -I..     -g -O2 -MT Queue.o -MD -MP -MF ".deps/Queue.Tpo" -c -o Queue.o Queue.cpp; \
then mv -f ".deps/Queue.Tpo" ".deps/Queue.Po"; else rm -f ".deps/Queue.Tpo"; exit 1; fi
g++  -g -O2   -o queue  main.o Queue.o
make[2]: Leaving directory `/home/onkar/practice/cc/queue/src'
make[2]: Entering directory `/home/onkar/practice/cc/queue'
make[2]: Leaving directory `/home/onkar/practice/cc/queue'
make[1]: Leaving directory `/home/onkar/practice/cc/queue'


[onkar@gnutools queue]$ make distcheck
..................
===========================================
queue-1.0 archives ready for distribution:
queue-1.0.tar.gz
===========================================


[onkar@gnutools queue]$ make clean
Making clean in src
make[1]: Entering directory `/home/onkar/practice/cc/queue/src'
test -z "queue" || rm -f queue
rm -f *.o
make[1]: Leaving directory `/home/onkar/practice/cc/queue/src'
Making clean in .
make[1]: Entering directory `/home/onkar/practice/cc/queue'
make[1]: Nothing to be done for `clean-am'.
make[1]: Leaving directory `/home/onkar/practice/cc/queue'


Done!