make: variable expansion in .for/.endfor

Ruslan Ermilov ru at freebsd.org
Fri May 16 05:30:19 PDT 2003


On Tue, Apr 29, 2003 at 03:01:43PM +0200, Roman Neuhauser wrote:
> # roam at ringlet.net / 2003-04-29 13:34:04 +0300:
> > On Sun, Apr 27, 2003 at 02:48:12PM +0200, Roman Neuhauser wrote:
> > > # roam at ringlet.net / 2003-04-26 14:53:36 +0300:
> > > > On Fri, Apr 25, 2003 at 08:44:00PM +0200, Roman Neuhauser wrote:
> > > > > Try the following makefile: it works if called with -DONE, but does not
> > > > > if called with -DTWO. Should I treat it as a bug and file a PR?
> > > > > 
> > > > > LIST= foo bar baz
> > > > > 
> > > > > .if defined(ONE)
> > > > > .  for v in ${LIST}
> > > > > .    if !defined(WITHOUT_${v:U})
> > > > > WITH_${v:U}=yes
> > > > > .    endif
> > > > > .  endfor
> > > > > .endif
> > > > > 
> > > > > .if defined(TWO)
> > > > > .  for v in ${LIST}
> > > > > V=${v:U}
> > > > > .    if !defined(WITHOUT_${V})
> > > > > WITH_${V}=yes
> > > > > .    endif
> > > > > .  endfor
> > > > > .endif
> > > > > 
> > > > > a:
> > > > > 	@echo \$${WITH_FOO}: ${WITH_FOO}
> > > > > 	@echo \$${WITH_BAR}: ${WITH_BAR}
> > > > > 	@echo \$${WITH_BAZ}: ${WITH_BAZ}
> > > > > 
> > > > > .PHONY: a
> > > > 
> > > > I think this is a known bug, and it seems to even be documented
> > > > in the BUGS section of -STABLE's make(1) manual page.
> > > 
> > >     I don't think this is covered. Can you point out the relevant text?
> > 
> > Looking at:
> >   $FreeBSD: src/usr.bin/make/make.1,v 1.29.2.15 2002/12/17 19:01:18 seanc Exp $
> > 
> > The rendered version reads, right at the end of the BUGS section:
> > 
> >   For loops are expanded before tests, so a fragment such as:
> > 
> >   .for TMACHINE in ${SHARED_ARCHS}
> >   .if ${TMACHINE} = ${MACHINE}
> >        ...
> >   .endif
> >   .endfor
> >   won't work, and should be rewritten the other way around.
> > 
> > This particular part of the manual page seems to have been there
> > for the past three years, since Tim Vanderhoek's merge from OpenBSD
> > in rev. 1.23.
> > 
> > At least, I think that this is similar (actually, nearly identical) to
> > your Makefile fragment.
> 
>     both fragments are "nearly identical", and the first one works. it's
>     the assignment (V=${v:U}) that breaks. so, where's the similarity
>     (or identity), given that the snippet from the make man page is
>     unrelated to assignments?
> 
>     again, the first version (wrapped in ifdef ONE) works just fine.
> 
Yes, BUGS section talks about a different problem, is that
.for loops are unrolled before parsing, so if loop variable
"v" has the value "X", the

.if ${v} == "foo"

becomes

.if X == "foo"

and now recall the (documented) limitation that "An expression
may also be an arithmetic or string comparison, with the left-hand
side being a variable expansion."

FWIW, the code snippet above works perfectly under 5.x make(1).
I recall this problem was fixed (perhaps, it was even me, not
sure).  Sorry, but I don't have enough time to invest into
backporting the bugfix into RELENG_4, the latter is becoming
less priority for me as 5.x evolves.

If you'll be able to extract it from HEAD (there is a huge
backlog of non-backported fixes for make(1) there), I will
happily commit it for you.

$ uname -r
5.1-BETA
$ make -DONE
${WITH_FOO}: yes
${WITH_BAR}: yes
${WITH_BAZ}: yes
$ make -DTWO
${WITH_FOO}: yes
${WITH_BAR}: yes
${WITH_BAZ}: yes


Cheers,
-- 
Ruslan Ermilov		Sysadmin and DBA,
ru at sunbay.com		Sunbay Software AG,
ru at FreeBSD.org		FreeBSD committer,
+380.652.512.251	Simferopol, Ukraine

http://www.FreeBSD.org	The Power To Serve
http://www.oracle.com	Enabling The Information Age
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-hackers/attachments/20030516/a55d1df3/attachment.bin


More information about the freebsd-hackers mailing list