SCons生成可变数量的目标

我试图让SCons生成多个目标(数字直接在SConscript未知)。

我有如下目录:

headers/
  Header1.h
  Header2.h
  Header3.h
  Header4.h
meta/
  headers_list.txt

现在我想让SConscript读取headers_list.txt ,根据它的内容从头headers/目录中选择文件(即它可能只包含Header1Header3 ),对于我想要使用某个函数生成源文件的每个文件。

我一直在尝试使用env.Command来做到这一点,但问题是它需要调用者指定目标列表,在调用env.Command时出于显而易见的原因env.Command

我能想到的唯一的事情就是运行:

for header in parse( headers_file ):
    source = mangle_source_name_for_header( header )
    env.Command( source, header, generator_action )

但这意味着我每次调用scons时都会运行parse( headers_file ) 。 如果解析费用高昂且文件不经常更改,则可轻松缓存此步骤。

什么SConsc构造/类/技术我失踪,以实现缓存?

编辑:

看起来我的问题与SCons目标的构建时间确定类似,但是没有没有伪造文件的技术吗?

此外,即使使用临时文件,我也没有看到我应该如何将Command变量的target变量传递给生成可变数目的目标的第二个目标变量,以便对它们进行迭代。

编辑2:

这看起来有希望。


我发现的唯一方法就是使用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.

还有一件事我不太喜欢,它是每个scons执行时解析headers_list.txt 。 我觉得应该有一种方法来解析它,只有当文件改变了。 我可以手动将其缓存,但我仍然希望有一些技巧可以让SCons为我处理缓存。

而我找不到一种不重复文件的方式( aac是相同的)。 一种方法是简单地在my_action中生成库,而不是源(这是我在最终解决方案中使用的方法)。

链接地址: http://www.djcxy.com/p/11643.html

上一篇: SCons to generate variable number of targets

下一篇: putExtra treeMap returns HashMap cannot be cast to TreeMap android