将环境状态显式附加到SCons Builder依赖关系MD5指纹

时间:2014-09-04 10:52:27

标签: caching build builder scons

我修改了我的SCons Builder操作,以使用subprocess.Popen()代替env.Execute()。但是我仍然需要在env的内容中添加一些md5-sum依赖性检查,以便它重建并缓存新的目标文件。如何附加到SCons内部MD5哈希?

1 个答案:

答案 0 :(得分:6)

假设您有一个使用环境变量的构建器:

# Just an example. The point is that the output of this function
# depends upon an environment variable
def bld_func(target, source, env):
    with open(str(target[0]), 'w') as f:
        f.write(' '.join(str(t) for t in target) + '\n')
        f.write(' '.join(str(s) for s in source) + '\n')
        f.write(env.subst('$MY_VAR') + '\n')
bld = Builder(action=bld_func)

和一个调用它的SConstruct:

env = Environment()
env['BUILDERS']['BLD'] = bld
env.BLD('output.txt', 'input.txt', MY_VAR=ARGUMENTS['MY_VAR'])

如果更改MY_VAR的值,则不会重新执行此构建器:

$ scons -Q MY_VAR=cc output.txt
bld_func(["output.txt"], ["input.txt"])
$ scons -Q MY_VAR=gcc output.txt
scons: `output.txt' is up to date.

但是,您可以使用env.Value添加字符串值的依赖关系:

env.Depends('output.txt', env.Value(env.subst('$MY_VAR')))

放置这种依赖关系的合理位置是在发射器中:

def bld_emitter(target, source, env):
    env.Depends(target, env.Value(env.subst('$MY_VAR')))
    return target, source
bld = Builder(action=bld_func, emitter=bld_emitter)

完成SConstruct:

# Create a builder that depends upon an environment variable
def bld_func(target, source, env):
    with open(str(target[0]), 'w') as f:
        f.write(' '.join(str(t) for t in target) + '\n')
        f.write(' '.join(str(s) for s in source) + '\n')
        f.write(env.subst('$MY_VAR') + '\n')
def bld_emitter(target, source, env):
    env.Depends(target, env.Value(env.subst('$MY_VAR')))
    return target, source
bld = Builder(action=bld_func, emitter=bld_emitter)

# Attach it to an environment
env = Environment()
env['BUILDERS']['BLD'] = bld

# Invoke the builder
env.BLD('output.txt', 'input.txt', MY_VAR=ARGUMENTS['MY_VAR'])

示例输出:

$ scons -Q MY_VAR=cc output.txt
bld_func(["output.txt"], ["input.txt"])
$ scons -Q MY_VAR=gcc output.txt
bld_func(["output.txt"], ["input.txt"])
$ scons -Q MY_VAR=gcc output.txt
scons: `output.txt' is up to date.