Makefile变量值在ifeq期间不可用 [英] Makefile variable value not available during ifeq

查看:93
本文介绍了Makefile变量值在ifeq期间不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下Makefile

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

无论我在./test.txt中输入什么值,我总是在ifeq& amp;中输入PASS.findstring条件,但变量的值正确显示在echo语句中.因此在评估ifeq时该值不可用

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

但是,对于TEST_STATIC变量,if-else行为正确.

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

任何帮助将不胜感激.谢谢.

Any help would be appreciated. Thanks.

推荐答案

ifeq .即使看起来像是食谱的一部分,也不会.您可以说不是,因为它没有使用TAB字符缩进.任何未由TAB缩进的内容,都是makefile的一部分,而不是配方的一部分,并且在读取makefile时(而不是在运行规则时)进行解析.

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.

因此,当您的规则开始运行并到达您的 eval 时,所有 ifeq 语句早已得到扩展和处理.

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.

通常,在食谱中使用 eval 实际上绝不是一个好主意.它几乎永远不会做您想要的.

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.

如果需要测试配方中的某些值,则必须编写shell代码来执行此操作,而不是makefile代码,并使用TAB缩进shell代码,以便将其传递给shell.

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.

这篇关于Makefile变量值在ifeq期间不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆