aboutsummaryrefslogtreecommitdiffstats
path: root/tools/version/version.cpp
blob: d59d1106905610d0b6c52806daffa37b4cc85595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <iostream> 
#include <QProcess> 
#include <QStringList> 
#include <QFile> 
#include <QTextStream> 
#include <QDate> 
#include <QTime> 
#include <QFileInfo> 
#include <QTemporaryFile> 
#include <cstdlib> 
 
using namespace std; 
 
static int getBuildNumber() 
{ 
  const QDate today(QDate::currentDate()); 
  return ((today.year() - 1994) * 1000) + today.dayOfYear(); 
} 
 
static int getSubversionRevision() 
{ 
  int revision = 0; 
  QProcess process; 
  process.start("svnversion", QStringList() << "." << "--no-newline"); 
  if (process.waitForStarted() && process.waitForReadyRead()) 
  { 
    const QString str(process.readAll().constData()); 
    const int pos = str.indexOf(':'); 
    if (pos != -1) 
    { 
      revision = atoi(str.mid(pos + 1).toAscii().constData()); 
    } 
    else 
    { 
      revision = atoi(str.toAscii().constData()); 
    } 
    process.waitForFinished(); 
  } 
  return revision; 
} 
 
static QByteArray readFile(const QString& fileName) 
{ 
  QFile file(fileName); 
  if (!file.open(QIODevice::ReadOnly)) 
  { 
    return QByteArray(); 
  } 
  return file.readAll(); 
} 
 
static int writeFile(const QString& fileName, const int major, const int minor, const int revision, const int build) 
{ 
  // Create a temp file containing the version info and 
  // only replace the existing one if they are different 
  QTemporaryFile tempFile; 
  if (tempFile.open()) 
  { 
    QTextStream out(&tempFile); 
    out << "#ifndef VERSION_H\r\n"; 
    out << "#define VERSION_H\r\n\r\n"; 
    out << "namespace Version\r\n"; 
    out << "{\r\n"; 
    out << "\tstatic const int MAJOR = " << major << ";\r\n"; 
    out << "\tstatic const int MINOR = " << minor << ";\r\n"; 
    out << "\tstatic const int REVISION = " << revision << ";\r\n"; 
    out << "\tstatic const int BUILD = " << build << ";\r\n"; 
    out << "}\r\n\r\n"; 
    out << "#endif // VERSION_H\r\n"; 
 
    const QString tempFileName = tempFile.fileName(); 
    tempFile.close(); 
 
    if (!QFile::exists(fileName) || readFile(fileName) != readFile(tempFileName)) 
    { 
      QFile::remove(fileName); 
      QFile::copy(tempFileName, fileName); 
    } 
 
    return 0; 
  } 
  else 
  { 
    cout << "Error creating temporary file!" << endl; 
    return 1; 
  } 
} 
 
int main(int argc, char *argv[]) 
{ 
  if (argc != 4) 
  { 
    cout << "Usage: version major minor filename" << endl; 
    return 1; 
  } 
 
  const int major = atoi(argv[1]); 
  const int minor = atoi(argv[2]); 
  const int revision = getSubversionRevision(); 
  const int build = getBuildNumber(); 
 
  cout << major << '.' << minor << '.' << revision << '.' << build << endl;   
 
  return writeFile(argv[3], major, minor, revision, build); 
}