Scons

From Omnia
Jump to navigation Jump to search

scons

SCons: A software construction tool - http://www.scons.org/

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.

-

SCons - Wikipedia - http://en.wikipedia.org/wiki/SCons

SCons is a computer software construction tool that automatically analyzes source code file dependencies and operating system adaptation requirements from a software project description and generates final binary executables for installation on the target operating system platform. Its function is analogous to the traditional GNU build system based on the make utility and the autoconf tools.

SCons uses the Python general purpose programming language as a foundation, so that all software project configurations and build process implementations are Python scripts.

  • Configuration files are Python scripts, which means that user-written builds have access to a complete general-purpose programming language.
  • Automatic dependency analysis built-in for C, C++ and Fortran. Dependency analysis is extensible through user-defined dependency scanners for other languages or file types. Unlike the GNU Compiler Collection's (GCC) built-in dependency analysis, it uses a regular expression scan for included source files.
  • Built-in support for C, C++, D, Java, Fortran, Objective-C, Yacc, Lex, Qt and SWIG, as well as TeX and LaTeX documents. Other languages or file types can be supported through user-defined Builders.
  • Building from central repositories of source code and pre-built targets.
  • Built-in support for fetching source files from revision control systems, such as SCCS, RCS, CVS, Subversion, BitKeeper and Perforce.
  • Built-in support for Microsoft Visual Studio, including generation of .dsp, .dsw, .sln and .vcproj files.
  • Detection of file content changes using MD5 signatures; optional, configurable support for traditional timestamps.
  • Support for parallel builds which maintains a specified number of jobs running simultaneously regardless of directory hierarchy.
  • Integrated Autoconf-like support for finding #include files, libraries, functions and typedefs.
  • Global view of all dependencies, so multiple build passes or reordering targets is not required.
  • Ability to share built files in a cache to speed up multiple builds - like ccache but for any type of target file, not just C/C++ compilation.
  • Designed from the ground up for cross-platform builds, and known to work on POSIX systems (including GNU/Linux, IBM AIX and OS/2, *BSD Unices, HP-UX, SGI IRIX, Solaris), MS Windows NT, Apple OS X.

usage

Bulding SConscript

scons

Cleaning Up After a Build:

scons -c

Bulding quiet:

scons -Q

documentation

SCons User Guide 2.3.0 (single file) - http://www.scons.org/doc/HTML/scons-user.html

SCons User Guide 2.3.0 - http://www.scons.org/doc/HTML/scons-user/index.html

install

RPMForge

yum --enablerepo rpmforge install scons

Manual 2.3

# Dependency - install Python

#SVER=2.3.0
SVER=2.3.1
mkdir -p ~/.src ; cd ~/.src
#wget http://sourceforge.net/projects/scons/files/scons/$SVER/scons-$SVER.tar.gz/download
wget http://sourceforge.net/projects/scons/files/scons/$SVER/scons-$SVER.tar.gz/download -O scons-$SVER.tar.gz
tar -zvxf scons-$SVER.tar.gz
cd scons-$SVER
python setup.py install

Manual 2.1.0

# Dependency - install Python
mkdir -p ~/.src ; cd ~/.src
wget http://sourceforge.net/projects/scons/files/scons/2.1.0/scons-2.1.0.tar.gz/download
tar -zvxf scons-2.1.0.tar.gz
cd scons-2.1.0
python setup.py install

Manual 1.3.1

# Dependency - install Python
mkdir -p ~/.src ; cd ~/.src
wget http://prdownloads.sourceforge.net/scons/scons-1.3.1.tar.gz
tar -zvxf scons-1.3.1.tar.gz
cd scons-1.3.1
python setup.py install

Test Installation

echo "print 'Hello World'" > SConstruct
scons

Notes

Some basic instructions: [1] 1- to compile, type 'scons prefix=/foo/bar' 2- to run unit tests, type 'scons check' 3- to install, type 'scons install' 4- to uninstall, type 'scons -c install' 5- to make a new tarball, type 'scons dist

code

help

Text to display when scons called with '-h'

# scons -h
Help("""
       Type: 'scons program' to build the production program,
             'scons debug' to build the debug version.
       """)
env = Environment()

Help("\nType: 'scons program' to build the production program.\n")

if env['PLATFORM'] == 'win32':
    Help("\nType: 'scons windebug' to build the Windows debug version.\n")

command line arguments

  • Options - Command-line options always begin with one or two - (hyphen) characters. SCons provides ways for you to examine and set options values from within your SConscript files, as well as the ability to define your own custom options.
  • Variables - Any command-line argument containing an = (equal sign) is considered a variable setting with the form variable=value. SCons provides direct access to all of the command-line variable settings, the ability to apply command-line variable settings to construction environments, and functions for configuring specific types of variables (Boolean values, path names, etc.) with automatic validation of the user's specified values.
  • Targets - Any command-line argument that is not an option or a variable setting (does not begin with a hyphen and does not contain an equal sign) is considered a target that the user (presumably) wants SCons to build. A list of Node objects representing the target or targets to build. SCons provides access to the list of specified targets, as well as ways to set the default list of targets from within the SConscript files.

--- variable=value

# scons fail=1

# Make the build fail if we pass fail=1 on the command line
if ARGUMENTS.get('fail', 0):
    Command('target', 'source', ['/bin/false'])


# scons -Q debug=0

env = Environment()
debug = ARGUMENTS.get('debug', 0)
if int(debug):
    env.Append(CCFLAGS = '-g')
env.Program('prog.c')

--- env['option']=value

Options:

import os
num_cpu = int(os.environ.get('NUM_CPU', 2))
SetOption('num_jobs', num_cpu)
print "running with -j", GetOption('num_jobs')
export NUM_CPU="4"
scons
scons -Q -j 3

The above snippet of code sets the value of the --jobs option to the value specified in the $NUM_CPU environment variable. (This is one of the exception cases where the string is spelled differently from the from command-line option. The string for fetching or setting the --jobs value is num_jobs for historical reasons.)

--- --option=value

AddOption('--prefix',
          dest='prefix',
          type='string',
          nargs=1,
          action='store',
          metavar='DIR',
          help='installation prefix')

env = Environment(PREFIX = GetOption('prefix'))

installed_foo = env.Install('$PREFIX/usr/bin', 'foo.in')
Default(installed_foo)

# scons -Q -n --prefix=/tmp/install

command line targets

# scons -Q bar

if 'bar' in COMMAND_LINE_TARGETS:
    print "Don't forget to copy `bar' to the archive!"
Default(Program('foo.c'))
Program('bar.c')

examples

hello world

Simple Builds - http://www.scons.org/doc/HTML/scons-user/c258.html

# cat > hello.c << "EOF"
#include <stdio.h>
int main() {
  printf("Hello\n");
}
EOF

# cat > SConstruct << "EOF"
Program('hello.c')
EOF

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

More Examples

Example #1:

Program('hello.c')

Example #2:

env = Environment()
env.Append(CPPFLAGS=['-Wall','-g'])
env.Program('hello',
           ['hello.c', 'main.c'])

Example #3:

env = Environment()
env.Program(target='bar', source=['foo.c'])

Example #4:

print "Calling Program('hello.c')"
Program('hello.c')
print "Calling Program('goodbye.c')"
Program('goodbye.c')
print "Finished calling Program()"

Example #5:

Program(['prog.c', 'file1.c', 'file2.c'])
Program('program', ['prog.c', 'file1.c', 'file2.c'])
common_sources = ['file1.c', 'file2.c']
Program('program1', common_sources + ['program1.c'])
Program('program2', common_sources + ['program2.c'])
Program('program', Split('main.c file1.c file2.c'))
src_files = Split('main.c file1.c file2.c')
Program(target = 'program', source = src_files)

Example #6: (Glob)

Program('program', Glob('*.c')

Library:

Library('foo', ['f1.c', 'f2.c', 'f3.c'])
StaticLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])
Library('foo', ['f1.c', 'f2.c', 'f3.c'])
Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.'
Program('prog.c', LIBS=['foo'], LIBPATH='.')
Program('prog.c', LIBS = 'm', LIBPATH = ['/usr/lib', '/usr/local/lib'])

Environment:

env = Environment()
print "CC is:", env['CC']
import os
env = Environment(CC = 'gcc', CCFLAGS = '-O2')
env.Program('foo.c')
env = Environment(FOO = 'foo', BAR = 'bar')
dict = env.Dictionary()
for key in ['OBJSUFFIX', 'LIBSUFFIX', 'PROGSUFFIX']:
    print "key = %s, value = %s" % (key, dict[key])
env = Environment()
for item in sorted(env.Dictionary().items()):
    print "construction variable = '%s', value = '%s'" % item