Search code examples
bashmakefilegnu-makegnu

Makefile Throws Error Even When In If Block


I have a validation function that I want to throw error when the validation fails. This is the code I use:

define validate
    if [ condition... ]; then \
        echo "ERROR"; \
        $(error Validation failed); \
    fi; \
    echo "NOERROR";
endef

The issue I'm having is that even when the condition is false the error still gets thrown. When I remove the error and run the function I can see the echo 'ERROR' is not showing.

Why is this happening?


Solution

  • That's because $(error) is a make function and is evaluated before passing all this statement to shell. If you want to fail from shell, just use exit 1 instead.