Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 10 Aug 2006 15:11:13 +0200 (CEST)
From:      Oliver Fromme <olli@lurza.secnetix.de>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   Re: make(1) is broken?
Message-ID:  <200608101311.k7ADBDWn075286@lurza.secnetix.de>
In-Reply-To: <freebsd-hackers.20060810160836.40eb4128@localhost>

next in thread | previous in thread | raw e-mail | index | archive | help
Stanislav Sedov <ssedov@mbsd.msk.ru> wrote:
 > Consider the following Makefile:
 > ---------------------------------------------------------------
 > COMPS=aa ab ac
 > AA=aa
 > 
 > VAR1=${COMPS:Maa}
 > VAR2=${COMPS:M${AA}}
 > 
 > .for COMP in ${AA}
 > VAR3=${COMPS:M${COMP}}
 > .endfor
 > ---------------------------------------------------------------
 > 
 > Now, running make(1) gives:
 > % make -V VAR1
 > aa
 > % make -V VAR2
 > }
 > % make -V VAR3
 > aa
 > 
 > Results for VAR2 seems quite strange for me. It looks like
 > ${COMP}!=${AA}
 > 
 > So, the question: is make(1) broken, or it's my misunderstanding?

Nested variable expansion is _only_ documented to work for
the ":S" modifier, see the make(1) manual page.  When the
VAR2 assignment is parsed, the second opening brace is
ignored, and the first closing brace closes the first
opening one.  The second closing brace doesn't belong to
anything and is just appended to the assignment.

A bit simplified, the parser sees these tokens:

   <VAR2> <=> <$> <{> <COMPS> <:> <M> <${AA> <}> <}>

Of course, "${AA" doesn't match anything, so the whole
expansion is empty.  Then the closing brace is appended,
so the value of VAR2 is just "}".

Now, why does it work within the for loop?  That's a side
effect of the way how .for loops work.  When such a loop
is parsed, an expanded (duplicated) text is generated, and
the control variable of the loop is expanded before
_anything_ else, i.e. the expanded text of the .for loop
is parsed a second time.

I think it is better not to rely on that side effect.  It
isn't well documented and might change without notice in
the future.

Best regards
   Oliver

-- 
Oliver Fromme,  secnetix GmbH & Co. KG, Marktplatz 29, 85567 Grafing
Dienstleistungen mit Schwerpunkt FreeBSD: http://www.secnetix.de/bsd
Any opinions expressed in this message may be personal to the author
and may not necessarily reflect the opinions of secnetix in any way.

'Instead of asking why a piece of software is using "1970s technology,"
start asking why software is ignoring 30 years of accumulated wisdom.'



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200608101311.k7ADBDWn075286>