Sakht Londa
Sakht Londa

Reputation: 3

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.

Upvotes: 0

Views: 622

Answers (2)

ndim
ndim

Reputation: 37777

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.

Upvotes: 2

MadScientist
MadScientist

Reputation: 100836

If you know what the values of the CONDITION_1 and CONDITION_2 variables will be when they are what you want you can just check them both for equality, something like:

ifeq ($(CONDITION_1)/$(CONDITION_2),no/no)

or similar.

Upvotes: 0

Related Questions