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