Compiling Applications with SCons

Studying how to compile the source code of Quake 3, I discovered a most interesting tool called SCons. It is a build system that replaces autoconf, automake and other GNU project tools.

One of the advantages SCons offers, beyond its simplicity, is that it provides a cross-platform build environment. Using SConts, it’s sufficient to just write a single file with the instructions to build the program (the SContruct file is the equivalent to a Makefile) and SCons takes care of translating our instructions into the appropriate commands for the system we’re building, for example, invoking GCC on Linux/UNIX or cl (Visual C++’s compiler) on MS Windows.

Another advantage of SCons is that files are in fact Python scripts, therefore we can use this excellent programming language to solve our build problems, no matter how complicated they might seem.

The SCons project has very good documentation. It suffices to read the (very user friendly) online manual to translate a Makefile with a SCons file. It’s incredibly simple. Let’s take a look at an example.

Here’s the Makefile I wrote some time ago to compile my raytracer on Fedora Linux:

    CXXFLAGS=-Wall -O3
    OBJECTS=color.o point3d.o myutils.o esfera.o light.o raytracer.o

    %.o: %.cpp
    g++ -c $(CXXFLAGS) $< -o $@

    all: $(OBJECTS)
    g++ $(OBJECTS) $(CXXLIBS) -o $(APP)

    rm -f *.o $(APP)

Now, this is the SConstruct file (written by someone who didn’t know Scons 1 hour ago):

Program(“raytracer”, Split(“color.cpp point3d.cpp myutils.cpp esfera.cpp light.cpp raytracer.cpp”), LIBS=”SDL”, CCFLAGS=[“-O3”, “-Wall”])

It’s truly remarkable. SCons tracks dependencies between source code files and automatically provides the commands to clean the build artifacts.

SCons is free under an MIT license and can be downloaded freely. On Fedora, you can install it using yum.