Scons - Introduction

Purpose

SCons is a tool equivalent to Makefile (but better). It is configured by simple Python code.

From the authors:

SCons is an Open Source software construction tool—that is, a next-generation build tool. Think of SCons as an improved, cross-platform substitute for the classic Make utility with integrated functionality similar to autoconf/automake nd compiler caches such as ccache. In short, SCons is an easier, more reliable and faster way to build software.

— scons.org

Features

It is working best with C/C++ language but can also works out of the box with:

  • Java

  • Latex (to PDF)

  • DocBook

  • Archives (tar, zip)

  • Text files

  • etc.

The list of builders can be found here and the list of tools it uses can be found here.

Additionally, any command can easily be added via the Command Builder.

Some of the highlights of this tool:

  • Configuration files are Python scripts—​use the power of a real programming language to solve build problems.

  • Reliable, automatic dependency analysis built-in for C, C++ and Fortran

  • Built-in support for C, C++, D, Java, Fortran, Yacc, Lex, Qt and SWIG, and building TeX and LaTeX documents.

  • Easily extensible through user-defined Builders for other languages or file types.

  • Reliable detection of build changes using MD5 signatures; optional, configurable support for traditional timestamps.

  • Integrated Autoconf-like support for finding #include files, libraries, functions and typedefs.

  • Global view of all dependencies

Installation

On Windows:

  1. Install Python

  2. Download Windows installer from: http://scons.org/pages/download.html

  3. Run the installer

  4. Check SCons has been installed successfully: scons --version

Although pip can find scons, it cannot install it. What a pity!
SCons is not (yet) compatible with Python 3.x

Another way to install SCons is to build from the sources.

  1. Download the sources (.zip or .tar.gz)

  2. Unzip somewhere and open a command line in that directory

  3. Install with Python: python setup.py install

  4. Check the installation: scons --version

If you have an issue with installation, please check FAQ.

A small example

The source file:

hello.c
int main()
{
    printf("Hello, world!\n");
    return 0;
}

The building program (which is using Python syntax):

SConstruct
Program('hello.c')

The command to run (without the prompt $):

$ scons

And that’s it: this will create the program built from hello.c.

For information, it will display the following on the console:

scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
cc -o hello.o -c hello.c
cc -o hello hello.o
scons: done building targets.

And if you want to do a clean-up, SCons will automatically decide what needs to be done. You just need to call with -c option (or --clean if you prefer).

$ scons -c

FAQ

"Command not found" when running scons

First, install SCons with python setup.py install method.

Then, go to your Python installation directory and put either of the following files in subfolder Scripts:

  • scons.bat if you run scons from cmd

  • scons if you run scons from Msys or Cygwin

<Python_dir>/Scripts/scons.bat
@REM Copyright (c) 2001 - 2017 The SCons Foundation
@REM src/script/scons.bat rel_3.0.0:4395:8972f6a2f699 2017/09/18 12:59:24 bdbaddog
@echo off
set SCONS_ERRORLEVEL=
if "%OS%" == "Windows_NT" goto WinNT

@REM for 9x/Me you better not have more than 9 args
python -c "from os.path import join; import sys; sys.path = [ join(sys.prefix, 'Lib', 'site-packages', 'scons-3.0.0'), join(sys.prefix, 'Lib', 'site-packages', 'scons'), join(sys.prefix, 'scons-3.0.0'), join(sys.prefix, 'scons')] + sys.path; import SCons.Script; SCons.Script.main()" %1 %2 %3 %4 %5 %6 %7 %8 %9
@REM no way to set exit status of this script for 9x/Me
goto endscons

@REM Credit where credit is due:  we return the exit code despite our
@REM use of setlocal+endlocal using a technique from Bear's Journal:
@REM http://code-bear.com/bearlog/2007/06/01/getting-the-exit-code-from-a-batch-file-that-is-run-from-a-python-program/

:WinNT
setlocal
@REM ensure the script will be executed with the Python it was installed for
set path=%~dp0;%~dp0..;%path%
@REM try the script named as the .bat file in current dir, then in Scripts subdir
set scriptname=%~dp0%~n0.py
if not exist "%scriptname%" set scriptname=%~dp0Scripts\%~n0.py
python "%scriptname%" %*
endlocal & set SCONS_ERRORLEVEL=%ERRORLEVEL%

if NOT "%COMSPEC%" == "%SystemRoot%\system32\cmd.exe" goto returncode
if errorlevel 9009 echo you do not have python in your PATH
goto endscons

:returncode
exit /B %SCONS_ERRORLEVEL%

:endscons
call :returncode %SCONS_ERRORLEVEL%
<Python_dir>/Scripts/scons
#!/bin/bash

# Script to launch scons from MSys or Cygwin
# Inspired by scons.bat delivered with SCons

# Ensure the script will be executed with the Python it was installed for
BASEDIR=$(dirname "$0")
PATH="$BASEDIR:$BASEDIR/..:$PATH"

# Try the script named as the .bat file in current dir, then in Scripts subdir
BASENAME=$(basename "$0")
SCRIPT=${BASENAME%.*}
SCRIPTNAME="$BASEDIR/$SCRIPT.py"
if ! [ -f "$SCRIPTNAME" ]; then
	SCRIPTNAME="$BASEDIR/Scripts/$SCRIPT.py"
fi

# Run
python "$SCRIPTNAME" $@
SCONS_ERRORLEVEL=$?

# Check error code
if [ SCONS_ERRORLEVEL == 9009 ]; then
	echo "You do not have python in your PATH"
fi

# End
exit $SCONS_ERRORLEVEL
Share Comments
comments powered by Disqus