Changes between Initial Version and Version 1 of Programming/Python/SconsRemoveExtraBuildOutputFiles


Ignore:
Timestamp:
Feb 19, 2016, 11:36:09 AM (8 years ago)
Author:
Vijay Varadan
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Programming/Python/SconsRemoveExtraBuildOutputFiles

    v1 v1  
     1= HOWTO: SCons - Remove Extra Build Output Files =
     2Jan 03 2016
     3
     4[[Image(htdocs:images/scons/SCons.png, alt="SCons Logo", align=center)]]
     5
     6I spent the end of 2015 and the first couple of days of 2016 porting build environment scripts to [https://en.wikipedia.org/wiki/Windows_PowerShell Powershell] and fixing up [http://scons.org/ SCons] based build scripts to work with Visual Studio 2015. When doing debug builds, incremental link (.ILK) and program database (.PDB) files are generated. These are considered extra files, since they're not the actual targets (like .OBJ, .EXE, .LIB or .DLL) being built.
     7
     8The recommended way to remove these files is to mark them as side effects and call the `Clean()` method on the environment object passing these as parameters. So, looking at [http://permalink.gmane.org/gmane.comp.programming.tools.scons.user/21082 this forum post], I did just that. The change to my SConscript looked something like this:
     9{{{#!python
     10if variant == 'debug':
     11    myenv.SideEffect('myproject.ilk', prog)
     12    myenv.Clean('myproject.ilk', prog)
     13    ...
     14    ...
     15}}}
     16And lo and behold, it **didn't** work. Unfortunately, SCons documentation is bit bare and scattered about with information split unevenly and non-intuitively in the [http://scons.org/doc/production/HTML/scons-user.html user guide], [http://scons.org/doc/production/HTML/scons-man.html man page], [http://scons.org/doc/production/HTML/scons-api/index.html API (object model) docs] and the [https://bitbucket.org/scons/scons/wiki/Home wiki]. The wiki, after repeatedly being hacked and abused was recently moved to !BitBucket, but doesn't seem to be searchable [aaaarrrrgggghhhh!]. This is even more true for information about `SideEffect()` and its use in conjunction with `Clean()`. I tried the typical sort of debugging via print statements to ensure that I wasn't munging up any values and nothing seemed amiss.
     17
     18I ended up finding a clue in the [https://bitbucket.org/scons/scons/wiki/SConsMethods/SideEffect wiki page for the `SideEffect()` function] - note that this page is not linked to from the main wiki page and neither is its parent. With search functionality missing for the wiki, I only found that page via a [http://duckduckgo.com DuckDuckGo] search because I was explicitly searching for documentation on the `SideEffect()` function. On that wiki page, I noticed the function signature which clearly showed that the order of parameters to the `SideEffect()` function is the reverse of that of `Clean()`. To be fair, the user guide does have examples and a listing with the correct parameter order - but it's not obvious or pointed out explicitly.
     19
     20In my not-so-humble opinion, it is **incredibly daft and very poor design** to reverse parameter order in API functions. Moreover, in this case, there doesn't even seem to be a good reason to do so. It results in increased user errors, just like with me and the original forum poster who posted the initial answer.
     21
     22I swapped the order of params and voilà! It worked.
     23{{{#!python
     24if variant == 'debug':
     25    myenv.SideEffect(<span style="color: #ff0000;">'myproject.ilk'</span>, <span style="color: #0000ff;">prog</span>)
     26    myenv.Clean(<span style="color: #0000ff;">prog</span>, <span style="color: #ff0000;">'myproject.ilk'</span>)
     27    ...
     28    ...
     29}}}
     30To ensure that I didn't run into this problem elsewhere, I added a function in my SConstruct that would take care of this, with an appropriate comment.
     31{{{#!python
     32function AddSideEffectForClean(env, side_effect, target):
     33    # NOTE: the order of params to SideEffect() is the reverse of Clean()
     34    env.SideEffect(side_effect, target)
     35    env.Clean(target, side_effect)
     36
     37...
     38...
     39...
     40
     41global_env.AddMethod(AddSideEffectForClean, "AddSideEffectForClean")
     42
     43...
     44...
     45...
     46}}}
     47In my various SConscript files, I invoke it as follows:
     48{{{#!python
     49if variant == 'debug':
     50    myenv.AddSideEffectForClean('myproject.ilk', target)
     51    myenv.AddSideEffectForClean('myproject.pdb', target)
     52    ...
     53    ...
     54}}}
     55I wasted a couple of hours on this, but it's sorted now. Hope this helps someone else looking to clean out extraneous build output files when using SCons.