Patrik
Patrik

Reputation: 855

make and shell redirection based on exit status

I have a make rule which generates a dependencies file for a list of sources. This is the rule:

.depend: $(SRCS)
    $(CC) $(INCLUDE) -MM $^ | sed 's-^\(.*\):-$(OBJDIR)\/\1:-'> .depend

What I want is: If gcc fails for any file in $(SRC) then .depend should not be created. Currently, if gcc fails on a source file I will get an incomplete .depend which will not be regenerated after I fix the issue. What can I do? I'd like to avoid having a .depend file for each source file

Upvotes: 1

Views: 133

Answers (2)

reinierpost
reinierpost

Reputation: 8611

This should work:

.dependlist: $(SRCS)
    $(CC) $(INCLUDE) -MM $^ > $@

.depends: .dependlist
    sed 's-^\(.*\):-$(OBJDIR)\/\1:-' $< > $@

Upvotes: 2

Eldar Abusalimov
Eldar Abusalimov

Reputation: 25533

Use .DELETE_ON_ERROR target:

.DELETE_ON_ERROR:
.depend: $(SRCS)
    $(CC) $(INCLUDE) -MM $^ | sed 's-^\(.*\):-$(OBJDIR)\/\1:-'> .depend

From here:

If .DELETE_ON_ERROR is mentioned as a target anywhere in the makefile, then make will delete the target of a rule if it has changed and its recipe exits with a nonzero exit status, just as it does when it receives a signal.

Upvotes: 0

Related Questions