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

Last change on this file since 3071 was 2944, checked in by ansari, 19 years ago

1- Correction/Amelioration gestion code de retour (Rc) ds les
CmdExecutor (basexecut.cc cxxexecutor.cc) - pour c++exec en particulier
2- ajout flag fgsmall (constructeur PIStdImgApp) pour petite fenetre
principale

Reza , 26/04/2006

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