SCons is a replacement for the make utility. As the make utility looks for a Makefile, scons looks for an SConstruct file by default. The format for the SConstruct file is a python script. Note that you do not have to know Python for basic operation of this build tool.
See SconsProcessOverview for a high level view of SCons processing.
When running scons it will automatically import an environment that makes an Environment class available. You must first instantiate this class:
env = Environment()
This sets up a basic environment. Afterwards, you can set up targets.
env.Program(target='bar', source=['foo.c'])
This will make a program 'bar' from 'foo.c'.
For more complex programs you must set up a more specialized environment. For example, setting up the flags the compiler will use, include directories, etc.
To do that you can specify named parameters such as CCFLAGS for C files or CPPFLAGS for the C++ Preprocessor. More of these can be seen in the Configuration File Reference section of the man page.
env = Environment(CCFLAGS='-O3')
Some parameters require specific lists, such as the source list. Reading the Configuration File Reference should be very helpful.
An important note is the Default command. It tells scons what to build by default. It will not build anything unless you specify a target otherwise.
Default( env.Program(target='bar', source=['foo.c']) )
Steven Knight (author of SCons) recommends that you use the return value from your target as the default value instead of specifying the name like you do in the target.
He explains it best: "The only stylistic suggestion I'll make is that if you use the object returned by env.Program as the input to Default(), then you'll be more portable, since you won't have to worry about whether the generated program will have a .exe suffix or not."
Another way to do this is to use Python:
t = env.Program(target='bar', source=['foo.c']) Default(t)
You can call Default many times to add to the default target list.
Quite a bunch of SCons techniques, including the use of SConf, can be seen in the http://madman.sf.net madman build scripts.
To avoid struggle, make sure you create at least two scripts - SConstruct and SConscript. This will allow you to build into a separate folder from your source files, and eventually this will allow you to harness more of the power of scons. Example: SConscript('SConscript', build_dir='.build', duplicate=0) And then you put all the usual stuff from the examples inside SConscript instead of SConstruct. I was hammering at this for an hour trying to do it all in one file, but the build_dir functionality just wouldn't cooperate until I did this, now I'm in heaven. Now that you've done it this way, you can pass some values into your SConscript and call it multiple times:
SConscript('SConscript', build_dir='.build_release', duplicate=0, exports={'MODE':'release'})
SConscript('SConscript', build_dir='.build_debug', duplicate=0, exports={'MODE':'debug'})
Common tasks
A few common tasks are shown below. Note that, although these examples mostly use 'Append', you can also specify the same information by using the same flags when calling e.g. Program. Add flags from a config
env.ParseConfig( 'pkg-config --cflags glib-2.0' )
Add header search path
env.Append(CPPPATH = ['/usr/local/include/'])
Add compile-time flags
env.Append(CCFLAGS = ['-g -O3'])
Add define
env.Append(CPPDEFINES=['BIG_ENDIAN'])
Add define with value (e.g. -DRELEASE_BUILD=1)
env.Append(CPPDEFINES={'RELEASE_BUILD' : '1'})Add library search path
env.Append(LIBPATH = ['/usr/local/lib/'])
Add libraries to link against
env.Append(LIBS = ['SDL_image','GL'])
Link time flags
env.Append(LINKFLAGS = ['-Wl,--rpath,/usr/local/lib/'])
Building a more complex program that the example outlined above can be done the following way :
sources = Split("""
main.cpp
utils.cpp
gui.cpp
""")
object_list = env.Object(source = sources)
env.Program( target = 'a.out', source = object_list )