help-make
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: David Deutsch
Subject: Re: Match anything pattern rules and pattern rules- help with understanding
Date: Mon, 29 May 2023 05:44:47 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]