Anope IRC Services  Version 2.0
version.cpp
Go to the documentation of this file.
1 /* Build bumper
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include <cstdlib>
13 #include <iostream>
14 #include <fstream>
15 #include <sstream>
16 #include <list>
17 
18 static std::string get_git_hash(const std::string &git_dir)
19 {
20  std::fstream fd;
21  std::string filebuf;
22 
23  fd.open((git_dir + "/HEAD").c_str(), std::ios::in);
24  if (!fd.is_open())
25  return "";
26  if (!getline(fd, filebuf) || filebuf.find("ref: ") != 0)
27  {
28  fd.close();
29  return "";
30  }
31 
32  fd.close();
33 
34  filebuf = filebuf.substr(5);
35  fd.open((git_dir + "/" + filebuf).c_str(), std::ios::in);
36  if (!fd.is_open())
37  return "";
38  if (!getline(fd, filebuf))
39  {
40  fd.close();
41  return "";
42  }
43  fd.close();
44 
45  return "g" + filebuf.substr(0, 7);
46 }
47 
48 int main(int argc, char *argv[])
49 {
50  if (argc < 3)
51  {
52  std::cerr << "Syntax: " << argv[0] << " <base> <version.h>" << std::endl;
53  return 1;
54  }
55 
56  std::string version_sh = std::string(argv[1]) + "/src/version.sh";
57  std::string git_dir = std::string(argv[1]) + "/.git";
58 
59  std::fstream fd;
60 
61  fd.clear();
62  fd.open(version_sh.c_str(), std::ios::in);
63  if (!fd.is_open())
64  {
65  std::cerr << "Error: Unable to open src/version.sh for reading: " << version_sh << std::endl;
66  return 1;
67  }
68 
69  std::string filebuf;
70  std::list<std::pair<std::string, std::string> > versions;
71  while (getline(fd, filebuf))
72  {
73  if (!filebuf.find("VERSION_"))
74  {
75  size_t eq = filebuf.find('=');
76 
77  std::string type = filebuf.substr(0, eq);
78  std::string value = filebuf.substr(eq + 2, filebuf.length() - eq - 3);
79  versions.push_back(std::make_pair(type, value));
80  }
81  }
82 
83  fd.close();
84 
85  std::string git_version = get_git_hash(git_dir);
86  if (!git_version.empty())
87  versions.push_back(std::make_pair("VERSION_GIT", git_version));
88 
89  fd.clear();
90  fd.open(argv[2], std::ios::in);
91 
92  std::string build = "#define BUILD 1";
93  if (fd.is_open())
94  {
95  while (getline(fd, filebuf))
96  {
97  if (!filebuf.find("#define BUILD"))
98  {
99  size_t tab = filebuf.find(' ');
100 
101  int ibuild = atoi(filebuf.substr(tab + 1).c_str()) + 1;
102 
103  std::stringstream ss;
104  ss << "#define BUILD " << ibuild;
105  build = ss.str();
106  }
107  }
108 
109  fd.close();
110  }
111 
112  fd.clear();
113  fd.open(argv[2], std::ios::out);
114 
115  if (!fd.is_open())
116  {
117  std::cerr << "Error: Unable to include/version.h for writing: " << argv[2] << std::endl;
118  return 1;
119  }
120 
121  fd << "/* This file is automatically generated by version.cpp - do not edit it! */" << std::endl;
122 
123  for (std::list<std::pair<std::string, std::string> >::iterator it = versions.begin(), it_end = versions.end(); it != it_end; ++it)
124  {
125  if (it->first == "VERSION_EXTRA" || it->first == "VERSION_GIT")
126  fd << "#define " << it->first << " \"" << it->second << "\"" << std::endl;
127  else
128  fd << "#define " << it->first << " " << it->second << std::endl;
129  }
130 
131  fd << build << std::endl;
132 
133  fd.close();
134 
135  return 0;
136 }
int main(int argc, char *argv[])
Definition: version.cpp:48
static std::string get_git_hash(const std::string &git_dir)
Definition: version.cpp:18
std::basic_string< char, ci_char_traits, std::allocator< char > > string
Definition: hashcomp.h:133