| 1 | #include "sopnamsp.h" | 
|---|
| 2 | #include "machdefs.h" | 
|---|
| 3 | #include <stdio.h> | 
|---|
| 4 | #include <stdlib.h> | 
|---|
| 5 | #include <math.h> | 
|---|
| 6 | #include <iostream> | 
|---|
| 7 | #include <fstream> | 
|---|
| 8 |  | 
|---|
| 9 | #include <string> | 
|---|
| 10 | #include <vector> | 
|---|
| 11 |  | 
|---|
| 12 | #include <typeinfo> | 
|---|
| 13 |  | 
|---|
| 14 | #include "tarrinit.h" | 
|---|
| 15 | #include "array.h" | 
|---|
| 16 | #include "sambainit.h" | 
|---|
| 17 | #include "skyinit.h" | 
|---|
| 18 |  | 
|---|
| 19 | #include "pexceptions.h" | 
|---|
| 20 | #include "cxxcmplnk.h" | 
|---|
| 21 | #include "pdlmgr.h" | 
|---|
| 22 | #include "timing.h" | 
|---|
| 23 |  | 
|---|
| 24 | /*! | 
|---|
| 25 | \ingroup PrgUtil | 
|---|
| 26 | \file runcxx.cc | 
|---|
| 27 | \brief \b runcxx: Compile and run simple C++ code using SOPHYA | 
|---|
| 28 |  | 
|---|
| 29 | \verbatim | 
|---|
| 30 | csh> runcxx -h | 
|---|
| 31 | PIOPersist::Initialize() Starting Sophya Persistence management service | 
|---|
| 32 | SOPHYA Version  1.6 Revision 6 (V_Mai2003) -- Jun 25 2003 12:01:04 cxx | 
|---|
| 33 | runcxx : compiling and running of a piece of C++ code | 
|---|
| 34 | Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions] | 
|---|
| 35 | [-tmpdir TmpDirectory] [-f C++CodeFileName] | 
|---|
| 36 | [-inc includefile] [-inc includefile ...] | 
|---|
| 37 | [-import modulename] [-import modulename ...] | 
|---|
| 38 | [-uarg UserArg1 UserArg2 ...] | 
|---|
| 39 | if no file name is specified, read from standard input | 
|---|
| 40 | modulenames: SkyMap, Samba, SkyT, FitsIOServer, | 
|---|
| 41 | LinAlg, IFFTW, XAstroPack | 
|---|
| 42 | \endverbatim | 
|---|
| 43 | */ | 
|---|
| 44 |  | 
|---|
| 45 | typedef void (* DlFunctionOfVecStr) (vector<string> & args, int & rc); | 
|---|
| 46 |  | 
|---|
| 47 | static void Usage(bool fgerr); | 
|---|
| 48 | static int  GetInput(string & codeflnm); | 
|---|
| 49 | static int  FillCxxFile(string & codeflnm, string & cxxfile, | 
|---|
| 50 | vector<string> & inc1, vector<string> & inc2); | 
|---|
| 51 |  | 
|---|
| 52 | /* --Fonction-- */ | 
|---|
| 53 | void Usage(bool fgerr) | 
|---|
| 54 | { | 
|---|
| 55 | if (fgerr) | 
|---|
| 56 | cout << " runcxx : Argument Error ! " << endl; | 
|---|
| 57 | else | 
|---|
| 58 | cout << " runcxx : compiling and running of a piece of C++ code " << endl; | 
|---|
| 59 |  | 
|---|
| 60 | cout << "   Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions] \n" | 
|---|
| 61 | << "                 [-tmpdir TmpDirectory] [-f C++CodeFileName] \n" | 
|---|
| 62 | << "                 [-inc includefile] [-inc includefile ...] \n" | 
|---|
| 63 | << "                 [-import modulename] [-import modulename ...] \n" | 
|---|
| 64 | << "                 [-uarg UserArg1 UserArg2 ...] \n" | 
|---|
| 65 | << "   if no file name is specified, read from standard input \n" | 
|---|
| 66 | << "   modulenames: SkyMap, Samba, SkyT, FitsIOServer, \n" | 
|---|
| 67 | << "                LinAlg, IFFTW, XAstroPack \n" | 
|---|
| 68 | << endl; | 
|---|
| 69 |  | 
|---|
| 70 | exit(0); | 
|---|
| 71 | } | 
|---|
| 72 |  | 
|---|
| 73 |  | 
|---|
| 74 |  | 
|---|
| 75 | /* --Main-- */ | 
|---|
| 76 | int main(int narg, char *arg[]) | 
|---|
| 77 | { | 
|---|
| 78 | //SambaInitiator smbinit; | 
|---|
| 79 |  | 
|---|
| 80 | SophyaInit(); | 
|---|
| 81 | InitTim(); | 
|---|
| 82 |  | 
|---|
| 83 | if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false); | 
|---|
| 84 |  | 
|---|
| 85 |  | 
|---|
| 86 | // Decoding options | 
|---|
| 87 | string codefile; | 
|---|
| 88 | string compoption; | 
|---|
| 89 | string linkoption; | 
|---|
| 90 | string tmpdir; | 
|---|
| 91 |  | 
|---|
| 92 | bool fgco = false; | 
|---|
| 93 | bool fglo = false; | 
|---|
| 94 | bool fgcofn = false; | 
|---|
| 95 | bool fgtmp = false; | 
|---|
| 96 |  | 
|---|
| 97 | vector<string> uarg; | 
|---|
| 98 | vector<string> inc1; | 
|---|
| 99 | vector<string> inc2; | 
|---|
| 100 |  | 
|---|
| 101 | cout << " runcxx: Decoding command line options ... " << endl; | 
|---|
| 102 | for (int k=1; k<narg; k++)   { | 
|---|
| 103 | if (strcmp(arg[k], "-compopt") == 0)  { | 
|---|
| 104 | if (k == narg-1) Usage(true); | 
|---|
| 105 | fgco = true;  k++; | 
|---|
| 106 | compoption = arg[k]; | 
|---|
| 107 | } | 
|---|
| 108 | else if (strcmp(arg[k], "-linkopt") == 0) { | 
|---|
| 109 | if (k == narg-1) Usage(true); | 
|---|
| 110 | fglo = true;  k++; | 
|---|
| 111 | linkoption = arg[k]; | 
|---|
| 112 | } | 
|---|
| 113 | else if (strcmp(arg[k], "-tmpdir") == 0) { | 
|---|
| 114 | if (k == narg-1) Usage(true); | 
|---|
| 115 | fgtmp = true;  k++; | 
|---|
| 116 | tmpdir = arg[k]; | 
|---|
| 117 | } | 
|---|
| 118 | else if (strcmp(arg[k], "-f") == 0) { | 
|---|
| 119 | if (k == narg-1) Usage(true); | 
|---|
| 120 | fgcofn = true;  k++; | 
|---|
| 121 | codefile = arg[k]; | 
|---|
| 122 | } | 
|---|
| 123 | else if (strcmp(arg[k], "-inc") == 0) { | 
|---|
| 124 | if (k == narg-1) Usage(true); | 
|---|
| 125 | k++; inc2.push_back(arg[k]); | 
|---|
| 126 | } | 
|---|
| 127 | else if (strcmp(arg[k], "-import") == 0) { | 
|---|
| 128 | if (k == narg-1) Usage(true); | 
|---|
| 129 | k++; | 
|---|
| 130 | if (strcmp(arg[k], "SkyMap") == 0)  inc1.push_back("skymap.h"); | 
|---|
| 131 | else if (strcmp(arg[k], "Samba") == 0)  inc1.push_back("samba.h"); | 
|---|
| 132 | else if (strcmp(arg[k], "SkyT") == 0)  inc1.push_back("skyt.h"); | 
|---|
| 133 | else if (strcmp(arg[k], "FitsIOServer") == 0)  inc1.push_back("fitsioserver.h"); | 
|---|
| 134 | else if (strcmp(arg[k], "LinAlg") == 0 )  inc1.push_back("intflapack.h"); | 
|---|
| 135 | else if (strcmp(arg[k], "IFFTW") == 0)  inc1.push_back("fftwserver.h"); | 
|---|
| 136 | else if (strcmp(arg[k], "XAstroPack") == 0)  inc1.push_back("xastropack.h"); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | else if (strcmp(arg[k], "-uarg") == 0) { | 
|---|
| 140 | if (k == narg-1) Usage(true); | 
|---|
| 141 | for(int kk=k+1; kk<narg; kk++) | 
|---|
| 142 | uarg.push_back(arg[kk]); | 
|---|
| 143 | break; | 
|---|
| 144 | } | 
|---|
| 145 | else Usage(true); | 
|---|
| 146 | } | 
|---|
| 147 |  | 
|---|
| 148 | // Checking TMPDIR | 
|---|
| 149 | if (!fgtmp) {  // TMPDIR pas defini par option de ligne de commande | 
|---|
| 150 | char* varenv; | 
|---|
| 151 | if ( (varenv=getenv("TMPDIR")) != NULL )  { | 
|---|
| 152 | tmpdir = varenv; | 
|---|
| 153 | fgtmp = true; | 
|---|
| 154 | } | 
|---|
| 155 | } | 
|---|
| 156 | if (tmpdir.length() > 0) | 
|---|
| 157 | if (tmpdir[tmpdir.length()-1] != '/') tmpdir += '/'; | 
|---|
| 158 |  | 
|---|
| 159 | int rc = 0; | 
|---|
| 160 |  | 
|---|
| 161 | try { | 
|---|
| 162 |  | 
|---|
| 163 | if (!fgcofn) {  // Pas de fichier specifie - Lecture de fichier sur stdin | 
|---|
| 164 | codefile = tmpdir + "aucode.icc"; | 
|---|
| 165 | rc = GetInput(codefile); | 
|---|
| 166 | } | 
|---|
| 167 | string flnm; | 
|---|
| 168 | if (rc == 0) { | 
|---|
| 169 | flnm = tmpdir + "acxxrun.cc"; | 
|---|
| 170 | rc = FillCxxFile(codefile, flnm, inc1, inc2); | 
|---|
| 171 | } | 
|---|
| 172 |  | 
|---|
| 173 | PrtTim(" End of FillCxxFile "); | 
|---|
| 174 |  | 
|---|
| 175 | string oname, soname; | 
|---|
| 176 | if (rc == 0) { | 
|---|
| 177 | CxxCompilerLinker cxx; | 
|---|
| 178 | if (fgtmp) cxx.SetTmpDir(tmpdir); | 
|---|
| 179 | if (fgco) cxx.AddCompileOptions(compoption); | 
|---|
| 180 | if (fglo) cxx.AddLinkOptions(linkoption); | 
|---|
| 181 | cxx.SetVerbose(true); | 
|---|
| 182 |  | 
|---|
| 183 | rc = cxx.Compile(flnm, oname); | 
|---|
| 184 | cout << " RC from cxx.Compile() = " << rc << " oname= " << oname << endl; | 
|---|
| 185 | if (rc == 0) { | 
|---|
| 186 | rc = cxx.BuildSO(oname, soname); | 
|---|
| 187 | cout << " RC from cxx.BuildSO() = " << rc << " soname= " << soname << endl; | 
|---|
| 188 | } | 
|---|
| 189 | PrtTim(" End of Compile-Link "); | 
|---|
| 190 | } | 
|---|
| 191 | if (rc == 0) { | 
|---|
| 192 | string funcname = "runcxx_usercode"; | 
|---|
| 193 | if (fgtmp) PDynLinkMgr::SetTmpDir(tmpdir); | 
|---|
| 194 | PDynLinkMgr dyl(soname); | 
|---|
| 195 | DlFunction f = dyl.GetFunction(funcname); | 
|---|
| 196 | if (f != NULL) { | 
|---|
| 197 | cout << "DlFunctionOfVecStr  f linked OK - calling f(uarg) ... \n" << endl; | 
|---|
| 198 | DlFunctionOfVecStr fvs = (DlFunctionOfVecStr)f; | 
|---|
| 199 | int rcu = 0; | 
|---|
| 200 | fvs(uarg, rcu); | 
|---|
| 201 | cout << "\n RC from UserCode() = " << rcu << endl; | 
|---|
| 202 | rc = rcu; | 
|---|
| 203 | } | 
|---|
| 204 | else { | 
|---|
| 205 | cout << " ERROR linking DlFunction f !!! " << endl; | 
|---|
| 206 | } | 
|---|
| 207 | } | 
|---|
| 208 | } | 
|---|
| 209 |  | 
|---|
| 210 | catch (PThrowable & exc) { | 
|---|
| 211 | cerr << " Catched Exception " << (string)typeid(exc).name() | 
|---|
| 212 | << " - Msg= " << exc.Msg() << endl; | 
|---|
| 213 | } | 
|---|
| 214 | catch (...) { | 
|---|
| 215 | cerr << " some other exception was caught ! " << endl; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | PrtTim(" End of runcxx "); | 
|---|
| 219 | return(rc); | 
|---|
| 220 | } | 
|---|
| 221 |  | 
|---|
| 222 | /* --Fonction-- */ | 
|---|
| 223 | int  GetInput(string & codeflnm) | 
|---|
| 224 | { | 
|---|
| 225 | cout << " runcxx/GetInput() : Type in your C++ code, \n" | 
|---|
| 226 | << "     end with a blanck line OR <Cntl>D " << endl; | 
|---|
| 227 | int line = 0; | 
|---|
| 228 | ofstream os(codeflnm.c_str(),ios::out); | 
|---|
| 229 | os << "\n" ; | 
|---|
| 230 | bool fg = true; | 
|---|
| 231 | char buff[512]; | 
|---|
| 232 | char * ret; | 
|---|
| 233 | while (fg) { | 
|---|
| 234 | printf("L%d ? ", line+1); | 
|---|
| 235 | fflush(stdout); | 
|---|
| 236 | buff[0] = '\0'; | 
|---|
| 237 | ret = fgets(buff, 512, stdin); | 
|---|
| 238 | buff[511] = '\0'; | 
|---|
| 239 | if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) { | 
|---|
| 240 | os << buff << endl;  line++; | 
|---|
| 241 | } | 
|---|
| 242 | else fg = false; | 
|---|
| 243 | } | 
|---|
| 244 | os << "\n" ; | 
|---|
| 245 | cout << " \n Total " << line << " lines copied to file " <<  codeflnm << endl; | 
|---|
| 246 | if (line > 0) return(0); | 
|---|
| 247 | else return(1); | 
|---|
| 248 | } | 
|---|
| 249 |  | 
|---|
| 250 | /* --Fonction-- */ | 
|---|
| 251 | int  FillCxxFile(string & code, string & cxxfile, | 
|---|
| 252 | vector<string>& inc1, vector<string>& inc2) | 
|---|
| 253 | { | 
|---|
| 254 | ofstream os(cxxfile.c_str(),ios::out); | 
|---|
| 255 |  | 
|---|
| 256 | os<<"#include \"machdefs.h\" \n"<<endl; | 
|---|
| 257 |  | 
|---|
| 258 | os<<"//---- System et stdc++ include files \n" | 
|---|
| 259 | <<"#include <stdio.h> \n" | 
|---|
| 260 | <<"#include <stdlib.h> \n" | 
|---|
| 261 | <<"#include <math.h> \n" | 
|---|
| 262 | <<"#include <ctype.h> \n" | 
|---|
| 263 | <<"#include <string.h> \n" | 
|---|
| 264 | <<"#include <iostream> \n" | 
|---|
| 265 | <<"#include <fstream> \n" | 
|---|
| 266 | <<"#include <complex> \n" | 
|---|
| 267 | <<endl | 
|---|
| 268 |  | 
|---|
| 269 | <<"#include <typeinfo> \n" | 
|---|
| 270 | <<"#include <string> \n" | 
|---|
| 271 | <<"#include <vector> \n" | 
|---|
| 272 | <<"#include <map> \n" | 
|---|
| 273 | <<"#include <functional> \n" | 
|---|
| 274 | <<"#include <list> \n" | 
|---|
| 275 | <<endl | 
|---|
| 276 |  | 
|---|
| 277 | <<"//---- Sophya include files \n" | 
|---|
| 278 | <<"#include \"sopnamsp.h\" \n" | 
|---|
| 279 | <<"#include \"basetools.h\" \n" | 
|---|
| 280 | <<"#include \"systools.h\" \n" | 
|---|
| 281 | <<"#include \"sutils.h\" \n" | 
|---|
| 282 | <<"#include \"ntools.h\" \n" | 
|---|
| 283 | <<"#include \"array.h\" \n" | 
|---|
| 284 | <<"#include \"histats.h\" \n" | 
|---|
| 285 | <<endl; | 
|---|
| 286 |  | 
|---|
| 287 | int ki; | 
|---|
| 288 | for(ki=0; ki<inc1.size(); ki++) | 
|---|
| 289 | os << "#include \"" << inc1[ki] << "\" \n" ; | 
|---|
| 290 | os << endl; | 
|---|
| 291 | for(ki=0; ki<inc2.size(); ki++) | 
|---|
| 292 | os << "#include \"" << inc2[ki] << "\" \n" ; | 
|---|
| 293 | os << endl; | 
|---|
| 294 |  | 
|---|
| 295 | os<<"//---- function for Adding and displaying Objects  " << endl; | 
|---|
| 296 | os<<"//---- compatibility with piapp cxxexecutor  " << endl; | 
|---|
| 297 | os<<"void Keep_Object(AnyDataObj & obj, string const & nom) \n{ \n" | 
|---|
| 298 | <<"  POutPersist pos(nom); \n" | 
|---|
| 299 | <<"  pos.PutObject(obj); \n" | 
|---|
| 300 | <<"} \n" << endl; | 
|---|
| 301 |  | 
|---|
| 302 | os<<"//---- Objects and variables saving"<<endl | 
|---|
| 303 | <<"#define KeepObj(obj) Keep_Object(obj, #obj);"<<endl | 
|---|
| 304 | <<endl; | 
|---|
| 305 |  | 
|---|
| 306 | os<<"//---- Displaying objects and command execution"<<endl; | 
|---|
| 307 | os<<"#define DisplayObj(obj, att) " | 
|---|
| 308 | <<" cout << \"DisplayObj() available only in piapp \" << endl; " << endl; | 
|---|
| 309 | os<<"#define ExecuteCommand(cmd) " | 
|---|
| 310 | <<" cout << \"ExecuteCommand() available only in piapp \" << endl; " << endl; | 
|---|
| 311 |  | 
|---|
| 312 |  | 
|---|
| 313 | os << "extern \"C\" { \n" | 
|---|
| 314 | << " void runcxx_usercode(vector<string>& arg, int & rc); \n" | 
|---|
| 315 | //     << " void runcxx_usercode(); \n" | 
|---|
| 316 | << " } \n " << endl; | 
|---|
| 317 |  | 
|---|
| 318 | os << "// ---- User Code function ----- \n" | 
|---|
| 319 | << " void runcxx_usercode(vector<string>& arg, int & rc) \n" | 
|---|
| 320 | //     << " void runcxx_usercode() \n" | 
|---|
| 321 | << "{ \n" | 
|---|
| 322 | << "#include \"" << code << "\" \n" | 
|---|
| 323 | << "} \n" << endl; | 
|---|
| 324 |  | 
|---|
| 325 | return(0); | 
|---|
| 326 | } | 
|---|
| 327 |  | 
|---|