diff options
Diffstat (limited to 'include/version.cpp')
-rw-r--r-- | include/version.cpp | 123 |
1 files changed, 88 insertions, 35 deletions
diff --git a/include/version.cpp b/include/version.cpp index b0ea0423d..119403e13 100644 --- a/include/version.cpp +++ b/include/version.cpp @@ -13,7 +13,7 @@ #include <iostream> #include <fstream> #include <sstream> -#include <list> +#include <map> static std::string get_git_hash(const std::string &git_dir) { @@ -45,29 +45,16 @@ static std::string get_git_hash(const std::string &git_dir) return "g" + filebuf.substr(0, 7); } -int main(int argc, char *argv[]) +static bool read_version_sh(const std::string &version_sh, std::map<std::string, std::string> &versions) { - if (argc < 3) - { - std::cerr << "Syntax: " << argv[0] << " <base> <version.h>" << std::endl; - return 1; - } - - std::string version_sh = std::string(argv[1]) + "/src/version.sh"; - std::string git_dir = std::string(argv[1]) + "/.git"; - - std::fstream fd; - - fd.clear(); - fd.open(version_sh.c_str(), std::ios::in); + std::fstream fd(version_sh.c_str(), std::ios::in); if (!fd.is_open()) { std::cerr << "Error: Unable to open src/version.sh for reading: " << version_sh << std::endl; - return 1; + return false; } std::string filebuf; - std::list<std::pair<std::string, std::string> > versions; while (getline(fd, filebuf)) { if (!filebuf.find("VERSION_")) @@ -75,24 +62,25 @@ int main(int argc, char *argv[]) size_t eq = filebuf.find('='); std::string type = filebuf.substr(0, eq); - std::string value = filebuf.substr(eq + 2, filebuf.length() - eq - 3); - versions.push_back(std::make_pair(type, value)); + std::string value = filebuf.substr(eq + 1); + + versions[type] = value; } } fd.close(); - std::string git_version = get_git_hash(git_dir); - if (!git_version.empty()) - versions.push_back(std::make_pair("VERSION_GIT", git_version)); + return true; +} - fd.clear(); - fd.open(argv[2], std::ios::in); +static bool write_build_h(const std::string &buildh) +{ + std::fstream fd(buildh.c_str(), std::ios::in); std::string build = "#define BUILD 1"; if (fd.is_open()) { - while (getline(fd, filebuf)) + for (std::string filebuf; getline(fd, filebuf);) { if (!filebuf.find("#define BUILD")) { @@ -110,27 +98,92 @@ int main(int argc, char *argv[]) } fd.clear(); - fd.open(argv[2], std::ios::out); - + fd.open(buildh.c_str(), std::ios::out); if (!fd.is_open()) { - std::cerr << "Error: Unable to include/version.h for writing: " << argv[2] << std::endl; - return 1; + std::cerr << "Error: Unable to open build.h for writing: " << buildh << std::endl; + return false; } fd << "/* This file is automatically generated by version.cpp - do not edit it! */" << std::endl; + fd << build << std::endl; + fd.close(); + + return true; +} - for (std::list<std::pair<std::string, std::string> >::iterator it = versions.begin(), it_end = versions.end(); it != it_end; ++it) +static void read_version_h(const std::string &versionh, std::map<std::string, std::string> &versions) +{ + std::fstream fd(versionh.c_str(), std::ios::in); + + if (!fd.is_open()) + return; + + for (std::string filebuf; getline(fd, filebuf);) { - if (it->first == "VERSION_EXTRA" || it->first == "VERSION_GIT") - fd << "#define " << it->first << " \"" << it->second << "\"" << std::endl; - else - fd << "#define " << it->first << " " << it->second << std::endl; + if (!filebuf.find("#define VERSION_")) + { + size_t space = filebuf.substr(8).find(' '); + + std::string name = filebuf.substr(8).substr(0, space), + version = filebuf.substr(8).substr(space + 1); + + versions[name] = version; + } } - fd << build << std::endl; + fd.close(); +} + +static bool write_version_h(const std::string &versionh, const std::map<std::string, std::string> &versions) +{ + std::fstream fd(versionh.c_str(), std::ios::out); + + if (!fd.is_open()) + return false; + + for (std::map<std::string, std::string>::const_iterator it = versions.begin(); it != versions.end(); ++it) + { + fd << "#define " << it->first << " " << it->second << std::endl; + } fd.close(); + return true; +} + +int main(int argc, char *argv[]) +{ + if (argc < 4) + { + std::cerr << "Syntax: " << argv[0] << " <base> <version.h> <build.h>" << std::endl; + return 1; + } + + std::string version_sh = std::string(argv[1]) + "/src/version.sh"; + std::string git_dir = std::string(argv[1]) + "/.git"; + std::string versionh = argv[2]; + std::string buildh = argv[3]; + + std::map<std::string, std::string> versions, old_versions; + + if (!read_version_sh(version_sh, versions)) + return -1; + + std::string git_version = get_git_hash(git_dir); + if (!git_version.empty()) + versions["VERSION_GIT"] = '"' + git_version + '"'; + + if (!write_build_h(buildh)) + return -1; + + read_version_h(versionh, old_versions); + + if (versions == old_versions) + return 0; + + if (!write_version_h(versionh, versions)) + return -1; + return 0; } |