在 Shake 中,规则的定义顺序重要吗?

In Shake, does the definition order of Rules matter?

如果我有针对 *.o 目标文件的一般构建规则,但有针对 foo.o 目标文件的更具体的构建规则,定义顺序是否重要?

%> 运算符文档中所述:

Patterns with no wildcards have higher priority than those with wildcards, and no file required by the system may be matched by more than one pattern at the same priority (see priority and alternatives to modify this behaviour).

所以定义顺序无关紧要,但文件不能以相同的优先级匹配多个规则。

所以*.ofoo.o的情况下就可以了。这是一个示例(使用 foo.txt*.txt):

import Development.Shake

main = shakeArgs shakeOptions $ do
    want ["foo.txt", "bar.txt"]
    "foo.txt" %> \out -> writeFile' out "foo"
    "*.txt"   %> \out -> writeFile' out "anything"

import Development.Shake

main = shakeArgs shakeOptions $ do
    want ["foo.txt", "bar.txt"]
    "*.txt"   %> \out -> writeFile' out "anything"
    "foo.txt" %> \out -> writeFile' out "foo"

在这两种情况下,foo.txt 将包含“foo”,而 bar.txt 将包含“anything”,因为“foo.txt”的定义不包含任何通配符。


或者,如果您想要使用定义顺序,您可以使用alternatives函数,它使用“先赢”匹配语义:

alternatives $ do
    "hello.*" %> \out -> writeFile' out "hello.*"
    "*.txt" %> \out -> writeFile' out "*.txt"

hello.txt 将匹配第一条规则,因为它已在之前定义。

最后,可以直接用priority函数给规则分配优先级:

priority 4 $ "hello.*" %> \out -> writeFile' out "hello.*"
priority 8 $ "*.txt" %> \out -> writeFile' out "*.txt"

hello.txt 将匹配第二条规则,因为它具有更高的优先级。