SCons 4.0.1

User Guide

Steven Knight and the SCons Development Team

Steven Knight

version 4.0.1

SCons User's Guide Copyright (c) 2004-2020 The SCons Foundation

2004 - 2020

Table of Contents

1. SCons Principles
2. A Caveat About This Guide's Completeness
3. Acknowledgements
4. Contact
1. Building and Installing SCons
1.1. Installing Python
1.2. Installing SCons
1.3. Building and Installing SCons on Any System
1.3.1. Building and Installing Multiple Versions of SCons Side-by-Side
1.3.2. Installing SCons in Other Locations
1.3.3. Building and Installing SCons Without Administrative Privileges
2. Simple Builds
2.1. Building Simple C / C++ Programs
2.2. Building Object Files
2.3. Simple Java Builds
2.4. Cleaning Up After a Build
2.5. The SConstruct File
2.5.1. SConstruct Files Are Python Scripts
2.5.2. SCons Functions Are Order-Independent
2.6. Making the SCons Output Less Verbose
3. Less Simple Things to Do With Builds
3.1. Specifying the Name of the Target (Output) File
3.2. Compiling Multiple Source Files
3.3. Making a list of files with Glob
3.4. Specifying Single Files Vs. Lists of Files
3.5. Making Lists of Files Easier to Read
3.6. Keyword Arguments
3.7. Compiling Multiple Programs
3.8. Sharing Source Files Between Multiple Programs
3.9. Overriding construction variables when calling a Builder
4. Building and Linking with Libraries
4.1. Building Libraries
4.1.1. Building Libraries From Source Code or Object Files
4.1.2. Building Static Libraries Explicitly: the StaticLibrary Builder
4.1.3. Building Shared (DLL) Libraries: the SharedLibrary Builder
4.2. Linking with Libraries
4.3. Finding Libraries: the $LIBPATH Construction Variable
5. Node Objects
5.1. Builder Methods Return Lists of Target Nodes
5.2. Explicitly Creating File and Directory Nodes
5.3. Printing Node File Names
5.4. Using a Node's File Name as a String
5.5. GetBuildPath: Getting the Path From a Node or String
6. Dependencies
6.1. Deciding When an Input File Has Changed: the Decider Function
6.1.1. Using MD5 Signatures to Decide if a File Has Changed
6.1.2. Using Time Stamps to Decide If a File Has Changed
6.1.3. Deciding If a File Has Changed Using Both MD Signatures and Time Stamps
6.1.4. Writing Your Own Custom Decider Function
6.1.5. Mixing Different Ways of Deciding If a File Has Changed
6.2. Implicit Dependencies: The $CPPPATH Construction Variable
6.3. Caching Implicit Dependencies
6.3.1. The --implicit-deps-changed Option
6.3.2. The --implicit-deps-unchanged Option
6.4. Explicit Dependencies: the Depends Function
6.5. Dependencies From External Files: the ParseDepends Function
6.6. Ignoring Dependencies: the Ignore Function
6.7. Order-Only Dependencies: the Requires Function
6.8. The AlwaysBuild Function
7. Environments
7.1. Using Values From the External Environment
7.2. Construction Environments
7.2.1. Creating a Construction Environment: the Environment Function
7.2.2. Fetching Values From a Construction Environment
7.2.3. Expanding Values From a Construction Environment: the subst Method
7.2.4. Handling Problems With Value Expansion
7.2.5. Controlling the Default Construction Environment: the DefaultEnvironment Function
7.2.6. Multiple Construction Environments
7.2.7. Making Copies of Construction Environments: the Clone Method
7.2.8. Replacing Values: the Replace Method
7.2.9. Setting Values Only If They're Not Already Defined: the SetDefault Method
7.2.10. Appending to the End of Values: the Append Method
7.2.11. Appending Unique Values: the AppendUnique Method
7.2.12. Appending to the Beginning of Values: the Prepend Method
7.2.13. Prepending Unique Values: the PrependUnique Method
7.3. Controlling the Execution Environment for Issued Commands
7.3.1. Propagating PATH From the External Environment
7.3.2. Adding to PATH Values in the Execution Environment
7.4. Using the toolpath for external Tools
7.4.1. The default tool search path
7.4.2. Providing an external directory to toolpath
7.4.3. Nested Tools within a toolpath
7.4.4. Using sys.path within the toolpath
7.4.5. Using the PyPackageDir function to add to the toolpath
8. Automatically Putting Command-line Options into their Construction Variables
8.1. Merging Options into the Environment: the MergeFlags Function
8.2. Separating Compile Arguments into their Variables: the ParseFlags Function
8.3. Finding Installed Library Information: the ParseConfig Function
9. Controlling Build Output
9.1. Providing Build Help: the Help Function
9.2. Controlling How SCons Prints Build Commands: the $*COMSTR Variables
9.3. Providing Build Progress Output: the Progress Function
9.4. Printing Detailed Build Status: the GetBuildFailures Function
10. Controlling a Build From the Command Line
10.1. Command-Line Options
10.1.1. Not Having to Specify Command-Line Options Each Time: the SCONSFLAGS Environment Variable
10.1.2. Getting Values Set by Command-Line Options: the GetOption Function
10.1.3. Setting Values of Command-Line Options: the SetOption Function
10.1.4. Strings for Getting or Setting Values of SCons Command-Line Options
10.1.5. Adding Custom Command-Line Options: the AddOption Function
10.2. Command-Line variable=value Build Variables
10.2.1. Controlling Command-Line Build Variables
10.2.2. Providing Help for Command-Line Build Variables
10.2.3. Reading Build Variables From a File
10.2.4. Pre-Defined Build Variable Functions
10.2.5. Adding Multiple Command-Line Build Variables at Once
10.2.6. Handling Unknown Command-Line Build Variables: the UnknownVariables Function
10.3. Command-Line Targets
10.3.1. Fetching Command-Line Targets: the COMMAND_LINE_TARGETS Variable
10.3.2. Controlling the Default Targets: the Default Function
10.3.3. Fetching the List of Build Targets, Regardless of Origin: the BUILD_TARGETS Variable
11. Installing Files in Other Directories: the Install Builder
11.1. Installing Multiple Files in a Directory
11.2. Installing a File Under a Different Name
11.3. Installing Multiple Files Under Different Names
11.4. Installing a Shared Library
12. Platform-Independent File System Manipulation
12.1. Copying Files or Directories: The Copy Factory
12.2. Deleting Files or Directories: The Delete Factory
12.3. Moving (Renaming) Files or Directories: The Move Factory
12.4. Updating the Modification Time of a File: The Touch Factory
12.5. Creating a Directory: The Mkdir Factory
12.6. Changing File or Directory Permissions: The Chmod Factory
12.7. Executing an action immediately: the Execute Function
13. Controlling Removal of Targets
13.1. Preventing target removal during build: the Precious Function
13.2. Preventing target removal during clean: the NoClean Function
13.3. Removing additional files during clean: the Clean Function
14. Hierarchical Builds
14.1. SConscript Files
14.2. Path Names Are Relative to the SConscript Directory
14.3. Top-Level Path Names in Subsidiary SConscript Files
14.4. Absolute Path Names
14.5. Sharing Environments (and Other Variables) Between SConscript Files
14.5.1. Exporting Variables
14.5.2. Importing Variables
14.5.3. Returning Values From an SConscript File
15. Separating Source and Build Directories
15.1. Specifying a Variant Directory Tree as Part of an SConscript Call
15.2. Why SCons Duplicates Source Files in a Variant Directory Tree
15.3. Telling SCons to Not Duplicate Source Files in the Variant Directory Tree
15.4. The VariantDir Function
15.5. Using VariantDir With an SConscript File
15.6. Using Glob with VariantDir
16. Variant Builds
17. Internationalization and localization with gettext
17.1. Prerequisites
17.2. Simple project
18. Writing Your Own Builders
18.1. Writing Builders That Execute External Commands
18.2. Attaching a Builder to a Construction Environment
18.3. Letting SCons Handle The File Suffixes
18.4. Builders That Execute Python Functions
18.5. Builders That Create Actions Using a Generator
18.6. Builders That Modify the Target or Source Lists Using an Emitter
18.7. Where To Put Your Custom Builders and Tools
19. Not Writing a Builder: the Command Builder
20. Pseudo-Builders: the AddMethod function
21. Writing Scanners
21.1. A Simple Scanner Example
21.2. Adding a search path to a scanner: FindPathDirs
21.3. Using scanners with Builders
22. Building From Code Repositories
22.1. The Repository Method
22.2. Finding source files in repositories
22.3. Finding #include files in repositories
22.3.1. Limitations on #include files in repositories
22.4. Finding the SConstruct file in repositories
22.5. Finding derived files in repositories
22.6. Guaranteeing local copies of files
23. Multi-Platform Configuration (Autoconf Functionality)
23.1. Configure Contexts
23.2. Checking for the Existence of Header Files
23.3. Checking for the Availability of a Function
23.4. Checking for the Availability of a Library
23.5. Checking for the Availability of a typedef
23.6. Checking the size of a datatype
23.7. Checking for the Presence of a program
23.8. Adding Your Own Custom Checks
23.9. Not Configuring When Cleaning Targets
24. Caching Built Files
24.1. Specifying the Shared Cache Directory
24.2. Keeping Build Output Consistent
24.3. Not Using the Shared Cache for Specific Files
24.4. Disabling the Shared Cache
24.5. Populating a Shared Cache With Already-Built Files
24.6. Minimizing Cache Contention: the --random Option
25. Alias Targets
26. Java Builds
26.1. Building Java Class Files: the Java Builder
26.2. How SCons Handles Java Dependencies
26.3. Building Java Archive (.jar) Files: the Jar Builder
26.4. Building C Header and Stub Files: the JavaH Builder
26.5. Building RMI Stub and Skeleton Class Files: the RMIC Builder
27. Miscellaneous Functionality
27.1. Verifying the Python Version: the EnsurePythonVersion Function
27.2. Verifying the SCons Version: the EnsureSConsVersion Function
27.3. Explicitly Terminating SCons While Reading SConscript Files: the Exit Function
27.4. Searching for Files: the FindFile Function
27.5. Handling Nested Lists: the Flatten Function
27.6. Finding the Invocation Directory: the GetLaunchDir Function
27.7. Virtual environments (virtualenvs)
28. Using SCons with other build tools
28.1. Creating a Compilation Database
29. Troubleshooting
29.1. Why is That Target Being Rebuilt? the --debug=explain Option
29.2. What's in That Construction Environment? the Dump Method
29.3. What Dependencies Does SCons Know About? the --tree Option
29.4. How is SCons Constructing the Command Lines It Executes? the --debug=presub Option
29.5. Where is SCons Searching for Libraries? the --debug=findlibs Option
29.6. Where is SCons Blowing Up? the --debug=stacktrace Option
29.7. How is SCons Making Its Decisions? the --taskmastertrace Option
29.8. Watch SCons prepare targets for building: the --debug=prepare Option
29.9. Why is a file disappearing? the --debug=duplicate Option
29.10. Keep it simple
A. Construction Variables
B. Builders
C. Tools
D. Functions and Environment Methods
E. Handling Common Tasks

List of Examples

E.1. Wildcard globbing to create a list of filenames
E.2. Filename extension substitution
E.3. Appending a path prefix to a list of filenames
E.4. Substituting a path prefix with another one
E.5. Filtering a filename list to exclude/retain only a specific set of extensions
E.6. The "backtick function": run a shell command and capture the output
E.7. Generating source code: how code can be generated and used by SCons