I guess this开发者_开发问答 makes sense somehow, but I can't grasp why: In the following code, I get both warnings (note that the original code was indented with tabs):
define variable-definition
ifndef $1
$(warning $1 is undefined)
else
$(warning $1 is defined)
endif
endef
PS: I want to check whether the variable with the name passed as $1
exists, not whether $1
was passed.
PPS: Dedenting the entire thing doesn't help.
Beta's analysis of the root cause is correct, you're not escaping your $
on the $(warning)
calls. Here's how I'd fix it:
define variable-def
ifndef $1
$$(warning $1 is undefined)
else
$$(warning $1 is defined)
endif
endef
FOO=bar
$(eval $(call variable-def,FOO))
Note that I am indenting with spaces, not tabs. If you indent with tabs, you get this error: *** commands commence before first target. Stop.
This uses GNUisms, but so does your sample (I think).
The reason it's giving you both warnings is that when you call this function (i.e. expand this variable), Make expands the variables within it, including both warnings. This happens before if tries to evaluate the ifndef
(and probably fails). Make simply doesn't handle conditionals the way you want.
Here's a way to do it, slightly clunky, but effective. (To do it really smoothly as a function would probably require a considerable amount of black magic). Write a separate makefile called variable-definition
:
# In makefile variable-definition
ifndef $(FOO)
$(warning $(FOO) is undefined)
else
$(warning $(FOO) is defined)
endif
Then in the main makefile:
# In the main makefile
bar = something
FOO := bar
include variable-definition
FOO := baz
include variable-definition
精彩评论