如何将构建版本添加到scons构建中

Mic*_*son 3 scons

目前我正在使用一些魔法将当前的git修订版本添加到我的scons版本中.我只是抓住它的版本将其粘贴到CPPDEFINES中.它工作得非常好......直到版本发生变化并且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} )
Run Code Online (Sandbox Code Playgroud)

Dav*_*her 5

您不需要自定义构建器,因为这只是一个文件.您可以使用函数(作为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)
Run Code Online (Sandbox Code Playgroud)