#include #include #include #include #include #include #include #include #include "sambainit.h" #include "skyinit.h" #include "tarrinit.h" #include "pexceptions.h" #include "cxxcmplnk.h" #include "pdlmgr.h" #include "timing.h" typedef void (* DlFunctionOfVecStr) (vector & args, int & rc); static void Usage(bool fgerr); static int GetInput(string & codeflnm); static int FillCxxFile(string & codeflnm, string & cxxfile); /* --Fonction-- */ void Usage(bool fgerr) { if (fgerr) cout << " runcxx : Argument Error ! " << endl; else cout << " runcxx : compiling and running of a piece of C++ code " << endl; cout << " Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions] \n" << " [-tmpdir TmpDirectory] [-f C++CodeFileName] \n" << " [-uarg UserArg1 UserArg2 ...] \n" << " if no file name is specified, read from standard input \n" << endl; exit(0); } /* --Main-- */ int main(int narg, char *arg[]) { if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false); // Decoding options string codefile; string compoption; string linkoption; string tmpdir; bool fgco = false; bool fglo = false; bool fgcofn = false; bool fgtmp = false; vector uarg; cout << " runcxx: Decoding command line options ... " << endl; for (int k=1; k 0) if (tmpdir[tmpdir.length()-1] != '/') tmpdir += '/'; int rc = 0; try { SophyaInit(); InitTim(); if (!fgcofn) { // Pas de fichier specifie - Lecture de fichier sur stdin codefile = tmpdir + "aucode.icc"; rc = GetInput(codefile); } string flnm; if (rc == 0) { flnm = tmpdir + "acxxrun.cc"; rc = FillCxxFile(codefile, flnm); } PrtTim(" End of FillCxxFile "); string oname, soname; if (rc == 0) { CxxCompilerLinker cxx; if (fgtmp) cxx.SetTmpDir(tmpdir); if (fgco) cxx.AddCompileOptions(compoption); if (fglo) cxx.AddLinkOptions(linkoption); cxx.SetVerbose(true); rc = cxx.Compile(flnm, oname); cout << " RC from cxx.Compile() = " << rc << " oname= " << oname << endl; rc = cxx.BuildSO(oname, soname); cout << " RC from cxx.BuildSO() = " << rc << " soname= " << soname << endl; PrtTim(" End of Compile-Link "); } if (rc == 0) { string funcname = "runcxx_usercode"; if (fgtmp) PDynLinkMgr::SetTmpDir(tmpdir); PDynLinkMgr dyl(soname); DlFunction f = dyl.GetFunction(funcname); if (f != NULL) { cout << "DlFunctionOfVecStr f linked OK - calling f(uarg) ... \n" << endl; DlFunctionOfVecStr fvs = (DlFunctionOfVecStr)f; int rcu = 0; fvs(uarg, rcu); cout << "\n RC from UserCode() = " << rcu << endl; rc = rcu; } else { cout << " ERROR linking DlFunction f !!! " << endl; } } } catch (PThrowable & exc) { cerr << " Catched Exception " << (string)typeid(exc).name() << " - Msg= " << exc.Msg() << endl; } catch (...) { cerr << " some other exception was caught ! " << endl; } PrtTim(" End of runcxx "); return(rc); } /* --Fonction-- */ int GetInput(string & codeflnm) { cout << " runcxx/GetInput() : Type in your C++ code, \n" << " end with a blanck line OR D " << endl; int line = 0; ofstream os(codeflnm.c_str(),ios::out); os << "\n" ; bool fg = true; char buff[512]; char * ret; while (fg) { printf("L%d ? ", line+1); fflush(stdout); buff[0] = '\0'; ret = fgets(buff, 512, stdin); buff[511] = '\0'; if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) { os << buff << endl; line++; } else fg = false; } os << "\n" ; cout << " \n Total " << line << " lines copied to file " << codeflnm << endl; if (line > 0) return(0); else return(1); } /* --Fonction-- */ int FillCxxFile(string & code, string & cxxfile) { ofstream os(cxxfile.c_str(),ios::out); os<<"#include \"machdefs.h\" \n"< \n" <<"#include \n" <<"#include \n" <<"#include \n" <<"#include \n" <<"#include \n" <<"#include \n" <<"#include \n" < \n" <<"#include \n" <<"#include \n" <<"#include \n" <<"#include \n" <<"#include \n" <& arg, int & rc); \n" // << " void runcxx_usercode(); \n" << " } \n " << endl; os << "// ---- User Code function ----- \n" << " void runcxx_usercode(vector& arg, int & rc) \n" // << " void runcxx_usercode() \n" << "{ \n" << "#include \"" << code << "\" \n" << "} \n" << endl; return(0); }