makefile:如果变量为空,则在单个 make 目标上失败
makefile: fail on single make target if variable empty
我是构建 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
或
export VER=foo
make step1
# should result in printing the "Success:" line
或
make step1 VER=foo
# should result in printing the "Success:" line
或
make step1
# should result in printing "VER is not set"
但是,当我 运行 make step
使用上述任何一项时,我总是会收到 VER is not set
错误。
简而言之,我如何测试特定 make 目标中的变量并在未设置时返回错误消息? (但其他 make 目标不会关心是否设置了变量)
几件事:
首先, 您必须将 Make 命令和 shell 命令整齐地分开。这个:
ifeq ($(A),$(B))
...
endif
是Make语法。如果将 ifeq (...)
传递给 shell,您可能会遇到麻烦。 makefile 配方中的命令是 shell 命令,将传递给 shell。要在规则中间使用 Make ifeq
条件,请这样做:
step1:
some command
ifeq ($(A),$(B))
another command
endif
yet another command
请注意,ifeq
和 endif
之前没有制表符;这些不是要传递给 shell 的命令,它们供 Make 使用。
第二个,这个:
ifeq(...)
应该是这样的:
ifeq (...)
space 很重要(至少在我的 Make 版本中)。
三、这个:
ifeq ($(VER), "")
应该是这样的:
ifeq ($(VER),)
除非您确实希望该变量包含字符串“””。
(您可能已经发现了最后一个自己,单独使用 ifeq
;始终单独测试新工具。)
经过这些更改后,生成文件对我有用。如果它不适合你,请告诉我,我们会敲定它。
我是构建 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
或
export VER=foo
make step1
# should result in printing the "Success:" line
或
make step1 VER=foo
# should result in printing the "Success:" line
或
make step1
# should result in printing "VER is not set"
但是,当我 运行 make step
使用上述任何一项时,我总是会收到 VER is not set
错误。
简而言之,我如何测试特定 make 目标中的变量并在未设置时返回错误消息? (但其他 make 目标不会关心是否设置了变量)
几件事:
首先, 您必须将 Make 命令和 shell 命令整齐地分开。这个:
ifeq ($(A),$(B))
...
endif
是Make语法。如果将 ifeq (...)
传递给 shell,您可能会遇到麻烦。 makefile 配方中的命令是 shell 命令,将传递给 shell。要在规则中间使用 Make ifeq
条件,请这样做:
step1:
some command
ifeq ($(A),$(B))
another command
endif
yet another command
请注意,ifeq
和 endif
之前没有制表符;这些不是要传递给 shell 的命令,它们供 Make 使用。
第二个,这个:
ifeq(...)
应该是这样的:
ifeq (...)
space 很重要(至少在我的 Make 版本中)。
三、这个:
ifeq ($(VER), "")
应该是这样的:
ifeq ($(VER),)
除非您确实希望该变量包含字符串“””。
(您可能已经发现了最后一个自己,单独使用 ifeq
;始终单独测试新工具。)
经过这些更改后,生成文件对我有用。如果它不适合你,请告诉我,我们会敲定它。