Thursday, May 14, 2009

SCons: cleaning variant_dir

A large software project will typically contain a hierarchy of SConscript files, with a root file invoking the subsidiary files. Consider the following hierarchy: The SConscript in /src contains: Just calling SConscript('src/SConscript') from the root SConstruct file results in the build files residing with the source files in the src/ directory. It can be useful to have the build output in a separate directory as it can provide easier cleaning (just nuke the directory) and packaging. This is what the variant_dir keyword argument is for. The root SConstruct file: Now calling scons will result in the build output residing below a new 'build' directory. It also copies the source files across to support legacy tools that will only spit their output into the directory containing the input. This duplication can be turned off by adding the 'duplicate=0' keyword argument to the SConscript() call. To clean up the build output one uses the regular 'scons -c' command. However, this will not remove the newly created build directory. This might be a performance improvement to save having to copy files from src to build each time. However, the build directory is also not removed when 'duplicate=0' is specified, i.e., when the build directory contains no copied source files. If you want the build directory nuked when you call 'scons -c' you can identify it for cleaning using the Clean() function. This function takes two arguments: the first a target and the second the pathname to remove. This tells SCons to remove pathname when it considers the target in its dependency checking. Directories are implicitly targets and '.' is always considered so this provides our target. SConstruct becomes: Calling 'scons -c' now will remove the build directory. (tested on Mac OS X with SCons 1.2.0.d20090223).

No comments: