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