[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: makefile parsing and variable expansion
From: |
Ian Lynagh |
Subject: |
Re: makefile parsing and variable expansion |
Date: |
Sat, 3 Oct 2009 21:09:58 +0100 |
User-agent: |
Mutt/1.5.18 (2008-05-17) |
On Fri, Oct 02, 2009 at 09:21:25AM -0400, Paul Smith wrote:
> On Wed, 2009-09-30 at 21:47 +0100, Ian Lynagh wrote:
> > I am trying to understand how makefiles are parsed, e.g. why this
> > makefile:
> > # -----------
> > default:
> > echo hello
> > define foo
> > q:
> > echo this is q1
> > echo this is q2
> > endef
> > $(foo)
> > # -----------
> >
> > ends up containing this (according to "make -pr"):
> >
> > # -----------
> > q: echo this is q1
> > echo this is q2
> > # -----------
>
> Because make's parser is line-based. Each logical line that is read
> from the makefile is considered a single line EVEN IF, after expansion,
> it contains newlines. A variable statement like this CANNOT expand into
> a construct that spans multiple logical lines.
Unless it's indented by a tab, right? e.g.:
# ----- begin
define foo
line 1
line 2
line 3
endef
z:
$(foo)
# ----- end
In which case it looks like the line is expanded, broken on \n into
lines, and each treated as a command. If there is a literal @ or - then
it applies to every command, and if any line expands to one starting @
or - then that applies to that line only.
Also, this doesn't work:
inc = inc
lude = lude
$(inc)$(lude) wibble.mk
nor this:
tab = $(nothing) $(nothing)
z:
$(tab)echo foo
so I guess that by the time you get to that point, if it doesn't begin
with a tab then it's committed to being some kind of "foo : ..." line.
> > I am also confused as to why the above Makefile is accepted, while:
> >
> > # -----------
> > default:
> > echo hello
> >
> > q: echo this is q1
> > echo this is q2
> > # -----------
>
> Because make, as above, is line-based. There's no _normal_ way to
> introduce a newline into a filename in a prerequisite list. The only
> way to do it is via define/endef as you've done.
Ah, I see. So when foo is expanded the first <newline><tab> turns into
"some whitespace" at the beginning of the dep list, as does the second
<tab> inbetween items, but the second <newline> is treated as a
filename-character.
But if there aren't any other (non-newline) filename characters
following the newline, then it looks like it is treated as whitespace
before the "endef".
> Actually, this is probably a bug in the make parser;
I would agree.
Thanks
Ian