sorting - Make 中模式规则的优先级

标签 sorting makefile

我(大致)有这个Makefile:

.PHONY: all
.SUFFIXES:

OUT = /www/web

all: $(OUT)/index.html

# rule 1
%.html: %.in
    build_html $< $@

# rule 2
$(OUT)/%: %
    cp $< $@

这个 Makefile 有问题,因为有两种不同的方式来构建 $(OUT)/index.html:

  1. 构建 ./index.html(规则 1),然后将其复制到 $(OUT)(规则 2)。
  2. ./index.in 复制到 $(OUT)(规则 2),然后构建 $(OUT)/index.html (规则 1)。

我希望 make 始终更喜欢选项 1。我如何表明这两个模式规则之间存在首选顺序?

(对于这个特殊情况,我可以想出一些古怪的方法来完成它,但我想要一个尽可能通用的解决方案——例如,将规则 2 的模式更改为 $(OUT )/%.html: %.html 将解决问题,但失去了通用性,因为如果我想稍后以相同的方式处理其他类型的文件,我需要重复自己。)

最佳答案

一个quote来自 GNU Makefile 手册:

It is possible that more than one pattern rule will meet these criteria. In that case, make will choose the rule with the shortest stem (that is, the pattern that matches most specifically). If more than one pattern rule has the shortest stem, make will choose the first one found in the makefile.

因此,您可以尝试创建规则来确保较短的词干优先。或者,您可以使用 static pattern rules限制复制内容的范围,如下所示:

%.html: %.in
      build_html $@ $<

$(expected_out) : (OBJS)/% : %
      cp $@ $<

然后用您想要的内容预填充$(expected_out)。最后,您可以添加:

$(OUT)/index.html : index.html

在 makefile 中的某个位置,因为 make 更喜欢使用“最短路径”来构建对象,在这种情况下这只是一种模式规则。

关于sorting - Make 中模式规则的优先级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43724971/

相关文章:

vb.net - VB - 如何将数字列表按降序排序

string - 流字符串值的近似直方图(卡片目录算法?)

mysql - 重新排序MySQL中表的显示顺序

makefile - makefile 中的百分号有什么作用?

c++ - 仅编译1个cpp文件时出现问题: “make: *** No rule to target '所需的 'executable' build/main.o。停止。”

c++ - 不处于 Debug模式时正常启动测试

c++ - Makefile : Undefined symbols for architecture x86_64

sorting - 只显示头部最大的数字

sorting - 如何最好地使用 Redis 创建排序集

makefile - 如何使用 cmake 链接 jemalloc 共享库