【问题标题】:Impose an order for the prerequisites of a target对目标的先决条件下达命令
【发布时间】:2019-02-01 19:52:11
【问题描述】:

我有一个 makefile sn-p:

all: $(objects)
fresh: clean all
clean: ;rm $(objects)

在这里,我想确保make fresh - clean 应该在all 之前。

但是我怎么能确保这一点,因为当我做make allclean 时不应该做?


我可以想象一种方式可能是这样的

fresh: clean
    make all

这是解决此问题的正确(或唯一)方法吗?

【问题讨论】:

    标签: makefile dependencies gnu-make target prerequisites


    【解决方案1】:

    正如您在问题中已经建议的那样,在先决条件为 clean 的配方中,在相同的 makefile 上为目标 all 调用 make 递归

    # At the very beginning of the makefile
    CURRENT_MAKEFILE :=  $(lastword $(MAKEFILE_LIST))
    # ...
    
    .PHONY: fresh
    fresh: clean
        $(MAKE) -f $(CURRENT_MAKEFILE) all
    

    这强加了一个命令,因为目标fresh 依赖于先决条件cleanclean 的配方将在fresh 的配方之前执行,而fresh 的配方将依次执行all 的配方.

    请注意,我在这里使用$(MAKE) instead of make 进行递归。

    【讨论】:

    【解决方案2】:

    如果你使用 GNU make:

    all:
        @echo $@
        @sleep 1
        @echo end $@
    
    clean:
        @echo $@
        @sleep 1
        @echo end $@
    
    fresh:: clean
    fresh:: all
    
    .PHONY: clean fresh all
    

    请注意目标后面的双冒号s fresh!见documentation

    目标的双冒号规则按照它们的顺序执行 出现在makefile中。

    如果您运行 make -j2 fresh,则表明它按预期工作:

    clean
    end clean
    all
    end all
    

    但是fresh:: clean all 不能正常并行工作(可能出乎意料)。

    使用 BSD 制作:

     all:
        @echo $@
        @sleep 1
        @echo end $@
    
    clean:
        @echo $@
        @sleep 1
        @echo end $@
    
    fresh:  clean all
        @echo $@
    
    .ORDER: clean all
    .PHONY: clean all fresh
    

    注意以.ORDER 开头的行。它也适用于并行化(参见man make)。如果没有并行化,fresh: 行中的依赖顺序会很重要。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    相关资源
    最近更新 更多