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.
A good first check (that autotools usually does first) is to check that the compiler is installed and working
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.
You can store the results of checks as variables in the environment :
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.
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.
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.
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.
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