Is a pattern rule implicitly double-colon?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is a pattern rule implicitly double-colon?

Fangrui Song
% cat Makefile
.SUFFIXES:
%.o: %.c
         touch $@
%.o: %.d
         touch $@


% rm -f a.o b.o; touch a.c b.d; make a.o b.o
removed 'a.o'
removed 'b.o'
touch a.o
touch b.o


The behavior is similar to double-colon rules. It can not be explained
by

> If more than one rule gives a recipe for the same file, make uses the
> last one given and prints an error message.

in https://www.gnu.org/software/make/manual/html_node/Multiple-Rules.html#Multiple-Rules


Context: I intend to use such multiple rules for one %.o target in an
lldb Makefile https://reviews.llvm.org/D94890

Reply | Threaded
Open this post in threaded view
|

Re: Is a pattern rule implicitly double-colon?

Nicholas Clark
Hmm, I don't read that as acting like a double-colon rule.

Instead, GNU Make does this:

1. You asked for a.o and b.o.
2. The Makefile has a rule that makes %.o from %.c, and one that makes %.o
from %.d
3. GNU Make looks for a.c and finds it, then uses recipe #1 for a.o.
4. GNU Make looks for b.c and doesn't find it.
5. GNU Make looks for b.d and finds it, then uses recipe #2 for b.o.

A double-colon rule would execute both recipes for each target, which this
example clearly doesn't do.

-Nick

On Sun, Jan 17, 2021 at 7:10 PM Fangrui Song <[hidden email]> wrote:

> % cat Makefile
> .SUFFIXES:
> %.o: %.c
>          touch $@
> %.o: %.d
>          touch $@
>
>
> % rm -f a.o b.o; touch a.c b.d; make a.o b.o
> removed 'a.o'
> removed 'b.o'
> touch a.o
> touch b.o
>
>
> The behavior is similar to double-colon rules. It can not be explained
> by
>
> > If more than one rule gives a recipe for the same file, make uses the
> > last one given and prints an error message.
>
> in
> https://www.gnu.org/software/make/manual/html_node/Multiple-Rules.html#Multiple-Rules
>
>
> Context: I intend to use such multiple rules for one %.o target in an
> lldb Makefile https://reviews.llvm.org/D94890
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Is a pattern rule implicitly double-colon?

Paul Smith-20
In reply to this post by Fangrui Song
On Sun, 2021-01-17 at 17:43 -0800, Fangrui Song wrote:
> % cat Makefile
> .SUFFIXES:
> %.o: %.c
>          touch $@
> %.o: %.d
>          touch $@

It might be more clear what was happening if you made the recipes for
these two rules different in some way:

  %.o : %.c ; echo from .c > $@
  %.o : %.d ; echo from .d > $@

> % rm -f a.o b.o; touch a.c b.d; make a.o b.o
> removed 'a.o'
> removed 'b.o'
> touch a.o
> touch b.o
>
> The behavior is similar to double-colon rules.

No, not really; as Nick explains if it were similar to double-colon
rules BOTH rules would be run for BOTH targets.

>  It can not be explained by
>
> > If more than one rule gives a recipe for the same file, make uses
> > the ast one given and prints an error message.
>
> in
> https://www.gnu.org/software/make/manual/html_node/Multiple-Rules.html

That section deals only with _explicit_ rules.  You are working with
implicit (pattern) rules, which follow a completely different algorithm
for matching; see here:

https://www.gnu.org/software/make/manual/html_node/Pattern-Match.html

If it helps you can think of explicit rules like classes in C++: there
can be only one with that name.  Pattern rules are like templates in
C++: each template must be different (can't have exactly the same set
of target and prerequisite patterns) but they can apply to a large
number of different targets.  The section above shows how make chooses
which implicit rule (template) to apply in a given situation.