[Scons-users] Modifying the provided environment in an emitter?

Andrew C. Morrow andrew.c.morrow at gmail.com
Sat Apr 22 10:27:18 EDT 2017


It appears that the answer is a little more interesting:

import SCons

env = DefaultEnvironment()

def injected_emitter(target, source, env):
    print(env, " for targets ", map(str, target))
    libs = env.get('LIBS', [])
    libs.append('foox')
    env['LIBS'] = libs
    if 'EXTRA' in env:
            print(env, " found EXTRA for targets ", map(str, target))
    return (target, source)

def add_emitter(builder):
    base_emitter = builder.emitter
    new_emitter = SCons.Builder.ListEmitter([injected_emitter,
base_emitter])
    builder.emitter = new_emitter

target_builders = ['Program']
for builder in target_builders:
    add_emitter(env['BUILDERS'][builder])

env.Program(
    target='foo1',
    source=['foo1.cpp']
)

env.Program(
    target='foo2',
    source=['foo2.cpp']
)

env.Program(
    target='foo3',
    source=['foo3.cpp'],
    EXTRA="asdf",
)


Gives me the output:

(<SCons.Script.SConscript.SConsEnvironment object at 0x10eb58950>, ' for
targets ', ['foo1'])
(<SCons.Script.SConscript.SConsEnvironment object at 0x10eb58950>, ' for
targets ', ['foo2'])
(<SCons.Environment.OverrideEnvironment object at 0x10ecfdb50>, ' for
targets ', ['foo3'])
(<SCons.Environment.OverrideEnvironment object at 0x10ecfdb50>, ' found
EXTRA for targets ', ['foo3'])


Note that foo1 and foo2 share an environment, but foo3 has its own.

If I add a LIBS=['m'] to foo2, then it too gets its own environment:

(<SCons.Script.SConscript.SConsEnvironment object at 0x10a540950>, ' for
targets ', ['foo1'])
(<SCons.Environment.OverrideEnvironment object at 0x10a6e5a10>, ' for
targets ', ['foo2'])
(<SCons.Environment.OverrideEnvironment object at 0x10a6e5bd0>, ' for
targets ', ['foo3'])
(<SCons.Environment.OverrideEnvironment object at 0x10a6e5bd0>, ' found
EXTRA for targets ', ['foo3'])


Still, that isn't a guarantee, since users might in fact not customize a
Program or Library declaration beyond providing target and source
arguments, which suggests that this isn't the right way to go about it.

Thanks,
Andrew



On Fri, Apr 21, 2017 at 6:45 PM, William Blevins <wblevins001 at gmail.com>
wrote:

> Using SCons.Builder.BuilderBase as a reference for my answer, emitters and
> environment are bound at the Builder level.
>
> On Fri, Apr 21, 2017 at 5:42 PM, Andrew C. Morrow <
> andrew.c.morrow at gmail.com> wrote:
>
>>
>> Thanks for the answer. I think cloning it would defeat my aim though. I
>> want the modified environment to apply to the target. If I clone it, I
>> don't think it possibly could. Or does each target have en env I can rebind
>> to the cloned and modified environment?
>>
>> On Fri, Apr 21, 2017 at 5:34 PM, William Blevins <wblevins001 at gmail.com>
>> wrote:
>>
>>> I would only do so on a clone of the provided environment in order to
>>> not modify the environment which may be used elsewhere.
>>>
>>> On Apr 21, 2017 5:29 PM, "Andrew C. Morrow" <andrew.c.morrow at gmail.com>
>>> wrote:
>>>
>>>>
>>>> Hi all -
>>>>
>>>> Is it legit to modify the passed in env in an emitter?
>>>>
>>>>     def add_lib_foo_emitter(target, source, env):
>>>>         libs = env.get('LIBS', [])
>>>>         libs.append('foo')
>>>>         env['LIBS'] = libs
>>>>         return (target, source)
>>>>
>>>>     def add_emitter(builder):
>>>>         base_emitter = builder.emitter
>>>>         new_emitter = SCons.Builder.ListEmitter([add_lib_foo_emitter,
>>>> base_emitter])
>>>>         builder.emitter = new_emitter
>>>>
>>>>     target_builders = ['Program', 'SharedLibrary', 'LoadableModule',
>>>> 'StaticLibrary']
>>>>     for builder in target_builders:
>>>>         add_emitter(env['BUILDERS'][builder])
>>>>
>>>> I've found myself wanting to do this with increasing frequency, but am
>>>> unclear on whether this is acceptable. It appears to work, but the
>>>> documentation doesn't seem to offer any clear guidance that I've found on
>>>> whether it is guaranteed to work. If so (which would be great!), am I
>>>> guaranteed that the modification of env is scoped only to the passed in
>>>> targets?
>>>>
>>>> Thanks,
>>>> Andrew
>>>>
>>>>
>>>> _______________________________________________
>>>> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist4.pair.net/pipermail/scons-users/attachments/20170422/bb28f47b/attachment.html>


More information about the Scons-users mailing list