Makefile变量作为先决条件 [英] Makefile variable as prerequisite
问题描述
在Makefile中,deploy
配方需要将环境变量ENV
设置为正确执行自身,而其他情况不在乎,例如:
In a Makefile, a deploy
recipe needs a environment variable ENV
to be set to properly execute itself, whereas others don't care, eg:
ENV =
.PHONY: deploy hello
deploy:
rsync . $(ENV).example.com:/var/www/myapp/
hello:
echo "I don't care about ENV, just saying hello!"
如何确保设置此变量,例如:有没有一种方法可以将该makefile变量声明为部署配方的先决条件,例如:
How can I make sure this variable is set, eg: is there a way to declare this makefile variable as a prerequisite of the deploy recipe, like:
deploy: make-sure-ENV-variable-is-set
?
谢谢.
推荐答案
如果未定义ENV
并且需要某些内容(无论如何,在GNUMake中),这将导致致命错误.
This will cause a fatal error if ENV
is undefined and something needs it (in GNUMake, anyway).
.PHONY: deploy check-env
deploy: check-env
...
other-thing-that-needs-env: check-env
...
check-env:
ifndef ENV
$(error ENV is undefined)
endif
(请注意,ifndef和endif不会缩进-它们控制哪些内容"sees" ,在运行Makefile之前生效."$(error)用制表符缩进,以便仅在规则的上下文中运行."
(Note that ifndef and endif are not indented - they control what make "sees", taking effect before the Makefile is run. "$(error" is indented with a tab so that it only runs in the context of the rule.)
这篇关于Makefile变量作为先决条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!