0

I have the following makefile:

SHELL += -eu
GREEN=$(shell echo -e "\033[0;32m")
RED=$(shell echo -e "\033[0;31m")

AWS=$(shell command aws --version 2> /dev/null)
all: prereqs
    @echo "${GREEN} ✓ Testing our local environment for dependencies."

prereqs:
    ifndef "${AWS}"
        $(error "${RED} AWS is not available please install aws-cli")
    endif
        "${AWS}"

How do I change it so that if there is dependancy missing to show a more appropriate error and continue with the check?

I tried:

prereqs:
    aws --version || $(error "${RED} AWS is not available please install aws-cli")
    @echo

But it does not seem to be working, what am I missing as aws exists!

Any advice is much appreciated.

3
  • This isn't bash, it's sh. Make doesn't use bash unless it's told to. Correct your tagging. Commented Nov 26, 2016 at 19:37
  • (that said, it's sh with some non-POSIX-compliant extensions -- echo -e isn't allowed to do anything other than print -e on output by the letter of the standard; see in particularly the APPLICATION USAGE section showing how to use printf '%b\n' instead). Commented Nov 26, 2016 at 19:38
  • ...for a case in point (assuming that /bin/sh is provided by bash running in POSIX mode on your system), see how your makefile behaves if run after export BASHOPTS=xpg_echo Commented Nov 26, 2016 at 19:43

1 Answer 1

1

You're missing a couple of points of knowledge about how make works:-

Write this instead:

SHELL += -eu
GREEN=$(shell echo -e "\033[0;32m")
RED=$(shell echo -e "\033[0;31m")

AWS:=$(shell command aws --version 2> /dev/null)
all: prereqs
    @echo "${GREEN} ✓ Testing our local environment for dependencies."

prereqs:
ifndef AWS
$(error "${RED} AWS is not available please install aws-cli")
endif
    @echo "${AWS}"

and note the differences.

I also think an improvement would be:

SHELL += -eu
GREEN=$(shell echo -e "\033[0;32m")
RED=$(shell echo -e "\033[0;31m")

AWS:=$(shell command aws --version 2> /dev/null)
$(info ${GREEN} ✓ Testing our local environment for dependencies.)
all: prereqs

prereqs:
ifndef AWS
$(error "${RED} AWS is not available please install aws-cli")
endif
    @echo "${AWS}"

as well not leaving the user in a coloured shell.

Sign up to request clarification or add additional context in comments.

2 Comments

grumbles a bit about keeping the OP's non-POSIX-compliant echo -e usage, vs converting to printf '%b' "\033[0;32m" or such
Also, leaving the ifndef in the context of the prereqs target is misleading: people might think it's only run when the prereqs target is run which is not true, it's run when the makefile is parsed. I recommend putting it up just after the info and before the all target so it's more clear what actually happens.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.