makefileconfigureautotoolsautomake

Combining two conditionals using AND in Makefile.am


Is it possible to have something like below in a Makefile.am?

if CONDITION_1 and CONDITION_2
...
endif

Currently I have something like below in my configure.ac

AM_CONDITIONAL([COMBINED_CONDITION],
               [test "$cond_1" != "no" -a "$cond_2" != "no"])

And use it as below in my Makefile.am.

if COMBINED_CONDITION
...
endif

I am fairly new to autotools and couldn't get the ANDed condition to work and want to know if it is at all possible. So far I have not been able to find any reference that suggests it is.


Solution

  • I would use nested if ... endif like

    if CONDITION_1
    if CONDITION_2
    ...
    endif
    endif
    

    to implement the pseudo code

    # pseudo code
    if CONDITION_1 and CONDITION_2
    ...
    endif
    

    The same logic applies for the other three pseudo code conditions

    # pseudo code
    if CONDITION_1 and not(CONDITION_2)
    
    if not(CONDITION_1) and CONDITION_2
    
    if not(CONDITION_1) and not(CONDITION_2)
    

    which can be implemented as e.g.

    if CONDITION1
    if !CONDITION2
    ...
    endif
    endif
    

    However, the negation of a complete boolean expression cannot be expressed by nested if ... endif. So if you need to implement OR logic

    # pseudo code
    if CONDITION_1 or CONDITION_2
    ...
    endif
    

    you cannot apply De Morgan's law and do something purely with Automake if ... else ... endif because you cannot formulate a combined else branch. So you will need to define specially formulated AM_CONDITIONAL for an OR condition, or any other more complicated condition than a bunch of ANDs.

    As an aside regarding the combined conditional, I recall -a and -o being listed as a non-portable argument to test and therefore

    AM_CONDITIONAL([COMBINED_CONDITION_AND],
                   [test "x$cond_1" != xno && test "x$cond_2" != xno])
    AM_CONDITIONAL([COMBINED_CONDITION_OR],
                   [test "x$cond_1" != xno || test "x$cond_2" != xno])
    

    being recommended over

    AM_CONDITIONAL([COMBINED_CONDITION_AND],
                   [test "$cond_1" != "no" -a "$cond_2" != "no"])
    AM_CONDITIONAL([COMBINED_CONDITION_OR],
                   [test "$cond_1" != "no" -o "$cond_2" != "no"])
    

    (See https://www.gnu.org/savannah-checkouts/gnu/autoconf/manual/autoconf-2.71/html_node/Limitations-of-Builtins.html)

    Note also the letter at the beginning of "x$cond" in the test arguments. This avoids unwanted results if e.g. the value of $cond is -z or -f or something similar which test would interpret. You can use any letter you want here, but "x" is a popular choice.