我发现我能做到的唯一方法是使用emitter
. 下面的示例由 3 个文件组成:
./
|-SConstruct
|-src/
| |-SConscript
| |-source.txt
|-build/
SConstruct
env = Environment()
dirname = 'build'
VariantDir(dirname, 'src', duplicate=0)
Export('env')
SConscript(dirname+'/SConscript')
src/SConscript
Import('env')
def my_emitter( env, target, source ):
data = str(source[0])
target = []
with open( data, 'r' ) as lines:
for line in lines:
line = line.strip()
name, contents = line.split(' ', 1)
if not name: continue
generated_source = env.Command( name, [], 'echo "{0}" > $TARGET'.format(contents) )
source.extend( generated_source )
target.append( name+'.c' )
return target, source
def my_action( env, target, source ):
for t,s in zip(target, source[1:]):
with open(t.abspath, 'w') as tf:
with open(s.abspath, 'r') as sf:
tf.write( sf.read() )
SourcesGenerator = env.Builder( action = my_action, emitter = my_emitter )
generated_sources = SourcesGenerator( env, source = 'source.txt' )
lib = env.Library( 'functions', generated_sources )
src/source.txt
a int a(){}
b int b(){}
c int c(){}
d int d(){}
g int g(){}
输出:
$ scons
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
echo "int a(){}" > build/a
echo "int b(){}" > build/b
echo "int c(){}" > build/c
echo "int d(){}" > build/d
echo "int g(){}" > build/g
my_action(["build/a.c", "build/b.c", "build/c.c", "build/d.c", "build/g.c"], ["src/source.txt", "build/a", "build/b", "build/c", "build/d", "build/g"])
gcc -o build/a.o -c build/a.c
gcc -o build/b.o -c build/b.c
gcc -o build/c.o -c build/c.c
gcc -o build/d.o -c build/d.c
gcc -o build/g.o -c build/g.c
ar rc build/libfunctions.a build/a.o build/b.o build/c.o build/d.o build/g.o
ranlib build/libfunctions.a
scons: done building targets.
还有一件事我不太喜欢,那就是headers_list.txt
每次scons
执行都要解析。我觉得只有在文件更改时才应该有一种方法来解析它。我可以手动缓存它,但我仍然希望有一些技巧可以让 SCons 为我处理缓存。
而且我找不到不重复文件的方法(a
并且a.c
相同)。一种方法是简单地在 my_action 而不是源中生成库(这是我在最终解决方案中使用的方法)。