【问题标题】:alternative to this rule calling rule on makefile替代此规则调用 makefile 上的规则
【发布时间】:2016-10-30 07:29:17
【问题描述】:

我有这个makefile,它确实可以完成这项工作,但我觉得它并不漂亮。我的目标很简单,当有人第一次调用“make”时,它会同时运行 compile 和 staging,但是连续调用 make,它只会调用 compile。如果用户想要运行 staging,用户必须调用“make staging”。谁有更好的主意?

all: compile

.PHONY: compile staging                                                         

compile:
    @echo "compile"
    @test -f ./.staging || make ./.staging

force_staging:
    @rm -f ./.staging

staging: force_staging compile
    @:

./.staging:
    @echo "staging"
    @touch $@

【问题讨论】:

    标签: makefile


    【解决方案1】:

    我认为您可以添加对 .staging 的依赖项,而不是测试文件是否存在

    all: compile
    
    .PHONY: compile
    
    compile: ./.staging
        @echo "compile"
    
    force_staging:
        @rm -f ./.staging
    
    staging: force_staging compile
        @:
    
    ./.staging:
        @echo "staging"
        @touch $@
    

    【讨论】:

    • 抱歉不清楚,但是staging之前必须先编译
    • 那么你的方法看起来最适合这项任务
    【解决方案2】:

    这听起来像是 order only 依赖项的工作:

    all: compile
    
    compile: | .staging
        echo $@
        touch $@
    
    .staging: 
        echo $@
        touch $@
    
    staging: .staging compile
    

    请注意,如果用户执行make staging,那么.staging 将是一个常规目标,并且如果过时将重建。如果用户做了make all,那么.staging只有在它不存在的时候才会被重建。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 2012-03-31
      相关资源
      最近更新 更多