Joe*_*e J 0 bash scripting makefile build 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"
Run Code Online (Sandbox Code Playgroud)
我希望能够运行以下测试用例:
VER="foo" make step1
# should result in printing the "Success:" line
Run Code Online (Sandbox Code Playgroud)
或者
export VER=foo
make step1
# should result in printing the "Success:" line
Run Code Online (Sandbox Code Playgroud)
或者
make step1 VER=foo
# should result in printing the "Success:" line
Run Code Online (Sandbox Code Playgroud)
或者
make step1
# should result in printing "VER is not set"
Run Code Online (Sandbox Code Playgroud)
但是,当我make step使用上述任何一种运行时,我总是收到VER is not set错误消息。
简而言之,如果未设置,如何测试特定 make 目标中的变量并以错误消息响应?(但其他 make 目标不会关心是否设置了变量)
几件事:
首先,您必须将 Make 命令和 shell 命令整齐地分开。这个:
ifeq ($(A),$(B))
...
endif
Run Code Online (Sandbox Code Playgroud)
是Make语法。如果你把它传递ifeq (...)给 shell ,你可能会遇到麻烦。makefile 配方中的命令是shell命令,要传递给 shell。要ifeq在规则中间使用 Make条件,请执行以下操作:
step1:
some command
ifeq ($(A),$(B))
another command
endif
yet another command
Run Code Online (Sandbox Code Playgroud)
请注意,ifeq和之前没有制表符endif;这些不是要传递给 shell 的命令,它们是供 Make 使用的。
二、这个:
ifeq(...)
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
ifeq (...)
Run Code Online (Sandbox Code Playgroud)
空间很重要(至少在我的 Make 版本中)。
三、这个:
ifeq ($(VER), "")
Run Code Online (Sandbox Code Playgroud)
应该是这样的:
ifeq ($(VER),)
Run Code Online (Sandbox Code Playgroud)
除非您确实希望该变量应包含字符串 '""'。
(你可以自己发现那些最后一个ifeq,孤立地玩;总是孤立地测试新工具。)
在这些更改之后,makefile 对我有用。如果它不适合你,请告诉我,我们会敲定它。