test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ExpressionEvaluator.cc
Go to the documentation of this file.
6 
7 #include "popenCPP.h"
8 
9 #include <fstream>
10 #include <regex>
11 #include <dlfcn.h>
12 
13 // #define VI_DEBUG
14 
15 #ifdef VI_DEBUG
16 #include <iostream>
17 #define COUT std::cout
18 #else
19 #define COUT LogDebug("ExpressionEvaluator")
20 #endif
21 
22 using namespace reco::exprEvalDetails;
23 
24 
25 namespace {
26  std::string generateName() {
27  auto n1 = execSysCommand("uuidgen | sed 's/-//g'");
28  n1.pop_back();
29  return n1;
30  }
31 
32  void remove(std::string const & name) {
33  std::string sfile = "/tmp/"+name+".cc";
34  std::string ofile = "/tmp/"+name+".so";
35 
36  std::string rm="rm -f "; rm+=sfile+' '+ofile;
37 
38  system(rm.c_str());
39 
40  }
41 
42  std::string patchArea() {
43  auto n1 = execSysCommand("pushd $CMSSW_BASE > /dev/null;scram tool tag cmssw CMSSW_BASE; popd > /dev/null");
44  n1.pop_back();
45  COUT << "base area " << n1 << std::endl;
46  return n1[0]=='/' ? n1 : std::string();
47  }
48 
49 
50 }
51 
52 namespace reco{
53 
54 ExpressionEvaluator::ExpressionEvaluator(const char * pkg, const char * iname, std::string const & iexpr) :
55  m_name("VI_"+generateName())
56 {
57 
58 
59  std::string pch = pkg; pch += "/src/precompile.h";
60  std::string quote("\"");
61 
62 
63  std::string sfile = "/tmp/"+m_name+".cc";
64  std::string ofile = "/tmp/"+m_name+".so";
65 
66  auto arch = edm::getEnvironmentVariable("SCRAM_ARCH");
67  auto baseDir = edm::getEnvironmentVariable("CMSSW_BASE");
68  auto relDir = edm::getEnvironmentVariable("CMSSW_RELEASE_BASE");
69 
70 
71  std::string incDir = "/include/" + arch + "/";
72  std::string cxxf;
73  {
74  // look in local dir
75  std::string file = baseDir + incDir + pch + ".cxxflags";
76  std::ifstream ss(file.c_str());
77  COUT << "local file: " << file << std::endl;
78  if (ss) {
79  std::getline(ss,cxxf);
80  incDir = baseDir + incDir;
81  } else {
82  // look in release area
83  std::string file = relDir + incDir + pch + ".cxxflags";
84  COUT << "file in release area: " << file << std::endl;
85  std::ifstream ss(file.c_str());
86  if (ss) {
87  std::getline(ss,cxxf);
88  incDir = relDir + incDir;
89  } else {
90  // look in release is a patch area
91  auto paDir = patchArea();
92  if (paDir.empty()) throw cms::Exception("ExpressionEvaluator", "error in opening patch area for "+ baseDir);
93  std::string file = paDir + incDir + pch + ".cxxflags";
94  COUT << "file in base release area: " << file << std::endl;
95  std::ifstream ss(file.c_str());
96  if (!ss) throw cms::Exception("ExpressionEvaluator", pch + " file not found neither in " + baseDir + " nor in " + relDir + " nor in " + paDir);
97  std::getline(ss,cxxf);
98  incDir = paDir + incDir;
99  }
100  }
101 
102  { std::regex rq("-I[^ ]+"); cxxf = std::regex_replace(cxxf,rq,std::string("")); }
103  { std::regex rq("=\""); cxxf = std::regex_replace(cxxf,rq,std::string("='\"")); }
104  { std::regex rq("\" "); cxxf = std::regex_replace(cxxf,rq,std::string("\"' ")); }
105  COUT << '|' << cxxf << "|\n" << std::endl;
106 
107  }
108 
109  std::string cpp = "c++ -H -Wall -shared -Winvalid-pch "; cpp+=cxxf;
110  cpp += " -I" + incDir;
111  cpp += " -o " + ofile + ' ' + sfile+" 2>&1\n";
112 
113  COUT << cpp << std::endl;
114 
115 
116  // prepare the file to compile
117  std::string factory = "factory" + m_name;
118 
119  std::string source = std::string("#include ")+quote+ pch +quote+"\n";
120  source+="struct "+m_name+" final : public "+iname + "{\n";
121  source+=iexpr;
122  source+="\n};\n";
123 
124 
125  source += "extern " + quote+'C'+quote+' ' + std::string(iname) + "* "+factory+"() {\n";
126  source += "static "+m_name+" local;\n";
127  source += "return &local;\n}\n";
128 
129 
130  COUT << source << std::endl;
131 
132  {
133  std::ofstream tmp(sfile.c_str());
134  tmp<<source << std::endl;
135  }
136 
137 
138 
139  // compile
140  auto ss = execSysCommand(cpp);
141  COUT << ss << std::endl;
142 
143  void * dl = dlopen(ofile.c_str(),RTLD_LAZY);
144  if (!dl) {
145  remove(m_name);
146  throw cms::Exception("ExpressionEvaluator", std::string("compilation/linking failed\n") + cpp + ss + "dlerror " + dlerror());
147  return;
148  }
149 
150  m_expr = dlsym(dl,factory.c_str());
151  remove(m_name);
152 }
153 
154 
156  remove(m_name);
157 }
158 
159 
160 
161 }
std::string execSysCommand(const std::string &cmdline)
Definition: popenCPP.h:14
ExpressionEvaluator(const char *pkg, const char *iname, const std::string &iexpr)
#define COUT
string rm
Definition: submit.py:76
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
static std::string const source
Definition: EdmProvDump.cc:42
std::string getEnvironmentVariable(std::string const &name, std::string const &defaultValue=std::string())