Makefile variable value not available during ifeq

痞子三分冷 提交于 2021-01-29 09:06:24

问题


I have the following Makefile

SHELL=/bin/bash
.SHELLFLAGS=-O extglob -o errexit -o pipefail -o nounset -c

.PHONY: testing

define getFileContents
$(shell cat ./test.txt)
endef

TEST_STATIC=dummy

deploy:
    $(eval TEST=$(getFileContents))
    @echo "$(TEST)"
ifeq ($(TEST),dummy)
    @echo "$(TEST) is FAILED"
else
    @echo "$(TEST) is PASS"
endif
ifneq (,$(findstring dummy,$(TEST)))
    @echo "$(TEST) is FAILED"
else
    @echo "$(TEST) is PASS"
endif

ifeq ($(TEST_STATIC),dummy)
    @echo "$(TEST) is FAILED"
else
    @echo "$(TEST) is PASS"
endif
ifneq (,$(findstring dummy,$(TEST_STATIC)))
    @echo "$(TEST) is FAILED"
else
    @echo "$(TEST) is PASS"
endif

No matter what value I put in ./test.txt, I always go into PASS in both the ifeq & the findstring conditions but the variable's values show up properly in the echo statements. So the value is not available during the evaluation of ifeq

However, the if-else behaves properly for the TEST_STATIC variable.

Any help would be appreciated. Thanks.


回答1:


ifeq is parsed while the makefile is read in. Even if it looks like it's part of a recipe, it isn't. You can tell that it isn't, because it isn't indented with a TAB character. Anything not indented by TAB, is part of the makefile not part of the recipe, and is parsed when the makefile is read in, not when the rule is run.

So by the time your rule is running and it gets to your eval, all the ifeq statements have long been expanded and dealt with.

In general, it's virtually never a good idea to use eval inside a recipe. It will almost never do what you're hoping for.

If you need to test some value inside a recipe then you have to write shell code to do it, not makefile code, and indent the shell code with a TAB so it's passed to the shell.



来源:https://stackoverflow.com/questions/62096217/makefile-variable-value-not-available-during-ifeq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!