2011-02-09 55 views
2

目前我正在使用一些魔法来将当前的git修订版本导入到我的scons构建版中。我只是将该版本粘贴到CPPDEFINES中。 它工作得非常好......直到版本发生变化,scons想要重建所有内容,而不仅仅是已经更改的文件 - 因为所有文件使用的定义已更改。如何将构建版本添加到scons构建

理想情况下,我会使用一个名为git_version.cpp和 的自定义构建器生成一个文件,只是有一个函数返回正确的标记。这样只有一个文件将被重建。

现在我确定我已经看过一个教程显示如何做到这一点..但我似乎无法跟踪它。我找到自定义生成器的东西,在scons的有点奇怪......

因此,任何指针将不胜感激......

反正仅供参考这是目前我在做什么:

# Lets get the version from git 
# first get the base version 
git_sha = subprocess.Popen(["git","rev-parse","--short=10","HEAD"], stdout=subprocess.PIPE).communicate()[0].strip() 
p1 = subprocess.Popen(["git", "status"], stdout=subprocess.PIPE) 
p2 = subprocess.Popen(["grep", "Changed but not updated\\|Changes to be committed"], stdin=p1.stdout,stdout=subprocess.PIPE) 
result = p2.communicate()[0].strip() 

if result!="": 
    git_sha += "[MOD]" 

print "Building version %s"%git_sha 

env = Environment() 
env.Append(CPPDEFINES={'GITSHAMOD':'"\\"%s\\""'%git_sha}) 

回答

4

您不需要自定义生成器,因为这只是一个文件。您可以使用一个函数(附加到目标版本文件作为一个Action)来生成您的版本文件。在下面的示例代码中,我已经计算出版本并将其放入环境变量中。你可以做同样的事情,或者你可以把你的代码在version_action函数中做git调用。

version_build_template="""/* 
* This file is automatically generated by the build process 
* DO NOT EDIT! 
*/ 

const char VERSION_STRING[] = "%s"; 

const char* getVersionString() { return VERSION_STRING; } 
""" 

def version_action(target, source, env): 
    """ 
    Generate the version file with the current version in it 
    """ 
    contents = version_build_template % (env['VERSION'].toString()) 
    fd = open(target[0].path, 'w') 
    fd.write(contents) 
    fd.close() 
    return 0 

build_version = env.Command('version.build.cpp', [], Action(version_action)) 
env.AlwaysBuild(build_version)