Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex conditions check in Makefile

Tags:

makefile

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

ifdef VAR1 || VAR2 || VAR3 action endif 

however the documentation says the syntax like that is not supported. So the only simple workaround that came to my mind is to use the concatenation:

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

Are there any other more correct solutions?

For the following case:

ifdef VAR1 && VAR2 && VAR3 action endif 

one need to write

ifdef VAR1 ifdef VAR2 ifdef VAR3 action endif endif endif 

which is also ugly. Are there more elegant alternatives?

like image 402
dma_k Avatar asked Apr 07 '11 17:04

dma_k


Video Answer


1 Answers

If your make is GNU-make and all the defined variables include a non-space character,

ifdef VAR1 && VAR2 && VAR3 

can be written as

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

On a related note, probably and function requires version 3.81 or later.

In case some defined variables may 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:

ifdef VAR1 || VAR2 ifdef VAR1 && VAR2 

can be written respectively using call function:

ifneq ($(call ifdef_any_of,VAR1 VAR2),) ifeq ($(call ifndef_any_of,VAR1 VAR2),) 
like image 134
Ise Wisteria Avatar answered Oct 07 '22 19:10

Ise Wisteria