复杂条件在Makefile中检查

复杂条件在Makefile中检查

本文介绍了复杂条件在Makefile中检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Makefile中,我想检查以下复杂情况:

ifdef VAR1 || VAR2 || VAR3
action
endif

但是文档中说不支持类似的语法.因此,我想到的唯一简单的解决方法是使用串联:

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

还有其他更正确的解决方案吗?

在以下情况下:

ifdef VAR1 && VAR2 && VAR3
action
endif

需要写

ifdef VAR1
ifdef VAR2
ifdef VAR3
action
endif
endif
endif

这也很难看.还有更优雅的选择吗?

解决方案

如果您的make是GNU-make,并且所有已定义的变量都包含非空格字符,

ifdef VAR1 && VAR2 && VAR3

可以写为

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

在相关说明中,可能函数需要版本3.81或更高版本.

如果某些已定义的变量可能是空字符串,如果我们准备以下功能:

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

然后满足以下条件:

ifdef VAR1 || VAR2
ifdef VAR1 && VAR2

可以分别使用调用

来编写

a>函数:

ifneq ($(call ifdef_any_of,VAR1 VAR2),)
ifeq ($(call ifndef_any_of,VAR1 VAR2),)

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?

解决方案

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

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),)

这篇关于复杂条件在Makefile中检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 16:41