Functions
generateVersionAndContext.cc File Reference
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fstream>
#include <ctime>

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 8 of file generateVersionAndContext.cc.

9 {
10 
11  if (argc != 2) {
12  std::cerr << " usage: generateVersionAndContext <cmake source directory> " << std::endl;
13  return 1;
14  }
15 
16  FILE *fp;
17  char gitRes[1035];
18  std::string cMakeDirectoryStr(argv[1]);
19  char *g4lbneDirChr = getenv("G4LBNE_DIR");
20  if (g4lbneDirChr == 0) {
21  std::cerr << " Environment variable G4LBNE_DIR not set.... " << std::endl;
22  return 1;
23  }
24 // std::string g4lbneDirStr(g4lbneDirChr);
25  chdir(g4lbneDirChr);
26  std::string g4lbneDirStr(g4lbneDirChr);
27  if (cMakeDirectoryStr != g4lbneDirStr) {
28  std::cerr << " Possible confusion on where the source was installed, CMake src " <<
29  cMakeDirectoryStr << " env. var. from setup " << g4lbneDirStr << std::endl;
30  }
31  /* Open the command for reading. */
32  fp = popen("git describe --all", "r");
33  if (fp == 0) {
34  std::cerr << "Failed to run the git command" << std::endl;
35  exit(1);
36  }
37  fgets(gitRes, sizeof(gitRes)-1, fp);
38  // remove the last character,
39  std::string gitResStr(gitRes);
40  gitResStr = gitResStr.substr(0, gitResStr.length()-1);
41  std::cerr << " git version.. " << gitResStr << std::endl;
42 
43  std::ofstream fOutCC("./VersionAndContext.cc");
44  fOutCC << "#include <iostream>" << std::endl;
45  fOutCC << "#include <stdlib.h>" << std::endl;
46  fOutCC << "#include <stdio.h>" << std::endl;
47  fOutCC << "#include <unistd.h>" << std::endl;
48  fOutCC << "#include <fstream>" << std::endl << std::endl;
49  fOutCC << "#include \"VersionAndContext.hh\" " << std::endl << std::endl;
50  fOutCC << "VersionAndContext::VersionAndContext():" << std::endl;
51  fOutCC << " gitDescription(\""<< gitResStr << "\")," << std::endl;
52  fOutCC << " g4lbnfDir(\""<< g4lbneDirStr << "\")," << std::endl;
53  time_t t = time(0); // get time now
54  struct tm * now = localtime( & t );
55  char bufTime[80];
56  // Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
57  // for more information about date/time format
58  strftime(bufTime, sizeof(bufTime), "%Y-%m-%d.%X", now);
59  fOutCC << " buildTimeStamp(\"" << std::string(bufTime)
60  << "\") " << std::endl;
61 
62  fOutCC << " { ; } " << std::endl << std::endl;
63  fOutCC << "VersionAndContext* VersionAndContext::fInstance = 0; " << std::endl << std::endl;
64  fOutCC << "VersionAndContext* VersionAndContext::Instance() { " << std::endl;
65  fOutCC << " if (fInstance == 0) fInstance = new VersionAndContext(); " << std::endl;
66  fOutCC << " return fInstance; } " << std::endl;
67 
68  fOutCC.close();
69  return 0;
70 }
std::string string
Definition: nybbler.cc:12
std::string getenv(std::string const &name)
Definition: getenv.cc:15
QTextStream & endl(QTextStream &s)