Checking complex conditions in a Makefile

In my Makefile, I would like to test the following complex condition:

ifdef VAR1 || VAR2 || VAR3 action endif 

however, the documentation says that this syntax is not supported. So the only thing that occurred to me was to use concatenation:

 ifneq ($(VAR1)$(VAR2)$(VAR3),) action endif 

Are there any other better solutions?

In the following case:

 ifdef VAR1 && VAR2 && VAR3 action endif 

need to write

 ifdef VAR1 ifdef VAR2 ifdef VAR3 action endif endif endif 

which is also ugly. Are there any more elegant alternatives?

+22
makefile
Apr 7 2018-11-17T00:
source share
1 answer

If your make is GNU-make and all defined variables contain a non-whitespace character,

 ifdef VAR1 && VAR2 && VAR3 

can be written as

 ifneq ($(and $(VAR1),$(VAR2),$(VAR3)),) 

On a related note, it is likely that the function requires version 3.81 or later.

In case some certain variables can be empty strings, if we prepare the following functions:

 ifndef_any_of = $(filter undefined,$(foreach v,$(1),$(origin $(v)))) ifdef_any_of = $(filter-out undefined,$(foreach v,$(1),$(origin $(v)))) 

then the following conditions are satisfied:

 ifdef VAR1 || VAR2 ifdef VAR1 && VAR2 

can be written accordingly using the call function:

 ifneq ($(call ifdef_any_of,VAR1 VAR2),) ifeq ($(call ifndef_any_of,VAR1 VAR2),) 
+29
Apr 07 '11 at 19:52
source share



All Articles