scons使用

时间:2023-03-09 16:40:03
scons使用

1.概述

scons是一个Python写的自动化构建工具,和GNU make相比优点明显:
    A.移植性:python能运行的地方,就能运行scons
    B. 扩展性:理论上scons只是提供了python的类,scons使用者可以在这个类的基础上做所有python能做的事情。比如想把一个已经使用了Makefile大型工程切换到scons,就可以保留原来的Makefile,并用python解析Makefile中的编译选项、源/目标文件等,作为参数传递给scons,完成编译。
    C. 智能:Scons继承了autoconf/automake的功能,自动解析系统的include路径、typedef等;“以全局的观点来看所有的依赖关系”

2.scons文件

scons中可能出现的文件:
        SConstruct,Sconstruct,sconstruct,SConscript

scons将在当前目录以下次序 SConstruct,Sconstruct,sconstruct 来搜索配置文件,从读取的第一个文件中读取相关配置。
    在配置文件SConstruct中可以使用函数SConscript()函数来定附属的配置文件。按惯例,这些附属配置文件被命名为”SConscript”,当然也可以使用任意其它名字。

3.scons的命令行参数

     scons: 执行SConstruct中脚本
scons -c clean
scons -Q 只显示编译信息,去除多余的打印信息
scons -Q --implicit-cache hello 保存依赖关系
--implicit-deps-changed 强制更新依赖关系
--implicit-deps-unchanged 强制使用原先的依赖关系,即使已经改变

4.SConstruct提供的方法

4.1 Program:生成可执行文件

     Program('hello.c')                         //编译hello.c可执行文件,根据系统自动生成(hello.exe on Windows; hello on POSIX)
Program('hello','hello.c')                    //指定Output文件名(hello.exe on Windows; hello on POSIX)
Program(['hello.c', 'file1.c', 'file2.c'])          //编译多个文件,Output文件名以第一个文件命名
Program(source = "hello.c",target = "hello")
Program(target = "hello" , source = "hello.c")
Program('hello', Split('hello.c file1.c file2.c'))      //编译多个文件 Program(Glob("*.c"))
src = ["hello.c","foo.c"];Program(src)

4.2 Object:生成目标文件

     Object('hello.c')                         //编译hello.c目标文件,根据系统自动生成(hello.obj on Windows; hello.o on POSIX)

4.3 Library:生成静态/动态库文件

 Library('foo', ['f1.c', 'f2.c', 'f3.c'])               //编译library
SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])            //编译 shared library
StaticLibrary('bar', ['f4.c', 'f5.c', 'f6.c'])            //编译 static library

库的使用:

  Program('prog.c', LIBS=['foo', 'bar'], LIBPATH='.')        //连接库,不需加后缀或是前缀

4.4 SourceSignatures:判断源文件是否修改

     SourceSignatures('MD5')                       //根据内容是否改变,默认方式
SourceSignatures('timestamp')                  //根据修改时间

4.5 TargetSignatures:判断目标文件是否改变

     TargetSignatures('build')                     //根据编译结果
TargetSignatures('content')                   //根据文件内容,如果只是加了句注释,将不会被重新编译

4.6 Ignore:忽略依赖关系

     Ignore(hello, 'hello.h')                     //忽略某个依赖关系

4.7 Depends:明确依赖关系

 Depends(hello, 'other_file')                     //明确依赖关系 

4.8 SConscript:scons的配置文件

源文件的目录结构如下:
    src:
    |    SConstruct
    |    test.cpp
    |    mA(目录):
         |     SConscript
         |     func.cpp
    其中test.cpp为主文件,中调用func.cpp中定义的函数
  
    SConstruct内容如下:

 env = Environment()
flags = env.ParseFlags(['-pthread -I/usr/include/stlport ',' -L .'])
env.MergeFlags(class_flags)
subobj = SConscript(['mA/SConscript'])
obj = subobj + env.Object(Glob("*.cpp"))
env.Program("test",list(obj),LIBS = ['libstlport.a'])

mA/SConscrip如下:

 obj = Object(Glob("*.cpp"))
Return("obj")

不出意外的话上边的工程编译可以通过,但是运行的时候会Aborted。因为test.cpp,mA/func.cpp都使用了包含string类型的那个类,但是由于编译环境的不同,test.cpp认为string变量的大小是24字节, mA/func.cpp认为string变量的大小是4个字节(libstlport.a捣的鬼)。

解决问题的办法就是环境变量输出,修改SConstruct和mA/SConscript如下:
    SConstruct:

 env = Environment()
flags = env.ParseFlags(['-pthread -I/usr/include/stlport ',' -L .'])
env.MergeFlags(class_flags)
Export('env')
subobj = SConscript(['mA/SConscript'],exports = 'env')
obj = subobj + env.Object(Glob("*.cpp"))
env.Program("test",list(obj),LIBS = ['libstlport.a'])

mA/SConscript:

 Import('env')
obj = env.Object(Glob("*.cpp"))
Return("obj")