help-make
[Top][All Lists]
Advanced

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

Re: make VAR+=VAL


From: David Boyce
Subject: Re: make VAR+=VAL
Date: Mon, 21 Mar 2016 10:57:09 -0700

On Sat, Mar 19, 2016 at 7:22 AM, Paul Smith <address@hidden> wrote:

> On Tue, 2016-03-15 at 10:29 +0800, 一瓶水 wrote:
> > I saw GNU make (I am using 3.8.1) allows the usage of 'make VAR+=VAL'
> > command line, but it does not append VAL to VAR and instead it
> > overrides VAR to VAL.
> >
> >
> > Is this expected behavior? I did not find any doc explaining the usage
> > of VAR+=VAL on command line.
>
> It is expected behavior.  Variables on the command line have a higher
> priority than variables set inside makefiles, and command line variables
> are set first when the command line is parsed.  So, by the time the
> makefile is read in and another variable assignment is seen, that will
> have a lower priority and it will be ignored.
>

I feel that, however correct, there's still something unsatisfying in this
answer because the behavior is counterintuitive. It's clear what a person
wants and expects when they use += on the command line but they (silently)
don't get that. Consider these sequences:

Using += in makefile with = on command line:

% echo -e 'override FOO += bar\n$(info FOO=$(FOO))\nall:;@:' | make -f -
FOO=foo
FOO=foo bar

Using += in makefile with += on command line:

% echo -e 'override FOO += bar\n$(info FOO=$(FOO))\nall:;@:' | make -f -
FOO+=foo
FOO=foo bar

The above two give the identical result. Same for this set with a = in the
makefile:

% echo -e 'override FOO = bar\n$(info FOO=$(FOO))\nall:;@:' | make -f -
FOO=foo
FOO=bar

% echo -e 'override FOO = bar\n$(info FOO=$(FOO))\nall:;@:' | make -f -
FOO+=foo
FOO=bar

So in what scenario would += on the command line make sense? I'd propose
that it either be "fixed" to do what people expect or to give an error. Or
at the very least this anti-intuitive behavior should be called out
specially in the documentation (I don't recall any such and didn't find it
in a quick scan).

David Boyce


reply via email to

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