Copying Construction Environments

Sometimes you want more than one construction environment to share the same values for one or more variables. Rather than always having to repeat all of the common variables when you create each construction environment, you can use the Clone method to create a copy of a construction environment.

Like the Environment call that creates a construction environment, the Clone method takes construction variable assignments, which will override the values in the copied construction environment. For example, suppose we want to use gcc to create three versions of a program, one optimized, one debug, and one with neither. We could do this by creating a "base" construction environment that sets $CC to gcc, and then creating two copies, one which sets $CCFLAGS for optimization and the other which sets $CCFLAGS for debugging:

      env = Environment(CC = 'gcc')
      opt = env.Clone(CCFLAGS = '-O2')
      dbg = env.Clone(CCFLAGS = '-g')

      env.Program('foo', 'foo.c')

      o = opt.Object('foo-opt', 'foo.c')
      opt.Program(o)

      d = dbg.Object('foo-dbg', 'foo.c')
      dbg.Program(d)
   

Then our output would look like:

      % scons -Q
      gcc -o foo.o -c foo.c
      gcc -o foo foo.o
      gcc -o foo-dbg.o -c -g foo.c
      gcc -o foo-dbg foo-dbg.o
      gcc -o foo-opt.o -c -O2 foo.c
      gcc -o foo-opt foo-opt.o