[Scons-users] Attaching an emitter to an existing builder

Jeremy Elson jelson at gmail.com
Sat Jan 30 19:07:02 EST 2021


Hi Bill,

I'd like to do better than file a bug -- how about a PR?

https://github.com/SCons/scons/compare/master...jelson:jelson/scons-mapfile

I don't have much experience with SCons internals, so I might have not done
this in the most idiomatic way, but I'm open to suggestions how to improve
it so I can make it into a PR. I tested this with a simple SConstruct file:

Program('prog1.c')
Program('prog2.c', mapfile='prog2.map')
Program('prog3', source='prog1.c')

It generates the output you would expect:

gcc -o prog1.o -c prog1.c
gcc -o prog1 prog1.o
gcc -o prog2.o -c prog2.c
gcc -o prog2 prog2.o -Wl,-Map=prog2.map,--cref
gcc -o prog3 prog1.o

If my implementation is way too off the mark to be useful, I'd be happy to
just file a bug instead :)

-Jeremy


On Fri, Jan 29, 2021 at 10:58 AM Bill Deegan <bill at baddogconsulting.com>
wrote:

> Jeremy,
>
> Glad that worked for you!
> Please consider filing an enhancement request issue to support generating
> map files?
>
> Thanks,
> Bill
>
> On Thu, Jan 28, 2021 at 4:20 PM Jeremy Elson <jelson at gmail.com> wrote:
>
>> Thanks, everyone for the help -- PROGEMITTER worked perfectly:
>>
>>         # Add map creation as a linker flag, and modify the standard
>> Program
>>         # emitter to know that map generation is a side-effect
>>         map_filename = os.path.join(build_obj_dir, f"{target.name}.map")
>>         env.Append(LINKFLAGS = f"-Wl,-Map={map_filename},--cref")
>>         def map_emitter(target, source, env):
>>             assert(str(target[0]) == os.path.abspath(program_name))
>>             target.append(map_filename)
>>             return target, source
>>         env.Append(PROGEMITTER = [map_emitter])
>>
>> My only concern with PROGEMITTER was that it was not a general solution
>> to this problem -- i.e., what if I want to override something other than
>> Program()? But from looking at MSlink.py as Mats suggested, it seems that
>> nearly all the standard builders have similar overrides (SHLIBEMITTER,
>> LDMODULEEMITTER, etc). I guess it's similar to the *COMSTR series of
>> constants for modifying the message printed at each build step.
>>
>> Thanks again for a great build system. SCons is a pleasure to use.
>>
>> -Jeremy
>>
>>
>>
>> On Thu, Jan 28, 2021 at 7:19 AM Mats Wichmann <mats at wichmann.us> wrote:
>>
>>> On 1/27/21 3:11 PM, Bill Deegan wrote:
>>> > Jeremy,
>>> >
>>> > Greetings!
>>> > You're not the first to mention adding the map file as a side effect.
>>> > (or target)
>>> > Please go ahead and file and Issue on github for this.
>>> > Ideally with a simple example, what the gcc flag is, and what the file
>>> > naming/location would be?
>>> >
>>> > So likely the easiest way to add an emitter to Program() would be the
>>> > following (not tested)
>>> >
>>> > env.Append(PROGEMITTER = [my_map_emitter])
>>> >
>>> > Give that a try and let us know if it works for you?
>>> >
>>> > Also there's a discord server for scons help if you'd like to ask
>>> > questions/get help there.
>>> > https://discord.gg/bXVpWAy <https://discord.gg/bXVpWAy>
>>> >
>>> > Hope that helps!
>>> > -Bill
>>>
>>> For inspiration, the mslink tool does this (as does the qt tool) - see
>>> the somewhat messy SCons/Tool/mslink.py.
>>>
>>> Meanwhile, while many tools use add_emitter, the code for add_emitter
>>> itself includes this comment:
>>>
>>>          This assumes that emitter has been initialized with an
>>>          appropriate dictionary type, and will throw a TypeError if
>>>          not, so the caller is responsible for knowing that this is an
>>>          appropriate method to call for the Builder in question.
>>>
>>> >              env["BUILDERS"]["Program"].add_emitter(
>>> >                  suffix = ".map",
>>> >                  emitter = map_emitter)
>>> >
>>> >     But it throws an exception:
>>> >
>>> >     TypeError: 'EmitterProxy' object does not support item assignment
>>>
>>> So I guess "expected"? although pretty awkward....
>>>
>>> _______________________________________________
>>> 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/20210130/2e094bad/attachment-0001.html>


More information about the Scons-users mailing list