【发布时间】:2014-11-04 07:06:08
【问题描述】:
考虑以下生成文件。我打算将其称为“make var=xxx”,用于正常构建,而在其他时候称为“make help”或“make clean”或“make showVars”。当我进行实际构建时,我需要确保在命令行中传递了“var”变量,但我不需要它存在于其他目标,例如 clean。目前,在未指定 var 的任何时候检查都会退出,这很安全,但在清理或乱搞时很烦人且不必要。如何仅对我的构建目标运行检查而不在其他时间运行检查?
var=
$(if $(var),,$(error var was not specified at commandline!))
var_RELEASE=`echo $(var) | sed -e 's/-/_/g'`
.PHONY all showVars clean prep rpm
all: prep rpm
prep:
# Do prep work. Requires valid var and var_RELEASE
rpm:
# Build rpm. Requires valid var_RELEASE
help:
# Does not require var and var_RELEASE
showVars:
# Display all vars. Requires valid var and var_RELEASE
clean:
# Does not require var and var_RELEASE
更新:
根据以下 Maxim 的建议,使用 $(MAKECMDGOALS) 检查指定了哪个目标并忽略任何不需要设置 var 的目标:
# Check if var is empty for prep and rpm targets, bail out if so.
ifneq ($(MAKECMDGOALS),clean)
ifneq ($(MAKECMDGOALS),help)
ifneq ($(MAKECMDGOALS),showvars)
$(if $(var),,$(error var was not specified at commandline! See 'make help'))
endif
endif
endif
它可以工作,但它有点粗糙......可以简化这个吗?
【问题讨论】:
-
您可以使用
filter:ifeq (,$(filter clean help showvars,$(MAKECMDGOALS))),然后检查var,如果没有设置则失败。
标签: variables makefile arguments target