Yearly copyright bump :)
[quassel.git] / src / common / genversion.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-09 by the Quassel Project                          *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) version 3.                                           *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 /** This is called at compile time and generates a suitable version.gen.
22  *  usage: genversion git_root target_file
23  */
24
25 #include <QDebug>
26 #include <QProcess>
27 #include <QString>
28 #include <QStringList>
29 #include <QRegExp>
30 #include <QFile>
31
32 int main(int argc, char **argv) {
33   if(argc < 3) {
34     qFatal("Usage: ./genversion <git_root> <target_file>");
35     return 255;
36   }
37   QString gitroot = argv[1];
38   QString target = argv[2];
39   QString basever, protover, clientneeds, coreneeds, descrver, dirty;
40   QString committish, commitdate;
41
42   // check Git for information if present
43   if(QFile::exists(gitroot + "/.git")) {
44     // try to execute git-describe to get a version string
45     QProcess git;
46     git.setWorkingDirectory(gitroot);
47     git.start("git", QStringList() << "describe" << "--long");
48     if(git.waitForFinished(10000)) {
49       QString descr = git.readAllStandardOutput().trimmed();
50       if(!descr.isEmpty() && !descr.contains("fatal")) {
51         // seems we have a valid git describe string
52         descrver = descr;
53         // check if the workdir is dirty
54         git.start("git", QStringList() << "diff-index" << "--name-only" << "HEAD");
55         if(git.waitForFinished(10000)) {
56           if(!git.readAllStandardOutput().isEmpty()) dirty = "*";
57         }
58         // get a full committish
59         git.start("git", QStringList() << "rev-parse" << "HEAD");
60         if(git.waitForFinished(10000)) {
61           committish = git.readAllStandardOutput().trimmed();
62         }
63         // Now we do some replacement magic...
64         //QRegExp rxCheckTag("(.*)-0-g[0-9a-f]+\n$");
65         //QRegExp rxGittify("(.*)-(\\d+)-g([0-9a-f]+)\n$");
66         //gitversion.replace(rxCheckTag, QString("\\1%1").arg(dirty));
67         //gitversion.replace(rxGittify, QString("\\1:git-\\3+\\2%1").arg(dirty));
68       }
69     }
70   }
71
72   // parse version.inc
73   QFile verfile(gitroot + "/version.inc");
74   if(verfile.open(QIODevice::ReadOnly | QIODevice::Text)) {
75     QString ver = verfile.readAll();
76
77     QRegExp rxBasever("baseVersion\\s*=\\s*\"(.*)\";");
78     if(rxBasever.indexIn(ver) >= 0)
79       basever = rxBasever.cap(1);
80
81     QRegExp rxProtover("protocolVersion\\s*=\\s*(\\d+)");
82     if(rxProtover.indexIn(ver) >= 0)
83       protover = rxProtover.cap(1);
84
85     QRegExp rxClientneeds("clientNeedsProtocol\\s*=\\s*(\\d+)");
86     if(rxClientneeds.indexIn(ver) >= 0)
87       clientneeds = rxClientneeds.cap(1);
88
89     QRegExp rxCoreneeds("coreNeedsProtocol\\s*=\\s*(\\d+)");
90     if(rxCoreneeds.indexIn(ver) >= 0)
91       coreneeds = rxCoreneeds.cap(1);
92
93     if(committish.isEmpty()) {
94       QRegExp rxCommit("distCommittish\\s*=\\s*([0-9a-f]+)");
95       if(rxCommit.indexIn(ver) >= 0) committish = rxCommit.cap(1);
96     }
97
98     QRegExp rxTimestamp("distCommitDate\\s*=\\s*([0-9]+)");
99     if(rxTimestamp.indexIn(ver) >= 0) commitdate = rxTimestamp.cap(1);
100     verfile.close();
101   }
102
103   // ok, create our version.gen now
104   QFile gen(target);
105   if(!gen.open(QIODevice::WriteOnly | QIODevice::Text)) {
106     qFatal("%s", qPrintable(QString("Could not write %1!").arg(target)));
107     return EXIT_FAILURE;
108   }
109   gen.write(QString("QString buildinfo = \"%1,%2,%3,%4,%5,%6,%7,%8\";\n")
110            .arg(basever, descrver, dirty, committish, commitdate, protover, clientneeds, coreneeds).toAscii());
111   gen.close();
112   return EXIT_SUCCESS;
113 }