- SCons is a software construction tool that can be used as an alternative to traditional build systems like Make and CMake.
- It is a Python-based build tool that provides a convenient and flexible way to define and manage the build process for software projects, including C++ programs.
- 基于python语言的构建工具,对开发者来说过度自然,简单,no need to learn domain-specific language like cmake
其余cmake有的, Scons 也有。
- cross-paltform,
- SCons has built-in support for dynamic dependency analysis, meaning it can automatically detect changes in source files and rebuild only what's necessary. This can result in faster builds for large projects.
Sconstruct
python file as compile entry
- add option for
scons
command
AddOption('--buildDir',
dest='buildDir',
type='string',
default="build/",
# default=False,
nargs=1,
action='store', # meaning save the string
# or action='store', meaning True or false
metavar='DIR',
help='Base build directory'
)
baseBuildDir = GetOption('buildDir')
- add sub scons config file and build result path using
variant_dir
env.SConscript("src/SConscript", variant_dir=buildDir, exports= {'env' : env.Clone()})
- achive debug mode
using scons debug=1
command.
env = Environment()
debug = ARGUMENTS.get("debug", 0)
if int(debug):
print "in debug mode"
- Define the Build Environment:
In the
SConstruct
file, define the build environment by creating an Environment
object. You can specify compiler options, flags, include paths, library paths, and other build settings within this object.
env = Environment(CXX='g++', CCFLAGS=['-O2', '-Wall'], CPPPATH=['include'], LIBPATH=['lib'])
libEnv = env.Clone()
- Specify Source Files and Targets:
Define the source files for your C++ program and specify the target(s) you want to build using the
Program()
function.
source_files = ['main.cpp', 'util.cpp', 'other.cpp']
# or select the src files
Object('hello.cpp')
program = env.Program(target='my_program', source=source_files)
In this example, main.cpp
, util.cpp
, and other.cpp
are the source files, and my_program
is the name of the target executable.
- static or dynamic lib
# static
Library("t", Glob("src/*.cpp"))
# dynamic
source = Glob("src/*.cpp")
SharedLibrary("t", source)
Program(["hello.cpp"], LIBS=["t"], LIBPATH=".")
-
execute command during compilation
-
this is usually to print info
- The command is executed when any of the specified dependencies (
allSrcs, ".git/index", or "SConstruct"
) change.
env.Command(
target='bar.out',
source='bar.in',
action=["rm -f $TARGET", "$BAR_BUILD < $SOURCES > $TARGET"],
ENV={'PATH': '/usr/local/bin/'},
)
env.Command(
versionFile,
allSrcs + [".git/index" "SConstruct"],
'printf "#define ZSIM_BUILDDATE \\"`date "+%Y-%m-%d %T"`\\"\\n#define ZSIM_BUILDVERSION \\"`python misc/getver.py`\\"" >>' + versionFile)
scons -c # Clean
scons debug=1 # Rebuild using `SConstruct` file in debug mode
TODO: multipim how to add a singel head file during compilation process.
暂无
暂无
上面回答部分来自ChatGPT-3.5,没有进行正确性的交叉校验。
https://scons.org/doc/production/PDF/scons-man.pdf