[Scons-users] Per target env variables

Jonathon Reinhart jonathon.reinhart at gmail.com
Wed Sep 5 20:09:02 EDT 2018


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


More information about the Scons-users mailing list