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 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
falseand0) as being ‘true’.That being said, the fact that
eqis missing is an annoyonace albeit a minor one. Generally you can get what you want fromfilterorfindstring, andfilteroften 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:
Which you unfortunately have to use as
$(call eq,…,…)