Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCons: save/redirect gcc textual output (warnings)

I'm using SCons to build a project at work, and I'm trying to parse through the textual output from GCC to make a summary report of all the compiler warnings for each target because our build scripts are quite long, and there is a lot of text output to the console.

I've searched Google and this site for quite a while, and I can't find a method built-in to SCons to accomplish this. I've tried redirecting the entire stdout and stderr stream to a file per this example, but only the output from SCons itself is captured and not that of any tools it calls.

My next thought was to find where SCons compiles the arguments to send to GCC and add redirection to the end of the arguments string. After reading the documentation, it seems that the construction variables CCCOM and CXXCOM contain the command line used to compile. However, when I added the lines below to my SConstruct, nothing changed in the command lines SCons is executing.

baseEnv['CCCOM'] += " 2> gcc-c-output.txt"
baseEnv['CXXCOM'] += " 2> gcc-cxx-output.txt"

One thing that did work was redirecting the stderr stream on the entire SCons command:

scons 2> stderr.txt

I would like to avoid this, however, and contain everything within SCons if possible. The output also doesn't necessarily have to go to a file. It can be saved anywhere as long as I can access it to parse and save to a file at the end of the build.

I've searched for so long and come up with nothing, so I don't know what else to try. I have to believe that I'm not the first one who has wanted to do something like this.

like image 451
EarlCrapstone Avatar asked Jun 21 '12 13:06

EarlCrapstone


2 Answers

I'm going to answer my own question here as I figured out what I was doing wrong. The CCCOM and CXXCOM variables were the correct ones to be modified, but the problem was I was creating shared-library objects, so these variables were not being used. The ones I should have been modifying are SHCCCOM and SHCXXCOM.

The following code got the job done for redirecting the GCC warning output (warnings and errors are written to stderr only):

baseEnv['SHCCCOM'] += " 2> gcc-c-output.txt"
baseEnv['SHCXXCOM'] += " 2> gcc-cxx-output.txt"

Hopefully this answer will help others as I could not find much information on this topic when searching.

like image 84
EarlCrapstone Avatar answered Nov 05 '22 18:11

EarlCrapstone


For full control, you can override the env['SPAWN'] construction variable. You will then supply a Python method which will be responsible for executing the command line given to it by argument. Typically, you would take this argument and spawn a subprocess (using some form of subprocess.Popen). This would make a pass-through replacement for the default env['SPAWN'].

But now, you have full control over the subprocess.Popen invocation. You are free to redirect STDOUT, STDERR or both to any buffer/file. You can also do post processing on that output before printing it to the console and/or saving it to a file. I've used this to color warnings in yellow and errors in red, for example.

For sample code on how to override env['SPAWN'], check out this page on the SCons Wiki.

like image 3
BenG Avatar answered Nov 05 '22 17:11

BenG