如何确保将Makefile变量设置为先决条件? [英] How to ensure Makefile variable is set as a prerequisite?
问题描述
Makefile deploy
配方需要将环境变量 ENV
设置为正确执行自身,而其他配方则无关紧要,例如
A Makefile deploy
recipe needs an environment variable ENV
to be set to properly execute itself, whereas other recipes don't care, e.g.,
ENV =
.PHONY: deploy hello
deploy:
rsync . $(ENV).example.com:/var/www/myapp/
hello:
echo "I don't care about ENV, just saying hello!"
如何确保设置此 ENV
变量?有没有办法将此Makefile变量声明为部署配方的前提条件?例如,
How can I make sure this ENV
variable is set? Is there a way to declare this makefile variable as a prerequisite of the deploy recipe? e.g.,
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屋!