开发者

Tracking code versions in an executable

I have a reasonable sized ( around 40k lines) machine learning system written in C++. This is still in active development and I need to run experiments regularly even as I make changes to my code.

The output of my experiments is captured in simple text files. What I would like to do when looking at these results is have some way of figuring out the exact version of the code that produced it. I usually have around 5 to 6 experiments running simultaneously, each on a slightly different version of the code.

I would like to know for instance that a set of results was obtained by compiling version 1 of file A, version 2 of file B etc (I just need some identifier and the output of "git describe" will do fine here ).

My idea is to somehow include this info when compiling the binary. This way, this can be printed out along with the results.

Any suggestions how this can be done in a nice way. 开发者_如何转开发In particular, any nice way of doing this with git?


I generate a single source file as part of my build process that looks like this:

static const char version_cstr[] = "93f794f674 (" __DATE__ ")";
const char * version()
{
  return version_cstr;
}

Then its easy to log the version out on startup.

I originally used a DEFINE on the command line, but that meant every version change everything got recompiled by the build system - not nice for a big project.

Here's the fragment of scons I use for generating it, maybe you can adapt it to your needs.

# 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


def version_action(target,source,env):
  """
  Generate file with current version info
  """
  fd=open(target[0].path,'w')
  fd.write( "static const char version_cstr[] = \"%s (\" __DATE__ \")\";\nconst char * version()\n{\n  return version_cstr;\n}\n" % git_sha )
  fd.close()
  return 0

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


You can use $Id:$ in your source file, and Git will substitute that with the sha1 hash, if you add the file containing this phrase in .gitattributes with the option "ident" (see gitattributes).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜