歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> Linux下C/C++版本發布自動腳本

Linux下C/C++版本發布自動腳本

日期:2017/3/3 12:57:26   编辑:Linux技術
軟件發布有軟件版本管理原則,這裡結合Linux下C/C++項目發布方式,簡單介紹一下如何自動的集成動態版本管理腳本。

軟件版本發布關鍵點

從軟件版本管理原則我們需要注意的是以下幾個關鍵點:
==》主版本(VER_MAJOR):項目(產品)負責人維護
==》次版本(VER_MINOR):技術(版本)接口人維護
==》版本號(VER_REVISION):代碼庫自動升級更新
==》編譯日期(BUILD_DATE):編譯機器的系統日期
==》編譯時間(BUILD_TIME):編譯機器的系統時間
==》編譯標識(BUILD_ID):編譯唯一碼
==》編譯人員(AUTHOR_NAME):編譯發布人員
==》聯系方式(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;
}

version.h
#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__ */

DEMO示例集成版本管理

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());
}

編譯、執行上述version和main程序
# 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
Copyright © Linux教程網 All Rights Reserved