How do I add revision and build date to source?

半腔热情 提交于 2019-11-27 21:43:15

问题


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 or anything like this.


回答1:


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 



回答2:


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".

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?".



来源:https://stackoverflow.com/questions/2696975/how-do-i-add-revision-and-build-date-to-source

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!