==》联系方式(AUTHOR_CONTACT):编译发布人员联系方式
==》支持主版本、次版本、联系方式自定义
==》支持发布人员或者发布账户自定义
==》支持版本号、编译日期、编译时间动态获取
==》自动生成version.c/version.h(version.h用于函数原型定义,直接在应用中include即可。)
#!/bin/bash # parameter check if [ $# -ne 4 ] && [ $# -ne 5 ] then echo "usage($#): $0 major_num minor_num DEBUG/RELEASE e-mail author" echo " or: $0 major_num minor_num DEBUG/RELEASE e-mail" exit fi # function version() { BUILD_DATE=`date "+%Y-%m-%d"` BUILD_TIME=`date "+%R:%S"` VERSION_NUM=`svn info|grep Revision |cut --delimiter=" " -f2` cat > version.c <<EEEEEEE #include "stdio.h" #include "version.h" #define VER_MAJOR $1 #define VER_MINOR $2 #define VER_REVISION $VERSION_NUM #define VER_DR_FLAG "$3" #define VER_BUILD_DATE "$BUILD_DATE" #define VER_BUILD_TIME "$BUILD_TIME" #define AUTHOR_CONTACT "$4" #define AUTHOR_NAME "$5" #define VERSION_ALL "$3_${BUILD_DATE}_${BUILD_TIME}_v$1_$2_$VERSION_NUM" char *get_version() { return VERSION_ALL; } char *get_ver_author() { return AUTHOR_NAME; } char *get_ver_author_contact() { return AUTHOR_CONTACT; } char *get_ver_flag() { return VER_DR_FLAG; } char *get_build_date() { return VER_BUILD_DATE; } char *get_build_time() { return VER_BUILD_TIME; } int get_ver_major() { return VER_MAJOR; } int get_ver_minor() { return VER_MINOR; } int get_ver_rev() { return VER_REVISION; } EEEEEEE cat > version.h <<EEEEEEE #ifndef __VERSION_H__ #define __VERSION_H__ char *get_version(); char *get_ver_author(); char *get_ver_author_contact(); char *get_ver_flag(); char *get_build_date(); char *get_build_time(); int get_ver_major(); int get_ver_minor(); int get_ver_rev(); #endif /* __VERSION_H__ */ EEEEEEE } # print echo "###############################" echo "######### $0 " echo "######### Major Number: $1" echo "######### Minor Number: $2" echo "######### D/R Flag: $3" echo "######### Contact: $4" if [ $# -eq 4 ] then AUTHOR=`who | cut --delimiter=" " -f1` else AUTHOR=$5 fi echo "######### Author: $AUTHOR" version $1 $2 $3 $4 $AUTHOR if [ $? -eq 0 ] then echo "######### Done! " else "######### Failed! " fi echo "###############################"
在SVN版本库根目录执行
# ./version.sh 1 2 DEBUG [email protected] lida ############################### ######### ../version.sh ######### Major Number: 1 ######### Minor Number: 2 ######### D/R Flag: DEBUG ######### Contact: [email protected] ######### Author: lida ######### Done! ###############################
version.c
#include "stdio.h" #include "version.h" #define VER_MAJOR 1 #define VER_MINOR 2 #define VER_REVISION 271 #define VER_DR_FLAG "DEBUG" #define VER_BUILD_DATE "2016-05-10" #define VER_BUILD_TIME "18:29:35" #define AUTHOR_CONTACT "[email protected]" #define AUTHOR_NAME "lida" #define VERSION_ALL "DEBUG_2016-05-10_18:29:35_v1_2_271" char *get_version() { return VERSION_ALL; } char *get_ver_author() { return AUTHOR_NAME; } char *get_ver_author_contact() { return AUTHOR_CONTACT; } char *get_ver_flag() { return VER_DR_FLAG; } char *get_build_date() { return VER_BUILD_DATE; } char *get_build_time() { return VER_BUILD_TIME; } int get_ver_major() { return VER_MAJOR; } int get_ver_minor() { return VER_MINOR; } int get_ver_rev() { return VER_REVISION; }
#ifndef __VERSION_H__ #define __VERSION_H__ char *get_version(); char *get_ver_author(); char *get_ver_author_contact(); char *get_ver_flag(); char *get_build_date(); char *get_build_time(); int get_ver_major(); int get_ver_minor(); int get_ver_rev(); #endif /* __VERSION_H__ */
main.c
#include <stdio.h> #include "version.h" void main() { printf("get_version: %s\n",get_version()); printf("get_ver_author: %s\n",get_ver_author()); printf("get_ver_author_contact: %s\n",get_ver_author_contact()); printf("get_ver_flag: %s\n",get_ver_flag()); printf("get_build_date: %s\n",get_build_date()); printf("get_build_time: %s\n",get_build_time()); printf("get_ver_major: %d\n",get_ver_major()); printf("get_ver_minor: %d\n",get_ver_minor()); printf("get_ver_rev: %d\n",get_ver_rev()); }
# gcc main.c version.c # ./a.out get_version: DEBUG_2016-05-10_18:29:35_v1_2_271 get_ver_author: lida get_ver_author_contact: [email protected] get_ver_flag: DEBUG get_build_date: 2016-05-10 get_build_time: 18:29:35 get_ver_major: 1 get_ver_minor: 2 get_ver_rev: 271