问题
I can define a custom build command output by defining $*COMSTR in my environment:
env['CXXCOMSTR'] = compile_source_message
However, this overrides the message shown for the build command. I want to augment the message instead, e.g. by prefixing it with the target. My goal is to have a message like this:
% scons
Compiling foo.o
cc -o foo.o -c foo.c
I tried the following:
env['CXXCOMSTR'] = compile_source_message + '\n' + env['CXXCOMSTR']
However, this doesn't work because CXXCOMSTR
is not in env
yet, I get a KeyError: 'CXXCOMSTR'
.
How can I augment/prefix the default $*COMSTR
?
回答1:
The problem you're having is due to the following behavior:
If the *COMSTR evaulates to a null string, then the CommandAction object which undelies the Action for this will default to using the *COM string to generate the output.
try:
print env.Dump('CXXCOMSTR')
except KeyError as e:
print env.Dump('CXXCOM')
Would output the value you're looking for. And for your specific usage:
try:
env['CXXCOMSTR'] = compile_source_message + '\n' + env['CXXCOMSTR']
except: KeyError as e:
env['CXXCOMSTR'] = compile_source_message + '\n' + env['CXXCOM']
Of course if some logic in your build were to later on alter the value of CXXCOM then that change would not affect your new setting.
If you wanted to ensure that changes to CXXCOM would propogate to your new value of CXXCOMSTR then the following should work:
if env.get('CXXCOMSTR',False):
env['CXXCOMSTR'] = compile_source_message + '\n' + env['CXXCOMSTR']
else:
env['CXXCOMSTR'] = "%s \n $CXXCOM"%compile_source_message
Note that having the $CXXCOM(*) in your new CXXCOMSTR allows SCons to substitute and fully expand the output for each target.
回答2:
As far as I know, there is currently no environment variable like "$ACTIONCMD
" containing the full command string for the running build step. So what you're trying to do:
env['CXXCOMSTR'] = "Compiling $TARGET:\n$ACTIONCMD"
isn't possible. It would require changing the SCons core sources, so it's not impossible in the long run. ;)
However, when creating a new Action
object you can also specify a callable
instead of a simple command string via the strfunction
argument. Within this function you could then compile the printed string to your liking. Search the MAN page for the keyword strfunction
to get an impression, and then decide for yourself whether you want to take this route or not. Please also consider subscribing to our user mailing list scons-users@scons.org
for questions like this.
来源:https://stackoverflow.com/questions/35329543/how-to-augment-scons-comstr