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