[2615] | 1 | #include "sopnamsp.h"
|
---|
[2429] | 2 | #include "machdefs.h"
|
---|
[1306] | 3 | #include <stdio.h>
|
---|
| 4 | #include <stdlib.h>
|
---|
| 5 | #include <math.h>
|
---|
[2322] | 6 | #include <iostream>
|
---|
| 7 | #include <fstream>
|
---|
[1306] | 8 |
|
---|
| 9 | #include <string>
|
---|
| 10 | #include <vector>
|
---|
| 11 |
|
---|
| 12 | #include <typeinfo>
|
---|
| 13 |
|
---|
[2429] | 14 | #include "tarrinit.h"
|
---|
| 15 | #include "array.h"
|
---|
[1306] | 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 |
|
---|
[1441] | 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
|
---|
[2411] | 31 | PIOPersist::Initialize() Starting Sophya Persistence management service
|
---|
| 32 | SOPHYA Version 1.6 Revision 6 (V_Mai2003) -- Jun 25 2003 12:01:04 cxx
|
---|
[1441] | 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
|
---|
[2411] | 40 | modulenames: SkyMap, Samba, SkyT, FitsIOServer,
|
---|
| 41 | LinAlg, IFFTW, XAstroPack
|
---|
[1441] | 42 | \endverbatim
|
---|
| 43 | */
|
---|
| 44 |
|
---|
[1306] | 45 | typedef void (* DlFunctionOfVecStr) (vector<string> & args, int & rc);
|
---|
| 46 |
|
---|
| 47 | static void Usage(bool fgerr);
|
---|
| 48 | static int GetInput(string & codeflnm);
|
---|
[1341] | 49 | static int FillCxxFile(string & codeflnm, string & cxxfile,
|
---|
| 50 | vector<string> & inc1, vector<string> & inc2);
|
---|
[1306] | 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"
|
---|
[1341] | 62 | << " [-inc includefile] [-inc includefile ...] \n"
|
---|
| 63 | << " [-import modulename] [-import modulename ...] \n"
|
---|
[1306] | 64 | << " [-uarg UserArg1 UserArg2 ...] \n"
|
---|
[1341] | 65 | << " if no file name is specified, read from standard input \n"
|
---|
[2411] | 66 | << " modulenames: SkyMap, Samba, SkyT, FitsIOServer, \n"
|
---|
| 67 | << " LinAlg, IFFTW, XAstroPack \n"
|
---|
[1341] | 68 | << endl;
|
---|
| 69 |
|
---|
[1306] | 70 | exit(0);
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | /* --Main-- */
|
---|
| 76 | int main(int narg, char *arg[])
|
---|
| 77 | {
|
---|
[2429] | 78 | //SambaInitiator smbinit;
|
---|
| 79 |
|
---|
| 80 | SophyaInit();
|
---|
| 81 | InitTim();
|
---|
| 82 |
|
---|
[1306] | 83 | if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false);
|
---|
| 84 |
|
---|
[2429] | 85 |
|
---|
[1306] | 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;
|
---|
[1341] | 98 | vector<string> inc1;
|
---|
| 99 | vector<string> inc2;
|
---|
[1306] | 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 | }
|
---|
[1341] | 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++;
|
---|
[2411] | 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");
|
---|
[1341] | 137 | }
|
---|
| 138 |
|
---|
[1306] | 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";
|
---|
[1341] | 170 | rc = FillCxxFile(codefile, flnm, inc1, inc2);
|
---|
[1306] | 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;
|
---|
[1329] | 185 | if (rc == 0) {
|
---|
| 186 | rc = cxx.BuildSO(oname, soname);
|
---|
| 187 | cout << " RC from cxx.BuildSO() = " << rc << " soname= " << soname << endl;
|
---|
| 188 | }
|
---|
[1306] | 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-- */
|
---|
[1341] | 251 | int FillCxxFile(string & code, string & cxxfile,
|
---|
| 252 | vector<string>& inc1, vector<string>& inc2)
|
---|
[1306] | 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"
|
---|
[2322] | 264 | <<"#include <iostream> \n"
|
---|
| 265 | <<"#include <fstream> \n"
|
---|
[1306] | 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"
|
---|
[1607] | 278 | <<"#include \"basetools.h\" \n"
|
---|
[1306] | 279 | <<"#include \"systools.h\" \n"
|
---|
[1607] | 280 | <<"#include \"sutils.h\" \n"
|
---|
[1306] | 281 | <<"#include \"ntools.h\" \n"
|
---|
| 282 | <<"#include \"array.h\" \n"
|
---|
| 283 | <<"#include \"histats.h\" \n"
|
---|
| 284 | <<endl;
|
---|
[1341] | 285 |
|
---|
| 286 | int ki;
|
---|
| 287 | for(ki=0; ki<inc1.size(); ki++)
|
---|
| 288 | os << "#include \"" << inc1[ki] << "\" \n" ;
|
---|
| 289 | os << endl;
|
---|
| 290 | for(ki=0; ki<inc2.size(); ki++)
|
---|
| 291 | os << "#include \"" << inc2[ki] << "\" \n" ;
|
---|
| 292 | os << endl;
|
---|
[1306] | 293 |
|
---|
[1341] | 294 | os<<"//---- function for Adding and displaying Objects " << endl;
|
---|
| 295 | os<<"//---- compatibility with piapp cxxexecutor " << endl;
|
---|
| 296 | os<<"void Keep_Object(AnyDataObj & obj, string const & nom) \n{ \n"
|
---|
| 297 | <<" POutPersist pos(nom); \n"
|
---|
| 298 | <<" pos.PutObject(obj); \n"
|
---|
| 299 | <<"} \n" << endl;
|
---|
| 300 |
|
---|
| 301 | os<<"//---- Objects and variables saving"<<endl
|
---|
| 302 | <<"#define KeepObj(obj) Keep_Object(obj, #obj);"<<endl
|
---|
| 303 | <<endl;
|
---|
| 304 |
|
---|
| 305 | os<<"//---- Displaying objects and command execution"<<endl;
|
---|
| 306 | os<<"#define DisplayObj(obj, att) "
|
---|
| 307 | <<" cout << \"DisplayObj() available only in piapp \" << endl; " << endl;
|
---|
| 308 | os<<"#define ExecuteCommand(cmd) "
|
---|
| 309 | <<" cout << \"ExecuteCommand() available only in piapp \" << endl; " << endl;
|
---|
| 310 |
|
---|
| 311 |
|
---|
[1306] | 312 | os << "extern \"C\" { \n"
|
---|
| 313 | << " void runcxx_usercode(vector<string>& arg, int & rc); \n"
|
---|
| 314 | // << " void runcxx_usercode(); \n"
|
---|
| 315 | << " } \n " << endl;
|
---|
| 316 |
|
---|
| 317 | os << "// ---- User Code function ----- \n"
|
---|
| 318 | << " void runcxx_usercode(vector<string>& arg, int & rc) \n"
|
---|
| 319 | // << " void runcxx_usercode() \n"
|
---|
| 320 | << "{ \n"
|
---|
| 321 | << "#include \"" << code << "\" \n"
|
---|
| 322 | << "} \n" << endl;
|
---|
| 323 |
|
---|
| 324 | return(0);
|
---|
| 325 | }
|
---|
| 326 |
|
---|