Skip to content
Search

SCons

lastmod 2018-08-17created 2015-02-1545 edits

SCons is an open-source software build tool. SCons is a replacement for the classic Make utility that integrates the functionality of autoconf/automake and compiler caches such as ccache. Compared to earlier tools, SCons aims to be easier to use, more reliable, and faster. SCons is a Python-based build tool.

Category

Command-line buind options

To pass a debugging option to GCC when entering something like scons debug=1 on the command line, do the following.

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

Custom builder

Example

Cleaning Up After a Build

To clean the generated files, add the -c option to the command-line options.

$ scons -c

Use the following to print the dependency tree.

$ scons -Q --tree=all

Use the following to check the preparation process.

$ scons --debug=prepare

Include path setting

Include paths (INCLUDE PATH) must be added to the CPPPATH variable.

env = Environment(CPPPATH='inc')
env.Program('foo', 'foo.c')

Note that entering the flag directly, as in Environment(CCFLAGS='-Iinc'), is not recommended.

use the Object

If compile options differ per Object, use the following.

import os
env = Environment(ENV=os.environ, tools=['mingw'])
obj1 = env.Object('fun.o', 'fun.c', CC='gcc', CPPDEFINES='TEST2', CCFLAGS=['-l.', '-Winline'])
obj2 = env.Object('fun2.o', 'fun2.cpp')
obj3 = env.Object('main.o', 'main.cpp', CXX='g++', CPPDEFINES='TEST', CCFLAGS='-Winline', CXXFLAGS='-O2 -l.')
env.Program('result', source = [obj1, obj2, obj3])

Define setting

To add a Define, add it as follows.

env = Environment()
env.Append(CPPDEFINES=['MAX(x,y)=(x>y ? x:y)'])
env.Program(target = 'main', source = 'main.cc')

As with CPPPATH above, entering the flag directly, as in Environment(CCFLAGS='-Iinc'), is not recommended. And if you want to add only one, use AppendUnique.

Note that to add to the front of the Defines, use Prepend or PrependUnique.

check for environment variables

# ---- check for environment variables
if 'CXX' in os.environ:
    conf.env.Replace(CXX = os.environ['CXX'])
    print(">> Using compiler " + os.environ['CXX'])

if 'CXXFLAGS' in os.environ:
    conf.env.Append(CCFLAGS = os.environ['CXXFLAGS'])
    print(">> Appending custom build flags : " + os.environ['CXXFLAGS'])

if 'LDFLAGS' in os.environ:
    conf.env.Append(LINKFLAGS = os.environ['LDFLAGS'])
    print(">> Appending custom link flags : " + os.environ['LDFLAGS'])

Explicit Dependencies

To specify dependencies explicitly, apply the following.

hello = Program('hello.c')
Depends(hello, 'other_file')

Or apply it

hello = Program('hello.c')
goodbye = Program('goodbye.c')
Depends(hello, goodbye)

as above.

Use PkgConfig

To apply pkg-config, use the following.

env = Environment()
# add support for GTK
env.ParseConfig('pkg-config --cflags --libs gtk+-2.0')
env.Program('toaster-test.c')

User define command

An example of a user-defined command is as follows.

NVCC_CMD='nvcc -o $TARGET -c $SOURCE'
env = Environment(ENV=os.environ)
env.Command('gpu.o', 'gpu.cu', NVCC_CMD)
env.Program('result', source = ['gpu.o'])

Change suffix name

To change the suffix name (extension), use Replace. Likewise, to change the soname, apply the following.

Replace(SHLIBSUFFIX = '.so.$SHLIB_VERSION')
SharedLibrary('hello', 'hello.c', SHLIB_VERSION = 1)

Troubleshooting

A summary of problems that can occur while using SCons.

Two environments with different actions were specified for the same target

The following error message may be printed.

scons: *** Two environments with different actions were specified for the same target

To avoid this problem, we must explicitly specify that each environment compile foo.c to a separately-named object file using the Object builder, like so:

opt = Environment(CCFLAGS = '-O2')
dbg = Environment(CCFLAGS = '-g')

o = opt.Object('foo-opt', 'foo.c')
opt.Program(o)

d = dbg.Object('foo-dbg', 'foo.c')
dbg.Program(d)

ImportError SCons.Script

Running scons may produce the following error.

Traceback (most recent call last):
  File "/usr/local/bin/scons", line 188, in <module>
    import SCons.Script
ImportError: No module named SCons.Script

This problem occurs because Python's import cannot find the path properly.

If you see this on MAC OSX, add the following path.

export PYTHONPATH=/Library/Python/2.7/site-packages/scons-2.3.0/:

Is not compatible with shared target

The following message is printed during the build.

scons: *** Source file: mylib/libMine.a is static and is not compatible with shared target: libstuff.so

In this case, set the following on the SCons:Environment object in the SConstruct file.

env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME'] = 1

See also

Favorite site

Documentation

Guide

References


  1. Scons.org-SCons_2.2.0_User_Guide.zip ↩