Sharing Environments (and Other Variables) Between SConscript Files

In the previous example, each of the subsidiary SConscript files created its own construction environment by calling Environment separately. This obviously works fine, but if each program must be built with the same construction variables, it's cumbersome and error-prone to initialize separate construction environments in the same way over and over in each subsidiary SConscript file.

SCons supports the ability to export variables from a parent SConscript file to its subsidiary SConscript files, which allows you to share common initialized values throughout your build hierarchy.

Exporting Variables

There are two ways to export a variable, such as a construction environment, from one SConscript file, so that it may be used by other SConscript files. First, you can call the Export function with a list of variables, or a string white-space separated variable names. Each call to Export adds one or more variables to a global list of variables that are available for import by other SConscript files.

        env = Environment()
        Export('env')
      

XXX

        env = Environment()
        debug = ARGUMENTS['debug']
        Export('env', 'debug')
      

XXX

        Export('env debug')
      

Second, you can specify a list of variables to exported as a second argument to the SConscript function call:

        SConscript('src/SConscript', 'env')
      

Or as the exports keyword argument:

        SConscript('src/SConscript', exports='env')
      

These calls export the specified variables to only the listed SConscript files. You may, however, specify more than one SConscript file in a list:

        SConscript(['src1/SConscript',
                    'src2/SConscript'], exports='env')
      

This is functionally equivalent to calling the SConscript function multiple times with the same exports argument, one per SConscript file.

Importing Variables

XXX

        Import('env')
        env.Program('prog', ['prog.c'])
      

XXX

        Import('env', 'debug')
        env = env.Copy(DEBUG = debug)
        env.Program('prog', ['prog.c'])
      

Which is exactly equivalent to:

        Import('env debug')
        env = env.Copy(DEBUG = debug)
        env.Program('prog', ['prog.c'])
      

XXX

Returning Values From an SConscript File

XXX

        obj = env.Object('foo.c')
        Return('obj')
      

XXX

        objs = []
        for subdir in ['foo', 'bar']:
            o = SConscript('%s/SConscript' % subdir)
            objs.append(o)
        env.Library('prog', objs)
      

XXX