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

Last change on this file since 2833 was 2762, checked in by ansari, 20 years ago

Suite corrections ZSync + correction (probable) du probleme de plantage a la fin lie a imagnav - reste a corriger menu::del_cur_widget - Reza 24/05/2005

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