[1306] | 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 |
|
---|
| 125 | CxxCompilerLinker cxx;
|
---|
| 126 | if (fgtmp) cxx.SetTmpDir(tmpdir);
|
---|
| 127 | if (fgco) cxx.AddCompileOptions(compoption);
|
---|
| 128 | if (fglo) cxx.AddLinkOptions(linkoption);
|
---|
| 129 | cxx.SetVerbose(true);
|
---|
| 130 |
|
---|
| 131 | rc = cxx.Compile(flnm, oname);
|
---|
| 132 | cout << " RC from cxx.Compile() = " << rc << " oname= " << oname << endl;
|
---|
| 133 | rc = cxx.BuildSO(oname, soname);
|
---|
| 134 | cout << " RC from cxx.BuildSO() = " << rc << " soname= " << soname << endl;
|
---|
| 135 | PrtTim(" End of Compile-Link ");
|
---|
| 136 | }
|
---|
| 137 | if (rc == 0) {
|
---|
| 138 | string funcname = "runcxx_usercode";
|
---|
| 139 | if (fgtmp) PDynLinkMgr::SetTmpDir(tmpdir);
|
---|
| 140 | PDynLinkMgr dyl(soname);
|
---|
| 141 | DlFunction f = dyl.GetFunction(funcname);
|
---|
| 142 | if (f != NULL) {
|
---|
| 143 | cout << "DlFunctionOfVecStr f linked OK - calling f(uarg) ... \n" << endl;
|
---|
| 144 | DlFunctionOfVecStr fvs = (DlFunctionOfVecStr)f;
|
---|
| 145 | int rcu = 0;
|
---|
| 146 | fvs(uarg, rcu);
|
---|
| 147 | cout << "\n RC from UserCode() = " << rcu << endl;
|
---|
| 148 | rc = rcu;
|
---|
| 149 | }
|
---|
| 150 | else {
|
---|
| 151 | cout << " ERROR linking DlFunction f !!! " << endl;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 | }
|
---|
| 155 |
|
---|
| 156 | catch (PThrowable & exc) {
|
---|
| 157 | cerr << " Catched Exception " << (string)typeid(exc).name()
|
---|
| 158 | << " - Msg= " << exc.Msg() << endl;
|
---|
| 159 | }
|
---|
| 160 | catch (...) {
|
---|
| 161 | cerr << " some other exception was caught ! " << endl;
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | PrtTim(" End of runcxx ");
|
---|
| 165 | return(rc);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | /* --Fonction-- */
|
---|
| 169 | int GetInput(string & codeflnm)
|
---|
| 170 | {
|
---|
| 171 | cout << " runcxx/GetInput() : Type in your C++ code, \n"
|
---|
| 172 | << " end with a blanck line OR <Cntl>D " << endl;
|
---|
| 173 | int line = 0;
|
---|
| 174 | ofstream os(codeflnm.c_str(),ios::out);
|
---|
| 175 | os << "\n" ;
|
---|
| 176 | bool fg = true;
|
---|
| 177 | char buff[512];
|
---|
| 178 | char * ret;
|
---|
| 179 | while (fg) {
|
---|
| 180 | printf("L%d ? ", line+1);
|
---|
| 181 | fflush(stdout);
|
---|
| 182 | buff[0] = '\0';
|
---|
| 183 | ret = fgets(buff, 512, stdin);
|
---|
| 184 | buff[511] = '\0';
|
---|
| 185 | if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) {
|
---|
| 186 | os << buff << endl; line++;
|
---|
| 187 | }
|
---|
| 188 | else fg = false;
|
---|
| 189 | }
|
---|
| 190 | os << "\n" ;
|
---|
| 191 | cout << " \n Total " << line << " lines copied to file " << codeflnm << endl;
|
---|
| 192 | if (line > 0) return(0);
|
---|
| 193 | else return(1);
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /* --Fonction-- */
|
---|
| 197 | int FillCxxFile(string & code, string & cxxfile)
|
---|
| 198 | {
|
---|
| 199 | ofstream os(cxxfile.c_str(),ios::out);
|
---|
| 200 |
|
---|
| 201 | os<<"#include \"machdefs.h\" \n"<<endl;
|
---|
| 202 |
|
---|
| 203 | os<<"//---- System et stdc++ include files \n"
|
---|
| 204 | <<"#include <stdio.h> \n"
|
---|
| 205 | <<"#include <stdlib.h> \n"
|
---|
| 206 | <<"#include <math.h> \n"
|
---|
| 207 | <<"#include <ctype.h> \n"
|
---|
| 208 | <<"#include <string.h> \n"
|
---|
| 209 | <<"#include <iostream.h> \n"
|
---|
| 210 | <<"#include <fstream.h> \n"
|
---|
| 211 | <<"#include <complex> \n"
|
---|
| 212 | <<endl
|
---|
| 213 |
|
---|
| 214 | <<"#include <typeinfo> \n"
|
---|
| 215 | <<"#include <string> \n"
|
---|
| 216 | <<"#include <vector> \n"
|
---|
| 217 | <<"#include <map> \n"
|
---|
| 218 | <<"#include <functional> \n"
|
---|
| 219 | <<"#include <list> \n"
|
---|
| 220 | <<endl
|
---|
| 221 |
|
---|
| 222 | <<"//---- Sophya include files \n"
|
---|
| 223 | <<"#include \"systools.h\" \n"
|
---|
| 224 | <<"#include \"ntools.h\" \n"
|
---|
| 225 | <<"#include \"array.h\" \n"
|
---|
| 226 | <<"#include \"histats.h\" \n"
|
---|
| 227 | <<"#include \"skymap.h\" \n"
|
---|
| 228 | <<"#include \"samba.h\" \n"
|
---|
| 229 | <<"#include \"skyt.h\" \n"
|
---|
| 230 | <<endl;
|
---|
| 231 |
|
---|
| 232 | os << "extern \"C\" { \n"
|
---|
| 233 | << " void runcxx_usercode(vector<string>& arg, int & rc); \n"
|
---|
| 234 | // << " void runcxx_usercode(); \n"
|
---|
| 235 | << " } \n " << endl;
|
---|
| 236 |
|
---|
| 237 | os << "// ---- User Code function ----- \n"
|
---|
| 238 | << " void runcxx_usercode(vector<string>& arg, int & rc) \n"
|
---|
| 239 | // << " void runcxx_usercode() \n"
|
---|
| 240 | << "{ \n"
|
---|
| 241 | << "#include \"" << code << "\" \n"
|
---|
| 242 | << "} \n" << endl;
|
---|
| 243 |
|
---|
| 244 | return(0);
|
---|
| 245 | }
|
---|
| 246 |
|
---|