Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can GNU make execute a rule whenever an error occurs?

This is slightly different from Can a Makefile execute code ONLY when an error has occurred?.

I'd like a rule or special target that is made whenever an error occurs (independent of the given target; without changing the rule for every target as the other answer seems to imply with the || operator).

In dmake there is special target .ERROR that is executed whenever an error condition is detected. Is there a similar thing with GNU make?

(I'm still using GNU make 3.81, but I didn't find anything in the documentation of the new GNU make 4.0 either)

like image 725
pesche Avatar asked Jan 14 '14 16:01

pesche


People also ask

How do I ignore errors in makefile?

To ignore errors in a recipe line, write a ' - ' at the beginning of the line's text (after the initial tab). The ' - ' is discarded before the line is passed to the shell for execution.

How does GNU Make work?

GNU Make is a program that automates the running of shell commands and helps with repetitive tasks. It is typically used to transform files into some other form, e.g. compiling source code files into programs or libraries. It does this by tracking prerequisites and executing a hierarchy of commands to produce targets.

What are makefile rules?

A rule appears in the makefile and says when and how to remake certain files, called the rule's targets (most often only one per rule). It lists the other files that are the prerequisites of the target, and the recipe to use to create or update the target.

What does ?= Mean in makefile?

?= indicates to set the KDIR variable only if it's not set/doesn't have a value. For example: KDIR ?= "foo" KDIR ?= "bar" test: echo $(KDIR) Would print "foo"


1 Answers

Gnu doesn't support it explicitly, but there's ways to hack almost anything. Make returns 1 if any of the makes fail. This means that you could, on the command line, rerun make with your error rule if the first make failed:

make || make error_targ

Of course, I'll assume you just want to put the added complexity within the makefile itself. If this is the case, you can create a recursive make file:

all: 
     $(MAKE) normal_targ || $(MAKE) error_targ

normal_targ:
      ... normal make rules ...

error_targ:
      ... other make rules ...

This will cause the makefile to try to build normal_targ, and iff it fails, it will run error_targ. It makes it a bit harder to read the makefile for the inexperienced, but it puts all the logic in one place.

like image 70
blackghost Avatar answered Oct 22 '22 14:10

blackghost