1

If I have a general build rule for *.o object files but a more specific build rule for a foo.o object file, does the definition order matter?

Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55

1 Answers1

1

As described in the documentation for %> operator:

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).

So definition order doesn't matter, but files can't match multiple rules at the same priority.

Therefore, in the case of *.o and foo.o, it'll be fine. Here's an example (using foo.txt and *.txt):

import Development.Shake

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

vs

import Development.Shake

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

In both cases foo.txt will contain "foo" and bar.txt will contain "anything" because the definition for "foo.txt" doesn't contain any wildcards.


Alternatively, if you want to use definition order, you can use the alternatives function which uses "first-wins" matching semantics:

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

hello.txt will match the first rule, because it's defined before.

Finally, you can directly assign the priority of a rule with the priority function:

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

hello.txt will match the second rule, because it has a higher priority.

Community
  • 1
  • 1
Dale Wijnand
  • 6,054
  • 5
  • 28
  • 55
  • Great answer. One thing worth noting though is that while order doesn't usually matter, there is a function to make it matter, using the `alternatives` function. It's unclear anyone ever uses it though. – Neil Mitchell Aug 11 '19 at 08:17
  • 1
    Thanks, Neil. You're right, it definitely worth mentioning `alternatives` so I've expanded the answer to include it, with an example. – Dale Wijnand Aug 11 '19 at 12:43