利用SVN實現自動版本號生成

 

以 vc6 爲例, 文件的版本信息保存在 rc 文件. 編譯成 res 文件, 然後和其他 obj 一起 link. 現在的思路就是. 編輯 rc 文件, 將版本號比如 2.2.4.0 改成 2.2.4.$WCREV$, 在每次 link 之前, 先用 subwcrev.exe 處理 rc 文件, 進行宏替換. 然後調用 
rc.exe 編譯替換後的新文件. 生成 res 之後一起 link.……
/* 
 * Copyright (C) 2005-2008 MaNGOS <http://www.mangosproject.org/>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <fstream>
#include <sstream>

#pragma warning(disable:4996)

int main(int argc, char **argv)
{
    std::string path;

    if(argc >= 1 && argv[1] )
    {
        path = argv[1];
        if(path.size() > 0 && (path[path.size()-1]!='/' || path[path.size()-1]!='//'))
            path += '/';
    }

    FILE* EntriesFile = fopen((path+".svn/entries").c_str(), "r");
    if(!EntriesFile)
        EntriesFile = fopen((path+"_svn/entries").c_str(), "r");

    std::ostringstream newData;

    if(!EntriesFile)
    {
        newData << "#ifndef __SVN_REVISION_H__" << std::endl;
        newData << "#define __SVN_REVISION_H__"  << std::endl;
        newData << " #define SVN_REVISION /"Unknown/"" << std::endl;
        newData << " #define SVN_DATE /"Unknown/"" << std::endl;
        newData << " #define SVN_TIME /"Unknown/""<< std::endl;
        newData << "#endif // __SVN_REVISION_H__" << std::endl;
    }
    else
    {
        char buf[200];
        int revision;
        char date_str[200];
        char time_str[200];

        fgets(buf,200,EntriesFile);
        fgets(buf,200,EntriesFile);
        fgets(buf,200,EntriesFile);
        fscanf(EntriesFile,"%i",&revision);
        fgets(buf,200,EntriesFile);
        fgets(buf,200,EntriesFile);
        fgets(buf,200,EntriesFile);
        fgets(buf,200,EntriesFile);
        fgets(buf,200,EntriesFile);
        fscanf(EntriesFile,"%10sT%8s",date_str,time_str);

        newData << "#ifndef __SVN_REVISION_H__" << std::endl;
        newData << "#define __SVN_REVISION_H__"  << std::endl;
        newData << " #define SVN_REVISION /"" << revision << "/"" << std::endl;
        newData << " #define SVN_REVISIONSTR /"" << revision/1000 << "." << revision%1000/100 << "." 
            << revision%100/10 << "." << revision%10
            << "/"" << std::endl;
        newData << " #define SVN_FILESTR " << revision/1000 << "," << revision%1000/100 << "," 
            << revision%100/10 << "," << revision%10
            << std::endl;
        newData << " #define SVN_DATE /"" << date_str << "/"" << std::endl;
        newData << " #define SVN_TIME /"" << time_str << "/""<< std::endl;
        newData << "#endif // __SVN_REVISION_H__" << std::endl;

        fclose(EntriesFile);
    }

    std::string oldData;

    if(FILE* HeaderFile = fopen((path+"svn_revision.h").c_str(),"rb"))
    {
        while(!feof(HeaderFile))
        {
            int c = fgetc(HeaderFile);
            if(c < 0)
                break;
            oldData += (char)c;
        }

        fclose(HeaderFile);
    }

    if(newData.str() != oldData)
    {
        if(FILE* OutputFile = fopen((path+"svn_revision.h").c_str(),"wb"))
        {
            fprintf(OutputFile,"%s",newData.str().c_str());
            fclose(OutputFile);
        }
    }

    return 0;
}

 

在vc的precompile步驟裏面調用gensvnrevision,生成svn_revision.h, 如:

 

#ifndef __SVN_REVISION_H__
#define __SVN_REVISION_H__
 #define SVN_REVISION "1707"
 #define SVN_REVISIONSTR "1.7.0.7"
 #define SVN_FILESTR 1,7,0,7
 #define SVN_DATE "2010-02-02"
 #define SVN_TIME "01:36:29"
#endif // __SVN_REVISION_H__

 

 

在 project.rc2裏面添加include "svn_revision.h", 在版本里面用SVN_REVISIONSTR和SVN_FILESTR替換ProductVersion、FileVersion和 FILEVERSION 、PRODUCTVERSION 。

搞定!使用tsvn的童鞋們可以用第一種方法,要方便很 多!;-)

發佈了48 篇原創文章 · 獲贊 15 · 訪問量 53萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章