How to make GNU Make fail if a shell command assigned to a variable failed?

社会主义新天地 提交于 2019-12-08 11:48:45

问题


I have a Make variable:

PASSWORD:=$(shell vault read -field=password test/password)

If vault is not installed, make will print make: vault: Command not found, but continue executing the recipe. How do I make it fail and stop execution if the expression fails?


回答1:


Here is one approach:

$ cat err.mk
PASSWORD:=$(shell vault read -field=password test/password)
ifndef PASSWORD
$(error PASSWORD not set (maybe vault failed?))
endif
$ make -f err.mk
make: vault: Command not found
err.mk:3: *** PASSWORD not set (maybe vault failed?).  Stop.



回答2:


Maybe this idea should work:

X!=lsx /

all:
ifeq (${.SHELLSTATUS},0)
    @echo OK
else
    @exit 1
endif

For example you can create a check: PHONY-target which is needed by every (other) target.

Explanation see here:

After the shell function or ‘!=’ assignment operator is used, its exit status is placed in the .SHELLSTATUS variable.

The X=$(shell ls /) doesn't work but IMHO should.



来源:https://stackoverflow.com/questions/50817458/how-to-make-gnu-make-fail-if-a-shell-command-assigned-to-a-variable-failed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!