bmargulies
bmargulies

Reputation: 100151

How can I propagate exit status through a shell 'if' command in a Makefile?

%/all:
    if [ -f $(@D)/src/Makefile ]; then \
        $(MAKE) -C $(@D); \
    fi

If the inner make fails, the outer make continues, presumably because the implicit exit status of the 'if' command is 0. Is there a way around this?

Upvotes: 0

Views: 144

Answers (2)

MadScientist
MadScientist

Reputation: 101051

This cannot be a real example. The shell will exit with the result of the last command executed, which if the if-statement succeeds will be the exit code of make, which is what you want. So obviously in your real code, you must be doing some other command between the make and the end. You can keep a copy of the result in a variable and use that as the exit:

if [ -f $(@D)/src/Makefile ]; then \
    $(MAKE) -C $(@D); \
    r=$$?; \
      ...do other stuff...; \
    exit $$r; \
fi

Upvotes: 1

downhillFromHere
downhillFromHere

Reputation: 1977

Somehow I couldn't reproduce your problem but I suppose the following should work for you :

%/all:
if [ -f $(@D)/src/Makefile ]; then \
    $(MAKE) -C $(@D) || (echo "make failure: $$?"; exit 1) \
fi

Upvotes: 1

Related Questions