[Scons-users] Per target env variables

Jonathon Reinhart jonathon.reinhart at gmail.com
Thu Sep 6 00:11:31 EDT 2018


Thanks Bill,

You're right, using the CFLAGS="$CFLAGS -Os" substitution is simpler.
I sometimes forget how well this works in SCons, and get worried about
bash-isms regarding spaces, etc.

I could have sworn that env.Override() was in the man page, but it
looks like I was wrong.  We've been trying to reduce the number of
Clone()s in a large codebase (specifically in some custom
pseudo-builders) and I've successfully used env.Override() for this
purpose. Given that there are tests for it (in EnvironmentTests.py),
it seemed unlikely to go away.

Jonathon


On Wed, Sep 5, 2018 at 11:57 PM Bill Deegan <bill at baddogconsulting.com> wrote:
>
> Don't use something which is not in the manpage.
> It can be dropped without warning.
>
> If you're going to use the modified CFLAGS as below many times
> Then using Clone() is reasonable.
>
> So here's what I'd sugguest:
> env=Environment(tools=[], MY_FLAG='ABC DEF')
> env2=env.Clone(MY_FLAG="$MY_FLAG XYZ PQR")
>
> print env['MY_FLAG']
> print env2['MY_FLAG']
>
> Here's the output you'd get from such:
> scons: Reading SConscript files ...
> ABC DEF
> ABC DEF XYZ PQR
> scons: done reading SConscript files.
> scons: Building targets ...
> scons: `.' is up to date.
> scons: done building targets.
>
> If you were only doing this for one target then:
> env.Program('target',[ sources ], CFLAG="$CFLAG -Os")
> (Jonathon Reinhart this is a bit simpler than the method you're recommending)
>
> Should do the trick.
>
> Here's the caveat.
> Don't make a gazillion Clone's they're not lightweight.
> If you make 100, it won't kill you, if you make 1000's you may start seeing memory and speed impacted.
>
> If you need to modify a flag, only for a single target, or a handful, and especially in your case where you want to programmatically convert a makefile which does such, then regular overrides are fine:
> I'm talking about this:
> env.Program('target',[ sources ], CFLAG="$CFLAG -Os")
>
>
> BTW if you get anything functional we'd be happy to pull it into:
>   https://github.com/SCons/scons-contrib
>
> -Bill
> SCons Project Co-Manager
>
> On Wed, Sep 5, 2018 at 8:41 PM RW via Scons-users <scons-users at scons.org> wrote:
>>
>> 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
>>
>> _______________________________________________
>> 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


More information about the Scons-users mailing list