1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel Project *
3 * devel@quassel-irc.org *
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. *
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. *
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 ***************************************************************************/
21 /** This is called at compile time and generates a suitable version.gen.
22 * usage: genversion git_root target_file
29 #include <QStringList>
33 int main(int argc, char **argv) {
35 qFatal("Usage: ./genversion <git_root> <target_file>");
38 QString gitroot = argv[1];
39 QString target = argv[2];
42 if(QFile::exists(gitroot + "/.git")) {
43 // try to execute git-describe to get a version string
45 git.setWorkingDirectory(gitroot);
46 git.start("git-describe", QStringList("--long"));
47 if(git.waitForFinished(10000)) {
48 QString gitversion = git.readAllStandardOutput();
49 if(!gitversion.isEmpty() && !gitversion.contains("fatal")) {
50 // seems we have a valid version string, now prettify it...
51 // check if the workdir is dirty first
53 QStringList params = QStringList() << "--name-only" << "HEAD";
54 git.start("git-diff-index", params);
55 if(git.waitForFinished(10000)) {
56 if(!git.readAllStandardOutput().isEmpty()) dirty = "*";
58 // Now we do some replacement magic...
59 QRegExp rxCheckTag("(.*)-0-g[0-9a-f]+\n$");
60 QRegExp rxGittify("(.*)-(\\d+)-g([0-9a-f]+)\n$");
61 gitversion.replace(rxCheckTag, QString("\\1%1").arg(dirty));
62 gitversion.replace(rxGittify, QString("\\1:git-\\3+\\2%1").arg(dirty));
63 if(!gitversion.isEmpty()) version = gitversion;
67 if(version.isEmpty()) {
68 // hmm, Git failed... let's check for version.dist instead
69 QFile dist(gitroot + "/version.dist");
70 if(dist.open(QIODevice::ReadOnly | QIODevice::Text)) {
71 version = dist.readAll();
75 // ok, create our version.gen now
77 if(!gen.open(QIODevice::WriteOnly | QIODevice::Text)) {
78 qFatal(QString("Could not write %1!").arg(target).toAscii());
81 gen.write(QString("quasselGeneratedVersion = \"%1\";\n"
82 "quasselBuildDate = \"%2\";\n"
83 "quasselBuildTime = \"%3\";\n").arg(version).arg(__DATE__).arg(__TIME__).toAscii());