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

Last change on this file since 2287 was 2287, checked in by ansari, 23 years ago

modifs cxxexecutor avec options declarevars + echo2file - Reza 5/12/02

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