source: Sophya/trunk/SophyaPI/PIext/cxxexecutor.cc@ 2615

Last change on this file since 2615 was 2615, checked in by cmv, 21 years ago

using namespace sophya enleve de machdefs.h, nouveau sopnamsp.h cmv 10/09/2004

  • Property svn:executable set to *
File size: 25.8 KB
RevLine 
[2615]1#include "sopnamsp.h"
[1224]2#include "cxxexecutor.h"
3
4#include <typeinfo>
5
[1237]6#include "strutilxx.h"
[1224]7#include "dvlist.h"
[1287]8#include "cxxcmplnk.h"
[1224]9
10#include "nomgadapter.h"
11#include "pistdimgapp.h"
12
13
14/* --Methode-- */
15CxxExecutor::CxxExecutor(PIACmd *mpiac, PIStdImgApp* /* app */)
[1287]16 : mUserCodeFn(""), mUserFctFn("")
17 , mCompOpt(""), mLinkOpt(""), mMyLibs("")
[2287]18 , mDefRoot("cxx_spiapp"), mDefFunc("usercxx"), mPrtLevel(2), mFgPutVar(false)
[1224]19{
20mIncList.resize(0);
21mCallArgs.resize(0);
22
[1297]23mIncImportList.resize(0);
24mModuleImportList.resize(0);
25
26// La liste de tous les modules que l'on peut ajouter
27mModuleImportDefaultList.resize(0);
28mModuleImportDefaultList.push_back("Samba");
29mModuleImportDefaultList.push_back("SkyMap");
[1319]30mModuleImportDefaultList.push_back("SkyT");
31mModuleImportDefaultList.push_back("FitsIOServer");
32mModuleImportDefaultList.push_back("IFFTW");
33mModuleImportDefaultList.push_back("LinAlg");
[1646]34mModuleImportDefaultList.push_back("XAstroPack");
[1297]35
[1224]36// On enregistre les nouvelles commandes
37string hgrp = "CxxExecutorCmd";
38string usage,kw;
39
40kw = "c++exec";
41usage = "c++exec: Execute the following c++ user code\n";
[1237]42usage+= "Usage: c++exec c++ user code\n";
[1268]43usage+= "Warning: c++ user code can be found in \"TmpDir/"+mDefRoot+".h\"\n";
44usage+= " total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
[1224]45mpiac->RegisterCommand(kw, usage, this, hgrp);
46
47kw = "c++execfrf";
[1268]48usage = "c++execfrf: Execute c++ user_code [user_function_code]\n";
49usage+= "Usage: c++execfrf fileuser.cc [fileuserfct.cc]\n";
50usage+= "Warning: total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
[1224]51mpiac->RegisterCommand(kw, usage, this, hgrp);
52
53kw = "c++args";
[1237]54usage = "c++args: Define user function arguments for c++exec and c++execfrf\n";
[1230]55usage+= "Usage: c++args arg1 arg2 arg3 ...\n";
56usage+= " c++args -? : give current arguments\n";
57usage+= " c++args : reset current arguments";
[1224]58mpiac->RegisterCommand(kw, usage, this, hgrp);
59
60kw = "c++create";
[1287]61usage = "c++create: create a file \"file.cc\" to be used by spiapp\n";
[1224]62usage+= "Usage: c++create file.cc func c++ user code...\n";
63mpiac->RegisterCommand(kw, usage, this, hgrp);
64
65kw = "c++createfrf";
[1287]66usage = "c++createfrf: create a file \"file.cc\" to be used by spiapp\n";
67usage+= " with a user file code \"fileuser.cc\"\n";
68usage+= " and an optional user function code \"fileuserfct.cc\"\n";
[1268]69usage+= "Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]\n";
[1224]70mpiac->RegisterCommand(kw, usage, this, hgrp);
71
72kw = "c++compile";
73usage = "c++compile: compile a file (file.cc -> file.so)\n";
74usage+= "Usage: c++compile file\n";
75usage+= "Warning: give \"file\" or \"file.so\" to create \"file.so\" from \"file.cc\"\n";
76usage+= " : to be used before c++link";
77mpiac->RegisterCommand(kw, usage, this, hgrp);
78
79kw = "c++link";
80usage = "c++link: link function \"func\" in file.so to spiapp\n";
81usage+= "Usage: c++link file.so func";
82mpiac->RegisterCommand(kw, usage, this, hgrp);
83
84kw = "c++include";
[1297]85usage = "c++include: add personnal includes to be used by executor\n";
[1224]86usage+= "Usage: c++include myinc1.h myinc2.h ...\n";
[1297]87usage+= " c++include -? : list current additionnal include files\n";
88usage+= " c++include : reset to no additionnal include files\n";
89usage+= "Warning: to be used before c++create c++compile c++exec...";
[1224]90mpiac->RegisterCommand(kw, usage, this, hgrp);
91
92kw = "c++compileopt";
[1297]93usage = "c++compileopt: add personnal compile options\n";
[1224]94usage+= "Usage: c++compileopt -g -O5 -IMy_Inc_Dir ...\n";
[1297]95usage+= " c++compileopt -? : list current additionnal compile options\n";
96usage+= " c++compileopt : reset to no additionnal compile options\n";
97usage+= "Warning: to be used before c++create c++compile c++exec ...";
[1224]98mpiac->RegisterCommand(kw, usage, this, hgrp);
99
100kw = "c++linkopt";
[1297]101usage = "c++linkopt: add personnal link options\n";
[1230]102usage+= "Usage: c++linkopt -g -O5 ...\n";
[1297]103usage+= " c++linkopt -? : list current additionnal link options\n";
104usage+= " c++linkopt : reset to no additionnal link options\n";
105usage+= "Warning: to be used before c++create c++compile c++exec ...";
[1224]106mpiac->RegisterCommand(kw, usage, this, hgrp);
107
108kw = "c++mylibs";
[1297]109usage = "c++mylibs: add personnal libraries\n";
[1230]110usage+= "Usage: c++mylibs -LMy_Lib_Dir -lmylib1 -lmylib2 ...\n";
[1297]111usage+= " c++mylibs -? : list current additionnal libraries\n";
112usage+= " c++mylibs : reset to no additionnal libraries\n";
113usage+= "Warning: to be used before c++create c++compile c++exec ...";
[1224]114mpiac->RegisterCommand(kw, usage, this, hgrp);
115
[1297]116kw = "c++import";
117usage = "c++import: Sophya additionnal modules to be use in C++ executor\n";
118usage+= "Usage: c++import module1 ... : set list of modules to be used by executor\n";
119usage+= " c++import all : set all modules known by executor\n";
120usage+= " c++import : reset to no additionnal module\n";
121usage+= " c++import -? : list modules currently used in executor\n";
122usage+= " c++import -?? : list of available modules for executor\n";
123usage+= "Warning: to be used before c++create c++compile c++exec ...";
124mpiac->RegisterCommand(kw, usage, this, hgrp);
[1230]125
[2287]126kw = "c++declareomgvar";
127usage = "c++declareomgvar: Activate/Deactivate ObjectManager variables declaration \n";
128usage+= "Usage: c++declareomgvar [false/true] \n";
129usage+= " if true, the ObjectManager variables are declared as $varname \n";
130usage+= " no arguments: display the current status of the flag (default=false\n";
131mpiac->RegisterCommand(kw, usage, this, hgrp);
132
[1289]133kw = "c++prtlevel";
134usage = "c++prtlevel: Print level\n";
135usage+= "Usage: c++prtlevel prtlevel\n";
136usage+= " prtlevel=0 : minimum printing\n";
137usage+= " prtlevel=1 : + compiler/linker output\n";
138usage+= " prtlevel=2 : + full infos (default)";
139mpiac->RegisterCommand(kw, usage, this, hgrp);
140
[1224]141}
142
143/* --Methode-- */
144CxxExecutor::~CxxExecutor()
145{
146}
147
148/* --Methode-- */
[1268]149int CxxExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
[1224]150{
151int rc=0;
[1268]152if(kw == "c++exec") {
[1224]153 if(tokens.size()<1) {
154 cout<<"Usage: c++exec c++ user code"<<endl;
155 return(1);
156 }
[1269]157 rc = ExecuteCXX(toks); if(rc) return(1);
[1268]158
159} else if(kw == "c++execfrf") {
160 if(tokens.size()<1) {
161 cout<<"Usage: c++execfrf fileuser.cc [fileuserfct.cc]"<<endl;
162 return(1);
163 }
[1269]164 rc = FillUserCode(tokens[0]); if(rc) return(1);
165 if(tokens.size()>1) rc = FillUserFctFrF(tokens[1]);
[1287]166 else rc = FillUserFctFrF();
167 if(rc) return(1);
[1224]168 rc = CrFile(); if(rc) return(1);
169 rc = Compile(); if(rc) return(1);
170 rc = Link(); if(rc) return(1);
171 rc = Call(); if(rc) return(1);
172
173} else if(kw == "c++args") {
[1233]174 if(tokens.size()==1) if(tokens[0]=="-?")
175 {cout<<"c++args "<<GetArgs()<<endl; return(0);}
[1230]176 FillArgs(tokens);
[1224]177
178} else if(kw == "c++create") {
179 if(tokens.size()<3) {
180 cout<<"Usage: c++create file.cc func c++ user code ..."<<endl;
181 return(1);
182 }
[1287]183 rc = FillUserCode(toks,2); if(rc) return(1);
184 rc = FillUserFctFrS(); if(rc) return(1);
185 rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
[1224]186
187} else if(kw == "c++createfrf") {
188 if(tokens.size()<3) {
[1268]189 cout<<"Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]"<<endl;
[1224]190 return(1);
191 }
[1269]192 rc = FillUserCode(tokens[2]); if(rc) return(1);
[1287]193 if(tokens.size()>3) rc = FillUserFctFrF(tokens[3]);
194 else rc = FillUserFctFrF(tokens[3]);
195 if(rc) return(1);
[1224]196 rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
197
198} else if(kw == "c++compile") {
199 if(tokens.size()>=1) rc = Compile(tokens[0]);
200 else rc = Compile();
201 if(rc) return(1);
202
203} else if(kw == "c++link") {
204 if(tokens.size()>=2) rc = Link(tokens[0],tokens[1]);
205 else if(tokens.size()>=1) rc = Link(tokens[0]);
206 else rc = Link();
207 if(rc) return(1);
208
209} else if(kw == "c++include") {
[1233]210 if(tokens.size()==1) if(tokens[0]=="-?")
211 {cout<<"c++include "<<GetInclude()<<endl; return(0);}
[1230]212 FillInclude(tokens);
[1224]213
214} else if(kw == "c++compileopt") {
[1233]215 if(tokens.size()==1) if(tokens[0]=="-?")
216 {cout<<"c++compileopt "<<GetCompileOpt()<<endl; return(0);}
[1230]217 FillCompileOpt(tokens);
[1224]218
219} else if(kw == "c++linkopt") {
[1233]220 if(tokens.size()==1) if(tokens[0]=="-?")
221 {cout<<"c++linkopt "<<GetLinkOpt()<<endl; return(0);}
[1230]222 FillLinkOpt(tokens);
[1224]223
224} else if(kw == "c++mylibs") {
[1233]225 if(tokens.size()==1) if(tokens[0]=="-?")
226 {cout<<"c++mylibs "<<GetLinkLibs()<<endl; return(0);}
[1230]227 FillLinkLibs(tokens);
[1289]228
[1297]229} else if(kw == "c++import") {
230 if(tokens.size()==1) {
231 if(tokens[0]=="-?")
232 {cout<<"c++import "<<GetModuleImport()<<endl; return(0);}
233 if(tokens[0]=="-??") {
234 cout<<"c++import possibilities :"<<endl;
235 if(mModuleImportDefaultList.size()>0) {
236 for(uint_4 i=0;i<mModuleImportDefaultList.size();i++)
237 cout<<" "<<mModuleImportDefaultList[i];
238 cout<<endl;
239 }
240 return(0);
241 }
242 }
243 FillModuleImport(tokens);
[2287]244} else if(kw == "c++declareomgvar") {
245 if (tokens.size() <1) {
246 string fgstat = (mFgPutVar) ? "TRUE" : "FALSE" ;
247 cout << " c++declareomgvar: Current flag status: " << fgstat << endl;
248 }
249 else {
250 if (tokens[0] == "true") DeclareObjMgrVar(true);
251 else if (tokens[0] == "false") DeclareObjMgrVar(false);
252 }
[1289]253} else if(kw == "c++prtlevel") {
254 if(tokens.size()==1) if(tokens[0]=="-?")
255 {cout<<"c++prtlevel prtlevel = "<<mPrtLevel<<endl; return(0);}
256 if(tokens.size()>0) {
257 int lp=atoi(tokens[0].c_str()); if(lp<0) lp=0;
258 mPrtLevel=(uint_2) lp;
259 } else mPrtLevel=2;
260
[1224]261}
262
263return(0);
264}
265
266/* --Methode-- */
[1269]267int CxxExecutor::ExecuteCXX(string usercode,string userfct)
268{
269int rc=0;
270rc = FillUserCode(usercode,0); if(rc) return(1);
[1287]271rc = FillUserFctFrS(userfct); if(rc) return(1);
[1269]272rc = CrFile(); if(rc) return(1);
273rc = Compile(); if(rc) return(1);
274rc = Link(); if(rc) return(1);
275rc = Call(); if(rc) return(1);
276return 0;
277}
278
279/* --Methode-- */
[1268]280int CxxExecutor::CrFile(string cfilename,string func)
[1287]281// Si un nom n'est pas precise alors TmpDir/cxx_spiapp.cc
[1262]282{
[2181]283if(cfilename.size()<1) cfilename = TmpDir() + mDefRoot + ".cc";
[1287]284
[1268]285if(func.size()<1) func = mDefFunc;
[1262]286
[1224]287ofstream os(cfilename.c_str(),ios::out);
[1237]288if(!os)
289 {cout<<"CxxExecutor::CrFile: unable to open "<<cfilename<<endl;
290 return 1;}
[1224]291
292PutInclude(os);
293os<<endl;
294
295PutIncludeUser(os);
296os<<endl;
297
[1268]298os<<"//-------------------------------------------------//"<<endl;
[1269]299os<<"//----------------- User Functions ----------------//"<<endl;
[1268]300os<<"//-------------------------------------------------//"<<endl;
301if(mUserFctFn.size()>0) os<<"#include \""<<mUserFctFn<<"\""<<endl;
302os<<endl;
303
304
[1224]305os<<"extern \"C\" {"<<endl;
306os<<" int "<<func<<"( vector<string>& args );"<<endl;
307os<<"}"<<endl<<endl;
308os<<"int "<<func<<"( vector<string>& args )"<<endl;
309os<<"{"<<endl;
310os<<"// Some definitions to help using spiapp;"<<endl;
311os<<"NamedObjMgr omg;"<<endl;
312os<<"Services2NObjMgr& srvo = *omg.GetServiceObj();"<<endl;
313os<<endl;
314
315PutObject(os);
316os<<endl;
317
[2287]318if (mFgPutVar) PutVar(os);
[1224]319os<<endl;
320
[1237]321os<<"//--------------------------------------------//"<<endl;
[1269]322os<<"//----------------- User Code ----------------//"<<endl;
[1237]323os<<"//--------------------------------------------//"<<endl;
[1269]324if(mUserCodeFn.size()>0) os<<"#include \""<<mUserCodeFn<<"\""<<endl;
[1224]325os<<endl;
326
327os<<"return 0;"<<endl;
328os<<"}"<<endl;
329
[1289]330if(mPrtLevel>1)
[1269]331 cout<<"File "<<cfilename<<" for function "<<func<<" created :"<<endl;
[1289]332if(mPrtLevel>1 && mUserCodeFn.size()>0)
[1269]333 cout<<" User code was in file "<<mUserCodeFn<<endl;
[1289]334if(mPrtLevel>1 && mUserFctFn.size()>0)
[1269]335 cout<<" User function code was in file "<<mUserFctFn<<endl;
[1224]336return 0;
337}
338
339/* --Methode-- */
340void CxxExecutor::PutInclude(ofstream& os)
341{
[1297]342os<<"#include \"machdefs.h\""<<endl<<endl;
[1224]343
[1297]344os<<"//---- System et stdc++ include files"<<endl
[1224]345 <<"#include <stdio.h>"<<endl
346 <<"#include <stdlib.h>"<<endl
347 <<"#include <math.h>"<<endl
348 <<"#include <ctype.h>"<<endl
349 <<"#include <string.h>"<<endl
[2322]350 <<"#include <iostream>"<<endl
351 <<"#include <fstream>"<<endl
[1224]352 <<"#include <complex>"<<endl
353 <<endl
354
355 <<"#include <typeinfo>"<<endl
356 <<"#include <string>"<<endl
357 <<"#include <vector>"<<endl
358 <<"#include <map>"<<endl
359 <<"#include <functional>"<<endl
360 <<"#include <list>"<<endl
361 <<endl
362
[2123]363#ifdef SANS_EVOLPLANCK
364 <<"//---- Peida include files"<<endl
365 <<"#include \"toutpeida.h\""<<endl
366#else
[1224]367 <<"//---- Sophya include files"<<endl
[1607]368 <<"#include \"basetools.h\""<<endl
[1224]369 <<"#include \"systools.h\""<<endl
[1607]370 <<"#include \"sutils.h\""<<endl
[1224]371 <<"#include \"ntools.h\""<<endl
372 <<"#include \"array.h\""<<endl
373 <<"#include \"histats.h\""<<endl
374 <<endl
[2123]375#endif
[1224]376
377 <<"//---- Spiapp include files"<<endl
378 <<"#include \"nobjmgr.h\""<<endl
379 <<"#include \"servnobjm.h\""<<endl
[1297]380 <<endl;
[1224]381
[1297]382os<<"//---- Include files from additionnal modules"<<endl;
383if(mIncImportList.size()>0)
384 for(uint_4 i=0;i<mIncImportList.size();i++)
385 os<<"#include \""<<mIncImportList[i]<<"\""<<endl;
386os<<endl;
387
[1469]388os<<"//---- function to compare bits on double \n"
389 <<"int_8 BitCmp64(double v,int_8 flg) \n"
390 <<"{return ((int_8)((v<0.) ? v-0.1 : v+0.1))&flg;} \n"
391 <<endl;
392
[1319]393os<<"//---- function for Adding and displaying Objects " << endl;
394os<<"void Keep_Object(AnyDataObj & obj, string const & nom) \n{ \n"
395 <<" string name = nom; \n NamedObjMgr om; \n"
396 <<" if (om.GetObj(name)) \n"
397 <<" cerr << \"KeepObj()/Warning Already kept object \" << endl; \n"
398 <<" else om.AddObj(obj, name); \n"
399 <<"} \n" << endl;
400os<<"void Display_Object(AnyDataObj & obj, string const & opt, string const & nom) \n { \n"
401 <<" string name = nom; \n NamedObjMgr om; \n"
402 <<" if (!om.GetObj(name)) \n"
403 <<" om.AddObj(obj, name); \n"
404 <<" om.DisplayObj(name, opt); \n"
405 <<"} \n" << endl;
[2287]406os<<"//---- function for getting and setting ObjectManager variables " << endl;
407os<<"void Set_ObjMgrVar(MuTyV v, string const & nom) \n{ \n"
408 <<" NamedObjMgr om; \n"
409 <<" om.SetVar(nom, (string)v); \n"
410 <<"} \n" << endl;
411os<<"MuTyV Get_ObjMgrVar(const char * nom) \n{ \n"
412 <<" string name = nom; NamedObjMgr om; \n"
413 <<" MuTyV v = om.GetVar(name); \n"
414 <<" return v; \n"
415 <<"} \n" << endl;
[1319]416
[2287]417os<<"//---- Macro for Objects and variables saving"<<endl
418 <<"#define KeepObj(obj) Keep_Object(obj, #obj)"<<endl
419 <<"#define GetOMVar(var) Get_ObjMgrVar( #var )"<<endl
420 <<"#define SetOMVar(var) Set_ObjMgrVar(var, #var )"<<endl
[1224]421 <<endl;
422
[2287]423os<<"//---- Macro Displaying objects and command execution"<<endl;
[1319]424os<<"#define DisplayObj(obj, att) Display_Object(obj, att, #obj); \n" << endl;
425os<<"#define ExecuteCommand(cmd) srvo.ExecuteCommand(cmd); \n" << endl;
426
[1224]427return;
428}
429
430/* --Methode-- */
431void CxxExecutor::PutIncludeUser(ofstream& os)
432{
433if(mIncList.size()<1) return;
434for(uint_4 i=0;i<mIncList.size();i++)
435 os<<"#include \""<<mIncList[i]<<"\""<<endl;
436}
437
438/* --Methode-- */
439void CxxExecutor::PutObject(ofstream& os)
440{
441NamedObjMgr omg;
442NObjMgrAdapter* objmgrad;
443vector<string> objlist;
444string patt = "*";
445omg.GetObjList(patt,objlist);
446int nobjs = objlist.size();
447
448os<<"//-------------- Object List --------------"<<endl;
449os<<"//Number of objects = "<<nobjs<<endl;
450os<<"string ___nomobj;"<<endl<<endl;
451if(nobjs<=0) return;
452
453string dir,nobj,stmp,obtype;
454for(int i=0;i<nobjs;i++) {
455 objmgrad = omg.GetObjAdapter(objlist[i]);
456 omg.ParseObjectName(objlist[i],dir,nobj);
457 obtype = objmgrad->GetDataObjType();
458 stmp = "___" + nobj;
459
460 os<<"___nomobj = \""<<nobj<<"\";"<<endl;
461 os<<obtype<<"* "<<stmp
462 <<" = dynamic_cast< "<<obtype<<" * >(omg.GetObj(___nomobj));"<<endl;
[1450]463#ifdef SANS_EVOLPLANCK
464 os<<"if("<<stmp<<"==NULL) { \n"
[2182]465 <<" long mExcErr = nullPtrErr; \n"
[1450]466 <<" cerr << \"CxxExecutor::PutObject: Non existing object "
467 <<" ... please update file \\n \" ;"
[2181]468 <<" throw mExcErr ; \n } "<<endl;
[1450]469#else
[1224]470 os<<"if("<<stmp<<"==NULL) throw NullPtrError"
471 <<"(\"CxxExecutor::PutObject: Non existing object "<<nobj
472 <<"... please update file\");"<<endl;
[1450]473#endif
[1224]474 os<<obtype<<"& "<<nobj<<" = (*"<<stmp<<");"<<endl<<endl;
475}
476
477return;
478}
479
480/* --Methode-- */
481void CxxExecutor::PutVar(ofstream& os)
482{
483os<<"//-------------- Variable List --------------"<<endl;
484NamedObjMgr omg;
485DVList& varlist = omg.GetVarList();
486// varlist.Show(); varlist.Print();
487DVList::ValList::const_iterator it;
488for(it=varlist.Begin(); it!=varlist.End(); it++) {
489 string key = (*it).first;
[1262]490 if (isalpha(key[0]) ) {
491 os<<"___nomobj = \""<<key<<"\";"<<endl;
492 os<<"MuTyV & $"<<key<<" = omg.GetVarList().Get(___nomobj);"<<endl;
493 }
[1224]494}
495
[1237]496return;
[1224]497}
498
499/* --Methode-- */
[1268]500int CxxExecutor::FillUserCode(string& usercode,uint_4 first)
[1287]501// User code is read from input.
[1268]502// - first is the first position in the "string" where the code starts
[1287]503// - Code is put into file "TmpDir/cxx_spiapp.h".
[1224]504{
[1237]505mUserCodeFn = "";
[1268]506
507// get the string part which is after word "first"
508string code = usercode;
509if(code.size()<=0) {cout<<"CxxExecutor::FillUserCode: no user code"<<endl;
510 return 1;}
511size_t q;
512for(uint_4 i=0;i<=first;i++) {
513 q = code.find_first_not_of(" \t");
514 if(q>=code.size()) {code=""; break;}
515 code = code.substr(q);
516 if(i==first) break;
517 q = code.find_first_of(" \t");
518 if(q>=code.size()) {code=""; break;}
519 code = code.substr(q);
520}
521if(code.size()<=0)
522 {cout<<"CxxExecutor::FillUserCode: no user code after "<<first<<endl;
523 return 1;}
524
525// Fill the file with user code
[2181]526mUserCodeFn = TmpDir() + mDefRoot + ".h";
[1237]527ofstream os(mUserCodeFn.c_str(),ios::out);
528if(!os) {cout<<"CxxExecutor::FillUserCode: unable to open "
529 <<mUserCodeFn<<endl; mUserCodeFn = ""; return 1;}
[1269]530os<<code<<endl;
[1289]531if(mPrtLevel>1)
[1269]532 cout<<"User code filled from standard input into "<<mUserCodeFn<<endl;
[1224]533return 0;
534}
535
536/* --Methode-- */
[1287]537int CxxExecutor::FillUserFctFrS(string userfctcode)
[1269]538// - Fill user Fonction code from string "userfct"
[1287]539// - Code is put into file "TmpDir/cxx_spiapp_fct.h".
[1224]540{
[1269]541mUserFctFn = "";
[1287]542if(userfctcode.size()<1) return 0;
[2181]543mUserFctFn = TmpDir() + mDefRoot + "_fct.h";
[1269]544ofstream os(mUserFctFn.c_str(),ios::out);
545if(!os) {cout<<"CxxExecutor::FillUserFctFrS: unable to open "
546 <<mUserFctFn<<endl; mUserFctFn = ""; return 1;}
[1287]547os<<userfctcode<<endl;
[1289]548if(mPrtLevel>1)
[1269]549 cout<<"User Function code filled from standard input into "<<mUserFctFn<<endl;
550return 0;
551}
552
553/* --Methode-- */
554int CxxExecutor::FillUserCode(string filename)
555// User code is read from "filename".
556{
[1237]557mUserCodeFn = filename;
[1289]558if(mPrtLevel>1 && mUserCodeFn.size()>0)
[1269]559 cout<<"User code filled from file "<<mUserCodeFn<<endl;
[1224]560return 0;
561}
562
563/* --Methode-- */
[1270]564int CxxExecutor::FillUserFctFrF(string filefctname)
[1287]565// User function code is read from "filefctname".
[1269]566{
567mUserFctFn = filefctname;
[1289]568if(mPrtLevel>1 && mUserFctFn.size()>0)
[1269]569 cout<<"User Function code filled from file "<<mUserFctFn<<endl;
570return 0;
571}
572
573/* --Methode-- */
[1224]574int CxxExecutor::Compile(string rootfilename)
[1287]575//--------------------------------------------------------//
576// rootfilename = | name | "" ou (default) //
577//--------------------------------------------------------//
578// fichier .cc | name.cc | TmpDir/cxx_spiapp.cc //
579// | ../dir/name.cc | //
580// fichier .o | TmpDir/name.o | TmpDir/cxx_spiapp.o //
581// fichier .so | TmpDir/name.so | TmpDir/cxx_spiapp.so //
582//--------------------------------------------------------//
[1224]583{
[2181]584if(rootfilename.size()<1) rootfilename = TmpDir() + mDefRoot;
[1289]585if(mPrtLevel>1) cout<<"Compile "<<rootfilename<<endl;
[1287]586
587int rc;
[2123]588#ifdef SANS_EVOLPLANCK
[1287]589CxxCompilerLinker cxx;
[2123]590string sophlib = " -lPIE -lm ";
591cxx.AddLinkOptions(sophlib);
592#else
593CxxCompilerLinker cxx(true,true,true);
594#endif
[2181]595if(TmpDir().size()>0) cxx.SetTmpDir(TmpDir());
[1289]596if(mPrtLevel>0) cxx.SetVerbose(true);
597 else cxx.SetVerbose(false);
[1287]598
599// Compilation
600string fcc = rootfilename + ".cc";
601string fo = "";
602cxx.AddCompileOptions(mCompOpt);
603rc = cxx.Compile(fcc,fo);
[1289]604if(mPrtLevel>1) cout << "Compilation rc = "<<rc<< endl;
[1287]605if(rc) return 1;
606
607// Fabrication Shared Lib.
608string fso = "";
609cxx.AddLinkOptions(mLinkOpt);
610cxx.AddLinkOptions(mMyLibs);
611rc = cxx.BuildSO(fo,fso);
[1289]612if(mPrtLevel>1) cout << "Shared Library rc = "<<rc<< endl;
[1287]613if(rc) return 2;
614
[1224]615return 0;
616}
617
618/* --Methode-- */
[1287]619//int CxxExecutor::Compile(string rootfilename)
620// Ne marche pas si TmpDir != ""
621//{
622//if(rootfilename.size()<1) rootfilename = mDefRoot;
[1289]623//if(mPrtLevel>1) cout<<"Compile: "<<rootfilename<<endl;
[1287]624//int rc = 0;
625//rc = CrMakefile();
626//if(rc) return(1);
627//string make = "";
628//make += "make -f " + mDefRoot + "_Makefile";
629//make += " CXXFLAGS=\"" + mCompOpt + "\"";
630//make += " LDFLAGS=\"" + mLinkOpt + "\"";
631//make += " MYLIBS=\"" + mMyLibs + "\"";
632//make += " " + rootfilename;
633//rc = system(make.c_str());
634//if(rc)
635// {cout<<"CxxExecutor::Compile : \n"<<make<<" Failed"<<endl;
636// return 1000+rc;}
637//return 0;
638//}
639
640/* --Methode-- */
641//int CxxExecutor::CrMakefile(void)
642// Ne marche pas si TmpDir != ""
643//{
[2181]644//string makename = TmpDir() + mDefRoot + "_Makefile";
[1287]645//ofstream os(makename.c_str(),ios::out);
646//if(!os)
647// {cout<<"CxxExecutor::CrMakefile: unable to open file for Makefile"<<endl;
648// return 1;}
[1224]649//---------------------------------------------------------------------
[1287]650//os<<"MODULEDECCXXFLAGS := -msg_quiet"<<endl;
[2437]651//os<<"include $(SOPHYABASEREP)/Include/MakefileUser.h"<<endl;
[1287]652//os<<"MYLIBS ="<<endl;
653//os<<"LIBS = -L$(SLB) -lPI -lextsophya -lsophya -lm"<<endl;
654//os<<"ifeq ($(MACHEROS),OSF1)"<<endl;
655//os<<"LIBS := $(LIBS) -lfor"<<endl;
656//os<<"endif"<<endl;
657//os<<"ifeq ($(MACHEROS),Linux)"<<endl;
[1473]658//os<<"#LIBS := $(LIBS) -ldl -lf2c"<<endl;
659//os<<"LIBS := $(LIBS) -ldl -lg2c"<<endl;
[1287]660//os<<"endif"<<endl;
661//os<<"%.so:%.o"<<endl;
662//os<<"%:%.cc"<<endl;
663//os<<"%:%.o"<<endl;
664//os<<"%.o:%.cc"<<endl;
665//os<<"%.o:%.c"<<endl;
666//os<<"%:%.c"<<endl;
667//os<<endl;
668//os<<".PRECIOUS: %.so"<<endl;
669//os<<endl;
670//os<<"%:%.so"<<endl;
671//os<<"\t"<<"echo $@ \" made (.so) \""<<endl;
672//os<<"%.so:%.o"<<endl;
673//os<<"\t"<<"$(LINK.cc) -shared -o $@ $< $(LIBS) $(MYLIBS)"<<endl;
674//os<<"%.o:%.cc"<<endl;
675//os<<"\t"<<"$(COMPILE.cc) -o $@ $<"<<endl;
676//os<<"%.o:%.c"<<endl;
677//os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS) $(USERFLAGS) -o $@ $<"<<endl;
[1224]678//---------------------------------------------------------------------
[1287]679//return 0;
680//}
[1224]681
682/* --Methode-- */
683int CxxExecutor::Link(string libname,string func)
684{
[2181]685if(libname.size()<1) libname = TmpDir() + mDefRoot + ".so";
686 else libname = TmpDir() + libname;
[1268]687if(func.size()<1) func = mDefFunc;
688
[1224]689NamedObjMgr omg;
690PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
[1287]691
[1276]692string key("linkff2");
[1224]693vector<string> arg; arg.push_back(libname); arg.push_back(func);
[1268]694string toks = libname + " " + func;
695int rc = mpiac->ExecuteCommand(key,arg,toks);
[1289]696if(mPrtLevel>1) cout<<"Link from "<<libname<<" for function "<<func
[1287]697 <<" (rc="<<rc<<")"<<endl;
[1224]698return 0;
699}
700
701/* --Methode-- */
702int CxxExecutor::Call(string func)
703{
[1268]704if(func.size()<1) func = mDefFunc;
705
[1224]706NamedObjMgr omg;
707PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
[1287]708
[1224]709string key("call");
710vector<string> arg; arg.push_back(func);
[1268]711string toks = func;
[1224]712if(mCallArgs.size()>0)
[1237]713 for(uint_4 i=0;i<mCallArgs.size();i++) arg.push_back(mCallArgs[i]);
[1268]714mpiac->ExecuteCommand(key,arg,toks);
[1224]715return 0;
716}
717
[1230]718/* --Methode-- */
719void CxxExecutor::FillArgs(vector<string>& args)
720{
721mCallArgs.resize(0);
722if(args.size()<1) return;
723for(uint_4 i=0;i<args.size();i++) mCallArgs.push_back(args[i]);
724}
725
726void CxxExecutor::FillArgs(string& args)
727{
728mCallArgs.resize(0);
729FillVStringFrString(args,mCallArgs,' ');
730}
731
732string CxxExecutor::GetArgs(void)
733{
734string dum = "";
735if(mCallArgs.size()<1) return dum;
736for(uint_4 i=0;i<mCallArgs.size();i++) dum += mCallArgs[i] + " ";
737return dum;
738}
739
740/* --Methode-- */
741void CxxExecutor::FillInclude(vector<string>& inc)
742{
743mIncList.resize(0);
744if(inc.size()<1) return;
745for(uint_4 i=0;i<inc.size();i++) mIncList.push_back(inc[i]);
746}
747
748void CxxExecutor::FillInclude(string& inc)
749{
750mIncList.resize(0);
751FillVStringFrString(inc,mIncList,' ');
752}
753
754string CxxExecutor::GetInclude(void)
755{
756string dum = "";
757if(mIncList.size()<1) return dum;
758for(uint_4 i=0;i<mIncList.size();i++) dum += mIncList[i] + " ";
759return dum;
760}
761
762/* --Methode-- */
763void CxxExecutor::FillCompileOpt(vector<string>& copt)
764{
765mCompOpt = "";
766if(copt.size()<1) return;
767for(uint_4 i=0;i<copt.size();i++) mCompOpt += copt[i] + " ";
768}
769
770void CxxExecutor::FillCompileOpt(string& copt)
771{
772mCompOpt = copt;
773}
774
775string CxxExecutor::GetCompileOpt(void)
776{
777return mCompOpt;
778}
779
780/* --Methode-- */
781void CxxExecutor::FillLinkOpt(vector<string>& lopt)
782{
783mLinkOpt = "";
784if(lopt.size()<1) return;
785for(uint_4 i=0;i<lopt.size();i++) mLinkOpt += lopt[i] + " ";
786}
787
788void CxxExecutor::FillLinkOpt(string& lopt)
789{
790mLinkOpt = lopt;
791}
792
793string CxxExecutor::GetLinkOpt(void)
794{
795return mLinkOpt;
796}
797
798/* --Methode-- */
799void CxxExecutor::FillLinkLibs(vector<string>& llibs)
800{
801mMyLibs = "";
802if(llibs.size()<1) return;
803for(uint_4 i=0;i<llibs.size();i++) mMyLibs += llibs[i] + " ";
804}
805
806void CxxExecutor::FillLinkLibs(string& llibs)
807{
808mMyLibs = llibs;
809}
810
811string CxxExecutor::GetLinkLibs(void)
812{
813return mMyLibs;
814}
[1297]815
816/* --Methode-- */
817void CxxExecutor::FillModuleImport(vector<string>& import)
818{
819mModuleImportList.resize(0);
820mIncImportList.resize(0);
821if(import.size()<1) return;
822
823if(import[0]=="all" || import[0]=="All" || import[0]=="ALL") {
824 mModuleImportList = mModuleImportDefaultList;
825} else {
826 for(uint_4 i=0;i<import.size();i++) mModuleImportList.push_back(import[i]);
827}
828
829// Set additionnal Includes
830if(mModuleImportList.size()>0)
831 for(uint_4 i=0;i<mModuleImportList.size();i++) {
[1646]832 if( mModuleImportList[i]=="FitsIOServer")
833 mIncImportList.push_back("fitsioserver.h");
834 else if(mModuleImportList[i]=="IFFTW")
835 mIncImportList.push_back("fftwserver.h");
836 else if(mModuleImportList[i]=="LinAlg")
837 mIncImportList.push_back("intflapack.h");
838 else if( mModuleImportList[i]=="Samba")
839 {mIncImportList.push_back("skymap.h"); mIncImportList.push_back("samba.h");}
840 else if(mModuleImportList[i]=="SkyMap")
[1617]841 mIncImportList.push_back("skymap.h");
[1646]842 else if(mModuleImportList[i]=="SkyT")
843 mIncImportList.push_back("skyt.h");
844 else if(mModuleImportList[i]=="XAstroPack")
845 mIncImportList.push_back("xastropack.h");
[1297]846}
847
848return;
849}
850
851void CxxExecutor::FillModuleImport(string& import)
852{
853mModuleImportList.resize(0);
854vector<string> vsimport;
855FillVStringFrString(import,vsimport,' ');
856FillModuleImport(vsimport);
857}
858
859string CxxExecutor::GetModuleImport(void)
860{
861string dum = "";
862if(mModuleImportList.size()<1) return dum;
863for(uint_4 i=0;i<mModuleImportList.size();i++) dum += mModuleImportList[i] + " ";
864return dum;
865}
Note: See TracBrowser for help on using the repository browser.