Re: Match anything pattern rules and pattern rules- help with understanding

David Deutsch <[email protected]>
Newsgroups gmane.comp.gnu.make.general
Message-ID <[email protected]>
Hi there,

I hope I'm not breaking any etiquette rules with this, but I was 
interested in seeing how GPT4 would respond to this request, so here goes:

https://chat.openai.com/share/5eb73ccc-27f6-4d79-934b-add344ab41f6

Content:

Sure, let's try to clarify the aspects of pattern rules and 
match-anything rules you mentioned.
Pattern Rules

Pattern rules in GNU make are a way to specify a recipe that can be used 
for multiple targets. The character % is used to define a pattern. For 
example, in a rule like %.o : %.c, the % represents a stem that must be 
the same in both the target and prerequisite.

The bit about "directory names in the file names are removed" is a bit 
misleading. What it really means is that GNU make will ignore directory 
prefixes when matching the pattern. For example, given a pattern rule 
like %.o : %.c, and a file named src/myfile.c, the rule can be used to 
build src/myfile.o even though the src/ part isn't explicitly mentioned 
in the rule. The stem, in this case, is src/myfile.

Now, when it says "After the comparison of the file name to the target 
pattern, the directory names, along with the slash that ends them, are 
added on to the prerequisite file names generated from the pattern 
rule’s prerequisite patterns and the file name.", it refers to how 
prerequisites are generated from the pattern rule. Using the stem 
(src/myfile), the %.c prerequisite in the rule %.o : %.c becomes 
src/myfile.c.

Here's an example:

%.o : %.c
     $(CC) -c $< -o $@

When you run make src/myfile.o, make recognizes src/myfile.o matches 
%.o, with src/myfile as the stem. It then substitutes the stem into %.c 
to generate the prerequisite, src/myfile.c. The rule becomes:

src/myfile.o : src/myfile.c
     $(CC) -c src/myfile.c -o src/myfile.o

Match-Anything Pattern Rules

Match-anything rules are pattern rules that match any target. A simple 
example is %: %. These are generally fallback rules and can potentially 
cause unwanted matches if you're not careful.

The primary application of match-anything rules is for specifying a 
recipe that will be used when no explicit or implicit rule can be found 
for a target.

However, use match-anything rules cautiously. Because they match any 
target, they can interfere with other rules or built-in rules if they're 
not more specific. They can also cause issues with incremental builds, 
as make may not be able to tell when the prerequisites of a 
match-anything rule have changed, leading to targets not being rebuilt 
when they should be.

Here's an example of a match-anything rule:

%:
     echo "Building $@"

If you run make myfile, and there's no rule for building myfile, make 
would use the above match-anything rule. The output will be "Building 
myfile".

Remember that the match-anything rules are only considered if no other 
pattern rule or explicit rule applies. If there was a rule for building 
myfile, that rule would be used instead, regardless of the order in the 
makefile.

I hope this explanation helps! Let me know if you need further 
clarifications.


On 29.05.23 00:52, Bartłomiej Wójcik wrote:
> Hello,
> As a developer, I am trying to get the idea of GNU make as deep as
> possible. I am stuck for a while with the topic of pattern rules and match
> anything pattern rules, the part of documentation that is related with that
> is not to clear for me.
>
> About match anything pattern rules:
> - what is the application of that rules?
> - what to avoid while using it? I would also appreciate any kind of
> informations that I have to know about that topic, and maybe any kind of up
> to date examples.
>
> About pattern rules:
> Documentation says "When the target pattern does not contain a slash (and
> it usually does not), directory names in the file names are removed from
> the file name before it is compared with the target prefix and suffix.
> After the comparison of the file name to the target pattern, the directory
> names, along with the slash that ends them, are added on to the
> prerequisite file names generated from the pattern rule’s prerequisite
> patterns and the file name. The directories are ignored only for the
> purpose of finding an implicit rule to use, not in the application of that
> rule. Thus, ‘e%t’ matches the file name src/eat, with ‘src/a’ as the stem.
> When prerequisites are turned into file names, the directories from the
> stem are added at the front, while the rest of the stem is substituted for
> the ‘%’. The stem ‘src/a’ with a prerequisite pattern ‘c%r’ gives the file
> name src/car."
>
> What does mean, that "directory names in the file names are removed"? Maybe
> it is about the definition of file name that the file name is in fact built
> also with the path? I am not able also to catch that part:
> "After the comparison of the file name to the target pattern, the directory
> names, along with the slash that ends them, are added on to the
> prerequisite file names generated from the pattern rule’s prerequisite
> patterns and the file name."
>
> I would appreciate any kind of explanation.
>
> BR,
> Bartek
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.