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

Last change on this file since 2642 was 2616, checked in by ansari, 21 years ago

Ajout include sopnamsp.h (using namespace SOPHYA) ds le code c++ genere automatiquement par piapp lors de c++exec c++execfrf - Reza 10/09/2004

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