3

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?

Zoltán
  • 21,321
  • 14
  • 93
  • 134

3 Answers3

5

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.
e.dan
  • 7,275
  • 1
  • 26
  • 29
  • If you have a new-enough GNU make you could use this instead of `ifndef`: `$(or $(PASSWORD), $(error PASSWORD not set))` which is slightly nicer to read (IMO). – MadScientist Jun 12 '18 at 14:07
2

In case you are intending to use a call function, and want to abort at one central place (so that future users of GET_SECRET do not forget to check e.g. .SHELLSTATUS), I found this hack practical:

GET_SECRET = $(shell vault read -field=$(1) $(2) || { echo >&2 "Error reading field $(1) from vault path $(2), aborting"; kill $$PPID; })

The parent make process is killed of shell error. See also 225542/how-to-make-a-failing-shell-command-interrupt-make and 50958731/assign-shell-output-to-variable-or-exit-terminate-makefile

jmuc
  • 1,551
  • 1
  • 14
  • 16
0

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.

uzsolt
  • 5,832
  • 2
  • 20
  • 32