小编典典

如何确保将 Makefile 变量设置为先决条件?

javascript

Makefiledeploy配方需要设置一个环境变量ENV以正确执行自身,而其他配方不关心,例如,

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 变量声明为部署配方的先决条件?例如,

deploy: make-sure-ENV-variable-is-set

阅读 127

收藏
2022-07-16

共1个答案

小编典典

ENV如果未定义并且需要它(无论如何在 GNUMake 中),这将导致致命错误。

.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 没有缩进 -它们控制 make “看到”的内容,在 Makefile 运行之前生效。 “$(error” 用制表符缩进,因此它只在规则的上下文中运行。)

2022-07-16