If you use automatic build, version information is a headache.
The version information should include the following three aspects:
模块名称:foo
版本号:v1.1.1
构建信息: 132
The build information is usually the number of records in git / svn, which can ensure the correct version when looking for the bug;
writing the version information in the code may cause the following problems:
- The version number in the program and the packaged version number are not correct for a moment of mistake;
- Not every time you need to modify the version number, it is easy to forget by human control;
- The build information comes from the version management tool, it changes as soon as you submit the code, and the writing is not rigorous;
Therefore, the best solution is to write the version information in the build script and modify the version number file during the build. Then compile.
At this time, you need a tool to obtain version information.
I use my favorite C language to achieve the following:
#include <stdio.h>
#include <string.h>
int getBuildInfo(const char* cmd)
{
FILE *fstream = NULL;
char buffer[1024];
int res = 0;
memset(buffer,0,sizeof(buffer));
if (NULL == (fstream = popen(cmd,"r")))
{
return -1;
}
fgets(buffer, sizeof(buffer), fstream);
pclose(fstream);
sscanf(buffer,"%d",&res);
return res;
}
int main(int argc, char *argv[])
{
FILE *fp = NULL;
char* gitcmd="git rev-list --all|wc -l";
char* svncmd="svn info|grep \"Last Changed Rev:\"|awk -F : '{print $2}'|sed 's/ //g'";
int res = 0;
char buffer[2048];
memset(buffer,0,sizeof(buffer));
if (argc < 4) {
printf("Usage:./getversion [NAME] [VERSION] [git/svn/other]\n");
return 1;
}
if (strcmp(argv[3],"git") == 0){
res = getBuildInfo(gitcmd);
}else if (strcmp(argv[3],"svn") == 0){
res = getBuildInfo(svncmd);
}else{
res = 58;
}
char* arr="#ifndef __VER_H_FILE__\
\r\n#define __VER_H_FILE__\
\r\n\
\r\n#define ___MODULE___ \"%s\"\
\r\n#define ___VERSION___ \"%s\"\
\r\n#define ___BUILD___ \"%d\"\
\r\n\
#endif";
sprintf(buffer,arr,argv[1],argv[2],res);
fp = fopen("verion.h", "w+");
fprintf(fp, "%s",buffer);
fclose(fp);
return 0;
}
carried out:
./getVersionInfo foo v1.1.1 git
Then compile version.h as a header file in your own code, so that the compilation information is consistent with the code information.