[Scons-users] Per target env variables

RW garlicbready at googlemail.com
Wed Sep 5 20:41:39 EDT 2018


Thanks for the info
I was wondering if there might have been a per target / file thing like
with Require()
I think I'll use the Override example you've given above to use an
OverrideEnvironment()
that seems to be the best way

this seems to work
env2 = env.Override(
    {'CFLAGS': env.get('CFLAGS', []) + ['-Os']}
)

I think the Override method isn't documented in the user manual / man page
list of functions from the looks of things.

For info I started writing a script to convert Makefiles to scons scripts
it's still very crude at this stage, and mostly tries to capture variable
assignments and if blocks (not so much the rules) just to try and take out
the majority of the leg work, still someone might find it usefull
https://github.com/grbd/Scons.Make2Scons

Many Thanks
Richard

On Thu, 6 Sep 2018 at 01:09, Jonathon Reinhart <jonathon.reinhart at gmail.com>
wrote:

> Hi Richard,
>
> You're correct that this syntax will override the variable (via an
> OverrideEnvironment), effectively replacing it:
>
>     env.Program('hello', 'hello.c', CFLAGS=['-Os'])
>
> I wish something like this would exist, but it's not valid Python syntax:
>
>     env.Program('hello', 'hello.c', CFLAGS+=['-Os'])
>
> The best method I've found is this:
>
>     env.Program('hello', 'hello.c',
>         CFLAGS = env.get('CFLAGS', []) + ['-Os'],
>     )
>
> Alternatively, you can clone the environment to customize it for a set
> of similar targets:
>
>     foo_env = env.Clone()
>     foo_env.Append(CFLAGS=['-Os'])
>     foo_env.Program('hello', 'hello.c')
>
> Do note that Clone() is expensive, and should be avoided in my
> opinion. If you're not doing anything too crazy, you can get by with
> an OverrideEnvironment:
>
>     envo = env.Override(
>         CFLAGS = env.get('CFLAGS', []) + ['-Os'],
>     )
>     envo.Program('hello', 'hello.c')
>
> Note: Don't try to Append() to an OverrideEnvironment; that call is
> proxied to the underlying environment which would then be modified.
> (This is possibly a bug.)
>
> Hope this helps,
> Jonathon
> On Wed, Sep 5, 2018 at 7:50 PM RW via Scons-users <scons-users at scons.org>
> wrote:
> >
> > Hi,
> > I was wondering, I know standard Makefiles have a way of specifying
> flags for a given file before the target / rule is defined
> >
> > such as
> > $(PY_BUILD)/nlr%.o: CFLAGS += -Os
> >
> > is this possible in scons? or is the only way to pass in custom env
> variables per target is when the target is defined?
> > https://www.scons.org/doc/latest/HTML/scons-user/ch03s09.html
> >
> > such as
> > env.Program('hello', 'hello.c', CFLAGS=['-Os'])
> >
> > Also the way the docs read I think it doesn't make it clear if your
> appending or replacing values in the environment variable (e.g. CFLAGS) for
> that specific target (I'm assuming replacing)
> >
> > I've been trying to see if I could port the Makefiles from micropython
> across to scons btw, but as an initial step I've been trying to keep the
> logic the same without changing the order of things too much
> >
> > Many Thanks
> > Richard
> > _______________________________________________
> > Scons-users mailing list
> > Scons-users at scons.org
> > https://pairlist4.pair.net/mailman/listinfo/scons-users
> _______________________________________________
> Scons-users mailing list
> Scons-users at scons.org
> https://pairlist4.pair.net/mailman/listinfo/scons-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20180906/76c142ff/attachment.html>


More information about the Scons-users mailing list