Qtcreator mingw编译下,垮平台项目定制编译、版本号自动增加pro

  1. pro1文件,生成可执行文件

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-08-20T18:46:00
    #
    #-------------------------------------------------

    QT       += core

    TARGET = ProductVersion

    TEMPLATE = app
    DESTDIR = ./

    CONFIG  += console
    CONFIG  -= app_bundle
    CONFIG  += stl warn_on qt

    SOURCES += \
            main.cpp

    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS

    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

    VERSION = 2018.11.21

  2. pro2文件

    #-------------------------------------------------
    #
    # Project created by QtCreator 2018-08-20T18:46:00
    #
    #-------------------------------------------------

    QT       += core

    TARGET = ProductVersion

    TEMPLATE = app
    DESTDIR = ./

    CONFIG  += console
    CONFIG  -= app_bundle
    CONFIG  += stl warn_on qt

    SOURCES += \
            useless.cpp

    # The following define makes your compiler emit warnings if you use
    # any feature of Qt which as been marked as deprecated (the exact warnings
    # depend on your compiler). Please consult the documentation of the
    # deprecated API in order to know how to port your code away from it.
    DEFINES += QT_DEPRECATED_WARNINGS

    # You can also make your code fail to compile if you use deprecated APIs.
    # In order to do so, uncomment the following line.
    # You can also select to disable deprecated APIs only up to a certain version of Qt.
    #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

    VERSION = 2018.11.21

    ##************************ProductVersion***************************#
    #QMAKE_POST_LINK += $$DESTDIR$$TARGET $$PWD/../POSBase/version.h
    ##QMAKE_POST_LINK += $$DESTDIR$$TARGET $$PWD/../POSBase/enjoypos.rc
    version.commands = $$DESTDIR$$TARGET $$PWD/../POSBase/version.h -pnEnjoyAutoPos
    #version.commands = $${DESTDIR}$${TARGET}.exe $$PWD/../custom.pri \"-iDEFINES += NO_QMESSAGEBOX\" \"-iDEFINES += Debug_For_No_Public\"
    custom.commands = $$DESTDIR$$TARGET $$PWD/../custom.pri \"-iDEFINES += NO_QMESSAGEBOX\"      \
                                                             \"-iDEFINES += Compile_AutoPos\"     \
                                                             \"-rDEFINES += Debug_For_No_Public\"  \
                                                             \"-rDEFINES += Compile_MPos\"
    #version.depends = FORCE
    #rcbuild.depends = version
    QMAKE_EXTRA_TARGETS += version custom
    PRE_TARGETDEPS += version custom

    win32:rcbuild.commands += $$DESTDIR$$TARGET $$PWD/../POSBase/enjoypos.rc
    win32:QMAKE_EXTRA_TARGETS += rcbuild
    win32:PRE_TARGETDEPS += rcbuild

    ##1add sub pro->include ../POSBase/version.h
    ##2add sub pro->win32:RC_FILE = POSBase/enjoypos.rc
    ##************************ProductVersion***************************#

  3. main.cpp

    #include <QtCore/QCoreApplication>
    #include <QtCore/QFile>
    #include <QtCore/QString>
    #include <QtCore/QStringList>
    #include <QtCore/QTextStream>
    #include <QtCore/QRegExp>
    #include <QDate>
    #include <iostream>

    const char* const pHelp = \
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"\
    "~ call the app with:                                                           ~\n"\
    "~     AutoIncreaseBuildNr <versionfile>                                        ~\n"\
    "~ or for help with                                                             ~\n"\
    "~     AutoIncreaseBuildNr -h                                                   ~\n"\
    "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

    int main(int argc, char *argv[])
    {
        QCoreApplication app(argc, argv);

        QStringList szArgs = app.arguments();

        QFile fileLog(QString("ProductVersion日志.txt"));
        if (fileLog.open(QIODevice::WriteOnly/*|QIODevice::Append*/))
        {
            QTextStream out(&fileLog);
            foreach (QString strArg,szArgs) {
                out << strArg << "\n";
            }
            fileLog.close();
        }

        // 1.) extract arguments
        if(szArgs.count() < 2)
        {
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            std::cout << "Command line errors: " << std::endl;
            std::cout << "the tool was called with an invalid set of arguments." << std::endl;
            std::cout << pHelp << std::endl;
            return 1;
        }

        QString strArgs1 = szArgs.at(1);
        if((0 == strArgs1.compare(QLatin1String("-h"), Qt::CaseInsensitive)) ||
           (0 == strArgs1.compare(QLatin1String("/h"), Qt::CaseInsensitive)) ||
           (0 == strArgs1.compare(QLatin1String("-?"), Qt::CaseInsensitive)) ||
           (0 == strArgs1.compare(QLatin1String("/?"), Qt::CaseInsensitive)) ||
           (0 == strArgs1.compare(QLatin1String("-help"), Qt::CaseInsensitive)) ||
           (0 == strArgs1.compare(QLatin1String("/help"), Qt::CaseInsensitive)))
        {
            std::cout << pHelp << std::endl;
            return 0;
        }

        if(0 && !QFile::exists(strArgs1))
        {
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            std::cout << "File not found: " << strArgs1.toLatin1().constData() << std::endl;
            std::cout << pHelp << std::endl;
            return 2;
        }

        // 2.) read version file
        QFile versionFile(strArgs1);
        if(!versionFile.open(QFile::ReadWrite))
        {
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            std::cout << "Error openeing the version file for reading: " << strArgs1.toLatin1().constData() << std::endl;
            std::cout << "Error: " << versionFile.errorString().toLatin1().constData() << std::endl;
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            return 3;
        }

        QStringList slContent;
        {
            QTextStream stream(&versionFile);
            if( strArgs1.contains(".h") )
                stream.setCodec("utf-8");
            while(!stream.atEnd())
            {
                slContent.append(stream.readLine());
            }
        }
        versionFile.close();

        //处理.h格式文件
        if( strArgs1.endsWith(".h",Qt::CaseInsensitive) )
        {
            QString strProductName;
            int nProductCode = 0;
            // 3.) change build nr
            for(int i = 2; i < szArgs.count(); i ++ )
            {
                QString strArgi = szArgs.at(i);
                if( strArgi.startsWith("-pn") )
                {
                    strArgi.remove(0,3);
                    strProductName = strArgi;
                }
                else if( strArgi.startsWith("-pc") )
                {
                    strArgi.remove(0,3);
                    nProductCode = strArgi.toInt();
                }
            }

            // 3.) change build nr
            for(int i = 0; i < slContent.count(); ++i)
            {
                if( !strProductName.isEmpty() )
                {// change product
                    QString& strLine = slContent[i];
                    QString strProduct = QString("PRODUCT_NAME");

                    strLine = strLine.trimmed();
                    QStringList szWordsOfLine = strLine.split(QRegExp("\\s"), QString::SkipEmptyParts);
                    if(3 == szWordsOfLine.count() && szWordsOfLine.at(0) == "#define" && szWordsOfLine.at(1) == strProduct)
                    {
                        szWordsOfLine[2] = QString("\"%1\"").arg(strProductName);
                        strLine = szWordsOfLine.join(" ");
                        continue;
                    }
                }
                if( nProductCode > -1 )
                {// change product
                    QString& strLine = slContent[i];
                    QString strProduct = QString("PRODUCT_CODE");

                    strLine = strLine.trimmed();
                    QStringList szWordsOfLine = strLine.split(QRegExp("\\s"), QString::SkipEmptyParts);
                    if(3 == szWordsOfLine.count() && szWordsOfLine.at(0) == "#define" && szWordsOfLine.at(1) == strProduct)
                    {
                        szWordsOfLine[2] = QString("%1").arg(nProductCode);
                        strLine = szWordsOfLine.join(" ");
                        continue;
                    }
                }

                {//change version
                    QString& rszLine = slContent[i];
                    QStringList slVersions;
                    slVersions << "FILE_VERSION_COMMA|," << "FILE_VERSION_DOT|.";
                    foreach ( QString strVersion, slVersions )
                    {
                        QString strVersionF,strVersionS;
                        strVersionF = strVersion.split("|").at(0);
                        strVersionS = strVersion.split("|").at(1);

                        if(rszLine.contains(QLatin1String("#define")) && rszLine.contains(strVersionF))
                        {
                            rszLine = rszLine.trimmed();
                            QStringList szWordsOfLine = rszLine.split(QRegExp("\\s"), QString::SkipEmptyParts);
                            if(3 == szWordsOfLine.count() && szWordsOfLine.at(0) == "#define" && szWordsOfLine.at(1) == strVersionF)
                            {
                                QString strCurrentDate = QDate::currentDate().toString(QString("yyyy%1MM%1dd").arg(strVersionS));
                                QString strVer = szWordsOfLine.at(2);
                                QString strVersionSymbol = "";
                                if( strVer.contains("\"") )
                                {
                                    strVer.replace(QString("\""),QString(""));
                                    strVersionSymbol = QString("\"");
                                }
                                QStringList szVersions = strVer.split(strVersionS,QString::SkipEmptyParts);
                                if( strVer.contains(strCurrentDate) && szVersions.size() > 3 )
                                {
                                    int nBuildNumber = szVersions.at(3).toInt();
                                    std::cout << "Old version was " << nBuildNumber << " and is now incremented by 1." << std::endl;
                                    nBuildNumber++;
                                    szWordsOfLine[2] = strVersionSymbol+strCurrentDate+strVersionS+QString::number(nBuildNumber)+strVersionSymbol;
                                }
                                else
                                {
                                    szWordsOfLine[2] = QString("%1%2%3%4%1").arg(strVersionSymbol).arg(strCurrentDate).arg(strVersionS).arg("0");
                                }
                                rszLine = szWordsOfLine.join(" ");

                                QFile file(QString("../Verson_%1.txt").arg(strProductName));if (file.open(QIODevice::WriteOnly)){QTextStream out(&file);out << szWordsOfLine[2].remove("\"") << "\n";file.close();}
                                break;
                            }
                        }
                    }
                }
            }
        }

        //处理.pri格式文件,把一些宏放到pri下。避免因为编译单元太大导致的编译太慢,pri文件只在变更的时候才被重写
        if( strArgs1.endsWith(".pri",Qt::CaseInsensitive) )
        {
            bool bNeedReWrite = false;
            // 3.) change build nr
            for(int i = 2; i < szArgs.count(); i ++ )
            {
                QString strArgi = szArgs.at(i);
                if( strArgi.startsWith("-r") )
                {
                    strArgi.remove(0,2);
                    for(int j = 0; j < slContent.count(); ++j)
                    {
                        QString strLine = slContent[j];
                        strArgi = strArgi.simplified();
                        strLine = strLine.simplified();
                        if( strArgi.compare( strLine, Qt::CaseInsensitive) == 0  )
                        {
                            slContent.removeAt(j);
                            j--;
                            bNeedReWrite = true;
                        }
                    }
                }
                else if( strArgi.startsWith("-a") || strArgi.startsWith("-i") )
                {
                    strArgi.remove(0,2);
                    bool bFinded = false;
                    int j = 0;
                    for(; j < slContent.count(); ++j)
                    {
                        QString strLine = slContent[j];
                        strArgi = strArgi.simplified();
                        strLine = strLine.simplified();
                        if( strArgi.compare( strLine, Qt::CaseInsensitive) == 0  )
                        {
                            bFinded = true;
                            break;
                        }
                    }
                    if( !bFinded )
                    {
                        if( szArgs.at(i).startsWith("-i") )
                        {
                            slContent.insert(0,strArgi);
                        }
                        else
                        {
                            slContent.append(strArgi);
                        }
                        bNeedReWrite = true;
                    }
                }
            }
            if( !bNeedReWrite )
                return 0;
        }

        fileLog.setFileName(QString("ProductVersion日志.txt"));
        // 4.) write version file again.
        if(!versionFile.open(QFile::WriteOnly) || !fileLog.open(QIODevice::WriteOnly|QIODevice::Append))
        {
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            std::cout << "Error openeing the version file for writing: " << strArgs1.toLatin1().constData() << std::endl;
            std::cout << "Error: " << versionFile.errorString().toLatin1().constData() << std::endl;
            std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << std::endl;
            return 4;
        }

        QTextStream out(&fileLog);

        QTextStream writeStream(&versionFile);
        if( strArgs1.contains(".h") )
            writeStream.setCodec("utf-8");
        for(int i = 0; i < slContent.count(); ++i)
        {
            writeStream << slContent.at(i) << "\r\n" << flush;
            out << slContent.at(i) << "\r\n" << flush;
        }
        std::cout << "version file written" << std::endl;
        fileLog.close();

        return 0;
     }
     

  4. windows下version.h

    #ifndef VERSION_H
    #define VERSION_H

    // 图标
    #define PRODUCT_ICON           "enjoypos.ico"

    #define STR1(R)  #R
    #define STR2(R)  STR1(R)

    #define FILE_VERSION_COMMA 2018,11,20,19
    #define FILE_VERSION_DOT "2018.11.20.19"
    // 文件版本
    #define FILE_VERSION FILE_VERSION_COMMA
    #define FILE_VERSION_STR       FILE_VERSION_DOT

    // 产品版本
    #define PRODUCT_VERSION        FILE_VERSION_COMMA
    #define PRODUCT_VERSION_STR    FILE_VERSION_DOT

    //公司名称
    #define COMPANY_NAME           "深圳昂捷"

    //内部文件名
    #define INTERNAL_NAME          "POSKernel"
    // 原始文件
    #define ORIGINAL_FILE_NAME     INTERNAL_NAME

    // 产品名称
    #define PRODUCT_NAME           INTERNAL_NAME

    // 文件说明
    #define FILE_DESCRIPTION       "Based on Qt 5.4.0 (Mingw, 32 bit)"

    // 版权
    #define LEGAL_COPYRIGHT        "Copyright 2016-2019 The Qt Company Ltd. All rights reserved."

    // 域名
    #define ORGANIZATION_DOMAIN    "http://58.59.18.147:8198/"

    #endif // VERSION_H
     

  5. useless.cpp
    #include "main.cpp"
     
  6. solution.pro

    TEMPLATE = subdirs
    CONFIG+=ordered

    SUBDIRS += \
        ./ProductVersion.pro \
        ./ProductVersion_Windows_Client_AutoPos.pro
    ......


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值