views:

62

answers:

2

Hi!

I have a gcc project and would like to automatically add defines for build date and revision number (from git) to my sources. What's the best way to do this?

My goal is simple to be able to do something like this on startup: printf("Test app build on %s, revision %d", BUILD_DATE, REVISION)

For building I'm using make with a simple Makefile.inc, not autoconf ot anything like this.

Thanks, Corin

+1  A: 

RCS keyword substitution is not natively supported by Git, but can be added with a gitattributes filter driver: See this question

alt text

For example (not exactly relate to your question, but illustrates the general principle):

git config filter.rcs-keyword.clean 'perl -pe "s/\\\$Date[^\\\$]*\\\$/\\\$Date\\\$/"'
git config filter.rcs-keyword.smudge 'perl -pe "s/\\\$Date[^\\\$]*\\\$/\\\$Date: `date`\\\$/"'

You will base your filter script on the result of git describe --tags called from your Makefile.

As mentioned in this SO answer, smudge/clear filter driver is not a perfect solution, and adding any kind of meta-data directly in the data (source) is generally a bad idea (you have a all debate about it here).

Yet you have a good example of such Git keyword expansion in this answer.

VonC
+1  A: 

I ended up using a simple command like this in my Makefile:

echo "#define GIT_REF \"git show-ref refs/heads/master | cut -d " " -f 1 | cut -c 31-40\"" > git_ref.h

gucki