Copyright © 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
3 General ideas
The following sections cover a few basic ideas that will help you
understand how Automake works.
3.1 General Operation
Automake works by reading a Makefile.am and generating a
Makefile.in. Certain variables and rules defined in the
Makefile.am instruct Automake to generate more specialized code;
for instance, a bin_PROGRAMS
variable definition will cause rules
for compiling and linking programs to be generated.
The variable definitions and rules in the Makefile.am are
copied verbatim into the generated file. This allows you to add
arbitrary code into the generated Makefile.in. For instance,
the Automake distribution includes a non-standard rule for the
git-dist
target, which the Automake maintainer uses to make
distributions from his source control system.
Note that most GNU make extensions are not recognized by Automake. Using
such extensions in a Makefile.am will lead to errors or confusing
behavior.
A special exception is that the GNU make append operator, ‘+=’, is
supported. This operator appends its right hand argument to the variable
specified on the left. Automake will translate the operator into
an ordinary ‘=’ operator; ‘+=’ will thus work with any make program.
Automake tries to keep comments grouped with any adjoining rules or
variable definitions.
A rule defined in Makefile.am generally overrides any such
rule of a similar name that would be automatically generated by
automake
. Although this is a supported feature, it is generally
best to avoid making use of it, as sometimes the generated rules are
very particular.
Similarly, a variable defined in Makefile.am or
AC_SUBST
ed from configure.ac will override any
definition of the variable that automake
would ordinarily
create. This feature is more often useful than the ability to
override a rule. Be warned that many of the variables generated by
automake
are considered to be for internal use only, and their
names might change in future releases.
When examining a variable definition, Automake will recursively examine
variables referenced in the definition. For example, if Automake is
looking at the content of foo_SOURCES
in this snippet
xs = a.c b.c
foo_SOURCES = c.c $(xs)
it would use the files a.c, b.c, and c.c as the
contents of foo_SOURCES
.
Automake also allows a form of comment that is not copied into
the output; all lines beginning with ‘##’ (leading spaces allowed)
are completely ignored by Automake.
It is customary to make the first line of Makefile.am read:
## Process this file with automake to produce Makefile.in
3.2 Strictness
While Automake is intended to be used by maintainers of GNU packages, it
does make some effort to accommodate those who wish to use it, but do
not want to use all the GNU conventions.
To this end, Automake supports three levels of strictness—the
strictness indicating how stringently Automake should check standards
conformance.
The valid strictness levels are:
- foreign
Automake will check for only those things that are absolutely
required for proper operations. For instance, whereas GNU standards
dictate the existence of a NEWS file, it will not be required in
this mode. The name comes from the fact that Automake is intended to be
used for GNU programs; these relaxed rules are not the standard mode of
operation.
- gnu
Automake will check—as much as possible—for compliance to the GNU
standards for packages. This is the default.
- gnits
Automake will check for compliance to the as-yet-unwritten Gnits
standards. These are based on the GNU standards, but are even more
detailed. Unless you are a Gnits standards contributor, it is
recommended that you avoid this option until such time as the Gnits
standard is actually published (which may never happen).
See The effect of --gnu and --gnits, for more information on the precise implications of the
strictness level.
Automake also has a special “cygnus” mode that is similar to
strictness but handled differently. This mode is useful for packages
that are put into a “Cygnus” style tree (e.g., the GCC tree).
See The effect of --cygnus, for more information on this mode.
3.3 The Uniform Naming Scheme
Automake variables generally follow a uniform naming scheme that
makes it easy to decide how programs (and other derived objects) are
built, and how they are installed. This scheme also supports
configure
time determination of what should be built.
At make
time, certain variables are used to determine which
objects are to be built. The variable names are made of several pieces
that are concatenated together.
The piece that tells automake what is being built is commonly called
the primary. For instance, the primary PROGRAMS
holds a
list of programs that are to be compiled and linked.
A different set of names is used to decide where the built objects
should be installed. These names are prefixes to the primary, and they
indicate which standard directory should be used as the installation
directory. The standard directory names are given in the GNU standards
(see Directory Variables in The GNU Coding Standards).
Automake extends this list with pkgdatadir
, pkgincludedir
,
pkglibdir
, and pkglibexecdir
; these are the same as the
non-‘pkg’ versions, but with ‘$(PACKAGE)’ appended. For instance,
pkglibdir
is defined as ‘$(libdir)/$(PACKAGE)’.
For each primary, there is one additional variable named by prepending
‘EXTRA_’ to the primary name. This variable is used to list
objects that may or may not be built, depending on what
configure
decides. This variable is required because Automake
must statically know the entire list of objects that may be built in
order to generate a Makefile.in that will work in all cases.
For instance, cpio
decides at configure time which programs
should be built. Some of the programs are installed in bindir
,
and some are installed in sbindir
:
EXTRA_PROGRAMS = mt rmt
bin_PROGRAMS = cpio pax
sbin_PROGRAMS = $(MORE_PROGRAMS)
Defining a primary without a prefix as a variable, e.g.,
‘PROGRAMS’, is an error.
Note that the common ‘dir’ suffix is left off when constructing the
variable names; thus one writes ‘bin_PROGRAMS’ and not
‘bindir_PROGRAMS’.
Not every sort of object can be installed in every directory. Automake
will flag those attempts it finds in error.
Automake will also diagnose obvious misspellings in directory names.
Sometimes the standard directories—even as augmented by
Automake—are not enough. In particular it is sometimes useful, for
clarity, to install objects in a subdirectory of some predefined
directory. To this end, Automake allows you to extend the list of
possible installation directories. A given prefix (e.g., ‘zar’)
is valid if a variable of the same name with ‘dir’ appended is
defined (e.g., ‘zardir’).
For instance, the following snippet will install file.xml into
‘$(datadir)/xml’.
xmldir = $(datadir)/xml
xml_DATA = file.xml
The special prefix ‘noinst_’ indicates that the objects in question
should be built but not installed at all. This is usually used for
objects required to build the rest of your package, for instance static
libraries (see Building a library), or helper scripts.
The special prefix ‘check_’ indicates that the objects in question
should not be built until the ‘make check’ command is run. Those
objects are not installed either.
The current primary names are ‘PROGRAMS’, ‘LIBRARIES’,
‘LISP’, ‘PYTHON’, ‘JAVA’, ‘SCRIPTS’, ‘DATA’,
‘HEADERS’, ‘MANS’, and ‘TEXINFOS’.
Some primaries also allow additional prefixes that control other
aspects of automake
’s behavior. The currently defined prefixes
are ‘dist_’, ‘nodist_’, ‘nobase_’, and ‘notrans_’.
These prefixes are explained later (see Program and Library Variables)
(see Man pages).
3.4 How derived variables are named
Sometimes a Makefile variable name is derived from some text the
maintainer supplies. For instance, a program name listed in
‘_PROGRAMS’ is rewritten into the name of a ‘_SOURCES’
variable. In cases like this, Automake canonicalizes the text, so that
program names and the like do not have to follow Makefile variable naming
rules. All characters in the name except for letters, numbers, the
strudel (@), and the underscore are turned into underscores when making
variable references.
For example, if your program is named sniff-glue, the derived
variable name would be ‘sniff_glue_SOURCES’, not
‘sniff-glue_SOURCES’. Similarly the sources for a library named
libmumble++.a should be listed in the
‘libmumble___a_SOURCES’ variable.
The strudel is an addition, to make the use of Autoconf substitutions in
variable names less obfuscating.
3.5 Variables reserved for the user
Some Makefile variables are reserved by the GNU Coding Standards
for the use of the “user”—the person building the package. For
instance, CFLAGS
is one such variable.
Sometimes package developers are tempted to set user variables such as
CFLAGS
because it appears to make their job easier. However,
the package itself should never set a user variable, particularly not
to include switches that are required for proper compilation of the
package. Since these variables are documented as being for the
package builder, that person rightfully expects to be able to override
any of these variables at build time.
To get around this problem, Automake introduces an automake-specific
shadow variable for each user flag variable. (Shadow variables are
not introduced for variables like CC
, where they would make no
sense.) The shadow variable is named by prepending ‘AM_’ to the
user variable’s name. For instance, the shadow variable for
YFLAGS
is AM_YFLAGS
. The package maintainer—that is,
the author(s) of the Makefile.am and configure.ac
files—may adjust these shadow variables however necessary.
See Flag Variables Ordering, for more discussion about these
variables and how they interact with per-target variables.
3.6 Programs automake might require
Automake sometimes requires helper programs so that the generated
Makefile can do its work properly. There are a fairly large
number of them, and we list them here.
Although all of these files are distributed and installed with
Automake, a couple of them are maintained separately. The Automake
copies are updated before each release, but we mention the original
source in case you need more recent versions.
ansi2knr.c
ansi2knr.1
These two files are used for de-ANSI-fication support (obsolete
see Automatic de-ANSI-fication).
compile
This is a wrapper for compilers that do not accept options -c
and -o at the same time. It is only used when absolutely
required. Such compilers are rare.
config.guess
config.sub
These two programs compute the canonical triplets for the given build,
host, or target architecture. These programs are updated regularly to
support new architectures and fix probes broken by changes in new
kernel versions. Each new release of Automake comes with up-to-date
copies of these programs. If your copy of Automake is getting old,
you are encouraged to fetch the latest versions of these files from
http://savannah.gnu.org/git/?group=config before making a
release.
config-ml.in
This file is not a program, it is a configure fragment used for
multilib support (see Support for Multilibs). This file is maintained in the
GCC tree at http://gcc.gnu.org/svn.html.
depcomp
This program understands how to run a compiler so that it will
generate not only the desired output but also dependency information
that is then used by the automatic dependency tracking feature
(see Automatic dependency tracking).
elisp-comp
This program is used to byte-compile Emacs Lisp code.
install-sh
This is a replacement for the install
program that works on
platforms where install
is unavailable or unusable.
mdate-sh
This script is used to generate a version.texi file. It examines
a file and prints some date information about it.
missing
This wraps a number of programs that are typically only required by
maintainers. If the program in question doesn’t exist,
missing
prints an informative warning and attempts to fix
things so that the build can continue.
mkinstalldirs
This script used to be a wrapper around ‘mkdir -p’, which is not
portable. Now we prefer to use ‘install-sh -d’ when configure
finds that ‘mkdir -p’ does not work, this makes one less script to
distribute.
For backward compatibility mkinstalldirs is still used and
distributed when automake
finds it in a package. But it is no
longer installed automatically, and it should be safe to remove it.
py-compile
This is used to byte-compile Python scripts.
symlink-tree
This program duplicates a tree of directories, using symbolic links
instead of copying files. Such an operation is performed when building
multilibs (see Support for Multilibs). This file is maintained in the GCC
tree at http://gcc.gnu.org/svn.html.
texinfo.tex
Not a program, this file is required for ‘make dvi’, ‘make
ps’ and ‘make pdf’ to work when Texinfo sources are in the
package. The latest version can be downloaded from
http://www.gnu.org/software/texinfo/.
ylwrap
This program wraps lex
and yacc
to rename their
output files. It also ensures that, for instance, multiple
yacc
instances can be invoked in a single directory in
parallel.
4 Some example packages
This section contains two small examples.
The first example (see A simple example, start to finish) assumes you have an existing
project already using Autoconf, with handcrafted Makefiles, and
that you want to convert it to using Automake. If you are discovering
both tools, it is probably better that you look at the Hello World
example presented earlier (see A Small Hello World).
The second example (see Building true and false) shows how two programs can be built
from the same file, using different compilation parameters. It
contains some technical digressions that are probably best skipped on
first read.
4.1 A simple example, start to finish
Let’s suppose you just finished writing zardoz
, a program to make
your head float from vortex to vortex. You’ve been using Autoconf to
provide a portability framework, but your Makefile.ins have been
ad-hoc. You want to make them bulletproof, so you turn to Automake.
The first step is to update your configure.ac to include the
commands that automake
needs. The way to do this is to add an
AM_INIT_AUTOMAKE
call just after AC_INIT
:
AC_INIT([zardoz], [1.0])
AM_INIT_AUTOMAKE
…
Since your program doesn’t have any complicating factors (e.g., it
doesn’t use gettext
, it doesn’t want to build a shared library),
you’re done with this part. That was easy!
Now you must regenerate configure. But to do that, you’ll need
to tell autoconf
how to find the new macro you’ve used. The
easiest way to do this is to use the aclocal
program to
generate your aclocal.m4 for you. But wait… maybe you
already have an aclocal.m4, because you had to write some hairy
macros for your program. The aclocal
program lets you put
your own macros into acinclude.m4, so simply rename and then
run:
mv aclocal.m4 acinclude.m4
aclocal
autoconf
Now it is time to write your Makefile.am for zardoz
.
Since zardoz
is a user program, you want to install it where the
rest of the user programs go: bindir
. Additionally,
zardoz
has some Texinfo documentation. Your configure.ac
script uses AC_REPLACE_FUNCS
, so you need to link against
‘$(LIBOBJS)’. So here’s what you’d write:
bin_PROGRAMS = zardoz
zardoz_SOURCES = main.c head.c float.c vortex9.c gun.c
zardoz_LDADD = $(LIBOBJS)
info_TEXINFOS = zardoz.texi
Now you can run ‘automake --add-missing’ to generate your
Makefile.in and grab any auxiliary files you might need, and
you’re done!
4.2 Building true and false
Here is another, trickier example. It shows how to generate two
programs (true
and false
) from the same source file
(true.c). The difficult part is that each compilation of
true.c requires different cpp
flags.
bin_PROGRAMS = true false
false_SOURCES =
false_LDADD = false.o
true.o: true.c
$(COMPILE) -DEXIT_CODE=0 -c true.c
false.o: true.c
$(COMPILE) -DEXIT_CODE=1 -o false.o -c true.c
Note that there is no true_SOURCES
definition. Automake will
implicitly assume that there is a source file named true.c
(see Default _SOURCES
), and
define rules to compile true.o and link true. The
‘true.o: true.c’ rule supplied by the above Makefile.am,
will override the Automake generated rule to build true.o.
false_SOURCES
is defined to be empty—that way no implicit value
is substituted. Because we have not listed the source of
false, we have to tell Automake how to link the program. This is
the purpose of the false_LDADD
line. A false_DEPENDENCIES
variable, holding the dependencies of the false target will be
automatically generated by Automake from the content of
false_LDADD
.
The above rules won’t work if your compiler doesn’t accept both
-c and -o. The simplest fix for this is to introduce a
bogus dependency (to avoid problems with a parallel make
):
true.o: true.c false.o
$(COMPILE) -DEXIT_CODE=0 -c true.c
false.o: true.c
$(COMPILE) -DEXIT_CODE=1 -c true.c && mv true.o false.o
Also, these explicit rules do not work if the obsolete de-ANSI-fication feature
is used (see Automatic de-ANSI-fication). Supporting de-ANSI-fication requires a little
more work:
true_.o: true_.c false_.o
$(COMPILE) -DEXIT_CODE=0 -c true_.c
false_.o: true_.c
$(COMPILE) -DEXIT_CODE=1 -c true_.c && mv true_.o false_.o
As it turns out, there is also a much easier way to do this same task.
Some of the above techniques are useful enough that we’ve kept the
example in the manual. However if you were to build true
and
false
in real life, you would probably use per-program
compilation flags, like so:
bin_PROGRAMS = false true
false_SOURCES = true.c
false_CPPFLAGS = -DEXIT_CODE=1
true_SOURCES = true.c
true_CPPFLAGS = -DEXIT_CODE=0
In this case Automake will cause true.c to be compiled twice,
with different flags. De-ANSI-fication will work automatically. In
this instance, the names of the object files would be chosen by
automake; they would be false-true.o and true-true.o.
(The name of the object files rarely matters.)
7 Directories
For simple projects that distributes all files in the same directory
it is enough to have a single Makefile.am that builds
everything in place.
In larger projects it is common to organize files in different
directories, in a tree. For instance one directory per program, per
library or per module. The traditional approach is to build these
subdirectory recursively: each directory contains its Makefile
(generated from Makefile.am), and when make
is run
from the top level directory it enters each subdirectory in turn to
build its contents.
7.1 Recursing subdirectories
In packages with subdirectories, the top level Makefile.am must
tell Automake which subdirectories are to be built. This is done via
the SUBDIRS
variable.
The SUBDIRS
variable holds a list of subdirectories in which
building of various sorts can occur. The rules for many targets
(e.g., all
) in the generated Makefile will run commands
both locally and in all specified subdirectories. Note that the
directories listed in SUBDIRS
are not required to contain
Makefile.ams; only Makefiles (after configuration).
This allows inclusion of libraries from packages that do not use
Automake (such as gettext
; see also Third-Party Makefiles).
In packages that use subdirectories, the top-level Makefile.am is
often very short. For instance, here is the Makefile.am from the
GNU Hello distribution:
EXTRA_DIST = BUGS ChangeLog.O README-alpha
SUBDIRS = doc intl po src tests
When Automake invokes make
in a subdirectory, it uses the value
of the MAKE
variable. It passes the value of the variable
AM_MAKEFLAGS
to the make
invocation; this can be set in
Makefile.am if there are flags you must always pass to
make
.
The directories mentioned in SUBDIRS
are usually direct
children of the current directory, each subdirectory containing its
own Makefile.am with a SUBDIRS
pointing to deeper
subdirectories. Automake can be used to construct packages of
arbitrary depth this way.
By default, Automake generates Makefiles that work depth-first
in postfix order: the subdirectories are built before the current
directory. However, it is possible to change this ordering. You can
do this by putting ‘.’ into SUBDIRS
. For instance,
putting ‘.’ first will cause a prefix ordering of
directories.
Using
will cause lib/ to be built before src/, then the
current directory will be built, finally the test/ directory
will be built. It is customary to arrange test directories to be
built after everything else since they are meant to test what has
been constructed.
All clean
rules are run in reverse order of build rules.
7.2 Conditional Subdirectories
It is possible to define the SUBDIRS
variable conditionally if,
like in the case of GNU Inetutils, you want to only build a subset of
the entire package.
To illustrate how this works, let’s assume we have two directories
src/ and opt/. src/ should always be built, but we
want to decide in configure
whether opt/ will be built
or not. (For this example we will assume that opt/ should be
built when the variable ‘$want_opt’ was set to ‘yes’.)
Running make
should thus recurse into src/ always, and
then maybe in opt/.
However ‘make dist’ should always recurse into both src/
and opt/. Because opt/ should be distributed even if it
is not needed in the current configuration. This means
opt/Makefile should be created unconditionally.
There are two ways to setup a project like this. You can use Automake
conditionals (see Conditionals) or use Autoconf AC_SUBST
variables (see Setting Output
Variables in The Autoconf Manual). Using Automake
conditionals is the preferred solution. Before we illustrate these
two possibilities, let’s introduce DIST_SUBDIRS
.
7.2.1 SUBDIRS
vs. DIST_SUBDIRS
Automake considers two sets of directories, defined by the variables
SUBDIRS
and DIST_SUBDIRS
.
SUBDIRS
contains the subdirectories of the current directory
that must be built (see Recursing subdirectories). It must be defined
manually; Automake will never guess a directory is to be built. As we
will see in the next two sections, it is possible to define it
conditionally so that some directory will be omitted from the build.
DIST_SUBDIRS
is used in rules that need to recurse in all
directories, even those that have been conditionally left out of the
build. Recall our example where we may not want to build subdirectory
opt/, but yet we want to distribute it? This is where
DIST_SUBDIRS
come into play: ‘opt’ may not appear in
SUBDIRS
, but it must appear in DIST_SUBDIRS
.
Precisely, DIST_SUBDIRS
is used by ‘make
maintainer-clean’, ‘make distclean’ and ‘make dist’. All
other recursive rules use SUBDIRS
.
If SUBDIRS
is defined conditionally using Automake
conditionals, Automake will define DIST_SUBDIRS
automatically
from the possibles values of SUBDIRS
in all conditions.
If SUBDIRS
contains AC_SUBST
variables,
DIST_SUBDIRS
will not be defined correctly because Automake
does not know the possible values of these variables. In this case
DIST_SUBDIRS
needs to be defined manually.
7.2.2 Conditional subdirectories with AM_CONDITIONAL
configure should output the Makefile for each directory
and define a condition into which opt/ should be built.
…
AM_CONDITIONAL([COND_OPT], [test "$want_opt" = yes])
AC_CONFIG_FILES([Makefile src/Makefile opt/Makefile])
…
Then SUBDIRS
can be defined in the top-level Makefile.am
as follows.
if COND_OPT
MAYBE_OPT = opt
endif
SUBDIRS = src $(MAYBE_OPT)
As you can see, running make
will rightly recurse into
src/ and maybe opt/.
As you can’t see, running ‘make dist’ will recurse into both
src/ and opt/ directories because ‘make dist’, unlike
‘make all’, doesn’t use the SUBDIRS
variable. It uses the
DIST_SUBDIRS
variable.
In this case Automake will define ‘DIST_SUBDIRS = src opt’
automatically because it knows that MAYBE_OPT
can contain
‘opt’ in some condition.
7.2.3 Conditional Subdirectories with AC_SUBST
Another possibility is to define MAYBE_OPT
from
./configure using AC_SUBST
:
…
if test "$want_opt" = yes; then
MAYBE_OPT=opt
else
MAYBE_OPT=
fi
AC_SUBST([MAYBE_OPT])
AC_CONFIG_FILES([Makefile src/Makefile opt/Makefile])
…
In this case the top-level Makefile.am should look as follows.
SUBDIRS = src $(MAYBE_OPT)
DIST_SUBDIRS = src opt
The drawback is that since Automake cannot guess what the possible
values of MAYBE_OPT
are, it is necessary to define
DIST_SUBDIRS
.
7.3 An Alternative Approach to Subdirectories
If you’ve ever read Peter Miller’s excellent paper,
Recursive Make Considered Harmful, the preceding sections on the use of
subdirectories will probably come as unwelcome advice. For those who
haven’t read the paper, Miller’s main thesis is that recursive
make
invocations are both slow and error-prone.
Automake provides sufficient cross-directory support 3 to enable you
to write a single Makefile.am for a complex multi-directory
package.
By default an installable file specified in a subdirectory will have its
directory name stripped before installation. For instance, in this
example, the header file will be installed as
$(includedir)/stdio.h:
include_HEADERS = inc/stdio.h
However, the ‘nobase_’ prefix can be used to circumvent this path
stripping. In this example, the header file will be installed as
$(includedir)/sys/types.h:
nobase_include_HEADERS = sys/types.h
‘nobase_’ should be specified first when used in conjunction with
either ‘dist_’ or ‘nodist_’ (see What Goes in a Distribution). For instance:
nobase_dist_pkgdata_DATA = images/vortex.pgm sounds/whirl.ogg
Finally, note that a variable using the ‘nobase_’ prefix can
always be replaced by several variables, one for each destination
directory (see The Uniform Naming Scheme). For instance, the last example could be
rewritten as follows:
imagesdir = $(pkgdatadir)/images
soundsdir = $(pkgdatadir)/sounds
dist_images_DATA = images/vortex.pgm
dist_sounds_DATA = sounds/whirl.ogg
This latter syntax makes it possible to change one destination
directory without changing the layout of the source tree.
7.4 Nesting Packages
In the GNU Build System, packages can be nested to arbitrary depth.
This means that a package can embedded other packages with their own
configure, Makefiles, etc.
These other packages should just appear as subdirectories of their
parent package. They must be listed in SUBDIRS
like other
ordinary directories. However the subpackage’s Makefiles
should be output by its own configure script, not by the
parent’s configure. This is achieved using the
AC_CONFIG_SUBDIRS
Autoconf macro (see Configuring Other Packages in Subdirectories in The Autoconf Manual).
Here is an example package for an arm
program that links with
an hand
library that is a nested package in subdirectory
hand/.
arm
’s configure.ac:
AC_INIT([arm], [1.0])
AC_CONFIG_AUX_DIR([.])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_CONFIG_FILES([Makefile])
# Call hand's ./configure script recursively.
AC_CONFIG_SUBDIRS([hand])
AC_OUTPUT
arm
’s Makefile.am:
# Build the library in the hand subdirectory first.
SUBDIRS = hand
# Include hand's header when compiling this directory.
AM_CPPFLAGS = -I$(srcdir)/hand
bin_PROGRAMS = arm
arm_SOURCES = arm.c
# link with the hand library.
arm_LDADD = hand/libhand.a
Now here is hand
’s hand/configure.ac:
AC_INIT([hand], [1.2])
AC_CONFIG_AUX_DIR([.])
AM_INIT_AUTOMAKE
AC_PROG_CC
AC_PROG_RANLIB
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
and its hand/Makefile.am:
lib_LIBRARIES = libhand.a
libhand_a_SOURCES = hand.c
When ‘make dist’ is run from the top-level directory it will
create an archive arm-1.0.tar.gz that contains the arm
code as well as the hand subdirectory. This package can be
built and installed like any ordinary package, with the usual
‘./configure && make && make install’ sequence (the hand
subpackage will be built and installed by the process).
When ‘make dist’ is run from the hand directory, it will create a
self-contained hand-1.2.tar.gz archive. So although it appears
to be embedded in another package, it can still be used separately.
The purpose of the ‘AC_CONFIG_AUX_DIR([.])’ instruction is to
force Automake and Autoconf to search for auxiliary scripts in the
current directory. For instance, this means that there will be two
copies of install-sh: one in the top-level of the arm
package, and another one in the hand/ subdirectory for the
hand
package.
The historical default is to search for these auxiliary scripts in
the parent directory and the grandparent directory. So if the
‘AC_CONFIG_AUX_DIR([.])’ line was removed from
hand/configure.ac, that subpackage would share the auxiliary
script of the arm
package. This may looks like a gain in size
(a few kilobytes), but it is actually a loss of modularity as the
hand
subpackage is no longer self-contained (‘make dist’
in the subdirectory will not work anymore).
Packages that do not use Automake need more work to be integrated this
way. See Third-Party Makefiles.
8 Building Programs and Libraries
A large part of Automake’s functionality is dedicated to making it easy
to build programs and libraries.
8.1 Building a program
In order to build a program, you need to tell Automake which sources
are part of it, and which libraries it should be linked with.
This section also covers conditional compilation of sources or
programs. Most of the comments about these also apply to libraries
(see Building a library) and libtool libraries (see Building a Shared Library).
8.1.1 Defining program sources
In a directory containing source that gets built into a program (as
opposed to a library or a script), the PROGRAMS
primary is used.
Programs can be installed in bindir
, sbindir
,
libexecdir
, pkglibdir
, pkglibexecdir
, or not at all
(noinst_
). They can also be built only for ‘make check’, in
which case the prefix is ‘check_’.
For instance:
In this simple case, the resulting Makefile.in will contain code
to generate a program named hello
.
Associated with each program are several assisting variables that are
named after the program. These variables are all optional, and have
reasonable defaults. Each variable, its use, and default is spelled out
below; we use the “hello” example throughout.
The variable hello_SOURCES
is used to specify which source files
get built into an executable:
hello_SOURCES = hello.c version.c getopt.c getopt1.c getopt.h system.h
This causes each mentioned .c file to be compiled into the
corresponding .o. Then all are linked to produce hello.
If hello_SOURCES
is not specified, then it defaults to the single
file hello.c (see Default _SOURCES
).
Multiple programs can be built in a single directory. Multiple programs
can share a single source file, which must be listed in each
_SOURCES
definition.
Header files listed in a _SOURCES
definition will be included in
the distribution but otherwise ignored. In case it isn’t obvious, you
should not include the header file generated by configure in a
_SOURCES
variable; this file should not be distributed. Lex
(.l) and Yacc (.y) files can also be listed; see Yacc and Lex support.
8.1.2 Linking the program
If you need to link against libraries that are not found by
configure
, you can use LDADD
to do so. This variable is
used to specify additional objects or libraries to link with; it is
inappropriate for specifying specific linker flags, you should use
AM_LDFLAGS
for this purpose.
Sometimes, multiple programs are built in one directory but do not share
the same link-time requirements. In this case, you can use the
prog_LDADD
variable (where prog is the name of the
program as it appears in some _PROGRAMS
variable, and usually
written in lowercase) to override the global LDADD
. If this
variable exists for a given program, then that program is not linked
using LDADD
.
For instance, in GNU cpio, pax
, cpio
and mt
are
linked against the library libcpio.a. However, rmt
is
built in the same directory, and has no such link requirement. Also,
mt
and rmt
are only built on certain architectures. Here
is what cpio’s src/Makefile.am looks like (abridged):
bin_PROGRAMS = cpio pax $(MT)
libexec_PROGRAMS = $(RMT)
EXTRA_PROGRAMS = mt rmt
LDADD = ../lib/libcpio.a $(INTLLIBS)
rmt_LDADD =
cpio_SOURCES = …
pax_SOURCES = …
mt_SOURCES = …
rmt_SOURCES = …
prog_LDADD
is inappropriate for passing program-specific
linker flags (except for -l, -L, -dlopen and
-dlpreopen). So, use the prog_LDFLAGS
variable for
this purpose.
It is also occasionally useful to have a program depend on some other
target that is not actually part of that program. This can be done
using the prog_DEPENDENCIES
variable. Each program
depends on the contents of such a variable, but no further
interpretation is done.
Since these dependencies are associated to the link rule used to
create the programs they should normally list files used by the link
command. That is *.$(OBJEXT), *.a, or *.la
files. In rare cases you may need to add other kinds of files such as
linker scripts, but listing a source file in
_DEPENDENCIES
is wrong. If some source file needs to be built
before all the components of a program are built, consider using the
BUILT_SOURCES
variable instead (see Built sources).
If prog_DEPENDENCIES
is not supplied, it is computed by
Automake. The automatically-assigned value is the contents of
prog_LDADD
, with most configure substitutions, -l,
-L, -dlopen and -dlpreopen options removed. The
configure substitutions that are left in are only ‘$(LIBOBJS)’ and
‘$(ALLOCA)’; these are left because it is known that they will not
cause an invalid value for prog_DEPENDENCIES
to be
generated.
Conditional compilation of sources shows a situation where _DEPENDENCIES
may be used.
We recommend that you avoid using -l options in LDADD
or prog_LDADD
when referring to libraries built by your
package. Instead, write the file name of the library explicitly as in
the above cpio
example. Use -l only to list
third-party libraries. If you follow this rule, the default value of
prog_DEPENDENCIES
will list all your local libraries and
omit the other ones.
8.1.3 Conditional compilation of sources
You can’t put a configure substitution (e.g., ‘@FOO@’ or
‘$(FOO)’ where FOO
is defined via AC_SUBST
) into a
_SOURCES
variable. The reason for this is a bit hard to
explain, but suffice to say that it simply won’t work. Automake will
give an error if you try to do this.
Fortunately there are two other ways to achieve the same result. One is
to use configure substitutions in _LDADD
variables, the other is
to use an Automake conditional.
8.1.3.1 Conditional compilation using _LDADD
substitutions
Automake must know all the source files that could possibly go into a
program, even if not all the files are built in every circumstance. Any
files that are only conditionally built should be listed in the
appropriate EXTRA_
variable. For instance, if
hello-linux.c or hello-generic.c were conditionally included
in hello
, the Makefile.am would contain:
bin_PROGRAMS = hello
hello_SOURCES = hello-common.c
EXTRA_hello_SOURCES = hello-linux.c hello-generic.c
hello_LDADD = $(HELLO_SYSTEM)
hello_DEPENDENCIES = $(HELLO_SYSTEM)
You can then setup the ‘$(HELLO_SYSTEM)’ substitution from
configure.ac:
…
case $host in
*linux*) HELLO_SYSTEM='hello-linux.$(OBJEXT)' ;;
*) HELLO_SYSTEM='hello-generic.$(OBJEXT)' ;;
esac
AC_SUBST([HELLO_SYSTEM])
…
In this case, the variable HELLO_SYSTEM
should be replaced by
either hello-linux.o or hello-generic.o, and added to
both hello_DEPENDENCIES
and hello_LDADD
in order to be
built and linked in.
8.1.3.2 Conditional compilation using Automake conditionals
An often simpler way to compile source files conditionally is to use
Automake conditionals. For instance, you could use this
Makefile.am construct to build the same hello example:
bin_PROGRAMS = hello
if LINUX
hello_SOURCES = hello-linux.c hello-common.c
else
hello_SOURCES = hello-generic.c hello-common.c
endif
In this case, configure.ac should setup the LINUX
conditional using AM_CONDITIONAL
(see Conditionals).
When using conditionals like this you don’t need to use the
EXTRA_
variable, because Automake will examine the contents of
each variable to construct the complete list of source files.
If your program uses a lot of files, you will probably prefer a
conditional ‘+=’.
bin_PROGRAMS = hello
hello_SOURCES = hello-common.c
if LINUX
hello_SOURCES += hello-linux.c
else
hello_SOURCES += hello-generic.c
endif
8.1.4 Conditional compilation of programs
Sometimes it is useful to determine the programs that are to be built
at configure time. For instance, GNU cpio
only builds
mt
and rmt
under special circumstances. The means to
achieve conditional compilation of programs are the same you can use
to compile source files conditionally: substitutions or conditionals.
8.1.4.2 Conditional programs using Automake conditionals
You can also use Automake conditionals (see Conditionals) to
select programs to be built. In this case you don’t have to worry
about ‘$(EXEEXT)’ or EXTRA_PROGRAMS
.
bin_PROGRAMS = cpio pax
if WANT_MT
bin_PROGRAMS += mt
endif
if WANT_RMT
libexec_PROGRAMS = rmt
endif
8.2 Building a library
Building a library is much like building a program. In this case, the
name of the primary is LIBRARIES
. Libraries can be installed in
libdir
or pkglibdir
.
See Building a Shared Library, for information on how to build shared
libraries using libtool and the LTLIBRARIES
primary.
Each _LIBRARIES
variable is a list of the libraries to be built.
For instance, to create a library named libcpio.a, but not install
it, you would write:
noinst_LIBRARIES = libcpio.a
libcpio_a_SOURCES = …
The sources that go into a library are determined exactly as they are
for programs, via the _SOURCES
variables. Note that the library
name is canonicalized (see How derived variables are named), so the _SOURCES
variable corresponding to libcpio.a is ‘libcpio_a_SOURCES’,
not ‘libcpio.a_SOURCES’.
Extra objects can be added to a library using the
library_LIBADD
variable. This should be used for objects
determined by configure
. Again from cpio
:
libcpio_a_LIBADD = $(LIBOBJS) $(ALLOCA)
In addition, sources for extra objects that will not exist until
configure-time must be added to the BUILT_SOURCES
variable
(see Built sources).
Building a static library is done by compiling all object files, then
by invoking ‘$(AR) $(ARFLAGS)’ followed by the name of the
library and the list of objects, and finally by calling
‘$(RANLIB)’ on that library. You should call
AC_PROG_RANLIB
from your configure.ac to define
RANLIB
(Automake will complain otherwise). AR
and
ARFLAGS
default to ar
and cru
respectively; you
can override these two variables my setting them in your
Makefile.am, by AC_SUBST
ing them from your
configure.ac, or by defining a per-library maude_AR
variable (see Program and Library Variables).
Be careful when selecting library components conditionally. Because
building an empty library is not portable, you should ensure that any
library contains always at least one object.
To use a static library when building a program, add it to
LDADD
for this program. In the following example, the program
cpio is statically linked with the library libcpio.a.
noinst_LIBRARIES = libcpio.a
libcpio_a_SOURCES = …
bin_PROGRAMS = cpio
cpio_SOURCES = cpio.c …
cpio_LDADD = libcpio.a
8.3 Building a Shared Library
Building shared libraries portably is a relatively complex matter.
For this reason, GNU Libtool (see Introduction in The
Libtool Manual) was created to help build shared libraries in a
platform-independent way.
8.3.5 Libtool Convenience Libraries
Sometimes you want to build libtool libraries that should not be
installed. These are called libtool convenience libraries and
are typically used to encapsulate many sublibraries, later gathered
into one big installed library.
Libtool convenience libraries are declared by directory-less variables
such as noinst_LTLIBRARIES
, check_LTLIBRARIES
, or even
EXTRA_LTLIBRARIES
. Unlike installed libtool libraries they do
not need an -rpath flag at link time (actually this is the only
difference).
Convenience libraries listed in noinst_LTLIBRARIES
are always
built. Those listed in check_LTLIBRARIES
are built only upon
‘make check’. Finally, libraries listed in
EXTRA_LTLIBRARIES
are never built explicitly: Automake outputs
rules to build them, but if the library does not appear as a Makefile
dependency anywhere it won’t be built (this is why
EXTRA_LTLIBRARIES
is used for conditional compilation).
Here is a sample setup merging libtool convenience libraries from
subdirectories into one main libtop.la library.
# -- Top-level Makefile.am --
SUBDIRS = sub1 sub2 …
lib_LTLIBRARIES = libtop.la
libtop_la_SOURCES =
libtop_la_LIBADD = \
sub1/libsub1.la \
sub2/libsub2.la \
…
# -- sub1/Makefile.am --
noinst_LTLIBRARIES = libsub1.la
libsub1_la_SOURCES = …
# -- sub2/Makefile.am --
# showing nested convenience libraries
SUBDIRS = sub2.1 sub2.2 …
noinst_LTLIBRARIES = libsub2.la
libsub2_la_SOURCES =
libsub2_la_LIBADD = \
sub21/libsub21.la \
sub22/libsub22.la \
…
When using such setup, beware that automake
will assume
libtop.la is to be linked with the C linker. This is because
libtop_la_SOURCES
is empty, so automake
picks C as
default language. If libtop_la_SOURCES
was not empty,
automake
would select the linker as explained in How the Linker is Chosen.
If one of the sublibraries contains non-C source, it is important that
the appropriate linker be chosen. One way to achieve this is to
pretend that there is such a non-C file among the sources of the
library, thus forcing automake
to select the appropriate
linker. Here is the top-level Makefile of our example updated
to force C++ linking.
SUBDIRS = sub1 sub2 …
lib_LTLIBRARIES = libtop.la
libtop_la_SOURCES =
# Dummy C++ source to cause C++ linking.
nodist_EXTRA_libtop_la_SOURCES = dummy.cxx
libtop_la_LIBADD = \
sub1/libsub1.la \
sub2/libsub2.la \
…
‘EXTRA_*_SOURCES’ variables are used to keep track of source
files that might be compiled (this is mostly useful when doing
conditional compilation using AC_SUBST
, see Libtool Libraries with Conditional Sources), and the nodist_
prefix means the listed
sources are not to be distributed (see Program and Library Variables). In effect the file dummy.cxx does not need to
exist in the source tree. Of course if you have some real source file
to list in libtop_la_SOURCES
there is no point in cheating with
nodist_EXTRA_libtop_la_SOURCES
.
8.3.8 LTLIBOBJS
and LTALLOCA
Where an ordinary library might include ‘$(LIBOBJS)’ or
‘$(ALLOCA)’ (see Special handling for LIBOBJS
and ALLOCA
), a libtool library must use
‘$(LTLIBOBJS)’ or ‘$(LTALLOCA)’. This is required because
the object files that libtool operates on do not necessarily end in
.o.
Nowadays, the computation of LTLIBOBJS
from LIBOBJS
is
performed automatically by Autoconf (see AC_LIBOBJ
vs. LIBOBJS
in The Autoconf Manual).
8.4 Program and Library Variables
Associated with each program are a collection of variables that can be
used to modify how that program is built. There is a similar list of
such variables for each library. The canonical name of the program (or
library) is used as a base for naming these variables.
In the list below, we use the name “maude” to refer to the program or
library. In your Makefile.am you would replace this with the
canonical name of your program. This list also refers to “maude” as a
program, but in general the same rules apply for both static and dynamic
libraries; the documentation below notes situations where programs and
libraries differ.
maude_SOURCES
¶
This variable, if it exists, lists all the source files that are
compiled to build the program. These files are added to the
distribution by default. When building the program, Automake will cause
each source file to be compiled to a single .o file (or
.lo when using libtool). Normally these object files are named
after the source file, but other factors can change this. If a file in
the _SOURCES
variable has an unrecognized extension, Automake
will do one of two things with it. If a suffix rule exists for turning
files with the unrecognized extension into .o files, then
automake will treat this file as it will any other source file
(see Support for Other Languages). Otherwise, the file will be
ignored as though it were a header file.
The prefixes dist_
and nodist_
can be used to control
whether files listed in a _SOURCES
variable are distributed.
dist_
is redundant, as sources are distributed by default, but it
can be specified for clarity if desired.
It is possible to have both dist_
and nodist_
variants of
a given _SOURCES
variable at once; this lets you easily
distribute some files and not others, for instance:
nodist_maude_SOURCES = nodist.c
dist_maude_SOURCES = dist-me.c
By default the output file (on Unix systems, the .o file) will
be put into the current build directory. However, if the option
subdir-objects is in effect in the current directory then the
.o file will be put into the subdirectory named after the
source file. For instance, with subdir-objects enabled,
sub/dir/file.c will be compiled to sub/dir/file.o. Some
people prefer this mode of operation. You can specify
subdir-objects in AUTOMAKE_OPTIONS
(see Changing Automake’s Behavior).
Automake needs to know the list of files you intend to compile
statically. For one thing, this is the only way Automake has of
knowing what sort of language support a given Makefile.in
requires. 4 This means that, for example, you can’t put a
configure substitution like ‘@my_sources@’ into a ‘_SOURCES’
variable. If you intend to conditionally compile source files and use
configure to substitute the appropriate object names into, e.g.,
_LDADD
(see below), then you should list the corresponding source
files in the EXTRA_
variable.
This variable also supports dist_
and nodist_
prefixes.
For instance, nodist_EXTRA_maude_SOURCES
would list extra
sources that may need to be built, but should not be distributed.
maude_AR
¶
A static library is created by default by invoking ‘$(AR)
$(ARFLAGS)’ followed by the name of the library and then the objects
being put into the library. You can override this by setting the
_AR
variable. This is usually used with C++; some C++
compilers require a special invocation in order to instantiate all the
templates that should go into a library. For instance, the SGI C++
compiler likes this variable set like so:
libmaude_a_AR = $(CXX) -ar -o
maude_LIBADD
¶
Extra objects can be added to a library using the _LIBADD
variable. For instance, this should be used for objects determined by
configure
(see Building a library).
In the case of libtool libraries, maude_LIBADD
can also refer
to other libtool libraries.
maude_LDADD
¶
Extra objects (*.$(OBJEXT)) and libraries (*.a,
*.la) can be added to a program by listing them in the
_LDADD
variable. For instance, this should be used for objects
determined by configure
(see Linking the program).
_LDADD
and _LIBADD
are inappropriate for passing
program-specific linker flags (except for -l, -L,
-dlopen and -dlpreopen). Use the _LDFLAGS
variable
for this purpose.
For instance, if your configure.ac uses AC_PATH_XTRA
, you
could link your program against the X libraries like so:
maude_LDADD = $(X_PRE_LIBS) $(X_LIBS) $(X_EXTRA_LIBS)
We recommend that you use -l and -L only when
referring to third-party libraries, and give the explicit file names
of any library built by your package. Doing so will ensure that
maude_DEPENDENCIES
(see below) is correctly defined by default.
maude_LDFLAGS
¶
This variable is used to pass extra flags to the link step of a program
or a shared library. It overrides the global AM_LDFLAGS
variable.
maude_LIBTOOLFLAGS
¶
This variable is used to pass extra options to libtool
.
It overrides the global AM_LIBTOOLFLAGS
variable.
These options are output before libtool
’s --mode=MODE
option, so they should not be mode-specific options (those belong to
the compiler or linker flags). See _LIBADD
, _LDFLAGS
, and _LIBTOOLFLAGS
.
maude_DEPENDENCIES
¶
It is also occasionally useful to have a target (program or library)
depend on some other file that is not actually part of that target.
This can be done using the _DEPENDENCIES
variable. Each
targets depends on the contents of such a variable, but no further
interpretation is done.
Since these dependencies are associated to the link rule used to
create the programs they should normally list files used by the link
command. That is *.$(OBJEXT), *.a, or *.la files
for programs; *.lo and *.la files for Libtool libraries;
and *.$(OBJEXT) files for static libraries. In rare cases you
may need to add other kinds of files such as linker scripts, but
listing a source file in _DEPENDENCIES
is wrong. If
some source file needs to be built before all the components of a
program are built, consider using the BUILT_SOURCES
variable
(see Built sources).
If _DEPENDENCIES
is not supplied, it is computed by Automake.
The automatically-assigned value is the contents of _LDADD
or
_LIBADD
, with most configure substitutions, -l, -L,
-dlopen and -dlpreopen options removed. The configure
substitutions that are left in are only ‘$(LIBOBJS)’ and
‘$(ALLOCA)’; these are left because it is known that they will not
cause an invalid value for _DEPENDENCIES
to be generated.
_DEPENDENCIES
is more likely used to perform conditional
compilation using an AC_SUBST
variable that contains a list of
objects. See Conditional compilation of sources, and Libtool Libraries with Conditional Sources.
maude_LINK
¶
You can override the linker on a per-program basis. By default the
linker is chosen according to the languages used by the program. For
instance, a program that includes C++ source code would use the C++
compiler to link. The _LINK
variable must hold the name of a
command that can be passed all the .o file names as arguments.
Note that the name of the underlying program is not passed to
_LINK
; typically one uses ‘$@’:
maude_LINK = $(CCLD) -magic -o $@
maude_CCASFLAGS
¶
maude_CFLAGS
¶
maude_CPPFLAGS
¶
maude_CXXFLAGS
¶
maude_FFLAGS
¶
maude_GCJFLAGS
¶
maude_LFLAGS
¶
maude_OBJCFLAGS
¶
maude_RFLAGS
¶
maude_UPCFLAGS
¶
maude_YFLAGS
¶
-
Automake allows you to set compilation flags on a per-program (or
per-library) basis. A single source file can be included in several
programs, and it will potentially be compiled with different flags for
each program. This works for any language directly supported by
Automake. These per-target compilation flags are
‘_CCASFLAGS’,
‘_CFLAGS’,
‘_CPPFLAGS’,
‘_CXXFLAGS’,
‘_FFLAGS’,
‘_GCJFLAGS’,
‘_LFLAGS’,
‘_OBJCFLAGS’,
‘_RFLAGS’,
‘_UPCFLAGS’, and
‘_YFLAGS’.
When using a per-target compilation flag, Automake will choose a
different name for the intermediate object files. Ordinarily a file
like sample.c will be compiled to produce sample.o.
However, if the program’s _CFLAGS
variable is set, then the
object file will be named, for instance, maude-sample.o. (See
also Why are object files sometimes renamed?.) The use of per-target compilation flags
with C sources requires that the macro AM_PROG_CC_C_O
be called
from configure.ac.
In compilations with per-target flags, the ordinary ‘AM_’ form of
the flags variable is not automatically included in the
compilation (however, the user form of the variable is included).
So for instance, if you want the hypothetical maude compilations
to also use the value of AM_CFLAGS
, you would need to write:
maude_CFLAGS = … your flags … $(AM_CFLAGS)
See Flag Variables Ordering, for more discussion about the
interaction between user variables, ‘AM_’ shadow variables, and
per-target variables.
maude_SHORTNAME
¶
On some platforms the allowable file names are very short. In order to
support these systems and per-target compilation flags at the same
time, Automake allows you to set a “short name” that will influence
how intermediate object files are named. For instance, in the following
example,
bin_PROGRAMS = maude
maude_CPPFLAGS = -DSOMEFLAG
maude_SHORTNAME = m
maude_SOURCES = sample.c …
the object file would be named m-sample.o rather than
maude-sample.o.
This facility is rarely needed in practice,
and we recommend avoiding it until you find it is required.
8.5 Default _SOURCES
_SOURCES
variables are used to specify source files of programs
(see Building a program), libraries (see Building a library), and Libtool
libraries (see Building a Shared Library).
When no such variable is specified for a target, Automake will define
one itself. The default is to compile a single C file whose base name
is the name of the target itself, with any extension replaced by
.c. (Defaulting to C is terrible but we are stuck with it for
historical reasons.)
For example if you have the following somewhere in your
Makefile.am with no corresponding libfoo_a_SOURCES
:
lib_LIBRARIES = libfoo.a sub/libc++.a
libfoo.a will be built using a default source file named
libfoo.c, and sub/libc++.a will be built from
sub/libc++.c. (In older versions sub/libc++.a
would be built from sub_libc___a.c, i.e., the default source
was the canonized name of the target, with .c appended.
We believe the new behavior is more sensible, but for backward
compatibility automake will use the old name if a file or a rule
with that name exist.)
Default sources are mainly useful in test suites, when building many
tests programs each from a single source. For instance, in
check_PROGRAMS = test1 test2 test3
test1, test2, and test3 will be built
from test1.c, test2.c, and test3.c.
Another case where is this convenient is building many Libtool modules
(moduleN.la), each defined in its own file (moduleN.c).
AM_LDFLAGS = -module
lib_LTLIBRARIES = module1.la module2.la module3.la
Finally, there is one situation where this default source computation
needs to be avoided: when a target should not be built from sources.
We already saw such an example in Building true and false; this happens when all
the constituents of a target have already been compiled and need just
to be combined using a _LDADD
variable. Then it is necessary
to define an empty _SOURCES
variable, so that automake does not
compute a default.
bin_PROGRAMS = target
target_SOURCES =
target_LDADD = libmain.a libmisc.a
8.6 Special handling for LIBOBJS
and ALLOCA
The ‘$(LIBOBJS)’ and ‘$(ALLOCA)’ variables list object
files that should be compiled into the project to provide an
implementation for functions that are missing or broken on the host
system. They are substituted by configure.
These variables are defined by Autoconf macros such as
AC_LIBOBJ
, AC_REPLACE_FUNCS
(see Generic Function Checks in The Autoconf Manual), or
AC_FUNC_ALLOCA
(see Particular
Function Checks in The Autoconf Manual). Many other Autoconf
macros call AC_LIBOBJ
or AC_REPLACE_FUNCS
to
populate ‘$(LIBOBJS)’.
Using these variables is very similar to doing conditional compilation
using AC_SUBST
variables, as described in Conditional compilation of sources. That is, when building a program, ‘$(LIBOBJS)’ and
‘$(ALLOCA)’ should be added to the associated ‘*_LDADD’
variable, or to the ‘*_LIBADD’ variable when building a library.
However there is no need to list the corresponding sources in
‘EXTRA_*_SOURCES’ nor to define ‘*_DEPENDENCIES’. Automake
automatically adds ‘$(LIBOBJS)’ and ‘$(ALLOCA)’ to the
dependencies, and it will discover the list of corresponding source
files automatically (by tracing the invocations of the
AC_LIBSOURCE
Autoconf macros).
These variables are usually used to build a portability library that
is linked with all the programs of the project. We now review a
sample setup. First, configure.ac contains some checks that
affect either LIBOBJS
or ALLOCA
.
# configure.ac
…
AC_CONFIG_LIBOBJ_DIR([lib])
…
AC_FUNC_MALLOC dnl May add malloc.$(OBJEXT) to LIBOBJS
AC_FUNC_MEMCMP dnl May add memcmp.$(OBJEXT) to LIBOBJS
AC_REPLACE_FUNCS([strdup]) dnl May add strdup.$(OBJEXT) to LIBOBJS
AC_FUNC_ALLOCA dnl May add alloca.$(OBJEXT) to ALLOCA
…
AC_CONFIG_FILES([
lib/Makefile
src/Makefile
])
AC_OUTPUT
The AC_CONFIG_LIBOBJ_DIR
tells Autoconf that the source files
of these object files are to be found in the lib/ directory.
Automake can also use this information, otherwise it expects the
source files are to be in the directory where the ‘$(LIBOBJS)’
and ‘$(ALLOCA)’ variables are used.
The lib/ directory should therefore contain malloc.c,
memcmp.c, strdup.c, alloca.c. Here is its
Makefile.am:
# lib/Makefile.am
noinst_LIBRARIES = libcompat.a
libcompat_a_SOURCES =
libcompat_a_LIBADD = $(LIBOBJS) $(ALLOCA)
The library can have any name, of course, and anyway it is not going
to be installed: it just holds the replacement versions of the missing
or broken functions so we can later link them in. In many projects
also include extra functions, specific to the project, in that
library: they are simply added on the _SOURCES
line.
There is a small trap here, though: ‘$(LIBOBJS)’ and
‘$(ALLOCA)’ might be empty, and building an empty library is not
portable. You should ensure that there is always something to put in
libcompat.a. Most projects will also add some utility
functions in that directory, and list them in
libcompat_a_SOURCES
, so in practice libcompat.a cannot
be empty.
Finally here is how this library could be used from the src/
directory.
# src/Makefile.am
# Link all programs in this directory with libcompat.a
LDADD = ../lib/libcompat.a
bin_PROGRAMS = tool1 tool2 …
tool1_SOURCES = …
tool2_SOURCES = …
When option subdir-objects is not used, as in the above
example, the variables ‘$(LIBOBJS)’ or ‘$(ALLOCA)’ can only
be used in the directory where their sources lie. E.g., here it would
be wrong to use ‘$(LIBOBJS)’ or ‘$(ALLOCA)’ in
src/Makefile.am. However if both subdir-objects and
AC_CONFIG_LIBOBJ_DIR
are used, it is OK to use these variables
in other directories. For instance src/Makefile.am could be
changed as follows.
# src/Makefile.am
AUTOMAKE_OPTIONS = subdir-objects
LDADD = $(LIBOBJS) $(ALLOCA)
bin_PROGRAMS = tool1 tool2 …
tool1_SOURCES = …
tool2_SOURCES = …
Because ‘$(LIBOBJS)’ and ‘$(ALLOCA)’ contain object
file names that end with ‘.$(OBJEXT)’, they are not suitable for
Libtool libraries (where the expected object extension is .lo):
LTLIBOBJS
and LTALLOCA
should be used instead.
LTLIBOBJS
is defined automatically by Autoconf and should not
be defined by hand (as in the past), however at the time of writing
LTALLOCA
still needs to be defined from ALLOCA
manually.
See AC_LIBOBJ
vs. LIBOBJS
in The Autoconf Manual.
8.7 Variables used when building a program
Occasionally it is useful to know which Makefile variables
Automake uses for compilations; for instance, you might need to do your
own compilation in some special cases.
Some variables are inherited from Autoconf; these are CC
,
CFLAGS
, CPPFLAGS
, DEFS
, LDFLAGS
, and
LIBS
.
There are some additional variables that Automake defines on its own:
AM_CPPFLAGS
¶
The contents of this variable are passed to every compilation that invokes
the C preprocessor; it is a list of arguments to the preprocessor. For
instance, -I and -D options should be listed here.
Automake already provides some -I options automatically, in a
separate variable that is also passed to every compilation that invokes
the C preprocessor. In particular it generates ‘-I.’,
‘-I$(srcdir)’, and a -I pointing to the directory holding
config.h (if you’ve used AC_CONFIG_HEADERS
or
AM_CONFIG_HEADER
). You can disable the default -I
options using the nostdinc option.
AM_CPPFLAGS
is ignored in preference to a per-executable (or
per-library) _CPPFLAGS
variable if it is defined.
INCLUDES
¶
This does the same job as AM_CPPFLAGS
(or any per-target
_CPPFLAGS
variable if it is used). It is an older name for the
same functionality. This variable is deprecated; we suggest using
AM_CPPFLAGS
and per-target _CPPFLAGS
instead.
AM_CFLAGS
¶
This is the variable the Makefile.am author can use to pass
in additional C compiler flags. It is more fully documented elsewhere.
In some situations, this is not used, in preference to the
per-executable (or per-library) _CFLAGS
.
COMPILE
¶
This is the command used to actually compile a C source file. The
file name is appended to form the complete command line.
AM_LDFLAGS
¶
This is the variable the Makefile.am author can use to pass
in additional linker flags. In some situations, this is not used, in
preference to the per-executable (or per-library) _LDFLAGS
.
LINK
¶
This is the command used to actually link a C program. It already
includes ‘-o $@’ and the usual variable references (for instance,
CFLAGS
); it takes as “arguments” the names of the object files
and libraries to link in.
8.8 Yacc and Lex support
Automake has somewhat idiosyncratic support for Yacc and Lex.
Automake assumes that the .c file generated by yacc
(or lex
) should be named using the basename of the input
file. That is, for a yacc source file foo.y, Automake will
cause the intermediate file to be named foo.c (as opposed to
y.tab.c, which is more traditional).
The extension of a yacc source file is used to determine the extension
of the resulting C or C++ file. Files with the extension .y
will be turned into .c files; likewise, .yy will become
.cc; .y++, c++; .yxx, .cxx; and
.ypp, .cpp.
Likewise, lex source files can be used to generate C or C++; the
extensions .l, .ll, .l++, .lxx, and
.lpp are recognized.
You should never explicitly mention the intermediate (C or C++) file
in any SOURCES
variable; only list the source file.
The intermediate files generated by yacc
(or lex
)
will be included in any distribution that is made. That way the user
doesn’t need to have yacc
or lex
.
If a yacc
source file is seen, then your configure.ac must
define the variable YACC
. This is most easily done by invoking
the macro AC_PROG_YACC
(see Particular
Program Checks in The Autoconf Manual).
When yacc
is invoked, it is passed YFLAGS
and
AM_YFLAGS
. The former is a user variable and the latter is
intended for the Makefile.am author.
AM_YFLAGS
is usually used to pass the -d option to
yacc
. Automake knows what this means and will automatically
adjust its rules to update and distribute the header file built by
‘yacc -d’. What Automake cannot guess, though, is where this
header will be used: it is up to you to ensure the header gets built
before it is first used. Typically this is necessary in order for
dependency tracking to work when the header is included by another
file. The common solution is listing the header file in
BUILT_SOURCES
(see Built sources) as follows.
BUILT_SOURCES = parser.h
AM_YFLAGS = -d
bin_PROGRAMS = foo
foo_SOURCES = … parser.y …
If a lex
source file is seen, then your configure.ac
must define the variable LEX
. You can use AC_PROG_LEX
to do this (see Particular Program Checks in The Autoconf Manual), but using AM_PROG_LEX
macro
(see Autoconf macros supplied with Automake) is recommended.
When lex
is invoked, it is passed LFLAGS
and
AM_LFLAGS
. The former is a user variable and the latter is
intended for the Makefile.am author.
When AM_MAINTAINER_MODE
(see missing
and AM_MAINTAINER_MODE
) is used, the
rebuild rule for distributed Yacc and Lex sources are only used when
maintainer-mode
is enabled, or when the files have been erased.
When lex
or yacc
sources are used, automake
-i
automatically installs an auxiliary program called
ylwrap
in your package (see Programs automake might require). This
program is used by the build rules to rename the output of these
tools, and makes it possible to include multiple yacc
(or
lex
) source files in a single directory. (This is necessary
because yacc’s output file name is fixed, and a parallel make could
conceivably invoke more than one instance of yacc
simultaneously.)
For yacc
, simply managing locking is insufficient. The output of
yacc
always uses the same symbol names internally, so it isn’t
possible to link two yacc
parsers into the same executable.
We recommend using the following renaming hack used in gdb
:
#define yymaxdepth c_maxdepth
#define yyparse c_parse
#define yylex c_lex
#define yyerror c_error
#define yylval c_lval
#define yychar c_char
#define yydebug c_debug
#define yypact c_pact
#define yyr1 c_r1
#define yyr2 c_r2
#define yydef c_def
#define yychk c_chk
#define yypgo c_pgo
#define yyact c_act
#define yyexca c_exca
#define yyerrflag c_errflag
#define yynerrs c_nerrs
#define yyps c_ps
#define yypv c_pv
#define yys c_s
#define yy_yys c_yys
#define yystate c_state
#define yytmp c_tmp
#define yyv c_v
#define yy_yyv c_yyv
#define yyval c_val
#define yylloc c_lloc
#define yyreds c_reds
#define yytoks c_toks
#define yylhs c_yylhs
#define yylen c_yylen
#define yydefred c_yydefred
#define yydgoto c_yydgoto
#define yysindex c_yysindex
#define yyrindex c_yyrindex
#define yygindex c_yygindex
#define yytable c_yytable
#define yycheck c_yycheck
#define yyname c_yyname
#define yyrule c_yyrule
For each define, replace the ‘c_’ prefix with whatever you like.
These defines work for bison
, byacc
, and
traditional yacc
s. If you find a parser generator that uses a
symbol not covered here, please report the new name so it can be added
to the list.
8.9 C++ Support
Automake includes full support for C++.
Any package including C++ code must define the output variable
CXX
in configure.ac; the simplest way to do this is to use
the AC_PROG_CXX
macro (see Particular
Program Checks in The Autoconf Manual).
A few additional variables are defined when a C++ source file is seen:
CXX
¶
The name of the C++ compiler.
CXXFLAGS
¶
Any flags to pass to the C++ compiler.
AM_CXXFLAGS
¶
The maintainer’s variant of CXXFLAGS
.
CXXCOMPILE
¶
The command used to actually compile a C++ source file. The file name
is appended to form the complete command line.
CXXLINK
¶
The command used to actually link a C++ program.
8.10 Objective C Support
Automake includes some support for Objective C.
Any package including Objective C code must define the output variable
OBJC
in configure.ac; the simplest way to do this is to use
the AC_PROG_OBJC
macro (see Particular
Program Checks in The Autoconf Manual).
A few additional variables are defined when an Objective C source file
is seen:
OBJC
¶
The name of the Objective C compiler.
OBJCFLAGS
¶
Any flags to pass to the Objective C compiler.
AM_OBJCFLAGS
¶
The maintainer’s variant of OBJCFLAGS
.
OBJCCOMPILE
¶
The command used to actually compile a Objective C source file. The
file name is appended to form the complete command line.
OBJCLINK
¶
The command used to actually link a Objective C program.
8.11 Unified Parallel C Support
Automake includes some support for Unified Parallel C.
Any package including Unified Parallel C code must define the output
variable UPC
in configure.ac; the simplest way to do
this is to use the AM_PROG_UPC
macro (see Public macros).
A few additional variables are defined when an Unified Parallel C
source file is seen:
UPC
¶
The name of the Unified Parallel C compiler.
UPCFLAGS
¶
Any flags to pass to the Unified Parallel C compiler.
AM_UPCFLAGS
¶
The maintainer’s variant of UPCFLAGS
.
UPCCOMPILE
¶
The command used to actually compile a Unified Parallel C source file.
The file name is appended to form the complete command line.
UPCLINK
¶
The command used to actually link a Unified Parallel C program.
8.12 Assembly Support
Automake includes some support for assembly code. There are two forms
of assembler files: normal (*.s) and preprocessed by CPP
(*.S or *.sx).
The variable CCAS
holds the name of the compiler used to build
assembly code. This compiler must work a bit like a C compiler; in
particular it must accept -c and -o. The values of
CCASFLAGS
and AM_CCASFLAGS
(or its per-target
definition) is passed to the compilation. For preprocessed files,
DEFS
, DEFAULT_INCLUDES
, INCLUDES
, CPPFLAGS
and AM_CPPFLAGS
are also used.
The autoconf macro AM_PROG_AS
will define CCAS
and
CCASFLAGS
for you (unless they are already set, it simply sets
CCAS
to the C compiler and CCASFLAGS
to the C compiler
flags), but you are free to define these variables by other means.
Only the suffixes .s, .S, and .sx are recognized by
automake
as being files containing assembly code.
8.13 Fortran 77 Support
Automake includes full support for Fortran 77.
Any package including Fortran 77 code must define the output variable
F77
in configure.ac; the simplest way to do this is to use
the AC_PROG_F77
macro (see Particular
Program Checks in The Autoconf Manual).
A few additional variables are defined when a Fortran 77 source file is
seen:
F77
¶
The name of the Fortran 77 compiler.
FFLAGS
¶
Any flags to pass to the Fortran 77 compiler.
AM_FFLAGS
¶
The maintainer’s variant of FFLAGS
.
RFLAGS
¶
Any flags to pass to the Ratfor compiler.
AM_RFLAGS
¶
The maintainer’s variant of RFLAGS
.
F77COMPILE
¶
The command used to actually compile a Fortran 77 source file. The file
name is appended to form the complete command line.
FLINK
¶
The command used to actually link a pure Fortran 77 program or shared
library.
Automake can handle preprocessing Fortran 77 and Ratfor source files in
addition to compiling them5. Automake
also contains some support for creating programs and shared libraries
that are a mixture of Fortran 77 and other languages (see Mixing Fortran 77 With C and C++).
These issues are covered in the following sections.
8.13.1 Preprocessing Fortran 77
N.f is made automatically from N.F or N.r. This
rule runs just the preprocessor to convert a preprocessable Fortran 77
or Ratfor source file into a strict Fortran 77 source file. The precise
command used is as follows:
- .F
$(F77) -F $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS)
$(AM_FFLAGS) $(FFLAGS)
- .r
$(F77) -F $(AM_FFLAGS) $(FFLAGS) $(AM_RFLAGS) $(RFLAGS)
8.13.2 Compiling Fortran 77 Files
N.o is made automatically from N.f, N.F or
N.r by running the Fortran 77 compiler. The precise command used
is as follows:
- .f
$(F77) -c $(AM_FFLAGS) $(FFLAGS)
- .F
$(F77) -c $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS)
$(AM_FFLAGS) $(FFLAGS)
- .r
$(F77) -c $(AM_FFLAGS) $(FFLAGS) $(AM_RFLAGS) $(RFLAGS)
8.13.3 Mixing Fortran 77 With C and C++
Automake currently provides limited support for creating programs
and shared libraries that are a mixture of Fortran 77 and C and/or C++.
However, there are many other issues related to mixing Fortran 77 with
other languages that are not (currently) handled by Automake, but
that are handled by other packages6.
Automake can help in two ways:
- Automatic selection of the linker depending on which combinations of
source code.
- Automatic selection of the appropriate linker flags (e.g., -L and
-l) to pass to the automatically selected linker in order to link
in the appropriate Fortran 77 intrinsic and run-time libraries.
These extra Fortran 77 linker flags are supplied in the output variable
FLIBS
by the AC_F77_LIBRARY_LDFLAGS
Autoconf macro
supplied with newer versions of Autoconf (Autoconf version 2.13 and
later). See Fortran 77 Compiler Characteristics in The
Autoconf.
If Automake detects that a program or shared library (as mentioned in
some _PROGRAMS
or _LTLIBRARIES
primary) contains source
code that is a mixture of Fortran 77 and C and/or C++, then it requires
that the macro AC_F77_LIBRARY_LDFLAGS
be called in
configure.ac, and that either $(FLIBS)
appear in the appropriate _LDADD
(for programs) or _LIBADD
(for shared libraries) variables. It is the responsibility of the
person writing the Makefile.am to make sure that ‘$(FLIBS)’
appears in the appropriate _LDADD
or
_LIBADD
variable.
For example, consider the following Makefile.am:
bin_PROGRAMS = foo
foo_SOURCES = main.cc foo.f
foo_LDADD = libfoo.la $(FLIBS)
pkglib_LTLIBRARIES = libfoo.la
libfoo_la_SOURCES = bar.f baz.c zardoz.cc
libfoo_la_LIBADD = $(FLIBS)
In this case, Automake will insist that AC_F77_LIBRARY_LDFLAGS
is mentioned in configure.ac. Also, if ‘$(FLIBS)’ hadn’t
been mentioned in foo_LDADD
and libfoo_la_LIBADD
, then
Automake would have issued a warning.
8.13.3.1 How the Linker is Chosen
When a program or library mixes several languages, Automake choose the
linker according to the following priorities. (The names in
parentheses are the variables containing the link command.)
-
Native Java (
GCJLINK
)
-
C++ (
CXXLINK
)
-
Fortran 77 (
F77LINK
)
-
Fortran (
FCLINK
)
-
Objective C (
OBJCLINK
)
-
Unified Parallel C (
UPCLINK
)
-
C (
LINK
)
For example, if Fortran 77, C and C++ source code is compiled
into a program, then the C++ linker will be used. In this case, if the
C or Fortran 77 linkers required any special libraries that weren’t
included by the C++ linker, then they must be manually added to an
_LDADD
or _LIBADD
variable by the user writing the
Makefile.am.
Automake only looks at the file names listed in _SOURCES
variables to choose the linker, and defaults to the C linker.
Sometimes this is inconvenient because you are linking against a
library written in another language and would like to set the linker
more appropriately. See Libtool Convenience Libraries, for a
trick with nodist_EXTRA_…_SOURCES
.
8.14 Fortran 9x Support
Automake includes support for Fortran 9x.
Any package including Fortran 9x code must define the output variable
FC
in configure.ac; the simplest way to do this is to use
the AC_PROG_FC
macro (see Particular
Program Checks in The Autoconf Manual).
A few additional variables are defined when a Fortran 9x source file is
seen:
FC
¶
The name of the Fortran 9x compiler.
FCFLAGS
¶
Any flags to pass to the Fortran 9x compiler.
AM_FCFLAGS
¶
The maintainer’s variant of FCFLAGS
.
FCCOMPILE
¶
The command used to actually compile a Fortran 9x source file. The file
name is appended to form the complete command line.
FCLINK
¶
The command used to actually link a pure Fortran 9x program or shared
library.
8.14.1 Compiling Fortran 9x Files
N.o is made automatically from N.f90,
N.f95, N.f03, or N.f08
by running the Fortran 9x compiler. The precise command used
is as follows:
- .f90
$(FC) $(AM_FCFLAGS) $(FCFLAGS) -c $(FCFLAGS_f90) $<
- .f95
$(FC) $(AM_FCFLAGS) $(FCFLAGS) -c $(FCFLAGS_f95) $<
- .f03
$(FC) $(AM_FCFLAGS) $(FCFLAGS) -c $(FCFLAGS_f03) $<
- .f08
$(FC) $(AM_FCFLAGS) $(FCFLAGS) -c $(FCFLAGS_f08) $<
8.15 Java Support
Automake includes support for compiled Java, using gcj
, the Java
front end to the GNU Compiler Collection.
Any package including Java code to be compiled must define the output
variable GCJ
in configure.ac; the variable GCJFLAGS
must also be defined somehow (either in configure.ac or
Makefile.am). The simplest way to do this is to use the
AM_PROG_GCJ
macro.
By default, programs including Java source files are linked with
gcj
.
As always, the contents of AM_GCJFLAGS
are passed to every
compilation invoking gcj
(in its role as an ahead-of-time
compiler, when invoking it to create .class files,
AM_JAVACFLAGS
is used instead). If it is necessary to pass
options to gcj
from Makefile.am, this variable, and not
the user variable GCJFLAGS
, should be used.
gcj
can be used to compile .java, .class,
.zip, or .jar files.
When linking, gcj
requires that the main class be specified
using the --main= option. The easiest way to do this is to use
the _LDFLAGS
variable for the program.
8.17 Automatic de-ANSI-fication
The features described in this section are obsolete; you should not
used any of them in new code, and they may be withdrawn in future
Automake releases.
When the C language was standardized in 1989, there was a long
transition period where package developers needed to worry about
porting to older systems that did not support ANSI C by default.
These older systems are no longer in practical use and are no longer
supported by their original suppliers, so developers need not worry
about this problem any more.
Automake allows you to write packages that are portable to K&R C by
de-ANSI-fying each source file before the actual compilation takes
place.
If the Makefile.am variable AUTOMAKE_OPTIONS
(see Changing Automake’s Behavior) contains the option ansi2knr then code to
handle de-ANSI-fication is inserted into the generated
Makefile.in.
This causes each C source file in the directory to be treated as ANSI C.
If an ANSI C compiler is available, it is used. If no ANSI C compiler
is available, the ansi2knr
program is used to convert the source
files into K&R C, which is then compiled.
The ansi2knr
program is simple-minded. It assumes the source
code will be formatted in a particular way; see the ansi2knr
man
page for details.
Support for the obsolete de-ANSI-fication feature
requires the source files ansi2knr.c
and ansi2knr.1 to be in the same package as the ANSI C source;
these files are distributed with Automake. Also, the package
configure.ac must call the macro AM_C_PROTOTYPES
(see Autoconf macros supplied with Automake).
Automake also handles finding the ansi2knr
support files in some
other directory in the current package. This is done by prepending the
relative path to the appropriate directory to the ansi2knr
option. For instance, suppose the package has ANSI C code in the
src and lib subdirectories. The files ansi2knr.c and
ansi2knr.1 appear in lib. Then this could appear in
src/Makefile.am:
AUTOMAKE_OPTIONS = ../lib/ansi2knr
If no directory prefix is given, the files are assumed to be in the
current directory.
Note that automatic de-ANSI-fication will not work when the package is
being built for a different host architecture. That is because automake
currently has no way to build ansi2knr
for the build machine.
Using LIBOBJS
with source de-ANSI-fication used to require
hand-crafted code in configure to append ‘$U’ to basenames
in LIBOBJS
. This is no longer true today. Starting with version
2.54, Autoconf takes care of rewriting LIBOBJS
and
LTLIBOBJS
. (see AC_LIBOBJ
vs. LIBOBJS
in The Autoconf Manual)
8.18 Automatic dependency tracking
As a developer it is often painful to continually update the
Makefile.in whenever the include-file dependencies change in a
project. Automake supplies a way to automatically track dependency
changes (see Automatic Dependency Tracking).
Automake always uses complete dependencies for a compilation,
including system headers. Automake’s model is that dependency
computation should be a side effect of the build. To this end,
dependencies are computed by running all compilations through a
special wrapper program called depcomp
. depcomp
understands how to coax many different C and C++ compilers into
generating dependency information in the format it requires.
‘automake -a’ will install depcomp
into your source
tree for you. If depcomp
can’t figure out how to properly
invoke your compiler, dependency tracking will simply be disabled for
your build.
Experience with earlier versions of Automake (see Dependency Tracking in Automake) taught us that it is not reliable to generate
dependencies only on the maintainer’s system, as configurations vary
too much. So instead Automake implements dependency tracking at build
time.
Automatic dependency tracking can be suppressed by putting
no-dependencies in the variable AUTOMAKE_OPTIONS
, or
passing no-dependencies as an argument to AM_INIT_AUTOMAKE
(this should be the preferred way). Or, you can invoke automake
with the -i option. Dependency tracking is enabled by default.
The person building your package also can choose to disable dependency
tracking by configuring with --disable-dependency-tracking.
8.19 Support for executable extensions
On some platforms, such as Windows, executables are expected to have an
extension such as .exe. On these platforms, some compilers (GCC
among them) will automatically generate foo.exe when asked to
generate foo.
Automake provides mostly-transparent support for this. Unfortunately
mostly doesn’t yet mean fully. Until the English
dictionary is revised, you will have to assist Automake if your package
must support those platforms.
One thing you must be aware of is that, internally, Automake rewrites
something like this:
to this:
bin_PROGRAMS = liver$(EXEEXT)
The targets Automake generates are likewise given the ‘$(EXEEXT)’
extension.
The variables TESTS
, XFAIL_TESTS
(see Support for test suites) are also
rewritten if it contains filenames that have been declared as programs
in the same Makefile. (This is mostly useful when some programs
from check_PROGRAMS
are listed in TESTS
.)
However, Automake cannot apply this rewriting to configure
substitutions. This means that if you are conditionally building a
program using such a substitution, then your configure.ac must
take care to add ‘$(EXEEXT)’ when constructing the output variable.
With Autoconf 2.13 and earlier, you must explicitly use AC_EXEEXT
to get this support. With Autoconf 2.50, AC_EXEEXT
is run
automatically if you configure a compiler (say, through
AC_PROG_CC
).
Sometimes maintainers like to write an explicit link rule for their
program. Without executable extension support, this is easy—you
simply write a rule whose target is the name of the program. However,
when executable extension support is enabled, you must instead add the
‘$(EXEEXT)’ suffix.
Unfortunately, due to the change in Autoconf 2.50, this means you must
always add this extension. However, this is a problem for maintainers
who know their package will never run on a platform that has
executable extensions. For those maintainers, the no-exeext
option (see Changing Automake’s Behavior) will disable this feature. This works in a
fairly ugly way; if no-exeext is seen, then the presence of a
rule for a target named foo
in Makefile.am will override
an automake-generated rule for ‘foo$(EXEEXT)’. Without
the no-exeext option, this use will give a diagnostic.
9 Other Derived Objects
Automake can handle derived objects that are not C programs. Sometimes
the support for actually building such objects must be explicitly
supplied, but Automake will still automatically handle installation and
distribution.
9.1 Executable Scripts
It is possible to define and install programs that are scripts. Such
programs are listed using the SCRIPTS
primary name. When the
script is distributed in its final, installable form, the
Makefile usually looks as follows:
# Install my_script in $(bindir) and distribute it.
dist_bin_SCRIPTS = my_script
Script are not distributed by default; as we have just seen, those
that should be distributed can be specified using a dist_
prefix as with other primaries.
Scripts can be installed in bindir
, sbindir
,
libexecdir
, or pkgdatadir
.
Scripts that need not being installed can be listed in
noinst_SCRIPTS
, and among them, those which are needed only by
‘make check’ should go in check_SCRIPTS
.
When a script needs to be built, the Makefile.am should include
the appropriate rules. For instance the automake
program
itself is a Perl script that is generated from automake.in.
Here is how this is handled:
bin_SCRIPTS = automake
CLEANFILES = $(bin_SCRIPTS)
EXTRA_DIST = automake.in
do_subst = sed -e 's,[@]datadir[@],$(datadir),g' \
-e 's,[@]PERL[@],$(PERL),g' \
-e 's,[@]PACKAGE[@],$(PACKAGE),g' \
-e 's,[@]VERSION[@],$(VERSION),g' \
…
automake: automake.in Makefile
$(do_subst) < $(srcdir)/automake.in > automake
chmod +x automake
Such scripts for which a build rule has been supplied need to be
deleted explicitly using CLEANFILES
(see What Gets Cleaned), and their
sources have to be distributed, usually with EXTRA_DIST
(see What Goes in a Distribution).
Another common way to build scripts is to process them from
configure with AC_CONFIG_FILES
. In this situation
Automake knows which files should be cleaned and distributed, and what
the rebuild rules should look like.
For instance if configure.ac contains
AC_CONFIG_FILES([src/my_script], [chmod +x src/my_script])
to build src/my_script from src/my_script.in, then an
src/Makefile.am to install this script in $(bindir)
can
be as simple as
bin_SCRIPTS = my_script
CLEANFILES = $(bin_SCRIPTS)
There is no need for EXTRA_DIST
or any build rule: Automake
infers them from AC_CONFIG_FILES
(see Configuration requirements).
CLEANFILES
is still useful, because by default Automake will
clean targets of AC_CONFIG_FILES
in distclean
, not
clean
.
Although this looks simpler, building scripts this way has one
drawback: directory variables such as $(datadir)
are not fully
expanded and may refer to other directory variables.
9.3 Architecture-independent data files
Automake supports the installation of miscellaneous data files using the
DATA
family of variables.
Such data can be installed in the directories datadir
,
sysconfdir
, sharedstatedir
, localstatedir
, or
pkgdatadir
.
By default, data files are not included in a distribution. Of
course, you can use the dist_
prefix to change this on a
per-variable basis.
Here is how Automake declares its auxiliary data files:
dist_pkgdata_DATA = clean-kr.am clean.am …
9.4 Built sources
Because Automake’s automatic dependency tracking works as a side-effect
of compilation (see Automatic dependency tracking) there is a bootstrap issue: a
target should not be compiled before its dependencies are made, but
these dependencies are unknown until the target is first compiled.
Ordinarily this is not a problem, because dependencies are distributed
sources: they preexist and do not need to be built. Suppose that
foo.c includes foo.h. When it first compiles
foo.o, make
only knows that foo.o depends on
foo.c. As a side-effect of this compilation depcomp
records the foo.h dependency so that following invocations of
make
will honor it. In these conditions, it’s clear there is
no problem: either foo.o doesn’t exist and has to be built
(regardless of the dependencies), or accurate dependencies exist and
they can be used to decide whether foo.o should be rebuilt.
It’s a different story if foo.h doesn’t exist by the first
make
run. For instance, there might be a rule to build
foo.h. This time file.o’s build will fail because the
compiler can’t find foo.h. make
failed to trigger the
rule to build foo.h first by lack of dependency information.
The BUILT_SOURCES
variable is a workaround for this problem. A
source file listed in BUILT_SOURCES
is made on ‘make all’
or ‘make check’ (or even ‘make install’) before other
targets are processed. However, such a source file is not
compiled unless explicitly requested by mentioning it in some
other _SOURCES
variable.
So, to conclude our introductory example, we could use
‘BUILT_SOURCES = foo.h’ to ensure foo.h gets built before
any other target (including foo.o) during ‘make all’ or
‘make check’.
BUILT_SOURCES
is actually a bit of a misnomer, as any file which
must be created early in the build process can be listed in this
variable. Moreover, all built sources do not necessarily have to be
listed in BUILT_SOURCES
. For instance, a generated .c file
doesn’t need to appear in BUILT_SOURCES
(unless it is included by
another source), because it’s a known dependency of the associated
object.
It might be important to emphasize that BUILT_SOURCES
is
honored only by ‘make all’, ‘make check’ and ‘make
install’. This means you cannot build a specific target (e.g.,
‘make foo’) in a clean tree if it depends on a built source.
However it will succeed if you have run ‘make all’ earlier,
because accurate dependencies are already available.
The next section illustrates and discusses the handling of built sources
on a toy example.
9.4.1 Built sources example
Suppose that foo.c includes bindir.h, which is
installation-dependent and not distributed: it needs to be built. Here
bindir.h defines the preprocessor macro bindir
to the
value of the make
variable bindir
(inherited from
configure).
We suggest several implementations below. It’s not meant to be an
exhaustive listing of all ways to handle built sources, but it will give
you a few ideas if you encounter this issue.
First try
This first implementation will illustrate the bootstrap issue mentioned
in the previous section (see Built sources).
Here is a tentative Makefile.am.
# This won't work.
bin_PROGRAMS = foo
foo_SOURCES = foo.c
nodist_foo_SOURCES = bindir.h
CLEANFILES = bindir.h
bindir.h: Makefile
echo '#define bindir "$(bindir)"' >$@
This setup doesn’t work, because Automake doesn’t know that foo.c
includes bindir.h. Remember, automatic dependency tracking works
as a side-effect of compilation, so the dependencies of foo.o will
be known only after foo.o has been compiled (see Automatic dependency tracking).
The symptom is as follows.
% make
source='foo.c' object='foo.o' libtool=no \
depfile='.deps/foo.Po' tmpdepfile='.deps/foo.TPo' \
depmode=gcc /bin/sh ./depcomp \
gcc -I. -I. -g -O2 -c `test -f 'foo.c' || echo './'`foo.c
foo.c:2: bindir.h: No such file or directory
make: *** [foo.o] Error 1
In this example bindir.h is not distributed nor installed, and
it is not even being built on-time. One may wonder if the
‘nodist_foo_SOURCES = bindir.h’ line has any use at all. This
line simply states that bindir.h is a source of foo
, so
for instance, it should be inspected while generating tags
(see Interfacing to etags
). In other words, it does not help our present problem,
and the build would fail identically without it.
Using BUILT_SOURCES
A solution is to require bindir.h to be built before anything
else. This is what BUILT_SOURCES
is meant for (see Built sources).
bin_PROGRAMS = foo
foo_SOURCES = foo.c
nodist_foo_SOURCES = bindir.h
BUILT_SOURCES = bindir.h
CLEANFILES = bindir.h
bindir.h: Makefile
echo '#define bindir "$(bindir)"' >$@
See how bindir.h get built first:
% make
echo '#define bindir "/usr/local/bin"' >bindir.h
make all-am
make[1]: Entering directory `/home/adl/tmp'
source='foo.c' object='foo.o' libtool=no \
depfile='.deps/foo.Po' tmpdepfile='.deps/foo.TPo' \
depmode=gcc /bin/sh ./depcomp \
gcc -I. -I. -g -O2 -c `test -f 'foo.c' || echo './'`foo.c
gcc -g -O2 -o foo foo.o
make[1]: Leaving directory `/home/adl/tmp'
However, as said earlier, BUILT_SOURCES
applies only to the
all
, check
, and install
targets. It still fails
if you try to run ‘make foo’ explicitly:
% make clean
test -z "bindir.h" || rm -f bindir.h
test -z "foo" || rm -f foo
rm -f *.o
% : > .deps/foo.Po # Suppress previously recorded dependencies
% make foo
source='foo.c' object='foo.o' libtool=no \
depfile='.deps/foo.Po' tmpdepfile='.deps/foo.TPo' \
depmode=gcc /bin/sh ./depcomp \
gcc -I. -I. -g -O2 -c `test -f 'foo.c' || echo './'`foo.c
foo.c:2: bindir.h: No such file or directory
make: *** [foo.o] Error 1
Recording dependencies manually
Usually people are happy enough with BUILT_SOURCES
because they
never build targets such as ‘make foo’ before ‘make all’, as
in the previous example. However if this matters to you, you can
avoid BUILT_SOURCES
and record such dependencies explicitly in
the Makefile.am.
bin_PROGRAMS = foo
foo_SOURCES = foo.c
nodist_foo_SOURCES = bindir.h
foo.$(OBJEXT): bindir.h
CLEANFILES = bindir.h
bindir.h: Makefile
echo '#define bindir "$(bindir)"' >$@
You don’t have to list all the dependencies of foo.o
explicitly, only those that might need to be built. If a dependency
already exists, it will not hinder the first compilation and will be
recorded by the normal dependency tracking code. (Note that after
this first compilation the dependency tracking code will also have
recorded the dependency between foo.o and
bindir.h; so our explicit dependency is really useful to
the first build only.)
Adding explicit dependencies like this can be a bit dangerous if you are
not careful enough. This is due to the way Automake tries not to
overwrite your rules (it assumes you know better than it).
‘foo.$(OBJEXT): bindir.h’ supersedes any rule Automake may want to
output to build ‘foo.$(OBJEXT)’. It happens to work in this case
because Automake doesn’t have to output any ‘foo.$(OBJEXT):’
target: it relies on a suffix rule instead (i.e., ‘.c.$(OBJEXT):’).
Always check the generated Makefile.in if you do this.
Build bindir.c, not bindir.h.
Another attractive idea is to define bindir
as a variable or
function exported from bindir.o, and build bindir.c
instead of bindir.h.
noinst_PROGRAMS = foo
foo_SOURCES = foo.c bindir.h
nodist_foo_SOURCES = bindir.c
CLEANFILES = bindir.c
bindir.c: Makefile
echo 'const char bindir[] = "$(bindir)";' >$@
bindir.h contains just the variable’s declaration and doesn’t
need to be built, so it won’t cause any trouble. bindir.o is
always dependent on bindir.c, so bindir.c will get built
first.
Which is best?
There is no panacea, of course. Each solution has its merits and
drawbacks.
You cannot use BUILT_SOURCES
if the ability to run ‘make
foo’ on a clean tree is important to you.
You won’t add explicit dependencies if you are leery of overriding
an Automake rule by mistake.
Building files from ./configure is not always possible, neither
is converting .h files into .c files.
11 Building documentation
Currently Automake provides support for Texinfo and man pages.
11.1 Texinfo
If the current directory contains Texinfo source, you must declare it
with the TEXINFOS
primary. Generally Texinfo files are converted
into info, and thus the info_TEXINFOS
variable is most commonly used
here. Any Texinfo source file must end in the .texi,
.txi, or .texinfo extension. We recommend .texi
for new manuals.
Automake generates rules to build .info, .dvi,
.ps, .pdf and .html files from your Texinfo
sources. Following the GNU Coding Standards, only the .info
files are built by ‘make all’ and installed by ‘make
install’ (unless you use no-installinfo, see below).
Furthermore, .info files are automatically distributed so that
Texinfo is not a prerequisite for installing your package.
Other documentation formats can be built on request by ‘make
dvi’, ‘make ps’, ‘make pdf’ and ‘make html’, and they
can be installed with ‘make install-dvi’, ‘make install-ps’,
‘make install-pdf’ and ‘make install-html’ explicitly.
‘make uninstall’ will remove everything: the Texinfo
documentation installed by default as well as all the above optional
formats.
All these targets can be extended using ‘-local’ rules
(see Extending Automake Rules).
If the .texi file @include
s version.texi, then
that file will be automatically generated. The file version.texi
defines four Texinfo flag you can reference using
@value{EDITION}
, @value{VERSION}
,
@value{UPDATED}
, and @value{UPDATED-MONTH}
.
EDITION
VERSION
Both of these flags hold the version number of your program. They are
kept separate for clarity.
UPDATED
This holds the date the primary .texi file was last modified.
UPDATED-MONTH
This holds the name of the month in which the primary .texi file
was last modified.
The version.texi support requires the mdate-sh
script; this script is supplied with Automake and automatically
included when automake
is invoked with the
--add-missing option.
If you have multiple Texinfo files, and you want to use the
version.texi feature, then you have to have a separate version
file for each Texinfo file. Automake will treat any include in a
Texinfo file that matches vers*.texi just as an automatically
generated version file.
Sometimes an info file actually depends on more than one .texi
file. For instance, in GNU Hello, hello.texi includes the file
gpl.texi. You can tell Automake about these dependencies using
the texi_TEXINFOS
variable. Here is how GNU Hello does it:
info_TEXINFOS = hello.texi
hello_TEXINFOS = gpl.texi
By default, Automake requires the file texinfo.tex to appear in
the same directory as the Makefile.am file that lists the
.texi files. If you used AC_CONFIG_AUX_DIR
in
configure.ac (see Finding ‘configure’ Input in The Autoconf Manual), then texinfo.tex is looked for
there. In both cases, automake then supplies texinfo.tex if
--add-missing is given, and takes care of its distribution.
However, if you set the TEXINFO_TEX
variable (see below),
it overrides the location of the file and turns off its installation
into the source as well as its distribution.
The option no-texinfo.tex can be used to eliminate the
requirement for the file texinfo.tex. Use of the variable
TEXINFO_TEX
is preferable, however, because that allows the
dvi
, ps
, and pdf
targets to still work.
Automake generates an install-info
rule; some people apparently
use this. By default, info pages are installed by ‘make
install’, so running make install-info
is pointless. This can
be prevented via the no-installinfo
option. In this case,
.info files are not installed by default, and user must
request this explicitly using ‘make install-info’
The following variables are used by the Texinfo build rules.
MAKEINFO
¶
The name of the program invoked to build .info files. This
variable is defined by Automake. If the makeinfo
program is
found on the system then it will be used by default; otherwise
missing
will be used instead.
MAKEINFOHTML
¶
The command invoked to build .html files. Automake
defines this to ‘$(MAKEINFO) --html’.
MAKEINFOFLAGS
¶
User flags passed to each invocation of ‘$(MAKEINFO)’ and
‘$(MAKEINFOHTML)’. This user variable (see Variables reserved for the user) is
not expected to be defined in any Makefile; it can be used by
users to pass extra flags to suit their needs.
AM_MAKEINFOFLAGS
¶
AM_MAKEINFOHTMLFLAGS
¶
Maintainer flags passed to each makeinfo
invocation. Unlike
MAKEINFOFLAGS
, these variables are meant to be defined by
maintainers in Makefile.am. ‘$(AM_MAKEINFOFLAGS)’ is
passed to makeinfo
when building .info files; and
‘$(AM_MAKEINFOHTMLFLAGS)’ is used when building .html
files.
For instance, the following setting can be used to obtain one single
.html file per manual, without node separators.
AM_MAKEINFOHTMLFLAGS = --no-headers --no-split
AM_MAKEINFOHTMLFLAGS
defaults to ‘$(AM_MAKEINFOFLAGS)’.
This means that defining AM_MAKEINFOFLAGS
without defining
AM_MAKEINFOHTMLFLAGS
will impact builds of both .info
and .html files.
TEXI2DVI
¶
The name of the command that converts a .texi file into a
.dvi file. This defaults to ‘texi2dvi’, a script that ships
with the Texinfo package.
TEXI2PDF
¶
The name of the command that translates a .texi file into a
.pdf file. This defaults to ‘$(TEXI2DVI) --pdf --batch’.
DVIPS
¶
The name of the command that build a .ps file out of a
.dvi file. This defaults to ‘dvips’.
TEXINFO_TEX
¶
-
If your package has Texinfo files in many directories, you can use the
variable TEXINFO_TEX
to tell Automake where to find the canonical
texinfo.tex for your package. The value of this variable should
be the relative path from the current Makefile.am to
texinfo.tex:
TEXINFO_TEX = ../doc/texinfo.tex
11.2 Man pages
A package can also include man pages (but see the GNU standards on this
matter, Man Pages in The GNU Coding Standards.) Man
pages are declared using the MANS
primary. Generally the
man_MANS
variable is used. Man pages are automatically installed in
the correct subdirectory of mandir
, based on the file extension.
File extensions such as .1c are handled by looking for the valid
part of the extension and using that to determine the correct
subdirectory of mandir
. Valid section names are the digits
‘0’ through ‘9’, and the letters ‘l’ and ‘n’.
Sometimes developers prefer to name a man page something like
foo.man in the source, and then rename it to have the correct
suffix, for example foo.1, when installing the file. Automake
also supports this mode. For a valid section named SECTION,
there is a corresponding directory named ‘manSECTIONdir’,
and a corresponding _MANS
variable. Files listed in such a
variable are installed in the indicated section. If the file already
has a valid suffix, then it is installed as-is; otherwise the file
suffix is changed to match the section.
For instance, consider this example:
man1_MANS = rename.man thesame.1 alsothesame.1c
In this case, rename.man will be renamed to rename.1 when
installed, but the other files will keep their names.
By default, man pages are installed by ‘make install’. However,
since the GNU project does not require man pages, many maintainers do
not expend effort to keep the man pages up to date. In these cases, the
no-installman option will prevent the man pages from being
installed by default. The user can still explicitly install them via
‘make install-man’.
Man pages are not currently considered to be source, because it is not
uncommon for man pages to be automatically generated. Therefore they
are not automatically included in the distribution. However, this can
be changed by use of the dist_
prefix. For instance here is
how to distribute and install the two man pages of GNU cpio
(which includes both Texinfo documentation and man pages):
dist_man_MANS = cpio.1 mt.1
The nobase_
prefix is meaningless for man pages and is
disallowed.
Executables and manpages may be renamed upon installation
(see Renaming Programs at Install Time). For manpages this can be avoided by use of the
notrans_
prefix. For instance, suppose an executable ‘foo’
allowing to access a library function ‘foo’ from the command line.
The way to avoid renaming of the foo.3 manpage is:
man_MANS = foo.1
notrans_man_MANS = foo.3
‘notrans_’ must be specified first when used in conjunction with
either ‘dist_’ or ‘nodist_’ (see What Goes in a Distribution). For instance:
notrans_dist_man3_MANS = bar.3
12 What Gets Installed
12.1 Basics of installation
Naturally, Automake handles the details of actually installing your
program once it has been built. All files named by the various
primaries are automatically installed in the appropriate places when the
user runs ‘make install’.
A file named in a primary is installed by copying the built file into
the appropriate directory. The base name of the file is used when
installing.
bin_PROGRAMS = hello subdir/goodbye
In this example, both ‘hello’ and ‘goodbye’ will be installed
in ‘$(bindir)’.
Sometimes it is useful to avoid the basename step at install time. For
instance, you might have a number of header files in subdirectories of
the source tree that are laid out precisely how you want to install
them. In this situation you can use the nobase_
prefix to
suppress the base name step. For example:
nobase_include_HEADERS = stdio.h sys/types.h
Will install stdio.h in ‘$(includedir)’ and types.h
in ‘$(includedir)/sys’.
12.2 The two parts of install
Automake generates separate install-data
and install-exec
rules, in case the installer is installing on multiple machines that
share directory structure—these targets allow the machine-independent
parts to be installed only once. install-exec
installs
platform-dependent files, and install-data
installs
platform-independent files. The install
target depends on both
of these targets. While Automake tries to automatically segregate
objects into the correct category, the Makefile.am author is, in
the end, responsible for making sure this is done correctly.
Variables using the standard directory prefixes ‘data’,
‘info’, ‘man’, ‘include’, ‘oldinclude’,
‘pkgdata’, or ‘pkginclude’ are installed by
install-data
.
Variables using the standard directory prefixes ‘bin’,
‘sbin’, ‘libexec’, ‘sysconf’, ‘localstate’,
‘lib’, or ‘pkglib’ are installed by install-exec
.
For instance, data_DATA
files are installed by install-data
,
while bin_PROGRAMS
files are installed by install-exec
.
Any variable using a user-defined directory prefix with ‘exec’ in
the name (e.g., myexecbin_PROGRAMS
) is installed by
install-exec
. All other user-defined prefixes are installed by
install-data
.
12.3 Extending installation
It is possible to extend this mechanism by defining an
install-exec-local
or install-data-local
rule. If these
rules exist, they will be run at ‘make install’ time. These
rules can do almost anything; care is required.
Automake also supports two install hooks, install-exec-hook
and
install-data-hook
. These hooks are run after all other install
rules of the appropriate type, exec or data, have completed. So, for
instance, it is possible to perform post-installation modifications
using an install hook. See Extending Automake Rules, for some examples.
12.4 Staged installs
Automake generates support for the DESTDIR
variable in all
install rules. DESTDIR
is used during the ‘make install’
step to relocate install objects into a staging area. Each object and
path is prefixed with the value of DESTDIR
before being copied
into the install area. Here is an example of typical DESTDIR usage:
mkdir /tmp/staging &&
make DESTDIR=/tmp/staging install
The mkdir
command avoids a security problem if the attacker
creates a symbolic link from /tmp/staging to a victim area;
then make
places install objects in a directory tree built under
/tmp/staging. If /gnu/bin/foo and
/gnu/share/aclocal/foo.m4 are to be installed, the above command
would install /tmp/staging/gnu/bin/foo and
/tmp/staging/gnu/share/aclocal/foo.m4.
This feature is commonly used to build install images and packages
(see Building Binary Packages Using DESTDIR).
Support for DESTDIR
is implemented by coding it directly into
the install rules. If your Makefile.am uses a local install
rule (e.g., install-exec-local
) or an install hook, then you
must write that code to respect DESTDIR
.
See Makefile Conventions in The GNU Coding Standards,
for another usage example.
12.5 Rules for the user
Automake also generates rules for targets uninstall
,
installdirs
, and install-strip
.
Automake supports uninstall-local
and uninstall-hook
.
There is no notion of separate uninstalls for “exec” and “data”, as
these features would not provide additional functionality.
Note that uninstall
is not meant as a replacement for a real
packaging tool.
14 What Goes in a Distribution
14.1 Basics of distribution
The dist
rule in the generated Makefile.in can be used
to generate a gzipped tar
file and other flavors of archive for
distribution. The files is named based on the PACKAGE
and
VERSION
variables defined by AM_INIT_AUTOMAKE
(see Autoconf macros supplied with Automake); more precisely the gzipped tar
file is named
‘package-version.tar.gz’.
You can use the make
variable GZIP_ENV
to control how gzip
is run. The default setting is --best.
For the most part, the files to distribute are automatically found by
Automake: all source files are automatically included in a distribution,
as are all Makefile.ams and Makefile.ins. Automake also
has a built-in list of commonly used files that are automatically
included if they are found in the current directory (either physically,
or as the target of a Makefile.am rule). This list is printed by
‘automake --help’. Also, files that are read by configure
(i.e. the source files corresponding to the files specified in various
Autoconf macros such as AC_CONFIG_FILES
and siblings) are
automatically distributed. Files included in Makefile.ams (using
include
) or in configure.ac (using m4_include
), and
helper scripts installed with ‘automake --add-missing’ are also
distributed.
Still, sometimes there are files that must be distributed, but which
are not covered in the automatic rules. These files should be listed in
the EXTRA_DIST
variable. You can mention files from
subdirectories in EXTRA_DIST
.
You can also mention a directory in EXTRA_DIST
; in this case the
entire directory will be recursively copied into the distribution.
Please note that this will also copy everything in the directory,
including CVS/RCS version control files. We recommend against using
this feature.
If you define SUBDIRS
, Automake will recursively include the
subdirectories in the distribution. If SUBDIRS
is defined
conditionally (see Conditionals), Automake will normally include
all directories that could possibly appear in SUBDIRS
in the
distribution. If you need to specify the set of directories
conditionally, you can set the variable DIST_SUBDIRS
to the
exact list of subdirectories to include in the distribution
(see Conditional Subdirectories).
14.2 Fine-grained distribution control
Sometimes you need tighter control over what does not go into the
distribution; for instance, you might have source files that are
generated and that you do not want to distribute. In this case
Automake gives fine-grained control using the dist
and
nodist
prefixes. Any primary or _SOURCES
variable can be
prefixed with dist_
to add the listed files to the distribution.
Similarly, nodist_
can be used to omit the files from the
distribution.
As an example, here is how you would cause some data to be distributed
while leaving some source code out of the distribution:
dist_data_DATA = distribute-this
bin_PROGRAMS = foo
nodist_foo_SOURCES = do-not-distribute.c
14.3 The dist hook
Occasionally it is useful to be able to change the distribution before
it is packaged up. If the dist-hook
rule exists, it is run
after the distribution directory is filled, but before the actual tar
(or shar) file is created. One way to use this is for distributing
files in subdirectories for which a new Makefile.am is overkill:
dist-hook:
mkdir $(distdir)/random
cp -p $(srcdir)/random/a1 $(srcdir)/random/a2 $(distdir)/random
Another way to use this is for removing unnecessary files that get
recursively included by specifying a directory in EXTRA_DIST:
EXTRA_DIST = doc
dist-hook:
rm -rf `find $(distdir)/doc -name CVS`
Two variables that come handy when writing dist-hook
rules are
‘$(distdir)’ and ‘$(top_distdir)’.
‘$(distdir)’ points to the directory where the dist
rule
will copy files from the current directory before creating the
tarball. If you are at the top-level directory, then ‘distdir =
$(PACKAGE)-$(VERSION)’. When used from subdirectory named
foo/, then ‘distdir = ../$(PACKAGE)-$(VERSION)/foo’.
‘$(distdir)’ can be a relative or absolute path, do not assume
any form.
‘$(top_distdir)’ always points to the root directory of the
distributed tree. At the top-level it’s equal to ‘$(distdir)’.
In the foo/ subdirectory
‘top_distdir = ../$(PACKAGE)-$(VERSION)’.
‘$(top_distdir)’ too can be a relative or absolute path.
Note that when packages are nested using AC_CONFIG_SUBDIRS
(see Nesting Packages), then ‘$(distdir)’ and
‘$(top_distdir)’ are relative to the package where ‘make
dist’ was run, not to any sub-packages involved.
14.4 Checking the distribution
Automake also generates a distcheck
rule that can be of help to
ensure that a given distribution will actually work. distcheck
makes a distribution, then tries to do a VPATH
build
(see Parallel Build Trees (a.k.a. VPATH Builds)), run the test suite, and finally make another
tarball to ensure the distribution is self-contained.
Building the package involves running ‘./configure’. If you need
to supply additional flags to configure
, define them in the
DISTCHECK_CONFIGURE_FLAGS
variable, either in your top-level
Makefile.am, or on the command line when invoking make
.
If the distcheck-hook
rule is defined in your top-level
Makefile.am, then it will be invoked by distcheck
after
the new distribution has been unpacked, but before the unpacked copy
is configured and built. Your distcheck-hook
can do almost
anything, though as always caution is advised. Generally this hook is
used to check for potential distribution errors not caught by the
standard mechanism. Note that distcheck-hook
as well as
DISTCHECK_CONFIGURE_FLAGS
are not honored in a subpackage
Makefile.am, but the DISTCHECK_CONFIGURE_FLAGS
are
passed down to the configure
script of the subpackage.
Speaking of potential distribution errors, distcheck
also
ensures that the distclean
rule actually removes all built
files. This is done by running ‘make distcleancheck’ at the end of
the VPATH
build. By default, distcleancheck
will run
distclean
and then make sure the build tree has been emptied by
running ‘$(distcleancheck_listfiles)’. Usually this check will
find generated files that you forgot to add to the DISTCLEANFILES
variable (see What Gets Cleaned).
The distcleancheck
behavior should be OK for most packages,
otherwise you have the possibility to override the definition of
either the distcleancheck
rule, or the
‘$(distcleancheck_listfiles)’ variable. For instance, to disable
distcleancheck
completely, add the following rule to your
top-level Makefile.am:
If you want distcleancheck
to ignore built files that have not
been cleaned because they are also part of the distribution, add the
following definition instead:
distcleancheck_listfiles = \
find -type f -exec sh -c 'test -f $(srcdir)/{} || echo {}' ';'
The above definition is not the default because it’s usually an error if
your Makefiles cause some distributed files to be rebuilt when the user
build the package. (Think about the user missing the tool required to
build the file; or if the required tool is built by your package,
consider the cross-compilation case where it can’t be run.) There is
a FAQ entry about this (see Files left in build directory after distclean), make sure you read it
before playing with distcleancheck_listfiles
.
distcheck
also checks that the uninstall
rule works
properly, both for ordinary and DESTDIR
builds. It does this
by invoking ‘make uninstall’, and then it checks the install tree
to see if any files are left over. This check will make sure that you
correctly coded your uninstall
-related rules.
By default, the checking is done by the distuninstallcheck
rule,
and the list of files in the install tree is generated by
‘$(distuninstallcheck_listfiles’) (this is a variable whose value is
a shell command to run that prints the list of files to stdout).
Either of these can be overridden to modify the behavior of
distcheck
. For instance, to disable this check completely, you
would write:
14.5 The types of distributions
Automake generates rules to provide archives of the project for
distributions in various formats. Their targets are:
dist-bzip2
Generate a bzip2 tar archive of the distribution. bzip2 archives are
frequently smaller than gzipped archives.
dist-gzip
Generate a gzip tar archive of the distribution.
dist-lzma
Generate a lzma tar archive of the distribution. lzma archives are
frequently smaller than bzip2
-compressed archives.
dist-shar
Generate a shar archive of the distribution.
dist-zip
Generate a zip archive of the distribution.
dist-tarZ
Generate a compressed tar archive of
the distribution.
The rule dist
(and its historical synonym dist-all
) will
create archives in all the enabled formats, Changing Automake’s Behavior. By
default, only the dist-gzip
target is hooked to dist
.
16 Rebuilding Makefiles
Automake generates rules to automatically rebuild Makefiles,
configure, and other derived files like Makefile.in.
If you are using AM_MAINTAINER_MODE
in configure.ac, then
these automatic rebuilding rules are only enabled in maintainer mode.
Sometimes you need to run aclocal
with an argument like
-I to tell it where to find .m4 files. Since
sometimes make
will automatically run aclocal
, you
need a way to specify these arguments. You can do this by defining
ACLOCAL_AMFLAGS
; this holds arguments that are passed verbatim
to aclocal
. This variable is only useful in the top-level
Makefile.am.
Sometimes it is convenient to supplement the rebuild rules for
configure or config.status with additional dependencies.
The variables CONFIGURE_DEPENDENCIES
and
CONFIG_STATUS_DEPENDENCIES
can be used to list these extra
dependencies. These variable should be defined in all
Makefiles of the tree (because these two rebuild rules are
output in all them), so it is safer and easier to AC_SUBST
them
from configure.ac. For instance, the following statement will
cause configure to be rerun each time version.sh is
changed.
AC_SUBST([CONFIG_STATUS_DEPENDENCIES], ['$(top_srcdir)/version.sh'])
Note the ‘$(top_srcdir)/’ in the file name. Since this variable
is to be used in all Makefiles, its value must be sensible at
any level in the build hierarchy.
Beware not to mistake CONFIGURE_DEPENDENCIES
for
CONFIG_STATUS_DEPENDENCIES
.
CONFIGURE_DEPENDENCIES
adds dependencies to the
configure rule, whose effect is to run autoconf
. This
variable should be seldom used, because automake
already tracks
m4_include
d files. However it can be useful when playing
tricky games with m4_esyscmd
or similar non-recommendable
macros with side effects.
CONFIG_STATUS_DEPENDENCIES
adds dependencies to the
config.status rule, whose effect is to run configure.
This variable should therefore carry any non-standard source that may
be read as a side effect of running configure, like version.sh
in the example above.
Speaking of version.sh scripts, we recommend against them
today. They are mainly used when the version of a package is updated
automatically by a script (e.g., in daily builds). Here is what some
old-style configure.acs may look like:
AC_INIT
. $srcdir/version.sh
AM_INIT_AUTOMAKE([name], $VERSION_NUMBER)
…
Here, version.sh is a shell fragment that sets
VERSION_NUMBER
. The problem with this example is that
automake
cannot track dependencies (listing version.sh
in CONFIG_STATUS_DEPENDENCIES
, and distributing this file is up
to the user), and that it uses the obsolete form of AC_INIT
and
AM_INIT_AUTOMAKE
. Upgrading to the new syntax is not
straightforward, because shell variables are not allowed in
AC_INIT
’s arguments. We recommend that version.sh be
replaced by an M4 file that is included by configure.ac:
m4_include([version.m4])
AC_INIT([name], VERSION_NUMBER)
AM_INIT_AUTOMAKE
…
Here version.m4 could contain something like
‘m4_define([VERSION_NUMBER], [1.2])’. The advantage of this
second form is that automake
will take care of the
dependencies when defining the rebuild rule, and will also distribute
the file automatically. An inconvenience is that autoconf
will now be rerun each time the version number is bumped, when only
configure had to be rerun in the previous setup.
23 When Automake Isn’t Enough
In some situations, where Automake is not up to one task, one has to
resort to handwritten rules or even handwritten Makefiles.
23.1 Extending Automake Rules
With some minor exceptions (for example _PROGRAMS
variables,
TESTS
, or XFAIL_TESTS
) being rewritten to append
‘$(EXEEXT)’), the contents of a Makefile.am is copied to
Makefile.in verbatim.
These copying semantics means that many problems can be worked around
by simply adding some make
variables and rules to
Makefile.am. Automake will ignore these additions.
Since a Makefile.in is built from data gathered from three
different places (Makefile.am, configure.ac, and
automake
itself), it is possible to have conflicting
definitions of rules or variables. When building Makefile.in
the following priorities are respected by automake
to ensure
the user always have the last word. User defined variables in
Makefile.am have priority over variables AC_SUBST
ed from
configure.ac, and AC_SUBST
ed variables have priority
over automake
-defined variables. As far rules are
concerned, a user-defined rule overrides any
automake
-defined rule for the same target.
These overriding semantics make it possible to fine tune some default
settings of Automake, or replace some of its rules. Overriding
Automake rules is often inadvisable, particularly in the topmost
directory of a package with subdirectories. The -Woverride
option (see Creating a Makefile.in) comes handy to catch overridden
definitions.
Note that Automake does not make any difference between rules with
commands and rules that only specify dependencies. So it is not
possible to append new dependencies to an automake
-defined
target without redefining the entire rule.
However, various useful targets have a ‘-local’ version you can
specify in your Makefile.am. Automake will supplement the
standard target with these user-supplied targets.
The targets that support a local version are all
, info
,
dvi
, ps
, pdf
, html
, check
,
install-data
, install-dvi
, install-exec
,
install-html
, install-info
, install-pdf
,
install-ps
, uninstall
, installdirs
,
installcheck
and the various clean
targets
(mostlyclean
, clean
, distclean
, and
maintainer-clean
).
Note that there are no uninstall-exec-local
or
uninstall-data-local
targets; just use uninstall-local
.
It doesn’t make sense to uninstall just data or just executables.
For instance, here is one way to erase a subdirectory during
‘make clean’ (see What Gets Cleaned).
clean-local:
-rm -rf testSubDir
You may be tempted to use install-data-local
to install a file
to some hard-coded location, but you should avoid this
(see Installing to Hard-Coded Locations).
With the -local
targets, there is no particular guarantee of
execution order; typically, they are run early, but with parallel
make, there is no way to be sure of that.
In contrast, some rules also have a way to run another rule, called a
hook; hooks are always executed after the main rule’s work is done.
The hook is named after the principal target, with ‘-hook’ appended.
The targets allowing hooks are install-data
,
install-exec
, uninstall
, dist
, and
distcheck
.
For instance, here is how to create a hard link to an installed program:
install-exec-hook:
ln $(DESTDIR)$(bindir)/program$(EXEEXT) \
$(DESTDIR)$(bindir)/proglink$(EXEEXT)
Although cheaper and more portable than symbolic links, hard links
will not work everywhere (for instance, OS/2 does not have
ln
). Ideally you should fall back to ‘cp -p’ when
ln
does not work. An easy way, if symbolic links are
acceptable to you, is to add AC_PROG_LN_S
to
configure.ac (see Particular Program
Checks in The Autoconf Manual) and use ‘$(LN_S)’ in
Makefile.am.
For instance, here is how you could install a versioned copy of a
program using ‘$(LN_S)’:
install-exec-hook:
cd $(DESTDIR)$(bindir) && \
mv -f prog$(EXEEXT) prog-$(VERSION)$(EXEEXT) && \
$(LN_S) prog-$(VERSION)$(EXEEXT) prog$(EXEEXT)
Note that we rename the program so that a new version will erase the
symbolic link, not the real binary. Also we cd
into the
destination directory in order to create relative links.
When writing install-exec-hook
or install-data-hook
,
please bear in mind that the exec/data distinction is based on the
installation directory, not on the primary used (see What Gets Installed). So
a foo_SCRIPTS
will be installed by install-data
, and a
barexec_SCRIPTS
will be installed by install-exec
. You
should define your hooks consequently.
23.2 Third-Party Makefiles
In most projects all Makefiles are generated by Automake. In
some cases, however, projects need to embed subdirectories with
handwritten Makefiles. For instance, one subdirectory could be
a third-party project with its own build system, not using Automake.
It is possible to list arbitrary directories in SUBDIRS
or
DIST_SUBDIRS
provided each of these directories has a
Makefile that recognizes all the following recursive targets.
When a user runs one of these targets, that target is run recursively
in all subdirectories. This is why it is important that even
third-party Makefiles support them.
all
Compile the entire package. This is the default target in
Automake-generated Makefiles, but it does not need to be the
default in third-party Makefiles.
distdir
¶
-
Copy files to distribute into ‘$(distdir)’, before a tarball is
constructed. Of course this target is not required if the
no-dist option (see Changing Automake’s Behavior) is used.
The variables ‘$(top_distdir)’ and ‘$(distdir)’
(see What Goes in a Distribution) will be passed from the outer package to the subpackage
when the distdir
target is invoked. These two variables have
been adjusted for the directory that is being recursed into, so they
are ready to use.
install
install-data
install-exec
uninstall
Install or uninstall files (see What Gets Installed).
install-dvi
install-html
install-info
install-ps
install-pdf
Install only some specific documentation format (see Texinfo).
installdirs
Create install directories, but do not install any files.
check
installcheck
Check the package (see Support for test suites).
mostlyclean
clean
distclean
maintainer-clean
Cleaning rules (see What Gets Cleaned).
dvi
pdf
ps
info
html
Build the documentation in various formats (see Texinfo).
tags
ctags
Build TAGS and CTAGS (see Interfacing to etags
).
If you have ever used Gettext in a project, this is a good example of
how third-party Makefiles can be used with Automake. The
Makefiles gettextize
puts in the po/ and
intl/ directories are handwritten Makefiles that
implement all these targets. That way they can be added to
SUBDIRS
in Automake packages.
Directories that are only listed in DIST_SUBDIRS
but not in
SUBDIRS
need only the distclean
,
maintainer-clean
, and distdir
rules (see Conditional Subdirectories).
Usually, many of these rules are irrelevant to the third-party
subproject, but they are required for the whole package to work. It’s
OK to have a rule that does nothing, so if you are integrating a
third-party project with no documentation or tag support, you could
simply augment its Makefile as follows:
EMPTY_AUTOMAKE_TARGETS = dvi pdf ps info html tags ctags
.PHONY: $(EMPTY_AUTOMAKE_TARGETS)
$(EMPTY_AUTOMAKE_TARGETS):
Another aspect of integrating third-party build systems is whether
they support VPATH builds (see Parallel Build Trees (a.k.a. VPATH Builds)). Obviously if the
subpackage does not support VPATH builds the whole package will not
support VPATH builds. This in turns means that ‘make distcheck’
will not work, because it relies on VPATH builds. Some people can
live without this (actually, many Automake users have never heard of
‘make distcheck’). Other people may prefer to revamp the
existing Makefiles to support VPATH. Doing so does not
necessarily require Automake, only Autoconf is needed (see Build Directories in The Autoconf Manual).
The necessary substitutions: ‘@srcdir@’, ‘@top_srcdir@’,
and ‘@top_builddir@’ are defined by configure when it
processes a Makefile (see Preset
Output Variables in The Autoconf Manual), they are not
computed by the Makefile like the aforementioned ‘$(distdir)’ and
‘$(top_distdir)’ variables..
It is sometimes inconvenient to modify a third-party Makefile
to introduce the above required targets. For instance, one may want to
keep the third-party sources untouched to ease upgrades to new
versions.
Here are two other ideas. If GNU make is assumed, one possibility is
to add to that subdirectory a GNUmakefile that defines the
required targets and include the third-party Makefile. For
this to work in VPATH builds, GNUmakefile must lie in the build
directory; the easiest way to do this is to write a
GNUmakefile.in instead, and have it processed with
AC_CONFIG_FILES
from the outer package. For example if we
assume Makefile defines all targets except the documentation
targets, and that the check
target is actually called
test
, we could write GNUmakefile (or
GNUmakefile.in) like this:
# First, include the real Makefile
include Makefile
# Then, define the other targets needed by Automake Makefiles.
.PHONY: dvi pdf ps info html check
dvi pdf ps info html:
check: test
A similar idea that does not use include
is to write a proxy
Makefile that dispatches rules to the real Makefile,
either with ‘$(MAKE) -f Makefile.real $(AM_MAKEFLAGS) target’ (if
it’s OK to rename the original Makefile) or with ‘cd
subdir && $(MAKE) $(AM_MAKEFLAGS) target’ (if it’s OK to store the
subdirectory project one directory deeper). The good news is that
this proxy Makefile can be generated with Automake. All we
need are -local targets (see Extending Automake Rules) that perform the
dispatch. Of course the other Automake features are available, so you
could decide to let Automake perform distribution or installation.
Here is a possible Makefile.am:
all-local:
cd subdir && $(MAKE) $(AM_MAKEFLAGS) all
check-local:
cd subdir && $(MAKE) $(AM_MAKEFLAGS) test
clean-local:
cd subdir && $(MAKE) $(AM_MAKEFLAGS) clean
# Assuming the package knows how to install itself
install-data-local:
cd subdir && $(MAKE) $(AM_MAKEFLAGS) install-data
install-exec-local:
cd subdir && $(MAKE) $(AM_MAKEFLAGS) install-exec
uninstall-local:
cd subdir && $(MAKE) $(AM_MAKEFLAGS) uninstall
# Distribute files from here.
EXTRA_DIST = subdir/Makefile subdir/program.c ...
Pushing this idea to the extreme, it is also possible to ignore the
subproject build system and build everything from this proxy
Makefile.am. This might sounds very sensible if you need VPATH
builds but the subproject does not support them.
25 Automake API versioning
New Automake releases usually include bug fixes and new features.
Unfortunately they may also introduce new bugs and incompatibilities.
This makes four reasons why a package may require a particular Automake
version.
Things get worse when maintaining a large tree of packages, each one
requiring a different version of Automake. In the past, this meant that
any developer (and sometime users) had to install several versions of
Automake in different places, and switch ‘$PATH’ appropriately for
each package.
Starting with version 1.6, Automake installs versioned binaries. This
means you can install several versions of Automake in the same
‘$prefix’, and can select an arbitrary Automake version by running
automake-1.6
or automake-1.7
without juggling with
‘$PATH’. Furthermore, Makefile’s generated by Automake 1.6
will use automake-1.6
explicitly in their rebuild rules.
The number ‘1.6’ in automake-1.6
is Automake’s API version,
not Automake’s version. If a bug fix release is made, for instance
Automake 1.6.1, the API version will remain 1.6. This means that a
package that works with Automake 1.6 should also work with 1.6.1; after
all, this is what people expect from bug fix releases.
If your package relies on a feature or a bug fix introduced in
a release, you can pass this version as an option to Automake to ensure
older releases will not be used. For instance, use this in your
configure.ac:
AM_INIT_AUTOMAKE([1.6.1]) dnl Require Automake 1.6.1 or better.
or, in a particular Makefile.am:
AUTOMAKE_OPTIONS = 1.6.1 # Require Automake 1.6.1 or better.
Automake will print an error message if its version is
older than the requested version.
What is in the API
Automake’s programming interface is not easy to define. Basically it
should include at least all documented variables and targets
that a Makefile.am author can use, any behavior associated with
them (e.g., the places where ‘-hook’’s are run), the command line
interface of automake
and aclocal
, …
What is not in the API
Every undocumented variable, target, or command line option, is not part
of the API. You should avoid using them, as they could change from one
version to the other (even in bug fix releases, if this helps to fix a
bug).
If it turns out you need to use such a undocumented feature, contact
automake@gnu.org and try to get it documented and exercised by
the test-suite.
27 Frequently Asked Questions about Automake
This chapter covers some questions that often come up on the mailing
lists.
27.1 CVS and generated files
27.1.1 Background: distributed generated files
Packages made with Autoconf and Automake ship with some generated
files like configure or Makefile.in. These files were
generated on the developer’s host and are distributed so that
end-users do not have to install the maintainer tools required to
rebuild them. Other generated files like Lex scanners, Yacc parsers,
or Info documentation, are usually distributed on similar grounds.
Automake outputs rules in Makefiles to rebuild these files. For
instance, make
will run autoconf
to rebuild
configure whenever configure.ac is changed. This makes
development safer by ensuring a configure is never out-of-date
with respect to configure.ac.
As generated files shipped in packages are up-to-date, and because
tar
preserves times-tamps, these rebuild rules are not
triggered when a user unpacks and builds a package.
27.1.2 Background: CVS and timestamps
Unless you use CVS keywords (in which case files must be updated at
commit time), CVS preserves timestamp during ‘cvs commit’ and
‘cvs import -d’ operations.
When you check out a file using ‘cvs checkout’ its timestamp is
set to that of the revision that is being checked out.
However, during cvs update
, files will have the date of the
update, not the original timestamp of this revision. This is meant to
make sure that make
notices sources files have been updated.
This timestamp shift is troublesome when both sources and generated
files are kept under CVS. Because CVS processes files in lexical
order, configure.ac will appear newer than configure
after a cvs update
that updates both files, even if
configure was newer than configure.ac when it was
checked in. Calling make
will then trigger a spurious rebuild
of configure.
27.1.3 Living with CVS in Autoconfiscated projects
There are basically two clans amongst maintainers: those who keep all
distributed files under CVS, including generated files, and those who
keep generated files out of CVS.
All files in CVS
- The CVS repository contains all distributed files so you know exactly
what is distributed, and you can checkout any prior version entirely.
- Maintainers can see how generated files evolve (for instance, you can
see what happens to your Makefile.ins when you upgrade Automake
and make sure they look OK).
- Users do not need the autotools to build a checkout of the project, it
works just like a released tarball.
- If users use
cvs update
to update their copy, instead of
cvs checkout
to fetch a fresh one, timestamps will be
inaccurate. Some rebuild rules will be triggered and attempt to
run developer tools such as autoconf
or automake
.
Actually, calls to such tools are all wrapped into a call to the
missing
script discussed later (see missing
and AM_MAINTAINER_MODE
).
missing
will take care of fixing the timestamps when these
tools are not installed, so that the build can continue.
- In distributed development, developers are likely to have different
version of the maintainer tools installed. In this case rebuilds
triggered by timestamp lossage will lead to spurious changes
to generated files. There are several solutions to this:
- All developers should use the same versions, so that the rebuilt files
are identical to files in CVS. (This starts to be difficult when each
project you work on uses different versions.)
- Or people use a script to fix the timestamp after a checkout (the GCC
folks have such a script).
- Or configure.ac uses
AM_MAINTAINER_MODE
, which will
disable all these rebuild rules by default. This is further discussed
in missing
and AM_MAINTAINER_MODE
.
- Although we focused on spurious rebuilds, the converse can also
happen. CVS’s timestamp handling can also let you think an
out-of-date file is up-to-date.
For instance, suppose a developer has modified Makefile.am and
has rebuilt Makefile.in. He then decide to do a last-minute
change to Makefile.am right before checking in both files
(without rebuilding Makefile.in to account for the change).
This last change to Makefile.am make the copy of
Makefile.in out-of-date. Since CVS processes files
alphabetically, when another developer ‘cvs update’ his or her
tree, Makefile.in will happen to be newer than
Makefile.am. This other developer will not see
Makefile.in is out-of-date.
Generated files out of CVS
One way to get CVS and make
working peacefully is to never
store generated files in CVS, i.e., do not CVS-control files that
are Makefile targets (also called derived files).
This way developers are not annoyed by changes to generated files. It
does not matter if they all have different versions (assuming they are
compatible, of course). And finally, timestamps are not lost, changes
to sources files can’t be missed as in the
Makefile.am/Makefile.in example discussed earlier.
The drawback is that the CVS repository is not an exact copy of what
is distributed and that users now need to install various development
tools (maybe even specific versions) before they can build a checkout.
But, after all, CVS’s job is versioning, not distribution.
Allowing developers to use different versions of their tools can also
hide bugs during distributed development. Indeed, developers will be
using (hence testing) their own generated files, instead of the
generated files that will be released actually. The developer who
prepares the tarball might be using a version of the tool that
produces bogus output (for instance a non-portable C file), something
other developers could have noticed if they weren’t using their own
versions of this tool.
27.1.4 Third-party files
Another class of files not discussed here (because they do not cause
timestamp issues) are files that are shipped with a package, but
maintained elsewhere. For instance, tools like gettextize
and autopoint
(from Gettext) or libtoolize
(from
Libtool), will install or update files in your package.
These files, whether they are kept under CVS or not, raise similar
concerns about version mismatch between developers’ tools. The
Gettext manual has a section about this, see Integrating with CVS in GNU gettext tools.
27.2 missing
and AM_MAINTAINER_MODE
27.2.1 missing
The missing
script is a wrapper around several maintainer
tools, designed to warn users if a maintainer tool is required but
missing. Typical maintainer tools are autoconf
,
automake
, bison
, etc. Because file generated by
these tools are shipped with the other sources of a package, these
tools shouldn’t be required during a user build and they are not
checked for in configure.
However, if for some reason a rebuild rule is triggered and involves a
missing tool, missing
will notice it and warn the user.
Besides the warning, when a tool is missing, missing
will
attempt to fix timestamps in a way that allows the build to continue.
For instance, missing
will touch configure if
autoconf
is not installed. When all distributed files are
kept under CVS, this feature of missing
allows user
with no maintainer tools to build a package off CVS, bypassing
any timestamp inconsistency implied by ‘cvs update’.
If the required tool is installed, missing
will run it and
won’t attempt to continue after failures. This is correct during
development: developers love fixing failures. However, users with
wrong versions of maintainer tools may get an error when the rebuild
rule is spuriously triggered, halting the build. This failure to let
the build continue is one of the arguments of the
AM_MAINTAINER_MODE
advocates.
27.2.2 AM_MAINTAINER_MODE
AM_MAINTAINER_MODE
disables the so called "rebuild rules" by
default. If you have AM_MAINTAINER_MODE
in
configure.ac, and run ‘./configure && make’, then
make
will *never* attempt to rebuilt configure,
Makefile.ins, Lex or Yacc outputs, etc. I.e., this disables
build rules for files that are usually distributed and that users
should normally not have to update.
If you run ‘./configure --enable-maintainer-mode’, then these
rebuild rules will be active.
People use AM_MAINTAINER_MODE
either because they do want their
users (or themselves) annoyed by timestamps lossage (see CVS and generated files), or
because they simply can’t stand the rebuild rules and prefer running
maintainer tools explicitly.
AM_MAINTAINER_MODE
also allows you to disable some custom build
rules conditionally. Some developers use this feature to disable
rules that need exotic tools that users may not have available.
Several years ago François Pinard pointed out several arguments
against this AM_MAINTAINER_MODE
macro. Most of them relate to
insecurity. By removing dependencies you get non-dependable builds:
change to sources files can have no effect on generated files and this
can be very confusing when unnoticed. He adds that security shouldn’t
be reserved to maintainers (what --enable-maintainer-mode
suggests), on the contrary. If one user has to modify a
Makefile.am, then either Makefile.in should be updated
or a warning should be output (this is what Automake uses
missing
for) but the last thing you want is that nothing
happens and the user doesn’t notice it (this is what happens when
rebuild rules are disabled by AM_MAINTAINER_MODE
).
Jim Meyering, the inventor of the AM_MAINTAINER_MODE
macro was
swayed by François’s arguments, and got rid of
AM_MAINTAINER_MODE
in all of his packages.
Still many people continue to use AM_MAINTAINER_MODE
, because
it helps them working on projects where all files are kept under CVS,
and because missing
isn’t enough if you have the wrong
version of the tools.
27.3 Why doesn’t Automake support wildcards?
Developers are lazy. They would often like to use wildcards in
Makefile.ams, so that they would not need to remember to
update Makefile.ams every time they add, delete, or rename
a file.
There are several objections to this:
Still, these are philosophical objections, and as such you may disagree,
or find enough value in wildcards to dismiss all of them. Before you
start writing a patch against Automake to teach it about wildcards,
let’s see the main technical issue: portability.
Although ‘$(wildcard ...)’ works with GNU make
, it is
not portable to other make
implementations.
The only way Automake could support $(wildcard ...)
is by
expending $(wildcard ...)
when automake
is run.
The resulting Makefile.ins would be portable since they would
list all files and not use ‘$(wildcard ...)’. However that
means developers would need to remember to run automake
each
time they add, delete, or rename files.
Compared to editing Makefile.am, this is a very small gain. Sure,
it’s easier and faster to type ‘automake; make’ than to type
‘emacs Makefile.am; make’. But nobody bothered enough to write a
patch to add support for this syntax. Some people use scripts to
generate file lists in Makefile.am or in separate
Makefile fragments.
Even if you don’t care about portability, and are tempted to use
‘$(wildcard ...)’ anyway because you target only GNU Make, you
should know there are many places where Automake need to know exactly
which files should be processed. As Automake doesn’t know how to
expand ‘$(wildcard ...)’, you cannot use it in these places.
‘$(wildcard ...)’ is a black box comparable to AC_SUBST
ed
variables as far Automake is concerned.
You can get warnings about ‘$(wildcard ...’) constructs using the
-Wportability flag.
27.4 Limitations on file names
Automake attempts to support all kinds of file names, even those that
contain unusual characters or are unusually long. However, some
limitations are imposed by the underlying operating system and tools.
Most operating systems prohibit the use of the null byte in file
names, and reserve ‘/’ as a directory separator. Also, they
require that file names are properly encoded for the user’s locale.
Automake is subject to these limits.
Portable packages should limit themselves to POSIX file
names. These can contain ASCII letters and digits,
‘_’, ‘.’, and ‘-’. File names consist of components
separated by ‘/’. File name components cannot begin with
‘-’.
Portable POSIX file names cannot contain components that exceed a
14-byte limit, but nowadays it’s normally safe to assume the
more-generous XOPEN limit of 255 bytes. POSIX
limits file names to 255 bytes (XOPEN allows 1023 bytes),
but you may want to limit a source tarball to file names to 99 bytes
to avoid interoperability problems with old versions of tar
.
If you depart from these rules (e.g., by using non-ASCII
characters in file names, or by using lengthy file names), your
installers may have problems for reasons unrelated to Automake.
However, if this does not concern you, you should know about the
limitations imposed by Automake itself. These limitations are
undesirable, but some of them seem to be inherent to underlying tools
like Autoconf, Make, M4, and the shell. They fall into three
categories: install directories, build directories, and file names.
The following characters:
should not appear in the names of install directories. For example,
the operand of configure
’s --prefix option should
not contain these characters.
Build directories suffer the same limitations as install directories,
and in addition should not contain the following characters:
For example, the full name of the directory containing the source
files should not contain these characters.
Source and installation file names like main.c are limited even
further: they should conform to the POSIX/XOPEN
rules described above. In addition, if you plan to port to
non-POSIX environments, you should avoid file names that
differ only in case (e.g., makefile and Makefile).
Nowadays it is no longer worth worrying about the 8.3 limits of
DOS file systems.
27.5 Files left in build directory after distclean
This is a diagnostic you might encounter while running ‘make
distcheck’.
As explained in What Goes in a Distribution, ‘make distcheck’ attempts to build
and check your package for errors like this one.
‘make distcheck’ will perform a VPATH
build of your
package (see Parallel Build Trees (a.k.a. VPATH Builds)), and then call ‘make distclean’.
Files left in the build directory after ‘make distclean’ has run
are listed after this error.
This diagnostic really covers two kinds of errors:
- files that are forgotten by distclean;
- distributed files that are erroneously rebuilt.
The former left-over files are not distributed, so the fix is to mark
them for cleaning (see What Gets Cleaned), this is obvious and doesn’t deserve
more explanations.
The latter bug is not always easy to understand and fix, so let’s
proceed with an example. Suppose our package contains a program for
which we want to build a man page using help2man
. GNU
help2man
produces simple manual pages from the --help
and --version output of other commands (see Overview in The Help2man Manual). Because we don’t to force want our
users to install help2man
, we decide to distribute the
generated man page using the following setup.
# This Makefile.am is bogus.
bin_PROGRAMS = foo
foo_SOURCES = foo.c
dist_man_MANS = foo.1
foo.1: foo$(EXEEXT)
help2man --output=foo.1 ./foo$(EXEEXT)
This will effectively distribute the man page. However,
‘make distcheck’ will fail with:
ERROR: files left in build directory after distclean:
./foo.1
Why was foo.1 rebuilt? Because although distributed,
foo.1 depends on a non-distributed built file:
foo$(EXEEXT). foo$(EXEEXT) is built by the user, so it
will always appear to be newer than the distributed foo.1.
‘make distcheck’ caught an inconsistency in our package. Our
intent was to distribute foo.1 so users do not need installing
help2man
, however since this our rule causes this file to be
always rebuilt, users do need help2man
. Either we
should ensure that foo.1 is not rebuilt by users, or there is
no point in distributing foo.1.
More generally, the rule is that distributed files should never depend
on non-distributed built files. If you distribute something
generated, distribute its sources.
One way to fix the above example, while still distributing
foo.1 is to not depend on foo$(EXEEXT). For instance,
assuming foo --version
and foo --help
do not
change unless foo.c or configure.ac change, we could
write the following Makefile.am:
bin_PROGRAMS = foo
foo_SOURCES = foo.c
dist_man_MANS = foo.1
foo.1: foo.c $(top_srcdir)/configure.ac
$(MAKE) $(AM_MAKEFLAGS) foo$(EXEEXT)
help2man --output=foo.1 ./foo$(EXEEXT)
This way, foo.1 will not get rebuilt every time
foo$(EXEEXT) changes. The make
call makes sure
foo$(EXEEXT) is up-to-date before help2man
. Another
way to ensure this would be to use separate directories for binaries
and man pages, and set SUBDIRS
so that binaries are built
before man pages.
We could also decide not to distribute foo.1. In
this case it’s fine to have foo.1 dependent upon
foo$(EXEEXT), since both will have to be rebuilt.
However it would be impossible to build the package in a
cross-compilation, because building foo.1 involves
an execution of foo$(EXEEXT).
Another context where such errors are common is when distributed files
are built by tools that are built by the package. The pattern is
similar:
distributed-file: built-tools distributed-sources
build-command
should be changed to
distributed-file: distributed-sources
$(MAKE) $(AM_MAKEFLAGS) built-tools
build-command
or you could choose not to distribute distributed-file, if
cross-compilation does not matter.
The points made through these examples are worth a summary:
- Distributed files should never depend upon non-distributed built
files.
- Distributed files should be distributed with all their dependencies.
- If a file is intended to be rebuilt by users, then there is no point
in distributing it.
|
For desperate cases, it’s always possible to disable this check by
setting distcleancheck_listfiles
as documented in What Goes in a Distribution.
Make sure you do understand the reason why ‘make distcheck’
complains before you do this. distcleancheck_listfiles
is a
way to hide errors, not to fix them. You can always do better.
27.6 Flag Variables Ordering
What is the difference between AM_CFLAGS
, CFLAGS
, and
mumble_CFLAGS
?
Why does automake
output CPPFLAGS
after
AM_CPPFLAGS
on compile lines? Shouldn’t it be the converse?
My configure adds some warning flags into CXXFLAGS
. In
one Makefile.am I would like to append a new flag, however if I
put the flag into AM_CXXFLAGS
it is prepended to the other
flags, not appended.
27.6.1 Compile Flag Variables
This section attempts to answer all the above questions. We will
mostly discuss CPPFLAGS
in our examples, but actually the
answer holds for all the compile flags used in Automake:
CCASFLAGS
, CFLAGS
, CPPFLAGS
, CXXFLAGS
,
FCFLAGS
, FFLAGS
, GCJFLAGS
, LDFLAGS
,
LFLAGS
, LIBTOOLFLAGS
, OBJCFLAGS
, RFLAGS
,
UPCFLAGS
, and YFLAGS
.
CPPFLAGS
, AM_CPPFLAGS
, and mumble_CPPFLAGS
are
three variables that can be used to pass flags to the C preprocessor
(actually these variables are also used for other languages like C++
or preprocessed Fortran). CPPFLAGS
is the user variable
(see Variables reserved for the user), AM_CPPFLAGS
is the Automake variable,
and mumble_CPPFLAGS
is the variable specific to the
mumble
target (we call this a per-target variable,
see Program and Library Variables).
Automake always uses two of these variables when compiling C sources
files. When compiling an object file for the mumble
target,
the first variable will be mumble_CPPFLAGS
if it is defined, or
AM_CPPFLAGS
otherwise. The second variable is always
CPPFLAGS
.
In the following example,
bin_PROGRAMS = foo bar
foo_SOURCES = xyz.c
bar_SOURCES = main.c
foo_CPPFLAGS = -DFOO
AM_CPPFLAGS = -DBAZ
xyz.o will be compiled with ‘$(foo_CPPFLAGS) $(CPPFLAGS)’,
(because xyz.o is part of the foo
target), while
main.o will be compiled with ‘$(AM_CPPFLAGS) $(CPPFLAGS)’
(because there is no per-target variable for target bar
).
The difference between mumble_CPPFLAGS
and AM_CPPFLAGS
being clear enough, let’s focus on CPPFLAGS
. CPPFLAGS
is a user variable, i.e., a variable that users are entitled to modify
in order to compile the package. This variable, like many others,
is documented at the end of the output of ‘configure --help’.
For instance, someone who needs to add /home/my/usr/include to
the C compiler’s search path would configure a package with
./configure CPPFLAGS='-I /home/my/usr/include'
and this flag would be propagated to the compile rules of all
Makefiles.
It is also not uncommon to override a user variable at
make
-time. Many installers do this with prefix
, but
this can be useful with compiler flags too. For instance, if, while
debugging a C++ project, you need to disable optimization in one
specific object file, you can run something like
rm file.o
make CXXFLAGS=-O0 file.o
make
The reason ‘$(CPPFLAGS)’ appears after ‘$(AM_CPPFLAGS)’ or
‘$(mumble_CPPFLAGS)’ in the compile command is that users
should always have the last say. It probably makes more sense if you
think about it while looking at the ‘CXXFLAGS=-O0’ above, which
should supersede any other switch from AM_CXXFLAGS
or
mumble_CXXFLAGS
(and this of course replaces the previous value
of CXXFLAGS
).
You should never redefine a user variable such as CPPFLAGS
in
Makefile.am. Use ‘automake -Woverride’ to diagnose such
mistakes. Even something like
CPPFLAGS = -DDATADIR=\"$(datadir)\" @CPPFLAGS@
is erroneous. Although this preserves configure’s value of
CPPFLAGS
, the definition of DATADIR
will disappear if a
user attempts to override CPPFLAGS
from the make
command line.
AM_CPPFLAGS = -DDATADIR=\"$(datadir)\"
is all what is needed here if no per-target flags are used.
You should not add options to these user variables within
configure either, for the same reason. Occasionally you need
to modify these variables to perform a test, but you should reset
their values afterwards. In contrast, it is OK to modify the
‘AM_’ variables within configure if you AC_SUBST
them, but it is rather rare that you need to do this, unless you
really want to change the default definitions of the ‘AM_’
variables in all Makefiles.
What we recommend is that you define extra flags in separate
variables. For instance, you may write an Autoconf macro that computes
a set of warning options for the C compiler, and AC_SUBST
them
in WARNINGCFLAGS
; you may also have an Autoconf macro that
determines which compiler and which linker flags should be used to
link with library libfoo, and AC_SUBST
these in
LIBFOOCFLAGS
and LIBFOOLDFLAGS
. Then, a
Makefile.am could use these variables as follows:
AM_CFLAGS = $(WARNINGCFLAGS)
bin_PROGRAMS = prog1 prog2
prog1_SOURCES = …
prog2_SOURCES = …
prog2_CFLAGS = $(LIBFOOCFLAGS) $(AM_CFLAGS)
prog2_LDFLAGS = $(LIBFOOLDFLAGS)
In this example both programs will be compiled with the flags
substituted into ‘$(WARNINGCFLAGS)’, and prog2
will
additionally be compiled with the flags required to link with
libfoo.
Note that listing AM_CFLAGS
in a per-target CFLAGS
variable is a common idiom to ensure that AM_CFLAGS
applies to
every target in a Makefile.in.
Using variables like this gives you full control over the ordering of
the flags. For instance, if there is a flag in $(WARNINGCFLAGS) that
you want to negate for a particular target, you can use something like
‘prog1_CFLAGS = $(AM_CFLAGS) -no-flag’. If all these flags had
been forcefully appended to CFLAGS
, there would be no way to
disable one flag. Yet another reason to leave user variables to
users.
Finally, we have avoided naming the variable of the example
LIBFOO_LDFLAGS
(with an underscore) because that would cause
Automake to think that this is actually a per-target variable (like
mumble_LDFLAGS
) for some non-declared LIBFOO
target.
27.6.2 Other Variables
There are other variables in Automake that follow similar principles
to allow user options. For instance, Texinfo rules (see Texinfo)
use MAKEINFOFLAGS
and AM_MAKEINFOFLAGS
. Similarly,
DejaGnu tests (see Support for test suites) use RUNTESTDEFAULTFLAGS
and
AM_RUNTESTDEFAULTFLAGS
. The tags and ctags rules
(see Interfacing to etags
) use ETAGSFLAGS
, AM_ETAGSFLAGS
,
CTAGSFLAGS
, and AM_CTAGSFLAGS
. Java rules
(see Java) use JAVACFLAGS
and AM_JAVACFLAGS
. None
of these rules supports per-target flags (yet).
To some extent, even AM_MAKEFLAGS
(see Recursing subdirectories)
obeys this naming scheme. The slight difference is that
MAKEFLAGS
is passed to sub-make
s implicitly by
make
itself.
However you should not think that all variables ending with
FLAGS
follow this convention. For instance,
DISTCHECK_CONFIGURE_FLAGS
(see What Goes in a Distribution),
ACLOCAL_AMFLAGS
(see Rebuilding Makefiles and Handling Local Macros),
are two variables that are only useful to the maintainer and have no
user counterpart.
ARFLAGS
(see Building a library) is usually defined by Automake and
has neither AM_
nor per-target cousin.
Finally you should not think either that the existence of a per-target
variable implies that of an AM_
variable or that of a user
variable. For instance, the mumble_LDADD
per-target variable
overrides the global LDADD
variable (which is not a user
variable), and mumble_LIBADD
exists only as a per-target
variable. See Program and Library Variables.
27.7 Why are object files sometimes renamed?
This happens when per-target compilation flags are used. Object
files need to be renamed just in case they would clash with object
files compiled from the same sources, but with different flags.
Consider the following example.
bin_PROGRAMS = true false
true_SOURCES = generic.c
true_CPPFLAGS = -DEXIT_CODE=0
false_SOURCES = generic.c
false_CPPFLAGS = -DEXIT_CODE=1
Obviously the two programs are built from the same source, but it
would be bad if they shared the same object, because generic.o
cannot be built with both ‘-DEXIT_CODE=0’ and
‘-DEXIT_CODE=1’. Therefore automake
outputs rules to
build two different objects: true-generic.o and
false-generic.o.
automake
doesn’t actually look whether source files are
shared to decide if it must rename objects. It will just rename all
objects of a target as soon as it sees per-target compilation flags
are used.
It’s OK to share object files when per-target compilation flags are not
used. For instance, true and false will both use
version.o in the following example.
AM_CPPFLAGS = -DVERSION=1.0
bin_PROGRAMS = true false
true_SOURCES = true.c version.c
false_SOURCES = false.c version.c
Note that the renaming of objects is also affected by the
_SHORTNAME
variable (see Program and Library Variables).
27.8 Per-Object Flags Emulation
One of my source files needs to be compiled with different flags. How
do I do?
Automake supports per-program and per-library compilation flags (see
Program and Library Variables and Flag Variables Ordering). With this you can define compilation flags that apply to
all files compiled for a target. For instance, in
bin_PROGRAMS = foo
foo_SOURCES = foo.c foo.h bar.c bar.h main.c
foo_CFLAGS = -some -flags
foo-foo.o, foo-bar.o, and foo-main.o will all be
compiled with ‘-some -flags’. (If you wonder about the names of
these object files, see Why are object files sometimes renamed?.) Note that
foo_CFLAGS
gives the flags to use when compiling all the C
sources of the program foo
, it has nothing to do with
foo.c or foo-foo.o specifically.
What if foo.c needs to be compiled into foo.o using some
specific flags, that none of the other files requires? Obviously
per-program flags are not directly applicable here. Something like
per-object flags are expected, i.e., flags that would be used only
when creating foo-foo.o. Automake does not support that,
however this is easy to simulate using a library that contains only
that object, and compiling this library with per-library flags.
bin_PROGRAMS = foo
foo_SOURCES = bar.c bar.h main.c
foo_CFLAGS = -some -flags
foo_LDADD = libfoo.a
noinst_LIBRARIES = libfoo.a
libfoo_a_SOURCES = foo.c foo.h
libfoo_a_CFLAGS = -some -other -flags
Here foo-bar.o and foo-main.o will all be
compiled with ‘-some -flags’, while libfoo_a-foo.o will
be compiled using ‘-some -other -flags’. Eventually, all
three objects will be linked to form foo.
This trick can also be achieved using Libtool convenience libraries,
for instance ‘noinst_LTLIBRARIES = libfoo.la’ (see Libtool Convenience Libraries).
Another tempting idea to implement per-object flags is to override the
compile rules automake
would output for these files.
Automake will not define a rule for a target you have defined, so you
could think about defining the ‘foo-foo.o: foo.c’ rule yourself.
We recommend against this, because this is error prone. For instance,
if you add such a rule to the first example, it will break the day you
decide to remove foo_CFLAGS
(because foo.c will then be
compiled as foo.o instead of foo-foo.o, see Why are object files sometimes renamed?). Also in order to support dependency tracking, the two
.o/.obj extensions, and all the other flags variables
involved in a compilation, you will end up modifying a copy of the
rule previously output by automake
for this file. If a new
release of Automake generates a different rule, your copy will need to
be updated by hand.
27.9 Handling Tools that Produce Many Outputs
This section describes a make
idiom that can be used when a
tool produces multiple output files. It is not specific to Automake
and can be used in ordinary Makefiles.
Suppose we have a program called foo
that will read one file
called data.foo and produce two files named data.c and
data.h. We want to write a Makefile rule that captures
this one-to-two dependency.
The naive rule is incorrect:
# This is incorrect.
data.c data.h: data.foo
foo data.foo
What the above rule really says is that data.c and
data.h each depend on data.foo, and can each be built by
running ‘foo data.foo’. In other words it is equivalent to:
# We do not want this.
data.c: data.foo
foo data.foo
data.h: data.foo
foo data.foo
which means that foo
can be run twice. Usually it will not
be run twice, because make
implementations are smart enough
to check for the existence of the second file after the first one has
been built; they will therefore detect that it already exists.
However there are a few situations where it can run twice anyway:
- The most worrying case is when running a parallel
make
. If
data.c and data.h are built in parallel, two ‘foo
data.foo’ commands will run concurrently. This is harmful.
- Another case is when the dependency (here data.foo) is
(or depends upon) a phony target.
A solution that works with parallel make
but not with
phony dependencies is the following:
data.c data.h: data.foo
foo data.foo
data.h: data.c
The above rules are equivalent to
data.c: data.foo
foo data.foo
data.h: data.foo data.c
foo data.foo
therefore a parallel make
will have to serialize the builds
of data.c and data.h, and will detect that the second is
no longer needed once the first is over.
Using this pattern is probably enough for most cases. However it does
not scale easily to more output files (in this scheme all output files
must be totally ordered by the dependency relation), so we will
explore a more complicated solution.
Another idea is to write the following:
# There is still a problem with this one.
data.c: data.foo
foo data.foo
data.h: data.c
The idea is that ‘foo data.foo’ is run only when data.c
needs to be updated, but we further state that data.h depends
upon data.c. That way, if data.h is required and
data.foo is out of date, the dependency on data.c will
trigger the build.
This is almost perfect, but suppose we have built data.h and
data.c, and then we erase data.h. Then, running
‘make data.h’ will not rebuild data.h. The above rules
just state that data.c must be up-to-date with respect to
data.foo, and this is already the case.
What we need is a rule that forces a rebuild when data.h is
missing. Here it is:
data.c: data.foo
foo data.foo
data.h: data.c
## Recover from the removal of $@
@if test -f $@; then :; else \
rm -f data.c; \
$(MAKE) $(AM_MAKEFLAGS) data.c; \
fi
The above scheme can be extended to handle more outputs and more
inputs. One of the outputs is selected to serve as a witness to the
successful completion of the command, it depends upon all inputs, and
all other outputs depend upon it. For instance, if foo
should additionally read data.bar and also produce
data.w and data.x, we would write:
data.c: data.foo data.bar
foo data.foo data.bar
data.h data.w data.x: data.c
## Recover from the removal of $@
@if test -f $@; then :; else \
rm -f data.c; \
$(MAKE) $(AM_MAKEFLAGS) data.c; \
fi
However there are now two minor problems in this setup. One is related
to the timestamp ordering of data.h, data.w,
data.x, and data.c. The other one is a race condition
if a parallel make
attempts to run multiple instances of the
recover block at once.
Let us deal with the first problem. foo
outputs four files,
but we do not know in which order these files are created. Suppose
that data.h is created before data.c. Then we have a
weird situation. The next time make
is run, data.h
will appear older than data.c, the second rule will be
triggered, a shell will be started to execute the ‘if…fi’
command, but actually it will just execute the then
branch,
that is: nothing. In other words, because the witness we selected is
not the first file created by foo
, make
will start
a shell to do nothing each time it is run.
A simple riposte is to fix the timestamps when this happens.
data.c: data.foo data.bar
foo data.foo data.bar
data.h data.w data.x: data.c
@if test -f $@; then \
touch $@; \
else \
## Recover from the removal of $@
rm -f data.c; \
$(MAKE) $(AM_MAKEFLAGS) data.c; \
fi
Another solution is to use a different and dedicated file as witness,
rather than using any of foo
’s outputs.
data.stamp: data.foo data.bar
@rm -f data.tmp
@touch data.tmp
foo data.foo data.bar
@mv -f data.tmp $@
data.c data.h data.w data.x: data.stamp
## Recover from the removal of $@
@if test -f $@; then :; else \
rm -f data.stamp; \
$(MAKE) $(AM_MAKEFLAGS) data.stamp; \
fi
data.tmp is created before foo
is run, so it has a
timestamp older than output files output by foo
. It is then
renamed to data.stamp after foo
has run, because we
do not want to update data.stamp if foo
fails.
This solution still suffers from the second problem: the race
condition in the recover rule. If, after a successful build, a user
erases data.c and data.h, and runs ‘make -j’, then
make
may start both recover rules in parallel. If the two
instances of the rule execute ‘$(MAKE) $(AM_MAKEFLAGS)
data.stamp’ concurrently the build is likely to fail (for instance, the
two rules will create data.tmp, but only one can rename it).
Admittedly, such a weird situation does not arise during ordinary
builds. It occurs only when the build tree is mutilated. Here
data.c and data.h have been explicitly removed without
also removing data.stamp and the other output files.
make clean; make
will always recover from these situations even
with parallel makes, so you may decide that the recover rule is solely
to help non-parallel make users and leave things as-is. Fixing this
requires some locking mechanism to ensure only one instance of the
recover rule rebuilds data.stamp. One could imagine something
along the following lines.
data.c data.h data.w data.x: data.stamp
## Recover from the removal of $@
@if test -f $@; then :; else \
trap 'rm -rf data.lock data.stamp' 1 2 13 15; \
## mkdir is a portable test-and-set
if mkdir data.lock 2>/dev/null; then \
## This code is being executed by the first process.
rm -f data.stamp; \
$(MAKE) $(AM_MAKEFLAGS) data.stamp; \
result=$$?; rm -rf data.lock; exit $$result; \
else \
## This code is being executed by the follower processes.
## Wait until the first process is done.
while test -d data.lock; do sleep 1; done; \
## Succeed if and only if the first process succeeded.
test -f data.stamp; \
fi; \
fi
Using a dedicated witness, like data.stamp, is very handy when
the list of output files is not known beforehand. As an illustration,
consider the following rules to compile many *.el files into
*.elc files in a single command. It does not matter how
ELFILES
is defined (as long as it is not empty: empty targets
are not accepted by POSIX).
ELFILES = one.el two.el three.el …
ELCFILES = $(ELFILES:=c)
elc-stamp: $(ELFILES)
@rm -f elc-temp
@touch elc-temp
$(elisp_comp) $(ELFILES)
@mv -f elc-temp $@
$(ELCFILES): elc-stamp
## Recover from the removal of $@
@if test -f $@; then :; else \
trap 'rm -rf elc-lock elc-stamp' 1 2 13 15; \
if mkdir elc-lock 2>/dev/null; then \
## This code is being executed by the first process.
rm -f elc-stamp; \
$(MAKE) $(AM_MAKEFLAGS) elc-stamp; \
rmdir elc-lock; \
else \
## This code is being executed by the follower processes.
## Wait until the first process is done.
while test -d elc-lock; do sleep 1; done; \
## Succeed if and only if the first process succeeded.
test -f elc-stamp; exit $$?; \
fi; \
fi
For completeness it should be noted that GNU make
is able to
express rules with multiple output files using pattern rules
(see Pattern Rule Examples in The GNU Make
Manual). We do not discuss pattern rules here because they are not
portable, but they can be convenient in packages that assume GNU
make
.
27.10 Installing to Hard-Coded Locations
My package needs to install some configuration file. I tried to use
the following rule, but ‘make distcheck’ fails. Why?
# Do not do this.
install-data-local:
$(INSTALL_DATA) $(srcdir)/afile $(DESTDIR)/etc/afile
My package needs to populate the installation directory of another
package at install-time. I can easily compute that installation
directory in configure, but if I install files therein,
‘make distcheck’ fails. How else should I do?
These two setups share their symptoms: ‘make distcheck’ fails
because they are installing files to hard-coded paths. In the later
case the path is not really hard-coded in the package, but we can
consider it to be hard-coded in the system (or in whichever tool that
supplies the path). As long as the path does not use any of the
standard directory variables (‘$(prefix)’, ‘$(bindir)’,
‘$(datadir)’, etc.), the effect will be the same:
user-installations are impossible.
When a (non-root) user wants to install a package, he usually has no
right to install anything in /usr or /usr/local. So he
does something like ‘./configure --prefix ~/usr’ to install
package in his own ~/usr tree.
If a package attempts to install something to some hard-coded path
(e.g., /etc/afile), regardless of this --prefix setting,
then the installation will fail. ‘make distcheck’ performs such
a --prefix installation, hence it will fail too.
Now, there are some easy solutions.
The above install-data-local
example for installing
/etc/afile would be better replaced by
by default sysconfdir
will be ‘$(prefix)/etc’, because
this is what the GNU Standards require. When such a package is
installed on a FHS compliant system, the installer will have to set
‘--sysconfdir=/etc’. As the maintainer of the package you
should not be concerned by such site policies: use the appropriate
standard directory variable to install your files so that installer
can easily redefine these variables to match their site conventions.
Installing files that should be used by another package is slightly
more involved. Let’s take an example and assume you want to install
shared library that is a Python extension module. If you ask Python
where to install the library, it will answer something like this:
% python -c 'from distutils import sysconfig;
print sysconfig.get_python_lib(1,0)'
/usr/lib/python2.3/site-packages
If you indeed use this absolute path to install your shared library,
non-root users will not be able to install the package, hence
distcheck fails.
Let’s do better. The ‘sysconfig.get_python_lib()’ function
actually accepts a third argument that will replace Python’s
installation prefix.
% python -c 'from distutils import sysconfig;
print sysconfig.get_python_lib(1,0,"${exec_prefix}")'
${exec_prefix}/lib/python2.3/site-packages
You can also use this new path. If you do
- root users can install your package with the same --prefix
as Python (you get the behavior of the previous attempt)
- non-root users can install your package too, they will have the
extension module in a place that is not searched by Python but they
can work around this using environment variables (and if you installed
scripts that use this shared library, it’s easy to tell Python were to
look in the beginning of your script, so the script works in both
cases).
The AM_PATH_PYTHON
macro uses similar commands to define
‘$(pythondir)’ and ‘$(pyexecdir)’ (see Python).
Of course not all tools are as advanced as Python regarding that
substitution of prefix. So another strategy is to figure the
part of the of the installation directory that must be preserved. For
instance, here is how AM_PATH_LISPDIR
(see Emacs Lisp)
computes ‘$(lispdir)’:
$EMACS -batch -q -eval '(while load-path
(princ (concat (car load-path) "\n"))
(setq load-path (cdr load-path)))' >conftest.out
lispdir=`sed -n
-e 's,/$,,'
-e '/.*\/lib\/x*emacs\/site-lisp$/{
s,.*/lib/\(x*emacs/site-lisp\)$,${libdir}/\1,;p;q;
}'
-e '/.*\/share\/x*emacs\/site-lisp$/{
s,.*/share/\(x*emacs/site-lisp\),${datarootdir}/\1,;p;q;
}'
conftest.out`
I.e., it just picks the first directory that looks like
*/lib/*emacs/site-lisp or */share/*emacs/site-lisp in
the search path of emacs, and then substitutes ‘${libdir}’ or
‘${datadir}’ appropriately.
The emacs case looks complicated because it processes a list and
expect two possible layouts, otherwise it’s easy, and the benefit for
non-root users are really worth the extra sed
invocation.