Scons 四

在scons中可以自己定义Builder。如下面的方式,Builder中的action来描述具体要执行的命令。

bld=Builder(action='$CC -o $TARGET -c $SOURCE')

env1=Environment(BUILDERS={'Foo':bld})

然后在Environment中通过BUILDERS来指定该build的别名。调用的时候用env1.Foo()就可以了。

在Builder中涉及到的TARGET和SOURCE参数,是在env1.Foo调用的时候传入进去的。有下面两种传入方式,第一种TARGET=test2,  SOURCE=test1。第二种就直接致命source和target的值。

env1.Foo('test2','test1')

env1.Foo(source='test1',target='test2')

在Builer中还可以指明source和target文件的后缀,suffic指示target文件的后缀,source指示source文件的后缀。如下的写法就是将*.c的source文件编译成*.o的目标文件

bld=Builder(action='$CC -o $TARGET -c $SOURCE',suffix='.o',src_suffix='.c')

env1.Foo('test2','test1')

编译结果:

scons: Reading SConscript files ...

scons: done reading SConscript files.

scons: Building targets ...

gcc -o test2.o -c test1.c

scons: done building targets.

在Builder中的action也可以指派执行python文件。方法如下。

bld=Builder(action='python py_test.py')

env1=Environment(BUILDERS={'Foo':bld})

env1.Foo('test1.c')

原文地址:https://www.cnblogs.com/zhanghongfeng/p/13066745.html