请考虑以下makefile。我打算在正常建筑中称其为“make var = xxx”,并在其他时间称其为“make help”或“make clean”或“make showVars”。当我进行实际构建时,我需要确保'var'变量在命令行中传递,但我不要求它为其他目标(如clean)提供它。目前,检查任何时候都没有指定var,这是安全的,但是在清理或者扒手时很麻烦并且不必要。我怎样才能运行检查我的生成目标而不是在其他时间?makefile中的条件检查
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
UPDATE:
每低于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($(过滤器的清洁帮助showvars,$(MAKECMDGOALS)))'然后检查'var',如果没有设置失败。 – MadScientist 2014-09-10 23:29:38