| 1 | #include <stdio.h> | 
|---|
| 2 | #include <stdlib.h> | 
|---|
| 3 | #include <math.h> | 
|---|
| 4 | #include <iostream.h> | 
|---|
| 5 | #include <fstream.h> | 
|---|
| 6 |  | 
|---|
| 7 | #include <string> | 
|---|
| 8 | #include <vector> | 
|---|
| 9 |  | 
|---|
| 10 | #include <typeinfo> | 
|---|
| 11 |  | 
|---|
| 12 | #include "sambainit.h" | 
|---|
| 13 | #include "skyinit.h" | 
|---|
| 14 | #include "tarrinit.h" | 
|---|
| 15 |  | 
|---|
| 16 | #include "pexceptions.h" | 
|---|
| 17 | #include "cxxcmplnk.h" | 
|---|
| 18 | #include "pdlmgr.h" | 
|---|
| 19 | #include "timing.h" | 
|---|
| 20 |  | 
|---|
| 21 | typedef void (* DlFunctionOfVecStr) (vector<string> & args, int & rc); | 
|---|
| 22 |  | 
|---|
| 23 | static void Usage(bool fgerr); | 
|---|
| 24 | static int  GetInput(string & codeflnm); | 
|---|
| 25 | static int  FillCxxFile(string & codeflnm, string & cxxfile); | 
|---|
| 26 |  | 
|---|
| 27 | /* --Fonction-- */ | 
|---|
| 28 | void Usage(bool fgerr) | 
|---|
| 29 | { | 
|---|
| 30 | if (fgerr) | 
|---|
| 31 | cout << " runcxx : Argument Error ! " << endl; | 
|---|
| 32 | else | 
|---|
| 33 | cout << " runcxx : compiling and running of a piece of C++ code " << endl; | 
|---|
| 34 |  | 
|---|
| 35 | cout << "   Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions] \n" | 
|---|
| 36 | << "                 [-tmpdir TmpDirectory] [-f C++CodeFileName] \n" | 
|---|
| 37 | << "                 [-uarg UserArg1 UserArg2 ...] \n" | 
|---|
| 38 | << "   if no file name is specified, read from standard input \n" << endl; | 
|---|
| 39 | exit(0); | 
|---|
| 40 | } | 
|---|
| 41 |  | 
|---|
| 42 |  | 
|---|
| 43 |  | 
|---|
| 44 | /* --Main-- */ | 
|---|
| 45 | int main(int narg, char *arg[]) | 
|---|
| 46 | { | 
|---|
| 47 | if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false); | 
|---|
| 48 |  | 
|---|
| 49 | // Decoding options | 
|---|
| 50 | string codefile; | 
|---|
| 51 | string compoption; | 
|---|
| 52 | string linkoption; | 
|---|
| 53 | string tmpdir; | 
|---|
| 54 |  | 
|---|
| 55 | bool fgco = false; | 
|---|
| 56 | bool fglo = false; | 
|---|
| 57 | bool fgcofn = false; | 
|---|
| 58 | bool fgtmp = false; | 
|---|
| 59 |  | 
|---|
| 60 | vector<string> uarg; | 
|---|
| 61 |  | 
|---|
| 62 | cout << " runcxx: Decoding command line options ... " << endl; | 
|---|
| 63 | for (int k=1; k<narg; k++)   { | 
|---|
| 64 | if (strcmp(arg[k], "-compopt") == 0)  { | 
|---|
| 65 | if (k == narg-1) Usage(true); | 
|---|
| 66 | fgco = true;  k++; | 
|---|
| 67 | compoption = arg[k]; | 
|---|
| 68 | } | 
|---|
| 69 | else if (strcmp(arg[k], "-linkopt") == 0) { | 
|---|
| 70 | if (k == narg-1) Usage(true); | 
|---|
| 71 | fglo = true;  k++; | 
|---|
| 72 | linkoption = arg[k]; | 
|---|
| 73 | } | 
|---|
| 74 | else if (strcmp(arg[k], "-tmpdir") == 0) { | 
|---|
| 75 | if (k == narg-1) Usage(true); | 
|---|
| 76 | fgtmp = true;  k++; | 
|---|
| 77 | tmpdir = arg[k]; | 
|---|
| 78 | } | 
|---|
| 79 | else if (strcmp(arg[k], "-f") == 0) { | 
|---|
| 80 | if (k == narg-1) Usage(true); | 
|---|
| 81 | fgcofn = true;  k++; | 
|---|
| 82 | codefile = arg[k]; | 
|---|
| 83 | } | 
|---|
| 84 | else if (strcmp(arg[k], "-uarg") == 0) { | 
|---|
| 85 | if (k == narg-1) Usage(true); | 
|---|
| 86 | for(int kk=k+1; kk<narg; kk++) | 
|---|
| 87 | uarg.push_back(arg[kk]); | 
|---|
| 88 | break; | 
|---|
| 89 | } | 
|---|
| 90 | else Usage(true); | 
|---|
| 91 | } | 
|---|
| 92 |  | 
|---|
| 93 | // Checking TMPDIR | 
|---|
| 94 | if (!fgtmp) {  // TMPDIR pas defini par option de ligne de commande | 
|---|
| 95 | char* varenv; | 
|---|
| 96 | if ( (varenv=getenv("TMPDIR")) != NULL )  { | 
|---|
| 97 | tmpdir = varenv; | 
|---|
| 98 | fgtmp = true; | 
|---|
| 99 | } | 
|---|
| 100 | } | 
|---|
| 101 | if (tmpdir.length() > 0) | 
|---|
| 102 | if (tmpdir[tmpdir.length()-1] != '/') tmpdir += '/'; | 
|---|
| 103 |  | 
|---|
| 104 | int rc = 0; | 
|---|
| 105 |  | 
|---|
| 106 | try { | 
|---|
| 107 | SophyaInit(); | 
|---|
| 108 | InitTim(); | 
|---|
| 109 |  | 
|---|
| 110 | if (!fgcofn) {  // Pas de fichier specifie - Lecture de fichier sur stdin | 
|---|
| 111 | codefile = tmpdir + "aucode.icc"; | 
|---|
| 112 | rc = GetInput(codefile); | 
|---|
| 113 | } | 
|---|
| 114 | string flnm; | 
|---|
| 115 | if (rc == 0) { | 
|---|
| 116 | flnm = tmpdir + "acxxrun.cc"; | 
|---|
| 117 | rc = FillCxxFile(codefile, flnm); | 
|---|
| 118 | } | 
|---|
| 119 |  | 
|---|
| 120 | PrtTim(" End of FillCxxFile "); | 
|---|
| 121 |  | 
|---|
| 122 | string oname, soname; | 
|---|
| 123 | if (rc == 0) { | 
|---|
| 124 | CxxCompilerLinker cxx; | 
|---|
| 125 | if (fgtmp) cxx.SetTmpDir(tmpdir); | 
|---|
| 126 | if (fgco) cxx.AddCompileOptions(compoption); | 
|---|
| 127 | if (fglo) cxx.AddLinkOptions(linkoption); | 
|---|
| 128 | cxx.SetVerbose(true); | 
|---|
| 129 |  | 
|---|
| 130 | rc = cxx.Compile(flnm, oname); | 
|---|
| 131 | cout << " RC from cxx.Compile() = " << rc << " oname= " << oname << endl; | 
|---|
| 132 | if (rc == 0) { | 
|---|
| 133 | rc = cxx.BuildSO(oname, soname); | 
|---|
| 134 | cout << " RC from cxx.BuildSO() = " << rc << " soname= " << soname << endl; | 
|---|
| 135 | } | 
|---|
| 136 | PrtTim(" End of Compile-Link "); | 
|---|
| 137 | } | 
|---|
| 138 | if (rc == 0) { | 
|---|
| 139 | string funcname = "runcxx_usercode"; | 
|---|
| 140 | if (fgtmp) PDynLinkMgr::SetTmpDir(tmpdir); | 
|---|
| 141 | PDynLinkMgr dyl(soname); | 
|---|
| 142 | DlFunction f = dyl.GetFunction(funcname); | 
|---|
| 143 | if (f != NULL) { | 
|---|
| 144 | cout << "DlFunctionOfVecStr  f linked OK - calling f(uarg) ... \n" << endl; | 
|---|
| 145 | DlFunctionOfVecStr fvs = (DlFunctionOfVecStr)f; | 
|---|
| 146 | int rcu = 0; | 
|---|
| 147 | fvs(uarg, rcu); | 
|---|
| 148 | cout << "\n RC from UserCode() = " << rcu << endl; | 
|---|
| 149 | rc = rcu; | 
|---|
| 150 | } | 
|---|
| 151 | else { | 
|---|
| 152 | cout << " ERROR linking DlFunction f !!! " << endl; | 
|---|
| 153 | } | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 |  | 
|---|
| 157 | catch (PThrowable & exc) { | 
|---|
| 158 | cerr << " Catched Exception " << (string)typeid(exc).name() | 
|---|
| 159 | << " - Msg= " << exc.Msg() << endl; | 
|---|
| 160 | } | 
|---|
| 161 | catch (...) { | 
|---|
| 162 | cerr << " some other exception was caught ! " << endl; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | PrtTim(" End of runcxx "); | 
|---|
| 166 | return(rc); | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | /* --Fonction-- */ | 
|---|
| 170 | int  GetInput(string & codeflnm) | 
|---|
| 171 | { | 
|---|
| 172 | cout << " runcxx/GetInput() : Type in your C++ code, \n" | 
|---|
| 173 | << "     end with a blanck line OR <Cntl>D " << endl; | 
|---|
| 174 | int line = 0; | 
|---|
| 175 | ofstream os(codeflnm.c_str(),ios::out); | 
|---|
| 176 | os << "\n" ; | 
|---|
| 177 | bool fg = true; | 
|---|
| 178 | char buff[512]; | 
|---|
| 179 | char * ret; | 
|---|
| 180 | while (fg) { | 
|---|
| 181 | printf("L%d ? ", line+1); | 
|---|
| 182 | fflush(stdout); | 
|---|
| 183 | buff[0] = '\0'; | 
|---|
| 184 | ret = fgets(buff, 512, stdin); | 
|---|
| 185 | buff[511] = '\0'; | 
|---|
| 186 | if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) { | 
|---|
| 187 | os << buff << endl;  line++; | 
|---|
| 188 | } | 
|---|
| 189 | else fg = false; | 
|---|
| 190 | } | 
|---|
| 191 | os << "\n" ; | 
|---|
| 192 | cout << " \n Total " << line << " lines copied to file " <<  codeflnm << endl; | 
|---|
| 193 | if (line > 0) return(0); | 
|---|
| 194 | else return(1); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | /* --Fonction-- */ | 
|---|
| 198 | int  FillCxxFile(string & code, string & cxxfile) | 
|---|
| 199 | { | 
|---|
| 200 | ofstream os(cxxfile.c_str(),ios::out); | 
|---|
| 201 |  | 
|---|
| 202 | os<<"#include \"machdefs.h\" \n"<<endl; | 
|---|
| 203 |  | 
|---|
| 204 | os<<"//---- System et stdc++ include files \n" | 
|---|
| 205 | <<"#include <stdio.h> \n" | 
|---|
| 206 | <<"#include <stdlib.h> \n" | 
|---|
| 207 | <<"#include <math.h> \n" | 
|---|
| 208 | <<"#include <ctype.h> \n" | 
|---|
| 209 | <<"#include <string.h> \n" | 
|---|
| 210 | <<"#include <iostream.h> \n" | 
|---|
| 211 | <<"#include <fstream.h> \n" | 
|---|
| 212 | <<"#include <complex> \n" | 
|---|
| 213 | <<endl | 
|---|
| 214 |  | 
|---|
| 215 | <<"#include <typeinfo> \n" | 
|---|
| 216 | <<"#include <string> \n" | 
|---|
| 217 | <<"#include <vector> \n" | 
|---|
| 218 | <<"#include <map> \n" | 
|---|
| 219 | <<"#include <functional> \n" | 
|---|
| 220 | <<"#include <list> \n" | 
|---|
| 221 | <<endl | 
|---|
| 222 |  | 
|---|
| 223 | <<"//---- Sophya include files \n" | 
|---|
| 224 | <<"#include \"systools.h\" \n" | 
|---|
| 225 | <<"#include \"ntools.h\" \n" | 
|---|
| 226 | <<"#include \"array.h\" \n" | 
|---|
| 227 | <<"#include \"histats.h\" \n" | 
|---|
| 228 | <<"#include \"skymap.h\" \n" | 
|---|
| 229 | <<"#include \"samba.h\" \n" | 
|---|
| 230 | <<"#include \"skyt.h\" \n" | 
|---|
| 231 | <<endl; | 
|---|
| 232 |  | 
|---|
| 233 | os << "extern \"C\" { \n" | 
|---|
| 234 | << " void runcxx_usercode(vector<string>& arg, int & rc); \n" | 
|---|
| 235 | //     << " void runcxx_usercode(); \n" | 
|---|
| 236 | << " } \n " << endl; | 
|---|
| 237 |  | 
|---|
| 238 | os << "// ---- User Code function ----- \n" | 
|---|
| 239 | << " void runcxx_usercode(vector<string>& arg, int & rc) \n" | 
|---|
| 240 | //     << " void runcxx_usercode() \n" | 
|---|
| 241 | << "{ \n" | 
|---|
| 242 | << "#include \"" << code << "\" \n" | 
|---|
| 243 | << "} \n" << endl; | 
|---|
| 244 |  | 
|---|
| 245 | return(0); | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|