Sanpi
Sanpi

Reputation: 420

How to abort a makefile when a command failed?

I would like to check if a third-party library is available before launching the compilation. It easy to do with pkg-config but I would like a better error message than:

pkg-config gtk+-3.0
make: *** [Makefile:17: gtk+-3.0] Error 1

After reading some answers here, I found a satisfying code to do that:

gtk+-3.0:
    $(shell pkg-config $@)
    ifneq ($(.SHELLSTATUS),0)
        $(error $@ is not installed)
    endif

But the error is always triggered.

If I replace $(error) by echo I have a syntax error:

ifneq (0,0)
/bin/sh: -c: line 0: syntax error near unexpected token `0,0'
/bin/sh: -c: line 0: `ifneq (0,0)'
make: *** [Makefile:3: gtk+-3.0] Error 1

GNU Make 4.2.1 on archlinux.

Upvotes: 0

Views: 1005

Answers (1)

Jens
Jens

Reputation: 72657

Make already stops when a command fails. Just write

gtk+-3.0:
        pkg-config $@

Or for more control of the message,

gtk+-3.0:
        @if pkg-config $@; then \
           printf '%s\n' "All good!"; \
        else \
           printf '%s\n' "Not installed." >&2; \
           exit 1; \
        fi

Note that make always uses a Bourne shell (probably /bin/sh) so there is no need for $(shell) for simple commands. Using make-conditionals is also not done as you seem to expect. The GNU make manual has all the details.

Upvotes: 3

Related Questions