Impose an order for Order-only-prerequisites of a target

£可爱£侵袭症+ 提交于 2020-01-05 05:29:04

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!