| 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 (std::exception & sex) {
 | 
|---|
| 215 |     cerr << " Catched std::exception " << (string)typeid(sex).name()
 | 
|---|
| 216 |          << " - Msg= " << sex.what() << endl;
 | 
|---|
| 217 |   }
 | 
|---|
| 218 |   catch (...) {
 | 
|---|
| 219 |     cerr << " some other exception was caught ! " << endl;
 | 
|---|
| 220 |   }
 | 
|---|
| 221 | 
 | 
|---|
| 222 |   PrtTim(" End of runcxx ");
 | 
|---|
| 223 |   return(rc);
 | 
|---|
| 224 | }
 | 
|---|
| 225 | 
 | 
|---|
| 226 | /* --Fonction-- */
 | 
|---|
| 227 | int  GetInput(string & codeflnm)
 | 
|---|
| 228 | {
 | 
|---|
| 229 |   cout << " runcxx/GetInput() : Type in your C++ code, \n" 
 | 
|---|
| 230 |        << "     end with a blanck line OR <Cntl>D " << endl;
 | 
|---|
| 231 |   int line = 0;
 | 
|---|
| 232 |   ofstream os(codeflnm.c_str(),ios::out); 
 | 
|---|
| 233 |   os << "\n" ; 
 | 
|---|
| 234 |   bool fg = true;
 | 
|---|
| 235 |   char buff[512];
 | 
|---|
| 236 |   char * ret;
 | 
|---|
| 237 |   while (fg) {
 | 
|---|
| 238 |     printf("L%d ? ", line+1);
 | 
|---|
| 239 |     fflush(stdout);
 | 
|---|
| 240 |     buff[0] = '\0';
 | 
|---|
| 241 |     ret = fgets(buff, 512, stdin);
 | 
|---|
| 242 |     buff[511] = '\0';
 | 
|---|
| 243 |     if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) { 
 | 
|---|
| 244 |       os << buff << endl;  line++;
 | 
|---|
| 245 |     }
 | 
|---|
| 246 |     else fg = false;
 | 
|---|
| 247 |   }
 | 
|---|
| 248 |   os << "\n" ; 
 | 
|---|
| 249 |   cout << " \n Total " << line << " lines copied to file " <<  codeflnm << endl;
 | 
|---|
| 250 |   if (line > 0) return(0);
 | 
|---|
| 251 |   else return(1);
 | 
|---|
| 252 | }
 | 
|---|
| 253 | 
 | 
|---|
| 254 | /* --Fonction-- */
 | 
|---|
| 255 | int  FillCxxFile(string & code, string & cxxfile, 
 | 
|---|
| 256 |                  vector<string>& inc1, vector<string>& inc2)
 | 
|---|
| 257 | {
 | 
|---|
| 258 |   ofstream os(cxxfile.c_str(),ios::out); 
 | 
|---|
| 259 |   
 | 
|---|
| 260 |   os<<"#include \"machdefs.h\" \n"<<endl;
 | 
|---|
| 261 | 
 | 
|---|
| 262 |   os<<"//---- System et stdc++ include files \n"
 | 
|---|
| 263 |     <<"#include <stdio.h> \n"
 | 
|---|
| 264 |     <<"#include <stdlib.h> \n"
 | 
|---|
| 265 |     <<"#include <math.h> \n"
 | 
|---|
| 266 |     <<"#include <ctype.h> \n"
 | 
|---|
| 267 |     <<"#include <string.h> \n"
 | 
|---|
| 268 |     <<"#include <iostream> \n"
 | 
|---|
| 269 |     <<"#include <fstream> \n"
 | 
|---|
| 270 |     <<"#include <complex> \n"
 | 
|---|
| 271 |     <<endl
 | 
|---|
| 272 | 
 | 
|---|
| 273 |     <<"#include <typeinfo> \n"
 | 
|---|
| 274 |     <<"#include <string> \n"
 | 
|---|
| 275 |     <<"#include <vector> \n"
 | 
|---|
| 276 |     <<"#include <map> \n"
 | 
|---|
| 277 |     <<"#include <functional> \n"
 | 
|---|
| 278 |     <<"#include <list> \n"
 | 
|---|
| 279 |     <<endl
 | 
|---|
| 280 |     
 | 
|---|
| 281 |     <<"//---- Sophya include files \n"
 | 
|---|
| 282 |     <<"#include \"sopnamsp.h\" \n"
 | 
|---|
| 283 |     <<"#include \"basetools.h\" \n"
 | 
|---|
| 284 |     <<"#include \"systools.h\" \n"
 | 
|---|
| 285 |     <<"#include \"sutils.h\" \n"
 | 
|---|
| 286 |     <<"#include \"ntools.h\" \n"
 | 
|---|
| 287 |     <<"#include \"array.h\" \n"
 | 
|---|
| 288 |     <<"#include \"histats.h\" \n"
 | 
|---|
| 289 |     <<endl;
 | 
|---|
| 290 |   
 | 
|---|
| 291 |   int ki;
 | 
|---|
| 292 |   for(ki=0; ki<inc1.size(); ki++)
 | 
|---|
| 293 |     os << "#include \"" << inc1[ki] << "\" \n" ;
 | 
|---|
| 294 |   os << endl;
 | 
|---|
| 295 |   for(ki=0; ki<inc2.size(); ki++)
 | 
|---|
| 296 |     os << "#include \"" << inc2[ki] << "\" \n" ;
 | 
|---|
| 297 |   os << endl;
 | 
|---|
| 298 | 
 | 
|---|
| 299 |   os<<"//---- function for Adding and displaying Objects  " << endl;
 | 
|---|
| 300 |   os<<"//---- compatibility with piapp cxxexecutor  " << endl;
 | 
|---|
| 301 |   os<<"void Keep_Object(AnyDataObj & obj, string const & nom) \n{ \n" 
 | 
|---|
| 302 |     <<"  POutPersist pos(nom); \n"
 | 
|---|
| 303 |     <<"  pos.PutObject(obj); \n"
 | 
|---|
| 304 |     <<"} \n" << endl;
 | 
|---|
| 305 | 
 | 
|---|
| 306 |   os<<"//---- Objects and variables saving"<<endl
 | 
|---|
| 307 |     <<"#define KeepObj(obj) Keep_Object(obj, #obj);"<<endl
 | 
|---|
| 308 |     <<endl;
 | 
|---|
| 309 | 
 | 
|---|
| 310 |   os<<"//---- Displaying objects and command execution"<<endl;
 | 
|---|
| 311 |   os<<"#define DisplayObj(obj, att) " 
 | 
|---|
| 312 |     <<" cout << \"DisplayObj() available only in piapp \" << endl; " << endl;
 | 
|---|
| 313 |   os<<"#define ExecuteCommand(cmd) "
 | 
|---|
| 314 |     <<" cout << \"ExecuteCommand() available only in piapp \" << endl; " << endl;
 | 
|---|
| 315 | 
 | 
|---|
| 316 | 
 | 
|---|
| 317 |   os << "extern \"C\" { \n" 
 | 
|---|
| 318 |      << " void runcxx_usercode(vector<string>& arg, int & rc); \n" 
 | 
|---|
| 319 |     //     << " void runcxx_usercode(); \n"
 | 
|---|
| 320 |      << " } \n " << endl;
 | 
|---|
| 321 |   
 | 
|---|
| 322 |   os << "// ---- User Code function ----- \n" 
 | 
|---|
| 323 |      << " void runcxx_usercode(vector<string>& arg, int & rc) \n" 
 | 
|---|
| 324 |     //     << " void runcxx_usercode() \n" 
 | 
|---|
| 325 |      << "{ \n"
 | 
|---|
| 326 |      << "#include \"" << code << "\" \n"
 | 
|---|
| 327 |      << "} \n" << endl;
 | 
|---|
| 328 | 
 | 
|---|
| 329 |   return(0);
 | 
|---|
| 330 | }
 | 
|---|
| 331 | 
 | 
|---|