Using Autotools
GNU Build System
Software portability and effective build systems are crucial aspects of modern software engineering practice. It is unlikely that a software project would be started today with the expectation that the software would run on only one platform. Autotools may help creating platform independent software packages and build systems, that make building software easier and less error prone are valuable.
Even if there is no need for creating platform independent software packages or adding Autotools to a project, Autotools are usefull to:
- Analyse the software build of a specific platform,
- Determine tools and settings for building software on a specific platform such as compiler and linker tools and flags,
- Learn how the GNU Build System works (library handling, installation directories, etc.),
- Learn how to write platform independent code (shell scripts, make files, support of different C standard libraries),
- Find common solutions for building packages, such as standard makefile rules, environment variables for directories, build scripts and parameters, etc.
Common open source package installation process:
$ ./configure
$ make
$ make install
Overview of Autotools
Aclocal
Generate aclocal.m4
by scanning configure.ac
or configure.in
. 'm4' is a powerful macro processor, in the sense that it copies its input to the output, expanding macros as it goes. This is needed by Autoconf for generating configure scripts.
Autoheader
Create a template file of C '#define' statements for configure
to use. To this end, scan TEMPLATE-FILE, or 'configure.ac' if present, or else 'configure.in'.
Libtool/Libtoolize
Libtool is a platform specific compiler/linker wrapper that provides a consistent interface for building software. Common modes are supported such as:
Compile mode:
- Building object files
Link mode:
- Building static and shared libraries
- Linking static and shared libraries
Exec mode:
- Building executables
- Debugging executables
Install mode:
- Installing static and shared libraries
- Installing executables
Uninstall mode:
- Removing installed static and shared libraries
- Removing installed executables
Clean mode:
- Removing libraries, executables, objects and temporary files
Since Libtool is platform dependent, Autoconf is used to generate a configure
script that determines platform specific settings and creates Libtool (see create Libtool with Autoconf).
The libtoolize
program provides a standard way to add libtool support to your package.
Automake
Automake is a tool for automatically generating Makefile.in
from files called Makefile.am
. Each 'Makefile.am' is basically a series of 'make' variable definitions, with rules being thrown in occasionally. The generated 'Makefile.in's are compliant with the GNU Makefile standards.
Autoconf
Generates a configuration script 'configure' from such as 'configure.ac' or 'configure.in'.
Autoscan
Examines source files in the project directory tree. Searchs the source files for common portability problems, checks for incompleteness of configure.ac
, and creates a file configure.scan
which is a preliminary configure.ac
for that package.
Autoreconf
Remake the GNU Build System files by scanning 'configure.ac' and running 'autoconf', 'autoheader', 'aclocal', 'automake' and 'libtoolize'.
Autoupdate
Updates 'configure.ac' to the syntax of the current version of Autoconf.
Ways to use Autotools
There are several ways to configure and build a project using Autotools. For each approach a set of various configuration files are needed:
Automake:
- configure.ac with Automake and Libtool macros
- Makefile.am with make variable definitions
Autoconf and Autoscan:
- Makefile.in with project build rules and Makefile substitutions
Autoconf and Libtool:
- configure.ac with Libtool macros and system feature tests
- Makefile.in with project build rules and Makefile substitutions
Autoconf, Autoscan and Libtool:
- configure.ac with Libtool macros
- Makefile.in with project build rules and Makefile substitutions
Autoconf only:
- configure.ac with macros that test system features the project needs
- Makefile.in with project build rules and Makefile substitutions
Libtool only:
- Libtool environment
- Plain Makefiles
Autoheader (usable in addition to all other tools)
- scans the project and generates a config.h file during project configuration with platform specific macro definitions, no configuration files needed
Build libraries using Automake
Automake provides the easiest way of setting up a GNU Build System. Files needed for setup are a standard configure.ac file with Automake macros and a makefile.am with some project specific make variable definitions. All remaining work such as the generation of Makefile templates and standard files needed for project build is processed by Automake. Since project setup depends on Automake tools, this approach is useful for configuration of standard projects, but can not provide the flexibility resulting in special project needs such as the addition of special makefile rules.
$ find
.
./configure.ac
./hello.c
./hello.h
./main.c
./Makefile.am
$ cat hello.c
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdio.h>
#include "hello.h"
int
hello (const char *who)
{
printf("Hello, %s!\n", who);
return 0;
}
$ cat hello.h
#ifndef HELLO_H
#define HELLO_H 1
extern int hello (const char *who);
#endif /* !HELLO_H */
$ cat main.c
#if HAVE_CONFIG_H
# include <config.h>
#endif
#include "hello.h"
int
main (int argc, const char *const argv[])
{
return hello("World");
}
$ cat configure.ac
# Process this file with autoconf to create configure.
AC_INIT(hello.h)
AM_CONFIG_HEADER(config.h:config.hin)
AM_INIT_AUTOMAKE(hello, 1.0)
AC_PROG_CC
AM_PROG_CC_STDC
AC_C_CONST
AC_LIBTOOL_WIN32_DLL
AM_PROG_LIBTOOL
AC_OUTPUT(Makefile)
$ cat Makefile.am
## Process this file with automake to produce Makefile.in.
lib_LTLIBRARIES = libhello.la
libhello_la_SOURCES = hello.c
libhello_la_LDFLAGS = -no-undefined -avoid-version
include_HEADERS = hello.h
bin_PROGRAMS = hello
hello_SOURCES = main.c
hello_LDADD = libhello.la
$ aclocal
...
$ autoheader
...
$ libtoolize --force --copy
...
$ automake --add-missing --copy --foreign
...
$ autoconf
...
$ ./configure
...
$ make
...
$ make DESTDIR=`pwd`/inst/ install
...
Setup a basic Autoconf environment
Autoconf creates a 'configure' script from a 'configure.ac' file. See Autoconf manual, chapter 3.1.3 for the standard layout of Autoconf input files. An initial setup of configure.ac could be:
$ cat configure.ac
AC_INIT
dnl configure Makefile creation from template file (Makefile.in):
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
This example configure.ac file needs a template make file (Makefile.in) for the package, what can be derived from the projects Makefile. (For just setting up the environment it can be empty first.) With both files Autoconf can be run properly:
$ touch Makefile.in
$ ls
Makefile.in configure.in
$ autoconf
$ ./configure
configure: creating ./config.status
config.status: creating Makefile
$ ls
Makefile autom4te.cache config.status configure.ac
Makefile.in config.log configure