[1224] | 1 | #include "cxxexecutor.h"
|
---|
| 2 |
|
---|
| 3 | #include <typeinfo>
|
---|
| 4 |
|
---|
[1237] | 5 | #include "strutilxx.h"
|
---|
[1224] | 6 | #include "dvlist.h"
|
---|
| 7 |
|
---|
| 8 | #include "nomgadapter.h"
|
---|
| 9 | #include "pistdimgapp.h"
|
---|
| 10 |
|
---|
| 11 |
|
---|
| 12 | /* --Methode-- */
|
---|
| 13 | CxxExecutor::CxxExecutor(PIACmd *mpiac, PIStdImgApp* /* app */)
|
---|
[1268] | 14 | : mUserCodeFn(""), mUserFctFn(""), mCompOpt(""), mLinkOpt(""), mMyLibs("")
|
---|
[1269] | 15 | , mDefTmp(""), mDefRoot("cxx_spiapp"), mDefFunc("usercxx"), mPrtLevel(1)
|
---|
[1224] | 16 | {
|
---|
| 17 | mIncList.resize(0);
|
---|
| 18 | mCallArgs.resize(0);
|
---|
| 19 |
|
---|
[1268] | 20 | // Gestion des fichiers par default dans TmpDir
|
---|
| 21 | //.... CMV a gerer mais pas forcement OK ... voir avec Rz
|
---|
| 22 | //NamedObjMgr omg;
|
---|
| 23 | //string tmpdir = omg.GetTmpDir();
|
---|
| 24 | //if(tmpdir.size()>1) mDefTmp = tmpdir;
|
---|
| 25 |
|
---|
[1224] | 26 | // On enregistre les nouvelles commandes
|
---|
| 27 | string hgrp = "CxxExecutorCmd";
|
---|
| 28 | string usage,kw;
|
---|
| 29 |
|
---|
| 30 | kw = "c++exec";
|
---|
| 31 | usage = "c++exec: Execute the following c++ user code\n";
|
---|
[1237] | 32 | usage+= "Usage: c++exec c++ user code\n";
|
---|
[1268] | 33 | usage+= "Warning: c++ user code can be found in \"TmpDir/"+mDefRoot+".h\"\n";
|
---|
| 34 | usage+= " total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
|
---|
[1224] | 35 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 36 |
|
---|
| 37 | kw = "c++execfrf";
|
---|
[1268] | 38 | usage = "c++execfrf: Execute c++ user_code [user_function_code]\n";
|
---|
| 39 | usage+= "Usage: c++execfrf fileuser.cc [fileuserfct.cc]\n";
|
---|
| 40 | usage+= "Warning: total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
|
---|
[1224] | 41 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 42 |
|
---|
| 43 | kw = "c++args";
|
---|
[1237] | 44 | usage = "c++args: Define user function arguments for c++exec and c++execfrf\n";
|
---|
[1230] | 45 | usage+= "Usage: c++args arg1 arg2 arg3 ...\n";
|
---|
| 46 | usage+= " c++args -? : give current arguments\n";
|
---|
| 47 | usage+= " c++args : reset current arguments";
|
---|
[1224] | 48 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 49 |
|
---|
| 50 | kw = "c++create";
|
---|
| 51 | usage = "c++create: create a file to be used by spiapp\n";
|
---|
| 52 | usage+= "Usage: c++create file.cc func c++ user code...\n";
|
---|
| 53 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 54 |
|
---|
| 55 | kw = "c++createfrf";
|
---|
| 56 | usage = "c++createfrf: create a file \"file.cc\"to be used by spiapp\n";
|
---|
[1268] | 57 | usage+= " with a user file code \"fileuser.cc\"\n";
|
---|
| 58 | usage+= " and an optional user function code \"fileuserfct.cc\"\n";
|
---|
| 59 | usage+= "Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]\n";
|
---|
[1224] | 60 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 61 |
|
---|
| 62 | kw = "c++compile";
|
---|
| 63 | usage = "c++compile: compile a file (file.cc -> file.so)\n";
|
---|
| 64 | usage+= "Usage: c++compile file\n";
|
---|
| 65 | usage+= "Warning: give \"file\" or \"file.so\" to create \"file.so\" from \"file.cc\"\n";
|
---|
| 66 | usage+= " : to be used before c++link";
|
---|
| 67 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 68 |
|
---|
| 69 | kw = "c++link";
|
---|
| 70 | usage = "c++link: link function \"func\" in file.so to spiapp\n";
|
---|
| 71 | usage+= "Usage: c++link file.so func";
|
---|
| 72 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 73 |
|
---|
| 74 | kw = "c++include";
|
---|
| 75 | usage = "c++include: give personnal includes to be used\n";
|
---|
| 76 | usage+= "Usage: c++include myinc1.h myinc2.h ...\n";
|
---|
[1230] | 77 | usage+= " c++include -? : give current include files\n";
|
---|
| 78 | usage+= " c++include : reset current include files\n";
|
---|
[1224] | 79 | usage+= "Warning: to be used before c++create... c++exec...";
|
---|
| 80 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 81 |
|
---|
| 82 | kw = "c++compileopt";
|
---|
| 83 | usage = "c++compileopt: give additionnal compile options\n";
|
---|
| 84 | usage+= "Usage: c++compileopt -g -O5 -IMy_Inc_Dir ...\n";
|
---|
[1230] | 85 | usage+= " c++compileopt -? : give current compile options\n";
|
---|
| 86 | usage+= " c++compileopt : reset current compile options\n";
|
---|
[1224] | 87 | usage+= "Warning: to be used before c++compile";
|
---|
| 88 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 89 |
|
---|
| 90 | kw = "c++linkopt";
|
---|
| 91 | usage = "c++linkopt: give additionnal link options\n";
|
---|
[1230] | 92 | usage+= "Usage: c++linkopt -g -O5 ...\n";
|
---|
| 93 | usage+= " c++linkopt -? : give current link options\n";
|
---|
| 94 | usage+= " c++linkopt : reset current link options\n";
|
---|
[1224] | 95 | usage+= "Warning: to be used before c++compile";
|
---|
| 96 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 97 |
|
---|
| 98 | kw = "c++mylibs";
|
---|
| 99 | usage = "c++mylibs: give additionnal libraries\n";
|
---|
[1230] | 100 | usage+= "Usage: c++mylibs -LMy_Lib_Dir -lmylib1 -lmylib2 ...\n";
|
---|
| 101 | usage+= " c++mylibs -? : give current additionnal libraries\n";
|
---|
| 102 | usage+= " c++mylibs : reset current additionnal libraries\n";
|
---|
[1224] | 103 | usage+= "Warning: to be used before c++compile";
|
---|
| 104 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
| 105 |
|
---|
[1230] | 106 |
|
---|
[1224] | 107 | }
|
---|
| 108 |
|
---|
| 109 | /* --Methode-- */
|
---|
| 110 | CxxExecutor::~CxxExecutor()
|
---|
| 111 | {
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | /* --Methode-- */
|
---|
[1268] | 115 | int CxxExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
|
---|
[1224] | 116 | {
|
---|
| 117 | int rc=0;
|
---|
[1268] | 118 | if(kw == "c++exec") {
|
---|
[1224] | 119 | if(tokens.size()<1) {
|
---|
| 120 | cout<<"Usage: c++exec c++ user code"<<endl;
|
---|
| 121 | return(1);
|
---|
| 122 | }
|
---|
[1269] | 123 | rc = ExecuteCXX(toks); if(rc) return(1);
|
---|
[1268] | 124 |
|
---|
| 125 | } else if(kw == "c++execfrf") {
|
---|
| 126 | if(tokens.size()<1) {
|
---|
| 127 | cout<<"Usage: c++execfrf fileuser.cc [fileuserfct.cc]"<<endl;
|
---|
| 128 | return(1);
|
---|
| 129 | }
|
---|
[1269] | 130 | rc = FillUserCode(tokens[0]); if(rc) return(1);
|
---|
| 131 | if(tokens.size()>1) rc = FillUserFctFrF(tokens[1]);
|
---|
[1224] | 132 | rc = CrFile(); if(rc) return(1);
|
---|
| 133 | rc = Compile(); if(rc) return(1);
|
---|
| 134 | rc = Link(); if(rc) return(1);
|
---|
| 135 | rc = Call(); if(rc) return(1);
|
---|
| 136 |
|
---|
| 137 | } else if(kw == "c++args") {
|
---|
[1233] | 138 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
| 139 | {cout<<"c++args "<<GetArgs()<<endl; return(0);}
|
---|
[1230] | 140 | FillArgs(tokens);
|
---|
[1224] | 141 |
|
---|
| 142 | } else if(kw == "c++create") {
|
---|
| 143 | if(tokens.size()<3) {
|
---|
| 144 | cout<<"Usage: c++create file.cc func c++ user code ..."<<endl;
|
---|
| 145 | return(1);
|
---|
| 146 | }
|
---|
[1268] | 147 | rc = FillUserCode(toks,2); if(rc) return(1);
|
---|
[1224] | 148 | rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
|
---|
| 149 |
|
---|
| 150 | } else if(kw == "c++createfrf") {
|
---|
| 151 | if(tokens.size()<3) {
|
---|
[1268] | 152 | cout<<"Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]"<<endl;
|
---|
[1224] | 153 | return(1);
|
---|
| 154 | }
|
---|
[1269] | 155 | rc = FillUserCode(tokens[2]); if(rc) return(1);
|
---|
| 156 | if(tokens.size()>3) FillUserFctFrF(tokens[3]);
|
---|
[1224] | 157 | rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
|
---|
| 158 |
|
---|
| 159 | } else if(kw == "c++compile") {
|
---|
| 160 | if(tokens.size()>=1) rc = Compile(tokens[0]);
|
---|
| 161 | else rc = Compile();
|
---|
| 162 | if(rc) return(1);
|
---|
| 163 |
|
---|
| 164 | } else if(kw == "c++link") {
|
---|
| 165 | if(tokens.size()>=2) rc = Link(tokens[0],tokens[1]);
|
---|
| 166 | else if(tokens.size()>=1) rc = Link(tokens[0]);
|
---|
| 167 | else rc = Link();
|
---|
| 168 | if(rc) return(1);
|
---|
| 169 |
|
---|
| 170 | } else if(kw == "c++include") {
|
---|
[1233] | 171 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
| 172 | {cout<<"c++include "<<GetInclude()<<endl; return(0);}
|
---|
[1230] | 173 | FillInclude(tokens);
|
---|
[1224] | 174 |
|
---|
| 175 | } else if(kw == "c++compileopt") {
|
---|
[1233] | 176 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
| 177 | {cout<<"c++compileopt "<<GetCompileOpt()<<endl; return(0);}
|
---|
[1230] | 178 | FillCompileOpt(tokens);
|
---|
[1224] | 179 |
|
---|
| 180 | } else if(kw == "c++linkopt") {
|
---|
[1233] | 181 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
| 182 | {cout<<"c++linkopt "<<GetLinkOpt()<<endl; return(0);}
|
---|
[1230] | 183 | FillLinkOpt(tokens);
|
---|
[1224] | 184 |
|
---|
| 185 | } else if(kw == "c++mylibs") {
|
---|
[1233] | 186 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
| 187 | {cout<<"c++mylibs "<<GetLinkLibs()<<endl; return(0);}
|
---|
[1230] | 188 | FillLinkLibs(tokens);
|
---|
[1224] | 189 | }
|
---|
| 190 |
|
---|
| 191 | return(0);
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | /* --Methode-- */
|
---|
[1269] | 195 | int CxxExecutor::ExecuteCXX(string usercode,string userfct)
|
---|
| 196 | {
|
---|
| 197 | int rc=0;
|
---|
| 198 | rc = FillUserCode(usercode,0); if(rc) return(1);
|
---|
| 199 | rc = FillUserFctFrS(userfct);
|
---|
| 200 | rc = CrFile(); if(rc) return(1);
|
---|
| 201 | rc = Compile(); if(rc) return(1);
|
---|
| 202 | rc = Link(); if(rc) return(1);
|
---|
| 203 | rc = Call(); if(rc) return(1);
|
---|
| 204 | return 0;
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /* --Methode-- */
|
---|
[1268] | 208 | int CxxExecutor::CrFile(string cfilename,string func)
|
---|
[1262] | 209 | {
|
---|
[1268] | 210 | if(cfilename.size()<1) cfilename = mDefTmp + mDefRoot + ".cc";
|
---|
| 211 | if(func.size()<1) func = mDefFunc;
|
---|
[1262] | 212 |
|
---|
[1224] | 213 | ofstream os(cfilename.c_str(),ios::out);
|
---|
[1237] | 214 | if(!os)
|
---|
| 215 | {cout<<"CxxExecutor::CrFile: unable to open "<<cfilename<<endl;
|
---|
| 216 | return 1;}
|
---|
[1224] | 217 |
|
---|
| 218 | PutInclude(os);
|
---|
| 219 | os<<endl;
|
---|
| 220 |
|
---|
| 221 | PutIncludeUser(os);
|
---|
| 222 | os<<endl;
|
---|
| 223 |
|
---|
[1268] | 224 | os<<"//-------------------------------------------------//"<<endl;
|
---|
[1269] | 225 | os<<"//----------------- User Functions ----------------//"<<endl;
|
---|
[1268] | 226 | os<<"//-------------------------------------------------//"<<endl;
|
---|
| 227 | if(mUserFctFn.size()>0) os<<"#include \""<<mUserFctFn<<"\""<<endl;
|
---|
| 228 | os<<endl;
|
---|
| 229 |
|
---|
| 230 |
|
---|
[1224] | 231 | os<<"extern \"C\" {"<<endl;
|
---|
| 232 | os<<" int "<<func<<"( vector<string>& args );"<<endl;
|
---|
| 233 | os<<"}"<<endl<<endl;
|
---|
| 234 | os<<"int "<<func<<"( vector<string>& args )"<<endl;
|
---|
| 235 | os<<"{"<<endl;
|
---|
| 236 | os<<"// Some definitions to help using spiapp;"<<endl;
|
---|
| 237 | os<<"NamedObjMgr omg;"<<endl;
|
---|
| 238 | os<<"Services2NObjMgr& srvo = *omg.GetServiceObj();"<<endl;
|
---|
| 239 | os<<endl;
|
---|
| 240 |
|
---|
| 241 | PutObject(os);
|
---|
| 242 | os<<endl;
|
---|
| 243 |
|
---|
| 244 | PutVar(os);
|
---|
| 245 | os<<endl;
|
---|
| 246 |
|
---|
[1237] | 247 | os<<"//--------------------------------------------//"<<endl;
|
---|
[1269] | 248 | os<<"//----------------- User Code ----------------//"<<endl;
|
---|
[1237] | 249 | os<<"//--------------------------------------------//"<<endl;
|
---|
[1269] | 250 | if(mUserCodeFn.size()>0) os<<"#include \""<<mUserCodeFn<<"\""<<endl;
|
---|
[1224] | 251 | os<<endl;
|
---|
| 252 |
|
---|
| 253 | os<<"return 0;"<<endl;
|
---|
| 254 | os<<"}"<<endl;
|
---|
| 255 |
|
---|
[1269] | 256 | if(mPrtLevel)
|
---|
| 257 | cout<<"File "<<cfilename<<" for function "<<func<<" created :"<<endl;
|
---|
| 258 | if(mPrtLevel && mUserCodeFn.size()>0)
|
---|
| 259 | cout<<" User code was in file "<<mUserCodeFn<<endl;
|
---|
| 260 | if(mPrtLevel && mUserFctFn.size()>0)
|
---|
| 261 | cout<<" User function code was in file "<<mUserFctFn<<endl;
|
---|
[1224] | 262 | return 0;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | /* --Methode-- */
|
---|
| 266 | void CxxExecutor::PutInclude(ofstream& os)
|
---|
| 267 | {
|
---|
| 268 | os<<"#include \"machdefs.h\""<<endl
|
---|
| 269 | <<endl
|
---|
| 270 |
|
---|
| 271 | <<"//---- System et stdc++ include files"<<endl
|
---|
| 272 | <<"#include <stdio.h>"<<endl
|
---|
| 273 | <<"#include <stdlib.h>"<<endl
|
---|
| 274 | <<"#include <math.h>"<<endl
|
---|
| 275 | <<"#include <ctype.h>"<<endl
|
---|
| 276 | <<"#include <string.h>"<<endl
|
---|
| 277 | <<"#include <iostream.h>"<<endl
|
---|
| 278 | <<"#include <fstream.h>"<<endl
|
---|
| 279 | <<"#include <complex>"<<endl
|
---|
| 280 | <<endl
|
---|
| 281 |
|
---|
| 282 | <<"#include <typeinfo>"<<endl
|
---|
| 283 | <<"#include <string>"<<endl
|
---|
| 284 | <<"#include <vector>"<<endl
|
---|
| 285 | <<"#include <map>"<<endl
|
---|
| 286 | <<"#include <functional>"<<endl
|
---|
| 287 | <<"#include <list>"<<endl
|
---|
| 288 | <<endl
|
---|
| 289 |
|
---|
| 290 | <<"//---- Sophya include files"<<endl
|
---|
| 291 | <<"#include \"systools.h\""<<endl
|
---|
| 292 | <<"#include \"ntools.h\""<<endl
|
---|
| 293 | <<"#include \"array.h\""<<endl
|
---|
| 294 | <<"#include \"histats.h\""<<endl
|
---|
| 295 | <<endl
|
---|
| 296 |
|
---|
| 297 | <<"//---- Spiapp include files"<<endl
|
---|
| 298 | <<"#include \"nobjmgr.h\""<<endl
|
---|
| 299 | <<"#include \"servnobjm.h\""<<endl
|
---|
| 300 | <<endl
|
---|
| 301 |
|
---|
| 302 | <<"#define KeepObj(obj) ___nomobj = #obj; omg.AddObj(obj,___nomobj);"<<endl
|
---|
| 303 | <<"#define KeepVar(var) ___nomobj = #var; omg.GetVarList().Get(___nomobj) = var ;"<<endl
|
---|
| 304 | <<endl;
|
---|
| 305 |
|
---|
| 306 | return;
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /* --Methode-- */
|
---|
| 310 | void CxxExecutor::PutIncludeUser(ofstream& os)
|
---|
| 311 | {
|
---|
| 312 | if(mIncList.size()<1) return;
|
---|
| 313 | for(uint_4 i=0;i<mIncList.size();i++)
|
---|
| 314 | os<<"#include \""<<mIncList[i]<<"\""<<endl;
|
---|
| 315 | }
|
---|
| 316 |
|
---|
| 317 | /* --Methode-- */
|
---|
| 318 | void CxxExecutor::PutObject(ofstream& os)
|
---|
| 319 | {
|
---|
| 320 | NamedObjMgr omg;
|
---|
| 321 | NObjMgrAdapter* objmgrad;
|
---|
| 322 | vector<string> objlist;
|
---|
| 323 | string patt = "*";
|
---|
| 324 | omg.GetObjList(patt,objlist);
|
---|
| 325 | int nobjs = objlist.size();
|
---|
| 326 |
|
---|
| 327 | os<<"//-------------- Object List --------------"<<endl;
|
---|
| 328 | os<<"//Number of objects = "<<nobjs<<endl;
|
---|
| 329 | os<<"string ___nomobj;"<<endl<<endl;
|
---|
| 330 | if(nobjs<=0) return;
|
---|
| 331 |
|
---|
| 332 | string dir,nobj,stmp,obtype;
|
---|
| 333 | for(int i=0;i<nobjs;i++) {
|
---|
| 334 | objmgrad = omg.GetObjAdapter(objlist[i]);
|
---|
| 335 | omg.ParseObjectName(objlist[i],dir,nobj);
|
---|
| 336 | obtype = objmgrad->GetDataObjType();
|
---|
| 337 | stmp = "___" + nobj;
|
---|
| 338 |
|
---|
| 339 | os<<"___nomobj = \""<<nobj<<"\";"<<endl;
|
---|
| 340 | os<<obtype<<"* "<<stmp
|
---|
| 341 | <<" = dynamic_cast< "<<obtype<<" * >(omg.GetObj(___nomobj));"<<endl;
|
---|
| 342 | os<<"if("<<stmp<<"==NULL) throw NullPtrError"
|
---|
| 343 | <<"(\"CxxExecutor::PutObject: Non existing object "<<nobj
|
---|
| 344 | <<"... please update file\");"<<endl;
|
---|
| 345 | os<<obtype<<"& "<<nobj<<" = (*"<<stmp<<");"<<endl<<endl;
|
---|
| 346 | }
|
---|
| 347 |
|
---|
| 348 | return;
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /* --Methode-- */
|
---|
| 352 | void CxxExecutor::PutVar(ofstream& os)
|
---|
| 353 | {
|
---|
| 354 | os<<"//-------------- Variable List --------------"<<endl;
|
---|
| 355 | NamedObjMgr omg;
|
---|
| 356 | DVList& varlist = omg.GetVarList();
|
---|
| 357 | // varlist.Show(); varlist.Print();
|
---|
| 358 | DVList::ValList::const_iterator it;
|
---|
| 359 | for(it=varlist.Begin(); it!=varlist.End(); it++) {
|
---|
| 360 | string key = (*it).first;
|
---|
[1262] | 361 | if (isalpha(key[0]) ) {
|
---|
| 362 | os<<"___nomobj = \""<<key<<"\";"<<endl;
|
---|
| 363 | os<<"MuTyV & $"<<key<<" = omg.GetVarList().Get(___nomobj);"<<endl;
|
---|
| 364 | }
|
---|
[1224] | 365 | }
|
---|
| 366 |
|
---|
[1237] | 367 | return;
|
---|
[1224] | 368 | }
|
---|
| 369 |
|
---|
| 370 | /* --Methode-- */
|
---|
[1268] | 371 | int CxxExecutor::FillUserCode(string& usercode,uint_4 first)
|
---|
| 372 | // - first is the first position in the "string" where the code starts
|
---|
| 373 | // User code is read from input. It is put into file "TmpDir/cxx_spiapp.h".
|
---|
[1224] | 374 | {
|
---|
[1237] | 375 | mUserCodeFn = "";
|
---|
[1268] | 376 |
|
---|
| 377 | // get the string part which is after word "first"
|
---|
| 378 | string code = usercode;
|
---|
| 379 | if(code.size()<=0) {cout<<"CxxExecutor::FillUserCode: no user code"<<endl;
|
---|
| 380 | return 1;}
|
---|
| 381 | size_t q;
|
---|
| 382 | for(uint_4 i=0;i<=first;i++) {
|
---|
| 383 | q = code.find_first_not_of(" \t");
|
---|
| 384 | if(q>=code.size()) {code=""; break;}
|
---|
| 385 | code = code.substr(q);
|
---|
| 386 | if(i==first) break;
|
---|
| 387 | q = code.find_first_of(" \t");
|
---|
| 388 | if(q>=code.size()) {code=""; break;}
|
---|
| 389 | code = code.substr(q);
|
---|
| 390 | }
|
---|
| 391 | if(code.size()<=0)
|
---|
| 392 | {cout<<"CxxExecutor::FillUserCode: no user code after "<<first<<endl;
|
---|
| 393 | return 1;}
|
---|
| 394 |
|
---|
| 395 | // Fill the file with user code
|
---|
| 396 | mUserCodeFn = mDefTmp + mDefRoot + ".h";
|
---|
[1237] | 397 | ofstream os(mUserCodeFn.c_str(),ios::out);
|
---|
| 398 | if(!os) {cout<<"CxxExecutor::FillUserCode: unable to open "
|
---|
| 399 | <<mUserCodeFn<<endl; mUserCodeFn = ""; return 1;}
|
---|
[1269] | 400 | os<<code<<endl;
|
---|
| 401 | if(mPrtLevel)
|
---|
| 402 | cout<<"User code filled from standard input into "<<mUserCodeFn<<endl;
|
---|
[1224] | 403 | return 0;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | /* --Methode-- */
|
---|
[1269] | 407 | int CxxExecutor::FillUserFctFrS(string userfct)
|
---|
| 408 | // - Fill user Fonction code from string "userfct"
|
---|
| 409 | // It is put into file "TmpDir/cxx_spiapp_fct.h".
|
---|
[1224] | 410 | {
|
---|
[1269] | 411 | mUserFctFn = "";
|
---|
| 412 | if(userfct.size()<1) return 0;
|
---|
| 413 | mUserFctFn = mDefTmp + mDefRoot + "_fct.h";
|
---|
| 414 | ofstream os(mUserFctFn.c_str(),ios::out);
|
---|
| 415 | if(!os) {cout<<"CxxExecutor::FillUserFctFrS: unable to open "
|
---|
| 416 | <<mUserFctFn<<endl; mUserFctFn = ""; return 1;}
|
---|
| 417 | os<<userfct<<endl;
|
---|
| 418 | if(mPrtLevel)
|
---|
| 419 | cout<<"User Function code filled from standard input into "<<mUserFctFn<<endl;
|
---|
| 420 | return 0;
|
---|
| 421 | }
|
---|
| 422 |
|
---|
| 423 | /* --Methode-- */
|
---|
| 424 | int CxxExecutor::FillUserCode(string filename)
|
---|
| 425 | // User code is read from "filename".
|
---|
| 426 | {
|
---|
[1237] | 427 | mUserCodeFn = filename;
|
---|
[1269] | 428 | if(mPrtLevel && mUserCodeFn.size()>0)
|
---|
| 429 | cout<<"User code filled from file "<<mUserCodeFn<<endl;
|
---|
[1224] | 430 | return 0;
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | /* --Methode-- */
|
---|
[1269] | 434 | int CxxExecutor::FillUserFctFrF(string filefctname="")
|
---|
| 435 | {
|
---|
| 436 | mUserFctFn = filefctname;
|
---|
| 437 | if(mPrtLevel && mUserFctFn.size()>0)
|
---|
| 438 | cout<<"User Function code filled from file "<<mUserFctFn<<endl;
|
---|
| 439 | return 0;
|
---|
| 440 | }
|
---|
| 441 |
|
---|
| 442 | /* --Methode-- */
|
---|
[1224] | 443 | int CxxExecutor::Compile(string rootfilename)
|
---|
| 444 | {
|
---|
[1268] | 445 | if(rootfilename.size()<1) rootfilename = mDefRoot;
|
---|
[1269] | 446 | if(mPrtLevel) cout<<"Compile: "<<rootfilename<<endl;
|
---|
[1224] | 447 | int rc = 0;
|
---|
| 448 | rc = CrMakefile();
|
---|
| 449 | if(rc) return(1);
|
---|
[1268] | 450 | string make = "";
|
---|
| 451 | make += "make -f " + mDefRoot + "_Makefile";
|
---|
| 452 | make += " CXXFLAGS=\"" + mCompOpt + "\"";
|
---|
| 453 | make += " LDFLAGS=\"" + mLinkOpt + "\"";
|
---|
| 454 | make += " MYLIBS=\"" + mMyLibs + "\"";
|
---|
| 455 | make += " " + rootfilename;
|
---|
[1224] | 456 | rc = system(make.c_str());
|
---|
| 457 | if(rc)
|
---|
| 458 | {cout<<"CxxExecutor::Compile : \n"<<make<<" Failed"<<endl;
|
---|
| 459 | return 1000+rc;}
|
---|
| 460 | return 0;
|
---|
| 461 | }
|
---|
| 462 |
|
---|
| 463 | /* --Methode-- */
|
---|
| 464 | int CxxExecutor::CrMakefile(void)
|
---|
| 465 | {
|
---|
[1268] | 466 | string makename = mDefTmp + mDefRoot + "_Makefile";
|
---|
| 467 | ofstream os(makename.c_str(),ios::out);
|
---|
[1224] | 468 | if(!os)
|
---|
| 469 | {cout<<"CxxExecutor::CrMakefile: unable to open file for Makefile"<<endl;
|
---|
| 470 | return 1;}
|
---|
| 471 | //---------------------------------------------------------------------
|
---|
| 472 | os<<"MODULEDECCXXFLAGS := -msg_quiet"<<endl;
|
---|
| 473 | os<<"include $(DPCBASEREP)/Include/MakefileUser.h"<<endl;
|
---|
| 474 | os<<"MYLIBS ="<<endl;
|
---|
| 475 | os<<"LIBS = -L$(SLB) -lPI -lextsophya -lsophya -lm"<<endl;
|
---|
| 476 | os<<"ifeq ($(MACHEROS),OSF1)"<<endl;
|
---|
| 477 | os<<"LIBS := $(LIBS) -lfor"<<endl;
|
---|
| 478 | os<<"endif"<<endl;
|
---|
| 479 | os<<"ifeq ($(MACHEROS),Linux)"<<endl;
|
---|
| 480 | os<<"LIBS := $(LIBS) -ldl -lf2c"<<endl;
|
---|
| 481 | os<<"endif"<<endl;
|
---|
[1265] | 482 | os<<"%.so:%.o"<<endl;
|
---|
[1224] | 483 | os<<"%:%.cc"<<endl;
|
---|
| 484 | os<<"%:%.o"<<endl;
|
---|
| 485 | os<<"%.o:%.cc"<<endl;
|
---|
| 486 | os<<"%.o:%.c"<<endl;
|
---|
| 487 | os<<"%:%.c"<<endl;
|
---|
| 488 | os<<endl;
|
---|
| 489 | os<<".PRECIOUS: %.so"<<endl;
|
---|
| 490 | os<<endl;
|
---|
| 491 | os<<"%:%.so"<<endl;
|
---|
| 492 | os<<"\t"<<"echo $@ \" made (.so) \""<<endl;
|
---|
[1265] | 493 | os<<"%.so:%.o"<<endl;
|
---|
[1224] | 494 | os<<"\t"<<"$(LINK.cc) -shared -o $@ $< $(LIBS) $(MYLIBS)"<<endl;
|
---|
[1265] | 495 | os<<"%.o:%.cc"<<endl;
|
---|
[1268] | 496 | os<<"\t"<<"$(COMPILE.cc) -o $@ $<"<<endl;
|
---|
[1265] | 497 | os<<"%.o:%.c"<<endl;
|
---|
[1268] | 498 | os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS) $(USERFLAGS) -o $@ $<"<<endl;
|
---|
[1224] | 499 | //---------------------------------------------------------------------
|
---|
| 500 | return 0;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | /* --Methode-- */
|
---|
| 504 | int CxxExecutor::Link(string libname,string func)
|
---|
| 505 | {
|
---|
[1268] | 506 | if(libname.size()<1) libname = mDefTmp + mDefRoot + ".so";
|
---|
| 507 | if(func.size()<1) func = mDefFunc;
|
---|
| 508 |
|
---|
[1224] | 509 | NamedObjMgr omg;
|
---|
| 510 | PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
|
---|
| 511 | string key("link");
|
---|
| 512 | vector<string> arg; arg.push_back(libname); arg.push_back(func);
|
---|
[1268] | 513 | string toks = libname + " " + func;
|
---|
| 514 | int rc = mpiac->ExecuteCommand(key,arg,toks);
|
---|
[1269] | 515 | if(mPrtLevel) cout<<"Link from "<<libname<<" for function "<<func
|
---|
| 516 | <<" (rc="<<rc<<")"<<endl;
|
---|
[1224] | 517 | return 0;
|
---|
| 518 | }
|
---|
| 519 |
|
---|
| 520 | /* --Methode-- */
|
---|
| 521 | int CxxExecutor::Call(string func)
|
---|
| 522 | {
|
---|
[1268] | 523 | if(func.size()<1) func = mDefFunc;
|
---|
| 524 |
|
---|
[1224] | 525 | NamedObjMgr omg;
|
---|
| 526 | PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
|
---|
| 527 | string key("call");
|
---|
| 528 | vector<string> arg; arg.push_back(func);
|
---|
[1268] | 529 | string toks = func;
|
---|
[1224] | 530 | if(mCallArgs.size()>0)
|
---|
[1237] | 531 | for(uint_4 i=0;i<mCallArgs.size();i++) arg.push_back(mCallArgs[i]);
|
---|
[1268] | 532 | mpiac->ExecuteCommand(key,arg,toks);
|
---|
[1224] | 533 | return 0;
|
---|
| 534 | }
|
---|
| 535 |
|
---|
[1230] | 536 | /* --Methode-- */
|
---|
| 537 | void CxxExecutor::FillArgs(vector<string>& args)
|
---|
| 538 | {
|
---|
| 539 | mCallArgs.resize(0);
|
---|
| 540 | if(args.size()<1) return;
|
---|
| 541 | for(uint_4 i=0;i<args.size();i++) mCallArgs.push_back(args[i]);
|
---|
| 542 | }
|
---|
| 543 |
|
---|
| 544 | void CxxExecutor::FillArgs(string& args)
|
---|
| 545 | {
|
---|
| 546 | mCallArgs.resize(0);
|
---|
| 547 | FillVStringFrString(args,mCallArgs,' ');
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 | string CxxExecutor::GetArgs(void)
|
---|
| 551 | {
|
---|
| 552 | string dum = "";
|
---|
| 553 | if(mCallArgs.size()<1) return dum;
|
---|
| 554 | for(uint_4 i=0;i<mCallArgs.size();i++) dum += mCallArgs[i] + " ";
|
---|
| 555 | return dum;
|
---|
| 556 | }
|
---|
| 557 |
|
---|
| 558 | /* --Methode-- */
|
---|
| 559 | void CxxExecutor::FillInclude(vector<string>& inc)
|
---|
| 560 | {
|
---|
| 561 | mIncList.resize(0);
|
---|
| 562 | if(inc.size()<1) return;
|
---|
| 563 | for(uint_4 i=0;i<inc.size();i++) mIncList.push_back(inc[i]);
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | void CxxExecutor::FillInclude(string& inc)
|
---|
| 567 | {
|
---|
| 568 | mIncList.resize(0);
|
---|
| 569 | FillVStringFrString(inc,mIncList,' ');
|
---|
| 570 | }
|
---|
| 571 |
|
---|
| 572 | string CxxExecutor::GetInclude(void)
|
---|
| 573 | {
|
---|
| 574 | string dum = "";
|
---|
| 575 | if(mIncList.size()<1) return dum;
|
---|
| 576 | for(uint_4 i=0;i<mIncList.size();i++) dum += mIncList[i] + " ";
|
---|
| 577 | return dum;
|
---|
| 578 | }
|
---|
| 579 |
|
---|
| 580 | /* --Methode-- */
|
---|
| 581 | void CxxExecutor::FillCompileOpt(vector<string>& copt)
|
---|
| 582 | {
|
---|
| 583 | mCompOpt = "";
|
---|
| 584 | if(copt.size()<1) return;
|
---|
| 585 | for(uint_4 i=0;i<copt.size();i++) mCompOpt += copt[i] + " ";
|
---|
| 586 | }
|
---|
| 587 |
|
---|
| 588 | void CxxExecutor::FillCompileOpt(string& copt)
|
---|
| 589 | {
|
---|
| 590 | mCompOpt = copt;
|
---|
| 591 | }
|
---|
| 592 |
|
---|
| 593 | string CxxExecutor::GetCompileOpt(void)
|
---|
| 594 | {
|
---|
| 595 | return mCompOpt;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
| 598 | /* --Methode-- */
|
---|
| 599 | void CxxExecutor::FillLinkOpt(vector<string>& lopt)
|
---|
| 600 | {
|
---|
| 601 | mLinkOpt = "";
|
---|
| 602 | if(lopt.size()<1) return;
|
---|
| 603 | for(uint_4 i=0;i<lopt.size();i++) mLinkOpt += lopt[i] + " ";
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | void CxxExecutor::FillLinkOpt(string& lopt)
|
---|
| 607 | {
|
---|
| 608 | mLinkOpt = lopt;
|
---|
| 609 | }
|
---|
| 610 |
|
---|
| 611 | string CxxExecutor::GetLinkOpt(void)
|
---|
| 612 | {
|
---|
| 613 | return mLinkOpt;
|
---|
| 614 | }
|
---|
| 615 |
|
---|
| 616 | /* --Methode-- */
|
---|
| 617 | void CxxExecutor::FillLinkLibs(vector<string>& llibs)
|
---|
| 618 | {
|
---|
| 619 | mMyLibs = "";
|
---|
| 620 | if(llibs.size()<1) return;
|
---|
| 621 | for(uint_4 i=0;i<llibs.size();i++) mMyLibs += llibs[i] + " ";
|
---|
| 622 | }
|
---|
| 623 |
|
---|
| 624 | void CxxExecutor::FillLinkLibs(string& llibs)
|
---|
| 625 | {
|
---|
| 626 | mMyLibs = llibs;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | string CxxExecutor::GetLinkLibs(void)
|
---|
| 630 | {
|
---|
| 631 | return mMyLibs;
|
---|
| 632 | }
|
---|