This is how you can get scons to behave like autoconf (having a 'configure' step first)

Many people need autoconf like functionality in their build, SConf provides this in scons. Say you have program X which depends on stdio.h, math.h, stdlib.h, libSDL.so. You could check for these in your build like this.

First you must create your configuration environment.

   1 conf = Configure(env)
   2 
   3 # ... use conf to perform checks here ...
   4 
   5 # keep any changes you may have performed
   6 env = conf.Finish()

A good first check (that autotools usually does first) is to check that the compiler is installed and working

   1 # ---- check for compiler and do sanity checks
   2 if not conf.CheckCXX():
   3     print('!! Your compiler and/or environment is not correctly configured.')
   4     Exit(0)
   5 
   6 if not conf.CheckFunc('printf'):
   7     print('!! Your compiler and/or environment is not correctly configured.')
   8     Exit(0)

Now we can run some checks for headers.

   1 # check for stdio.h
   2 if not config.CheckHeader('stdio.h'):
   3    print "You need 'stdio.h' to compile this program"
   4    Exit(1)
   5 
   6 # check for math.h
   7 if not config.CheckHeader('math.h'):
   8    print "You need 'math.h' to compile this program"
   9    Exit(1)
  10 else:
  11   config.env.Append('-DHAS_MATH')
  12 
  13 # check for stdlib.h
  14 if not config.CheckCHeader('stdlib.h'):
  15    print "You need 'stdlib.h' to compile this program"
  16    Exit(1)

We can also check for libraries.

   1 # check for libSDL.so
   2 if not config.CheckLib('libSDL'):
   3    print "You need libSDL to compile this program"
   4    Exit(1)

You can store the results of checks as variables in the environment :

   1 if not conf.CheckLib('openal'):
   2     conf.env['haveopenal'] = "no"

When we are all finished we call Finish() which returns an Environment. Don't run any more checks after a call to Finish(). Any changes to the environment not made through Configure(env).env after the Configure() object was created and before Finish() was called will be lost, so don't change the environment after you run Configure() until you have Finish()ed.

   1 # we are all done, don't run any checks after this
   2 env = config.Finish()

Configurations are also often a good place to allow the user to define custom build flags without importing the entire environment :

   1 # ---- check for environment variables
   2 if 'CXX' in os.environ:
   3     conf.env.Replace(CXX = os.environ['CXX'])
   4     print(">> Using compiler " + os.environ['CXX'])
   5 
   6 if 'CXXFLAGS' in os.environ:
   7     conf.env.Append(CCFLAGS = os.environ['CXXFLAGS'])
   8     print(">> Appending custom build flags : " + os.environ['CXXFLAGS'])
   9     
  10 if 'LDFLAGS' in os.environ:
  11     conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
  12     print(">> Appending custom link flags : " + os.environ['LDFLAGS'])

When to run

You may want to check if you are cleaning before you start configuring.

   1 # are we configuring?
   2 if env.GetOption('clean'):
   3    print "We are cleaning, skip the config"
   4 else:
   5    print "We are staying dirty, let's go!"

How can you skip the config step if which targets should be built (and therefore also cleaned) is determined during the configure step?

If you want to do the configure only once you can check for the conf.log file which is created after each configure.

   1 if not os.path.exists("config.log"):
   2         print 'Configuring... '
   3         conf = Configure(env)
   4         #do your checks...
   5         env = conf.Finish()

note: The above does not work if you are using Configure to auotoadd the locations of your libs to LIBS, also I suspect the above will not work if the configuration failed.

If you'd rather have a autotools-like 'configure' step done first and once by calling 'scons configure', then call 'scons' to build, do a check like this :

   1 if 'configure' in COMMAND_LINE_TARGETS:

in order to have the results of such a configured step be remembered, check article SavingVariablesToAFile.

More documentation

The SConf functions of scons are documented in the scons user guide.

More (very detailed) information can be found under the "Configure Contexts" section of the scons manpage. There are also instructions on how to write your own checks there.

Other autocong macros have been contributed in scons format in article AutoconfRecipes

SconsAutoconf (last edited 2009-09-05 16:52:48 by MarianneGagnon)