Back: Advanced GNU Automake Usage
Forward: Language support
FastBack: Advanced GNU Automake Usage
Up: Advanced GNU Automake Usage
FastForward: A Complex GNU Autotools Project
Top: Autoconf, Automake, and Libtool
Contents: Table of Contents
Index: Index
About: About this document

19.1 Conditionals

Automake conditionals are a way to omit or include different parts of the `Makefile' depending on what configure discovers. A conditional is introduced in `' using the `AM_CONDITIONAL' macro. This macro takes two arguments: the first is the name of the condition, and the second is a shell expression which returns true when the condition is true.

For instance, here is how to make a condition named `TRUE' which is always true:


As another example, here is how to make a condition named `DEBUG' which is true when the user has given the `--enable-debug' option to configure:

AM_CONDITIONAL(DEBUG, test "$enable_debug" = yes)

Once you've defined a condition in `', you can refer to it in your `' using the `if' statement. Here is a part of a sample `' that uses the conditions defined above:

## This is always used.
bin_PROGRAMS = foo


It's important to remember that Automake conditionals are configure-time conditionals. They don't rely on any special feature of make, and there is no way for the user to affect the conditionals from the make command line. Automake conditionals work by rewriting the `Makefile' -- make is unaware that these conditionals even exist.

Traditionally, Automake conditionals have been considered an advanced feature. However, practice has shown that they are often easier to use and understand than other approaches to solving the same problem. I now recommend the use of conditionals to everyone.

For instance, consider this example:

bin_PROGRAMS = echo
echo_SOURCES = echo.c extras.c getopt.c
echo_SOURCES = echo.c

In this case, the equivalent code without conditionals is more confusing and correspondingly more difficult for the new Automake user to figure out:

bin_PROGRAMS = echo
echo_SOURCES = echo.c
echo_LDADD   = @echo_extras@
EXTRA_echo_SOURCES = extras.c getopt.c

Automake conditionals have some limitations. One known problem is that conditionals don't interact properly with `+=' assignment. For instance, consider this code:

bin_PROGRAMS = z
z_SOURCES = z.c
z_SOURCES += cond.c

This code appears to have an unambiguous meaning, but Automake 1.4 doesn't implement this and will give an error. This bug will be fixed in the next major Automake release.

This document was generated by Gary V. Vaughan on February, 8 2006 using texi2html