introduction to s cons
TRANSCRIPT
02/29/12 Introduction to SCons 1
Introduction to SCons
Steve Christensen
Presented to Utah Python Users Group http://www.utahpython.org
02/29/12 Introduction to SCons 2
What is SCons?
SCons is an Open Source software construction tool
Think of SCons as an improved, cross-platform substitute for the classic Make utility with integrated functionality similar to autoconf/automake and compiler caches such as ccache.
In short, SCons is an easier, more reliable way to build software.
02/29/12 Introduction to SCons 3
SCons Benefits
Both SCons configuration files and user-created extensions to SCons are Python scripts
Global view of dependencies for entire project.
Support for sharing cached build results among developers
02/29/12 Introduction to SCons 4
SCons Benefits (continued)
Improved support for parallel builds -- like ‘make –j’ but keeps N jobs running simultaneously regardless of directory hierarchy.
Reliable detection of build changes using MD5 signatures (signatures track file content as well as command-line options)
02/29/12 Introduction to SCons 5
SCons Benefits (continued)
Reliable, automatic dependency analysis for C, C++, and Fortran. (extendible)
Built-in support for C, C++, D, Java, Fortran, Yacc, Lex, Qt, SWIG, building TeX and LaTeX documents, and many others. (extendible)
Great way for Python to infiltrate your C/C++ work environment
02/29/12 Introduction to SCons 6
SCons Drawbacks Java support isn’t as complete as the C/C++
support Incremental / no-op build performance is bad.
Compared to other tools, SCons can be significantly slower (e.g. 1-2 minute no-op build?)
Recipes on the Wiki / Mail Lists for speed ups
Complex builds require a fair amount of Python code to achieve the functionality built- in to some other build tools (e.g. variant builds ala BoostJam)
02/29/12 Introduction to SCons 7
SCons Basics
At startup, SCons will look for a file named ‘SConstruct’ in the current directory.
SConstruct is a Python script. SCons processes the script to build a dependency graph. Any nodes in the dependency graph that are out-of-date will be rebuilt.
SCons tracks dependencies by creating ‘.sconsign’ files to store build signatures.
02/29/12 Introduction to SCons 8
Example #1: HelloWorld
Simple helloworld.c :
int main() {
printf(“Hello, world!\n”);
}
Simple SConstruct:
Program('helloworld.c')
02/29/12 Introduction to SCons 9
Example #2: Static Libraries
02/29/12 Introduction to SCons 10
Example #2.1 : Shared Libraries
02/29/12 Introduction to SCons 11
Example #3: Builders return Lists
02/29/12 Introduction to SCons 12
Example #3.1 : Why lists?
In addition to file prefix / suffix problems, the same builder may produce a different number of output files on different platforms / compilers.
E.g. SharedLibrary(‘foo’,foo_sources) will return a single-item list on win32/cygwin [<Node: ‘foo.dll’>], and a three-item list on win32/msvc [<Node: ‘foo.dll’>,<Node: ‘foo.lib’>, <Node: ‘foo.exp’>]
02/29/12 Introduction to SCons 13
Example #4: Construction Environments
What if we want to compile the program in multiple ways?
02/29/12 Introduction to SCons 14
Example #4.2: Modifying a Construction Environment
Result:gcc -DFIRST -DDEFINE2 -DLAST -c -o helloworld.o helloworld.cgcc -o helloworld.exe helloworld.o
02/29/12 Introduction to SCons 15
Hierarchical Builds
Source files of large projects are typically organized into a hierarchy of directories. Builds with SCons use a hierarchy of SCons build scripts.
The top-level build script remains named ‘SConstruct’
The lower-level build scripts are conventionally named ‘SConscript’, and are executed via SConscript() method in the calling script(s).
02/29/12 Introduction to SCons 16
Example #5: Heirarchical Builds
02/29/12 Introduction to SCons 17
Example #6: Source vs. Build Directories?
02/29/12 Introduction to SCons 18
Variant Builds
BuildDir is cool, but I want release / debug and win32 / cygwin binaries and objects built into separate trees.
And, I want separate bin/lib/obj directories.
02/29/12 Introduction to SCons 19
Example #7: Variant Builds
02/29/12 Introduction to SCons 20
Example #7: Variant Builds (cont’d)
02/29/12 Introduction to SCons 21
Example #7: Variant Builds (cont’d)
02/29/12 Introduction to SCons 22
Example #8: Caching Built Files
Need a network share with read/write access for all your developers
Add ‘CacheDir(“/my/fancy/bulidcache”)’ to your build script.
Command-line options can be used to enable or disable uploads/downloads from the cache.
02/29/12 Introduction to SCons 23
Example #9: Extending SCons
02/29/12 Introduction to SCons 24
Example #9: Extending SCons
sed 's/x/y/' < baz.in > baz.out
02/29/12 Introduction to SCons 25
Example #9: Extending SCons
sed 's/x/y/' < baz.input > baz.output
02/29/12 Introduction to SCons 26
Example #9: Extending SCons
02/29/12 Introduction to SCons 27
Example #10: Java Builds
02/29/12 Introduction to SCons 28
Example #11: MSVC PDB, PCH, Resources
02/29/12 Introduction to SCons 29
More examples?
Options? Configuration?
02/29/12 Introduction to SCons 30
References
SCons Homepage SCons Wiki Article (Freshmeat.net): Make Alternatives Article (Games from Within): The Quest for
the Perfect Build System