15.2. Why SCons Duplicates Source Files in a Variant Directory Tree

When you set up a variant directory SCons conceptually behaves as if you requested a build in that directory. As noted in the previous chapter, all builds actually happen from the top level directory, but as an aid to understanding how SCons operates, think of it as build in place in the variant directory, not build in source but send build artifacts to the variant directory. It turns out in place builds are easier to get right than out of tree builds - so by default SCons simulates an in place build by making the variant directory look just like the source directory. The most straightforward way to do that is by making copies of the files needed for the build.

The most direct reason to duplicate source files in variant directories is simply that some tools (mostly older versions) are written to only build their output files in the same directory as the source files - such tools often don't have any option to specify the output file, and the tool just uses a predefined output file name, or uses a derived variant of the source file name, dropping the result in the same directory. In this case, the choices are either to build the output file in the source directory and move it to the variant directory, or to duplicate the source files in the variant directory.

Additionally, relative references between files can cause problems which are resolved by just duplicating the hierarchy of source files into the variant directory. You can see this at work in use of the C preprocessor #include mechanism with double quotes, not angle brackets:

#include "file.h"

The de facto standard behavior for most C compilers in this case is to first look in the same directory as the source file that contains the #include line, then to look in the directories in the preprocessor search path. Add to this that the SCons implementation of support for code repositories (described below) means not all of the files will be found in the same directory hierarchy, and the simplest way to make sure that the right include file is found is to duplicate the source files into the variant directory, which provides a correct build regardless of the original location(s) of the source files.

Although source-file duplication guarantees a correct build even in these edge cases, it can usually be safely disabled. The next section describes how you can disable the duplication of source files in the variant directory.