[Top][All Lists]

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

Re: Should this makefile really hang make?

From: Paul Smith
Subject: Re: Should this makefile really hang make?
Date: Sat, 19 Nov 2011 19:25:05 -0500

Gak.  This is a big mess.  Handling of : and % in second expansion
situations is very tricky.

On Tue, 2011-11-15 at 13:30 -0800, Bryan Ischo wrote:
> all: foo.a
> %.a: $$(@:%.a=%.o)

This is a bug due to make trying to parse the result of the second
expansion as if it were a library reference (e.g., "libfoo(a.o)" etc.)
If you change this to use {} instead of () in the braces, then you won't
get the infinite loop, but:

  $ cat Makefile
  %.a: $${@:.a=.o} ; ar cr $@ $^

  $ make
  Makefile:2: *** target pattern contains no `%'.  Stop.

Make is mis-parsing the extra ":" here, not realizing that it's part of
the variable reference, and interpreting this as a static pattern rule.

> %.a: $$(patsubst %.a,%.o,$$@)
>      ar cr $@ $^

The problem here is that make interprets the first "%" as part of the
pattern rule, and expands it.  So for "foo.a", make first expands the %
in the prerequisites list to "foo", and then it expands the function:

  $(patsubst foo.a,%.o,foo.a)

Since there's no pattern in the first string, there's no stem and "%" is
used verbatim, so we convert "foo.a" to "%.o".

The issue is it's not obvious how to fix this.  You could say that "%"
inside variable references should not be considered for the stem of a
pattern rule, but in other places we WANT the "%" to be replaced; for

  foo_OBJS = obj1.o obj2.o obj3.o
  %.a: $$(%_OBJS)

Here we want "%" to be replaced by the stem before we expand the
variable, so we get "$(foo_OBJS)" and expand that.  Of course in this
situation you can just use:

  %.a: $$($$*_OBJS)

instead if you want... but there is special handling of stems in the
context of directories that the latter doesn't allow for.

Maybe we need to allow escaping of "%"; something like:

  %.a: $$(patsubst \%.a,\%.o,$$@)

These areas need more consideration, to be sure.

 Paul D. Smith <address@hidden>          Find some GNU make tips at:            
 "Please remain calm...I may be mad, but I am a professional." --Mad Scientist

reply via email to

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