13

我有一个 GCC 项目,并希望自动将构建日期和修订号(来自 git)的定义添加到我的源代码中。最好的方法是什么?

我的目标很简单,就是能够在启动时做这样的事情:

printf("Test app build on %s, revision %d", BUILD_DATE, REVISION)

对于构建,我使用 make 和一个简单的 Makefile.inc,而不是 autoconf 或类似的东西。

4

2 回答 2

7

我最终在我的Makefile

echo "#define GIT_REF \"`git show-ref refs/heads/master | cut -d " " -f 1 | cut -c 31-40`\"" > git_ref.h
于 2010-08-27T13:04:48.223 回答
4

RCS keyword substitution is not natively supported by Git, but can be added with a gitattributes filter driver: See "Git equivalent of subversion's $URL$ keyword expansion".

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 answer to "Git equivalent of subversion's $URL$ keyword expansion", 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 debate about it in "What are the basic clearcase concepts every developer should know?").

Yet you have a good example of such Git keyword expansion in this answer in "How do I enable ident string for Git repos?".

于 2010-04-23T11:22:55.517 回答