Gambatte: Some Trace/Disassemble fixes
[lsnes.git] / buildaux / version.cpp
blob7c1b4ba37d111b9b80b123e4dff5c5d0a8143b98
1 #include <cstdio>
2 #include <cstdlib>
3 #include <string>
4 #include <iostream>
5 #include <sstream>
6 #include <fstream>
8 std::string X = "$Format:%h by %cn on %ci$";
10 std::string derive_format(std::string kwformat)
12 if(kwformat[0] != '$' || kwformat[1] != 'F' || kwformat[kwformat.length() - 1] != '$') {
13 std::cerr << "Bad keyword format '" << kwformat << "'" << std::endl;
14 exit(1);
16 return "--pretty=f" + kwformat.substr(2, kwformat.length() - 3);
19 std::string shellquote(std::string arg)
21 std::ostringstream x;
22 x << "'";
23 for(size_t i = 0; i < arg.length(); i++) {
24 if(arg[i] == '\'')
25 x << "\\'";
26 else
27 x << arg[i];
29 x << "'";
30 return x.str();
33 std::string runlog(std::string logformat)
35 std::string command = "git log " + shellquote(logformat) + " -1";
36 std::string retval;
37 int r;
38 char buf[4096] = {0};
39 FILE* out = popen(command.c_str(), "r");
40 if(!out) {
41 std::cerr << "Can't invoke git to get the version" << std::endl;
42 exit(1);
44 while((r = fread(buf, 1, 4095, out)) > 0) {
45 buf[r] = 0;
46 retval = retval + buf;
48 if(ferror(out)) {
49 std::cerr << "Error reading git version output" << std::endl;
50 exit(1);
52 pclose(out);
53 return retval;
56 std::string get_main_version()
58 std::ifstream x("VERSION");
59 if(!x) {
60 std::cerr << "Error reading main version" << std::endl;
61 exit(1);
63 std::string out;
64 std::getline(x, out);
65 if(out == "") {
66 std::cerr << "Error reading main version" << std::endl;
67 exit(1);
69 return out;
72 int main()
74 std::string gitversion;
75 std::string mainversion = get_main_version();
76 if(X[0] == '$') {
77 std::string logformat = derive_format(X);
78 gitversion = runlog(logformat);
79 } else
80 gitversion = X;
81 std::cout << "#include <string>" << std::endl;
82 std::cout << "std::string lsnes_git_revision = \"" << gitversion << "\";" << std::endl;
83 std::cout << "std::string lsnes_version = \"" << mainversion << "\";" << std::endl;
84 return 0;