问题
I have a makefile snippet:
all: $(objects)
fresh: all | clean directory
directory: ;mkdir -p OutputDirectory
clean: ;rm $(objects); rm -rf OutputDirectory
Here, I want to ensure that when I do make fresh - clean should succeed by directory which should be followed by all.
Semantically, here it might not make sense for clean to be order only prerequisite. Assume it to some order only dependency that has to be executed in some order.
The following link shows similar problem but for normal dependencies: makefile - Impose an order for the prerequisites of a target - Stack Overflow
回答1:
In fresh's recipe, you could call make twice recursively on the same makefile, for the target that creates the directory and the all target, respectively:
# At the very beginning of the makefile
CURRENT_MAKEFILE := $(lastword $(MAKEFILE_LIST))
# ...
.PHONY: all clean fresh
directory := OutputDirectory
all: $(objects)
fresh: clean
$(MAKE) -f $(CURRENT_MAKEFILE) $(directory)
$(MAKE) -f $(CURRENT_MAKEFILE) all
$(directory): ;mkdir -p $@
clean: ;rm -f $(objects); rm -rf $(directory)
This way, the target all is preceded by the target $(directory), which is in turn preceded by clean.
来源:https://stackoverflow.com/questions/52053988/impose-an-order-for-order-only-prerequisites-of-a-target