makefile:如果变量为空,则在单个make目标上失败

时间:2018-08-16 00:25:51

标签: bash makefile build scripting gnu-make

我不熟悉构建Makefile,并且试图确定如果变量为空,构建目标将如何失败。我希望能够将变量作为环境变量或make参数传递。

说我有一个这样的makefile:

VER ?=

step0:
    echo "step0 should work"

step1: 
    echo "step1 should enforce variable"
    ifeq($(VER), "")
    $(error VER is not set)
    endif 
    echo "Success: Value of Ver ${VER}"

step2:
    echo "step2 should work"

我希望能够运行以下测试用例:

VER="foo" make step1  
# should result in printing the "Success:" line

OR

export VER=foo
make step1  
# should result in printing the "Success:" line

OR

make step1 VER=foo  
# should result in printing the "Success:" line

OR

make step1  
# should result in printing "VER is not set"

但是,当我使用上述任何一种方法运行make step时,总是会收到VER is not set错误。

简单地说,如何在特定的make目标中测试变量,并在未设置错误消息的情况下进行响应? (但其他make目标将不在乎是否设置了变量)

1 个答案:

答案 0 :(得分:3)

几件事:

首先,必须使Make命令和shell命令整齐分开。这个:

ifeq ($(A),$(B))
...
endif

Make 语法。如果将ifeq (...)传递给shell,可能会遇到麻烦。 Makefile配方中的命令是 shell 命令,将传递给Shell。要在规则中间使用使ifeq为条件,请按以下步骤进行操作:

step1:
    some command
ifeq ($(A),$(B))
    another command
endif
    yet another command

请注意,ifeqendif之前没有TAB;那些不是要传递给外壳的命令,它们是供Make使用的。

第二,此:

ifeq(...)

应该是这样:

ifeq (...)

空间很重要(至少在我的Make版本中)。

第三

ifeq ($(VER), "")

应该是这样:

ifeq ($(VER),)

除非您实际上打算让变量包含字符串“””。

(您可能已经发现了最后一批自己的东西,孤立地ifeq玩;总是孤立地测试新工具。)

这些更改之后,makefile对我有用。如果对您不起作用,请告诉我,我们会尽快解决。

相关问题