In a GNU makefile, I would like to set an output variable to one value (let's say "true") if an input variable is equal to one of two values and to another value ("false") when it is not.
Thanks to this SO answer I've learned about the and
and the or
functions and soon after that I have found the if
function. These functions seem to be available in my version of make, so I would like to use them. I would like to write something like that:
TEST_INPUT = `hostname`
TEST_OUTPUT = $(if $(or $(eq $(TEST_INPUT),hal9000),
$(eq $(TEST_INPUT),pipboy)),true,false)
Unfortunately I can't, because I couldn't find any obvious form of the expected eq
function. I am able to achieve what I want using the filter
function:
TRUE_HOSTS = hal9000 pipboy
TEST_OUTPUT = $(if $(filter $(TEST_INPUT),$(TRUE_HOSTS)),true,false)
or the subst
function:
TEST_OUTPUT = $(if $(and $(subst hal9000,,$(TEST_INPUT)),
$(subst pipboy,,$(TEST_INPUT))),
false,true)
but for me it isn't a nice looking nor readable code. Are there solutions closer to the first example (the one using not existing eq
function)? Maybe I don't catch the purpose of the if
, and
and or
functions at all?
The multiple IF conditions in Excel are IF statements contained within another IF statement. They are used to test multiple conditions simultaneously and return distinct values. The additional IF statements can be included in the “value if true” and “value if false” arguments of a standard IF formula.
Step 1: Put the number you want to test in cell C6 (150). Step 2: Put the criteria in cells C8 and C9 (100 and 999). Step 3: Put the results if true or false in cells C11 and C12 (100 and 0). Step 4: Type the formula =IF(AND(C6>=C8,C6<=C9),C11,C12).
When you combine each one of them with an IF statement, they read like this: AND – =IF(AND(Something is True, Something else is True), Value if True, Value if False) OR – =IF(OR(Something is True, Something else is True), Value if True, Value if False) NOT – =IF(NOT(Something is True), Value if True, Value if False)
The thing that is odd about GNUmake conditionals is that there is no boolean type in make -- everything is a string. So the conditionals all work with the empty string for 'false' and all non-empty strings (including strings like false
and 0
) as being 'true'.
That being said, the fact that eq
is missing is an annoyonace albeit a minor one. Generally you can get what you want from filter
or findstring
, and filter
often allows you to search a whole list of strings to match as in your second example.
If you really need it, you can define your own eq function:
eq = $(and $(findstring $(1),$(2)),$(findstring $(2),$(1)))
Which you unfortunately have to use as $(call eq,
...,
...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With