[Scons-users] Per target env variables

Gary Oberbrunner garyo at oberbrunner.com
Thu Sep 6 07:05:14 EDT 2018


Another approach to this is to add something like $EXTRA_CFLAGS to CFLAGS,
then you can just do
  env.Program('hello', 'hello.c', EXTRA_CFLAGS=['-Os'])

-- Gary

On Thu, Sep 6, 2018 at 6:27 AM RW via Scons-users <scons-users at scons.org>
wrote:

>
> Hi Bill,
> Thanks for the info
>
> As a seperate question I'm wondering if I should try to add Override() to
> the user docs, or if it's classed as a non public API which might change
> later on
> I dont think it's depreciated looking at the code
> it looks like the Builder.py uses it to actually do it's overrides under
> the hood
> and the Override() call is just a wrapper around the OverrideEnvironment
> class
>
> Many Thanks
> Richard
>
>
> On Thu, 6 Sep 2018 at 05:17, Bill Deegan <bill at baddogconsulting.com>
> wrote:
>
>> If it's not in the users guide or manpage it's not subject to deprecation
>> cycle..
>> (Those are the definitive spec for the user facing API for SCons.  Thus
>> the checklist item on pull requests for docs and tests (and src/CHANGES.txt
>> modifications))
>>
>> Why so many clones? Are the environment's being modified?
>> Are you seeing that the # of clones is actually causing you issues?
>> Are you Clone'ing as a form of write protection on the original env?
>> How many objects does "--debug=count" output for your build?
>>
>> -Bill
>>
>>
>> On Thu, Sep 6, 2018 at 12:11 AM Jonathon Reinhart <
>> jonathon.reinhart at gmail.com> wrote:
>>
>>> 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
>>> _______________________________________________
>>> 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
>


-- 
Gary
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20180906/16bcd55d/attachment-0001.html>


More information about the Scons-users mailing list