开发者

"Negating" a variable in a makefile

I'm trying to use the if function (as opposed to one of the conditional statements) in GNU make in order to add a word to a list if a specific variable isn't defined. The way I've ended up doing this is to leave the second argument blank while providing the third argument, like so:

FOO := baz $(if $(BAR),,quux)

This sees to work OK, but strike开发者_运维问答s me as a little silly. Is there any sort of simple, built-in way to do what I want? I could define my own not function, but

FOO := baz $(if $(call not,$BAR),quux) 

isn't much of an improvement in my opinion.


I would go this way:

ifndef BAR
FOO +=quux
endif

or if empty string has meaning "not defined", then:

ifeq ($(BAR),)
FOO +=quux
endif


According to the docs:

The first argument, condition, first has all preceding and trailing whitespace stripped, then is expanded. If it expands to any non-empty string, then the condition is considered to be true. If it expands to an empty string, the condition is considered to be false.

So the question is how would you make a defined variable give an empty string and a non-defined variable give a non-empty string.

You could do this for example using the filter function combined with the origin function ($(if $(filter undefined,$(origin BAR)),quux)), but that's even uglier than the two examples you provide ^_^

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜