15.4. The VariantDir Function

You can also use the VariantDir function to establish that target files should be built in a separate directory tree from the source files:

VariantDir('build', 'src')
env = Environment()
env.Program('build/hello.c')
      

When using this form, you have to tell SCons that sources and targets are in the variant directory, and those references will trigger the remapping, necessary file copying, etc. for an already established variant directory. Here is the same example in a more spelled out form to show this more clearly:

VariantDir('build', 'src')
env = Environment()
env.Program(target='build/hello', source=['build/hello.c'])
    

When using the VariantDir function directly, SCons still duplicates the source files in the variant directory by default:

% ls src
hello.c
% scons -Q
cc -o build/hello.o -c build/hello.c
cc -o build/hello build/hello.o
% ls build
hello  hello.c  hello.o

You can specify the same duplicate=False argument that you can specify for an SConscript call:

VariantDir('build', 'src', duplicate=False)
env = Environment()
env.Program('build/hello.c')
      

In which case SCons will disable duplication of the source files:

% ls src
hello.c
% scons -Q
cc -o build/hello.o -c src/hello.c
cc -o build/hello build/hello.o
% ls build
hello  hello.o