Skip to content


Billy Donahue edited this page Mar 15, 2019 · 2 revisions

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, You could check for these in your build like this.

First you must create your configuration environment.

conf = Configure(env)

# ... use conf to perform checks here ...

# keep any changes you may have performed
env = conf.Finish()

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

# ---- check for compiler and do sanity checks
if not conf.CheckCXX():
    print('!! Your compiler and/or environment is not correctly configured.')

if not conf.CheckFunc('printf'):
    print('!! Your compiler and/or environment is not correctly configured.')

Now we can run some checks for headers.

# check for stdio.h
if not config.CheckHeader('stdio.h'):
   print "You need 'stdio.h' to compile this program"

# check for math.h
if not config.CheckHeader('math.h'):
   print "You need 'math.h' to compile this program"

# check for stdlib.h
if not config.CheckCHeader('stdlib.h'):
   print "You need 'stdlib.h' to compile this program"

We can also check for libraries.

# check for
if not config.CheckLib('libSDL'):
   print "You need libSDL to compile this program"

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

if not conf.CheckLib('openal'):
    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.

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

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

# ---- check for environment variables
if 'CXX' in os.environ:
    conf.env.Replace(CXX = os.environ['CXX'])
    print(">> Using compiler " + os.environ['CXX'])

if 'CXXFLAGS' in os.environ:
    conf.env.Append(CCFLAGS = os.environ['CXXFLAGS'])
    print(">> Appending custom build flags : " + os.environ['CXXFLAGS'])
if 'LDFLAGS' in os.environ:
    conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
    print(">> Appending custom link flags : " + os.environ['LDFLAGS'])

When to run

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

# are we configuring?
if env.GetOption('clean'):
   print "We are cleaning, skip the config"
   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.

if not os.path.exists("config.log"):
        print 'Configuring... '
        conf = Configure(env)
        #do your checks...
        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 :

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 autoconf macros have been contributed in scons format in article AutoconfRecipes

Clone this wiki locally
You can’t perform that action at this time.