跳转至

Scons

Scons

  • 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.

Scons VS cmake

  1. 基于python语言的构建工具,对开发者来说过度自然,简单,no need to learn domain-specific language like cmake

其余cmake有的, Scons 也有。

  1. cross-paltform,
  2. 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.

Project structure

  1. Sconstruct python file as compile entry

framework grammar

  1. 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') 
  1. add sub scons config file and build result path using variant_dir
env.SConscript("src/SConscript", variant_dir=buildDir, exports= {'env' : env.Clone()})     
  1. achive debug mode

using scons debug=1 command.

env = Environment()
debug = ARGUMENTS.get("debug", 0)
if int(debug):
   print "in debug mode"

main construct grammar

  1. 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()
  1. 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.

  1. static or dynamic lib
# static
Library("t", Glob("src/*.cpp"))
# dynamic
source = Glob("src/*.cpp")
SharedLibrary("t", source)
Program(["hello.cpp"], LIBS=["t"], LIBPATH=".")
  1. execute command during compilation

  2. this is usually to print info

  3. 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) 

Command

scons -c  # Clean
scons debug=1    # Rebuild using `SConstruct` file in debug mode

scons-project analysis

TODO: multipim how to add a singel head file during compilation process.

需要进一步的研究学习

暂无

遇到的问题

暂无

开题缘由、总结、反思、吐槽~~

参考文献

上面回答部分来自ChatGPT-3.5,没有进行正确性的交叉校验。

https://scons.org/doc/production/PDF/scons-man.pdf