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