在Make中对模式规则进行优先级排序 [英] Prioritizing pattern rules in Make

查看:120
本文介绍了在Make中对模式规则进行优先级排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我(大约)有这个Makefile:

I have (roughly) this 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:

This Makefile has a problem, since there are two different ways to build $(OUT)/index.html:

  1. 构建./index.html(规则1),然后将其复制到$(OUT)(规则2).
  2. ./index.in复制到$(OUT)(规则2),然后构建$(OUT)/index.html(规则1).
  1. build ./index.html (rule 1), then copy it to $(OUT) (rule 2).
  2. copy ./index.in to $(OUT) (rule 2), then build $(OUT)/index.html (rule 1).

我希望make总是 优先选择选项1.如何指示这两个模式规则之间存在优先顺序?

I want make to always prefer option 1. How can I indicate that there is a preferred order between these two pattern rules?

(我可以想到一些特殊的方法来完成此特定情况,但是我想要一个尽可能通用的解决方案-例如,将规则2的模式更改为$(OUT)/%.html: %.html可以解决问题,但失去了普遍性,因为如果以后我想以相同的方式处理其他类型的文件,则需要重复自己的操作.)

(I can think of a few hacky ways to accomplish it for this particular case, but I want a solution that is as general as possible---for instance, changing the pattern of rule 2 to $(OUT)/%.html: %.html will fix the problem, but loses generality since I need to repeat myself if I want to handle other kinds of files in the same way later.)

推荐答案

A quote :

一个以上的模式规则可能会满足这些条件.在这种情况下,make将选择词干最短的规则(即最匹配的模式).如果多个规则规则的词干最短,make将选择在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.

因此,您可以尝试创建规则,以确保较短的词干优先.或者,您可以使用静态模式规则限制复制内容的范围,例如:

So, you can try to create rules which ensure shorter stems to take priority. Alternatively, you could use static pattern rules to limit the scope of what gets copied where, as so:

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

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

,然后在其中使用所需的内容预填充$(expected_out).最后,您可以添加:

and then prepopulate $(expected_out) with what you want in there. Finally, you can add:

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

make文件中的某个位置,因为make倾向于使用最短路径"来构建对象,在这种情况下,这只是一种模式规则.

somewhere in your makefile, as make prefers the 'shortest path' to building an object, which would only be one pattern rule in this case.

这篇关于在Make中对模式规则进行优先级排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆