[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

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 ‘configure.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:

 
AM_CONDITIONAL(TRUE, 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 ‘configure.in’, you can refer to it in your ‘Makefile.am’ using the ‘if’ statement. Here is a part of a sample ‘Makefile.am’ that uses the conditions defined above:

 
if TRUE
## This is always used.
bin_PROGRAMS = foo
endif

if DEBUG
AM_CFLAGS = -g -DDEBUG
endif

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
if FULL_ECHO
echo_SOURCES = echo.c extras.c getopt.c
else
echo_SOURCES = echo.c
endif

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
if SOME_CONDITION
z_SOURCES += cond.c
endif

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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]

This document was generated by Ben Elliston on May 30, 2015 using texi2html 1.82.