20.1 Usage of Conditionals

Before using a conditional, you must define it by using AM_CONDITIONAL in the configure.ac file (see Autoconf macros supplied with Automake).

Macro: AM_CONDITIONAL (conditional, condition)

The conditional name, conditional, should be a simple string starting with a letter and containing only letters, digits, and underscores. It must be different from ‘TRUE’ and ‘FALSE’, which are reserved by Automake.

The shell condition (suitable for use in a shell if statement) is evaluated when configure is run. Note that you must arrange for every AM_CONDITIONAL to be invoked every time configure is run. If AM_CONDITIONAL is run conditionally (e.g., in a shell if statement), then the result will confuse automake.

For portability, it is best to use shell operators && and || and parentheses, when constructing a compound condition using the test command, and not the -a and -o options and parentheses as options to test, all of which have been marked obsolescent by POSIX (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html#tag_20_128_161). The name test is also more portable than [. See Limitations of Builtins in The Autoconf Manual.

Conditionals typically depend upon options that the user provides to the configure script. Here is an example of how to write a conditional that is true if the user uses the --enable-debug option.

AC_ARG_ENABLE([debug],
[  --enable-debug    Turn on debugging],
[case "${enableval}" in
  yes) debug=true ;;
  no)  debug=false ;;
  *) AC_MSG_ERROR([bad value ${enableval} for --enable-debug]) ;;
esac],[debug=false])
AM_CONDITIONAL([DEBUG], [test x$debug = xtrue])

Here is an example of how to use that conditional in Makefile.am:

if DEBUG
DBG = debug
else
DBG =
endif
noinst_PROGRAMS = $(DBG)

This trivial example could also be handled using EXTRA_PROGRAMS (see Conditional compilation of programs).

You may only test a single variable in an if statement, possibly negated using ‘!’. The else statement may be omitted. Conditionals may be nested to any depth. You may specify an argument to else in which case it must be the negation of the condition used for the current if. Similarly you may specify the condition that is closed on the endif line:

if DEBUG
DBG = debug
else !DEBUG
DBG =
endif !DEBUG

Unbalanced conditions are errors. The if, else, and endif statements should not be indented, i.e., start on column one.

The else branch of the above two examples could be omitted, since assigning the empty string to an otherwise undefined variable makes no difference.

In order to allow access to the condition registered by AM_CONDITIONAL inside configure.ac, and to allow conditional AC_CONFIG_FILES, AM_COND_IF may be used:

Macro: AM_COND_IF (conditional, [if-true], [if-false])

If conditional is fulfilled, execute if-true, otherwise execute if-false. If either branch contains AC_CONFIG_FILES, it will cause automake to output the rules for the respective files only for the given condition.

AM_COND_IF macros may be nested when M4 quotation is used properly (see M4 Quotation in The Autoconf Manual).

Here is an example of how to define a conditional config file:

AM_CONDITIONAL([SHELL_WRAPPER], [test "x$with_wrapper" = xtrue])
AM_COND_IF([SHELL_WRAPPER],
           [AC_CONFIG_FILES([wrapper:wrapper.in])])