GNU make: run a target after all others, regardles

2019-07-13 12:26发布

问题:

I have a makefile with some targets (say data1 through dataN, on which alldata depends) that generate some data, and a prettify target which iterates over the output and creates a pretty report. (note: there are lots of dataN targets and the makefile is machine-generated)

Some of the dataX targets occasionally fail, but I would like to run prettify anyway, so prettify doesn't depend on alldata.

Is there a way to run the equivalent of make -k alldata || make prettify in a single invocation of make such that make does a best-effort at building all the data, and then builds my report on whatever got made?

回答1:

You can prepend the recipes for the dataX targets with a ‘-’,
or you can list the dataX targets as prerequisites of the special target .IGNORE.



回答2:

Make normally bails out when a command fails. If you put "|| true" behind the command that fails make will continue to execute, which means your prettify will also be executed.



回答3:

You can write a recursive target with any control logic you like. This doesn't prevent someone from running a target from the command line, so you cannot enforce your logic, but it's nice for a convenience target. Something like this, maybe:

.PHONY: all
all:
        $(MAKE) -k -$(MAKEFLAGS) alldata \
        ; rc=$$? \
        ; $(MAKE) $(MAKEFLAGS) prettify \
        ; exit $$rc