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.
Print Dependencies Tree
Use the following to print the dependency tree.
Use the following to check the preparation process.
Include path setting
Include paths (INCLUDE PATH) must be added to the CPPPATH variable.
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.
Or apply it
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.
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.
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.
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.
See also
Favorite site
Documentation
- SCons man page
- SCons user guide 1
- Scons Vs Other Build Tools
- Scons Tutorial 1
- Platform Specific Notes
Guide
- Build tool Scons (ko)
- How to get executable in debug mode using scons
- Integrating scons into IDEs
- Here are some tips for using SCons with Mac OSX
- SConstruct usage
- SCons / InstallTargets
- [Recommended] SCons / SconsProcessOverview
- [Recommended] SCons Design and Implementation
References
-
Scons.org-SCons_2.2.0_User_Guide.zip ↩