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

Last change on this file since 4027 was 3572, checked in by cmv, 17 years ago

char* -> const char* pour regler les problemes de deprecated string const... + comparaison unsigned signed + suppression EVOL_PLANCK rz+cmv 07/02/2009

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