1 | #include "cxxexecutor.h"
|
---|
2 |
|
---|
3 | #include <typeinfo>
|
---|
4 |
|
---|
5 | #include "strutilxx.h"
|
---|
6 | #include "dvlist.h"
|
---|
7 | #include "cxxcmplnk.h"
|
---|
8 |
|
---|
9 | #include "nomgadapter.h"
|
---|
10 | #include "pistdimgapp.h"
|
---|
11 |
|
---|
12 |
|
---|
13 | /* --Methode-- */
|
---|
14 | CxxExecutor::CxxExecutor(PIACmd *mpiac, PIStdImgApp* /* app */)
|
---|
15 | : mUserCodeFn(""), mUserFctFn("")
|
---|
16 | , mCompOpt(""), mLinkOpt(""), mMyLibs("")
|
---|
17 | , mDefTmp(""), mDefRoot("cxx_spiapp"), mDefFunc("usercxx"), mPrtLevel(1)
|
---|
18 | {
|
---|
19 | mIncList.resize(0);
|
---|
20 | mCallArgs.resize(0);
|
---|
21 |
|
---|
22 | // Gestion des fichiers par default dans TmpDir
|
---|
23 | NamedObjMgr omg;
|
---|
24 | string tmpdir = omg.GetTmpDir();
|
---|
25 | if(tmpdir.size()>1) mDefTmp = tmpdir;
|
---|
26 |
|
---|
27 | // On enregistre les nouvelles commandes
|
---|
28 | string hgrp = "CxxExecutorCmd";
|
---|
29 | string usage,kw;
|
---|
30 |
|
---|
31 | kw = "c++exec";
|
---|
32 | usage = "c++exec: Execute the following c++ user code\n";
|
---|
33 | usage+= "Usage: c++exec c++ user code\n";
|
---|
34 | usage+= "Warning: c++ user code can be found in \"TmpDir/"+mDefRoot+".h\"\n";
|
---|
35 | usage+= " total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
|
---|
36 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
37 |
|
---|
38 | kw = "c++execfrf";
|
---|
39 | usage = "c++execfrf: Execute c++ user_code [user_function_code]\n";
|
---|
40 | usage+= "Usage: c++execfrf fileuser.cc [fileuserfct.cc]\n";
|
---|
41 | usage+= "Warning: total generated code can be found in \"TmpDir/"+mDefRoot+".cc\"";
|
---|
42 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
43 |
|
---|
44 | kw = "c++args";
|
---|
45 | usage = "c++args: Define user function arguments for c++exec and c++execfrf\n";
|
---|
46 | usage+= "Usage: c++args arg1 arg2 arg3 ...\n";
|
---|
47 | usage+= " c++args -? : give current arguments\n";
|
---|
48 | usage+= " c++args : reset current arguments";
|
---|
49 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
50 |
|
---|
51 | kw = "c++create";
|
---|
52 | usage = "c++create: create a file \"file.cc\" to be used by spiapp\n";
|
---|
53 | usage+= "Usage: c++create file.cc func c++ user code...\n";
|
---|
54 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
55 |
|
---|
56 | kw = "c++createfrf";
|
---|
57 | usage = "c++createfrf: create a file \"file.cc\" to be used by spiapp\n";
|
---|
58 | usage+= " with a user file code \"fileuser.cc\"\n";
|
---|
59 | usage+= " and an optional user function code \"fileuserfct.cc\"\n";
|
---|
60 | usage+= "Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]\n";
|
---|
61 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
62 |
|
---|
63 | kw = "c++compile";
|
---|
64 | usage = "c++compile: compile a file (file.cc -> file.so)\n";
|
---|
65 | usage+= "Usage: c++compile file\n";
|
---|
66 | usage+= "Warning: give \"file\" or \"file.so\" to create \"file.so\" from \"file.cc\"\n";
|
---|
67 | usage+= " : to be used before c++link";
|
---|
68 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
69 |
|
---|
70 | kw = "c++link";
|
---|
71 | usage = "c++link: link function \"func\" in file.so to spiapp\n";
|
---|
72 | usage+= "Usage: c++link file.so func";
|
---|
73 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
74 |
|
---|
75 | kw = "c++include";
|
---|
76 | usage = "c++include: give personnal includes to be used\n";
|
---|
77 | usage+= "Usage: c++include myinc1.h myinc2.h ...\n";
|
---|
78 | usage+= " c++include -? : give current include files\n";
|
---|
79 | usage+= " c++include : reset current include files\n";
|
---|
80 | usage+= "Warning: to be used before c++create... c++exec...";
|
---|
81 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
82 |
|
---|
83 | kw = "c++compileopt";
|
---|
84 | usage = "c++compileopt: give additionnal compile options\n";
|
---|
85 | usage+= "Usage: c++compileopt -g -O5 -IMy_Inc_Dir ...\n";
|
---|
86 | usage+= " c++compileopt -? : give current compile options\n";
|
---|
87 | usage+= " c++compileopt : reset current compile options\n";
|
---|
88 | usage+= "Warning: to be used before c++compile";
|
---|
89 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
90 |
|
---|
91 | kw = "c++linkopt";
|
---|
92 | usage = "c++linkopt: give additionnal link options\n";
|
---|
93 | usage+= "Usage: c++linkopt -g -O5 ...\n";
|
---|
94 | usage+= " c++linkopt -? : give current link options\n";
|
---|
95 | usage+= " c++linkopt : reset current link options\n";
|
---|
96 | usage+= "Warning: to be used before c++compile";
|
---|
97 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
98 |
|
---|
99 | kw = "c++mylibs";
|
---|
100 | usage = "c++mylibs: give additionnal libraries\n";
|
---|
101 | usage+= "Usage: c++mylibs -LMy_Lib_Dir -lmylib1 -lmylib2 ...\n";
|
---|
102 | usage+= " c++mylibs -? : give current additionnal libraries\n";
|
---|
103 | usage+= " c++mylibs : reset current additionnal libraries\n";
|
---|
104 | usage+= "Warning: to be used before c++compile";
|
---|
105 | mpiac->RegisterCommand(kw, usage, this, hgrp);
|
---|
106 |
|
---|
107 |
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* --Methode-- */
|
---|
111 | CxxExecutor::~CxxExecutor()
|
---|
112 | {
|
---|
113 | }
|
---|
114 |
|
---|
115 | /* --Methode-- */
|
---|
116 | int CxxExecutor::Execute(string& kw, vector<string>& tokens, string& toks)
|
---|
117 | {
|
---|
118 | int rc=0;
|
---|
119 | if(kw == "c++exec") {
|
---|
120 | if(tokens.size()<1) {
|
---|
121 | cout<<"Usage: c++exec c++ user code"<<endl;
|
---|
122 | return(1);
|
---|
123 | }
|
---|
124 | rc = ExecuteCXX(toks); if(rc) return(1);
|
---|
125 |
|
---|
126 | } else if(kw == "c++execfrf") {
|
---|
127 | if(tokens.size()<1) {
|
---|
128 | cout<<"Usage: c++execfrf fileuser.cc [fileuserfct.cc]"<<endl;
|
---|
129 | return(1);
|
---|
130 | }
|
---|
131 | rc = FillUserCode(tokens[0]); if(rc) return(1);
|
---|
132 | if(tokens.size()>1) rc = FillUserFctFrF(tokens[1]);
|
---|
133 | else rc = FillUserFctFrF();
|
---|
134 | if(rc) return(1);
|
---|
135 | rc = CrFile(); if(rc) return(1);
|
---|
136 | rc = Compile(); if(rc) return(1);
|
---|
137 | rc = Link(); if(rc) return(1);
|
---|
138 | rc = Call(); if(rc) return(1);
|
---|
139 |
|
---|
140 | } else if(kw == "c++args") {
|
---|
141 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
142 | {cout<<"c++args "<<GetArgs()<<endl; return(0);}
|
---|
143 | FillArgs(tokens);
|
---|
144 |
|
---|
145 | } else if(kw == "c++create") {
|
---|
146 | if(tokens.size()<3) {
|
---|
147 | cout<<"Usage: c++create file.cc func c++ user code ..."<<endl;
|
---|
148 | return(1);
|
---|
149 | }
|
---|
150 | rc = FillUserCode(toks,2); if(rc) return(1);
|
---|
151 | rc = FillUserFctFrS(); if(rc) return(1);
|
---|
152 | rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
|
---|
153 |
|
---|
154 | } else if(kw == "c++createfrf") {
|
---|
155 | if(tokens.size()<3) {
|
---|
156 | cout<<"Usage: c++createfrf file.cc func fileuser.cc [fileuserfct.cc]"<<endl;
|
---|
157 | return(1);
|
---|
158 | }
|
---|
159 | rc = FillUserCode(tokens[2]); if(rc) return(1);
|
---|
160 | if(tokens.size()>3) rc = FillUserFctFrF(tokens[3]);
|
---|
161 | else rc = FillUserFctFrF(tokens[3]);
|
---|
162 | if(rc) return(1);
|
---|
163 | rc = CrFile(tokens[0],tokens[1]); if(rc) return(1);
|
---|
164 |
|
---|
165 | } else if(kw == "c++compile") {
|
---|
166 | if(tokens.size()>=1) rc = Compile(tokens[0]);
|
---|
167 | else rc = Compile();
|
---|
168 | if(rc) return(1);
|
---|
169 |
|
---|
170 | } else if(kw == "c++link") {
|
---|
171 | if(tokens.size()>=2) rc = Link(tokens[0],tokens[1]);
|
---|
172 | else if(tokens.size()>=1) rc = Link(tokens[0]);
|
---|
173 | else rc = Link();
|
---|
174 | if(rc) return(1);
|
---|
175 |
|
---|
176 | } else if(kw == "c++include") {
|
---|
177 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
178 | {cout<<"c++include "<<GetInclude()<<endl; return(0);}
|
---|
179 | FillInclude(tokens);
|
---|
180 |
|
---|
181 | } else if(kw == "c++compileopt") {
|
---|
182 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
183 | {cout<<"c++compileopt "<<GetCompileOpt()<<endl; return(0);}
|
---|
184 | FillCompileOpt(tokens);
|
---|
185 |
|
---|
186 | } else if(kw == "c++linkopt") {
|
---|
187 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
188 | {cout<<"c++linkopt "<<GetLinkOpt()<<endl; return(0);}
|
---|
189 | FillLinkOpt(tokens);
|
---|
190 |
|
---|
191 | } else if(kw == "c++mylibs") {
|
---|
192 | if(tokens.size()==1) if(tokens[0]=="-?")
|
---|
193 | {cout<<"c++mylibs "<<GetLinkLibs()<<endl; return(0);}
|
---|
194 | FillLinkLibs(tokens);
|
---|
195 | }
|
---|
196 |
|
---|
197 | return(0);
|
---|
198 | }
|
---|
199 |
|
---|
200 | /* --Methode-- */
|
---|
201 | int CxxExecutor::ExecuteCXX(string usercode,string userfct)
|
---|
202 | {
|
---|
203 | int rc=0;
|
---|
204 | rc = FillUserCode(usercode,0); if(rc) return(1);
|
---|
205 | rc = FillUserFctFrS(userfct); if(rc) return(1);
|
---|
206 | rc = CrFile(); if(rc) return(1);
|
---|
207 | rc = Compile(); if(rc) return(1);
|
---|
208 | rc = Link(); if(rc) return(1);
|
---|
209 | rc = Call(); if(rc) return(1);
|
---|
210 | return 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* --Methode-- */
|
---|
214 | int CxxExecutor::CrFile(string cfilename,string func)
|
---|
215 | // Si un nom n'est pas precise alors TmpDir/cxx_spiapp.cc
|
---|
216 | {
|
---|
217 | if(cfilename.size()<1) cfilename = mDefTmp + mDefRoot + ".cc";
|
---|
218 |
|
---|
219 | if(func.size()<1) func = mDefFunc;
|
---|
220 |
|
---|
221 | ofstream os(cfilename.c_str(),ios::out);
|
---|
222 | if(!os)
|
---|
223 | {cout<<"CxxExecutor::CrFile: unable to open "<<cfilename<<endl;
|
---|
224 | return 1;}
|
---|
225 |
|
---|
226 | PutInclude(os);
|
---|
227 | os<<endl;
|
---|
228 |
|
---|
229 | PutIncludeUser(os);
|
---|
230 | os<<endl;
|
---|
231 |
|
---|
232 | os<<"//-------------------------------------------------//"<<endl;
|
---|
233 | os<<"//----------------- User Functions ----------------//"<<endl;
|
---|
234 | os<<"//-------------------------------------------------//"<<endl;
|
---|
235 | if(mUserFctFn.size()>0) os<<"#include \""<<mUserFctFn<<"\""<<endl;
|
---|
236 | os<<endl;
|
---|
237 |
|
---|
238 |
|
---|
239 | os<<"extern \"C\" {"<<endl;
|
---|
240 | os<<" int "<<func<<"( vector<string>& args );"<<endl;
|
---|
241 | os<<"}"<<endl<<endl;
|
---|
242 | os<<"int "<<func<<"( vector<string>& args )"<<endl;
|
---|
243 | os<<"{"<<endl;
|
---|
244 | os<<"// Some definitions to help using spiapp;"<<endl;
|
---|
245 | os<<"NamedObjMgr omg;"<<endl;
|
---|
246 | os<<"Services2NObjMgr& srvo = *omg.GetServiceObj();"<<endl;
|
---|
247 | os<<endl;
|
---|
248 |
|
---|
249 | PutObject(os);
|
---|
250 | os<<endl;
|
---|
251 |
|
---|
252 | PutVar(os);
|
---|
253 | os<<endl;
|
---|
254 |
|
---|
255 | os<<"//--------------------------------------------//"<<endl;
|
---|
256 | os<<"//----------------- User Code ----------------//"<<endl;
|
---|
257 | os<<"//--------------------------------------------//"<<endl;
|
---|
258 | if(mUserCodeFn.size()>0) os<<"#include \""<<mUserCodeFn<<"\""<<endl;
|
---|
259 | os<<endl;
|
---|
260 |
|
---|
261 | os<<"return 0;"<<endl;
|
---|
262 | os<<"}"<<endl;
|
---|
263 |
|
---|
264 | if(mPrtLevel)
|
---|
265 | cout<<"File "<<cfilename<<" for function "<<func<<" created :"<<endl;
|
---|
266 | if(mPrtLevel && mUserCodeFn.size()>0)
|
---|
267 | cout<<" User code was in file "<<mUserCodeFn<<endl;
|
---|
268 | if(mPrtLevel && mUserFctFn.size()>0)
|
---|
269 | cout<<" User function code was in file "<<mUserFctFn<<endl;
|
---|
270 | return 0;
|
---|
271 | }
|
---|
272 |
|
---|
273 | /* --Methode-- */
|
---|
274 | void CxxExecutor::PutInclude(ofstream& os)
|
---|
275 | {
|
---|
276 | os<<"#include \"machdefs.h\""<<endl
|
---|
277 | <<endl
|
---|
278 |
|
---|
279 | <<"//---- System et stdc++ include files"<<endl
|
---|
280 | <<"#include <stdio.h>"<<endl
|
---|
281 | <<"#include <stdlib.h>"<<endl
|
---|
282 | <<"#include <math.h>"<<endl
|
---|
283 | <<"#include <ctype.h>"<<endl
|
---|
284 | <<"#include <string.h>"<<endl
|
---|
285 | <<"#include <iostream.h>"<<endl
|
---|
286 | <<"#include <fstream.h>"<<endl
|
---|
287 | <<"#include <complex>"<<endl
|
---|
288 | <<endl
|
---|
289 |
|
---|
290 | <<"#include <typeinfo>"<<endl
|
---|
291 | <<"#include <string>"<<endl
|
---|
292 | <<"#include <vector>"<<endl
|
---|
293 | <<"#include <map>"<<endl
|
---|
294 | <<"#include <functional>"<<endl
|
---|
295 | <<"#include <list>"<<endl
|
---|
296 | <<endl
|
---|
297 |
|
---|
298 | <<"//---- Sophya include files"<<endl
|
---|
299 | <<"#include \"systools.h\""<<endl
|
---|
300 | <<"#include \"ntools.h\""<<endl
|
---|
301 | <<"#include \"array.h\""<<endl
|
---|
302 | <<"#include \"histats.h\""<<endl
|
---|
303 | <<endl
|
---|
304 |
|
---|
305 | <<"//---- Spiapp include files"<<endl
|
---|
306 | <<"#include \"nobjmgr.h\""<<endl
|
---|
307 | <<"#include \"servnobjm.h\""<<endl
|
---|
308 | <<endl
|
---|
309 |
|
---|
310 | <<"#define KeepObj(obj) ___nomobj = #obj; omg.AddObj(obj,___nomobj);"<<endl
|
---|
311 | <<"#define KeepVar(var) ___nomobj = #var; omg.GetVarList().Get(___nomobj) = var ;"<<endl
|
---|
312 | <<endl;
|
---|
313 |
|
---|
314 | return;
|
---|
315 | }
|
---|
316 |
|
---|
317 | /* --Methode-- */
|
---|
318 | void CxxExecutor::PutIncludeUser(ofstream& os)
|
---|
319 | {
|
---|
320 | if(mIncList.size()<1) return;
|
---|
321 | for(uint_4 i=0;i<mIncList.size();i++)
|
---|
322 | os<<"#include \""<<mIncList[i]<<"\""<<endl;
|
---|
323 | }
|
---|
324 |
|
---|
325 | /* --Methode-- */
|
---|
326 | void CxxExecutor::PutObject(ofstream& os)
|
---|
327 | {
|
---|
328 | NamedObjMgr omg;
|
---|
329 | NObjMgrAdapter* objmgrad;
|
---|
330 | vector<string> objlist;
|
---|
331 | string patt = "*";
|
---|
332 | omg.GetObjList(patt,objlist);
|
---|
333 | int nobjs = objlist.size();
|
---|
334 |
|
---|
335 | os<<"//-------------- Object List --------------"<<endl;
|
---|
336 | os<<"//Number of objects = "<<nobjs<<endl;
|
---|
337 | os<<"string ___nomobj;"<<endl<<endl;
|
---|
338 | if(nobjs<=0) return;
|
---|
339 |
|
---|
340 | string dir,nobj,stmp,obtype;
|
---|
341 | for(int i=0;i<nobjs;i++) {
|
---|
342 | objmgrad = omg.GetObjAdapter(objlist[i]);
|
---|
343 | omg.ParseObjectName(objlist[i],dir,nobj);
|
---|
344 | obtype = objmgrad->GetDataObjType();
|
---|
345 | stmp = "___" + nobj;
|
---|
346 |
|
---|
347 | os<<"___nomobj = \""<<nobj<<"\";"<<endl;
|
---|
348 | os<<obtype<<"* "<<stmp
|
---|
349 | <<" = dynamic_cast< "<<obtype<<" * >(omg.GetObj(___nomobj));"<<endl;
|
---|
350 | os<<"if("<<stmp<<"==NULL) throw NullPtrError"
|
---|
351 | <<"(\"CxxExecutor::PutObject: Non existing object "<<nobj
|
---|
352 | <<"... please update file\");"<<endl;
|
---|
353 | os<<obtype<<"& "<<nobj<<" = (*"<<stmp<<");"<<endl<<endl;
|
---|
354 | }
|
---|
355 |
|
---|
356 | return;
|
---|
357 | }
|
---|
358 |
|
---|
359 | /* --Methode-- */
|
---|
360 | void CxxExecutor::PutVar(ofstream& os)
|
---|
361 | {
|
---|
362 | os<<"//-------------- Variable List --------------"<<endl;
|
---|
363 | NamedObjMgr omg;
|
---|
364 | DVList& varlist = omg.GetVarList();
|
---|
365 | // varlist.Show(); varlist.Print();
|
---|
366 | DVList::ValList::const_iterator it;
|
---|
367 | for(it=varlist.Begin(); it!=varlist.End(); it++) {
|
---|
368 | string key = (*it).first;
|
---|
369 | if (isalpha(key[0]) ) {
|
---|
370 | os<<"___nomobj = \""<<key<<"\";"<<endl;
|
---|
371 | os<<"MuTyV & $"<<key<<" = omg.GetVarList().Get(___nomobj);"<<endl;
|
---|
372 | }
|
---|
373 | }
|
---|
374 |
|
---|
375 | return;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* --Methode-- */
|
---|
379 | int CxxExecutor::FillUserCode(string& usercode,uint_4 first)
|
---|
380 | // User code is read from input.
|
---|
381 | // - first is the first position in the "string" where the code starts
|
---|
382 | // - Code is put into file "TmpDir/cxx_spiapp.h".
|
---|
383 | {
|
---|
384 | mUserCodeFn = "";
|
---|
385 |
|
---|
386 | // get the string part which is after word "first"
|
---|
387 | string code = usercode;
|
---|
388 | if(code.size()<=0) {cout<<"CxxExecutor::FillUserCode: no user code"<<endl;
|
---|
389 | return 1;}
|
---|
390 | size_t q;
|
---|
391 | for(uint_4 i=0;i<=first;i++) {
|
---|
392 | q = code.find_first_not_of(" \t");
|
---|
393 | if(q>=code.size()) {code=""; break;}
|
---|
394 | code = code.substr(q);
|
---|
395 | if(i==first) break;
|
---|
396 | q = code.find_first_of(" \t");
|
---|
397 | if(q>=code.size()) {code=""; break;}
|
---|
398 | code = code.substr(q);
|
---|
399 | }
|
---|
400 | if(code.size()<=0)
|
---|
401 | {cout<<"CxxExecutor::FillUserCode: no user code after "<<first<<endl;
|
---|
402 | return 1;}
|
---|
403 |
|
---|
404 | // Fill the file with user code
|
---|
405 | mUserCodeFn = mDefTmp + mDefRoot + ".h";
|
---|
406 | ofstream os(mUserCodeFn.c_str(),ios::out);
|
---|
407 | if(!os) {cout<<"CxxExecutor::FillUserCode: unable to open "
|
---|
408 | <<mUserCodeFn<<endl; mUserCodeFn = ""; return 1;}
|
---|
409 | os<<code<<endl;
|
---|
410 | if(mPrtLevel)
|
---|
411 | cout<<"User code filled from standard input into "<<mUserCodeFn<<endl;
|
---|
412 | return 0;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /* --Methode-- */
|
---|
416 | int CxxExecutor::FillUserFctFrS(string userfctcode)
|
---|
417 | // - Fill user Fonction code from string "userfct"
|
---|
418 | // - Code is put into file "TmpDir/cxx_spiapp_fct.h".
|
---|
419 | {
|
---|
420 | mUserFctFn = "";
|
---|
421 | if(userfctcode.size()<1) return 0;
|
---|
422 | mUserFctFn = mDefTmp + mDefRoot + "_fct.h";
|
---|
423 | ofstream os(mUserFctFn.c_str(),ios::out);
|
---|
424 | if(!os) {cout<<"CxxExecutor::FillUserFctFrS: unable to open "
|
---|
425 | <<mUserFctFn<<endl; mUserFctFn = ""; return 1;}
|
---|
426 | os<<userfctcode<<endl;
|
---|
427 | if(mPrtLevel)
|
---|
428 | cout<<"User Function code filled from standard input into "<<mUserFctFn<<endl;
|
---|
429 | return 0;
|
---|
430 | }
|
---|
431 |
|
---|
432 | /* --Methode-- */
|
---|
433 | int CxxExecutor::FillUserCode(string filename)
|
---|
434 | // User code is read from "filename".
|
---|
435 | {
|
---|
436 | mUserCodeFn = filename;
|
---|
437 | if(mPrtLevel && mUserCodeFn.size()>0)
|
---|
438 | cout<<"User code filled from file "<<mUserCodeFn<<endl;
|
---|
439 | return 0;
|
---|
440 | }
|
---|
441 |
|
---|
442 | /* --Methode-- */
|
---|
443 | int CxxExecutor::FillUserFctFrF(string filefctname)
|
---|
444 | // User function code is read from "filefctname".
|
---|
445 | {
|
---|
446 | mUserFctFn = filefctname;
|
---|
447 | if(mPrtLevel && mUserFctFn.size()>0)
|
---|
448 | cout<<"User Function code filled from file "<<mUserFctFn<<endl;
|
---|
449 | return 0;
|
---|
450 | }
|
---|
451 |
|
---|
452 | /* --Methode-- */
|
---|
453 | int CxxExecutor::Compile(string rootfilename)
|
---|
454 | //--------------------------------------------------------//
|
---|
455 | // rootfilename = | name | "" ou (default) //
|
---|
456 | //--------------------------------------------------------//
|
---|
457 | // fichier .cc | name.cc | TmpDir/cxx_spiapp.cc //
|
---|
458 | // | ../dir/name.cc | //
|
---|
459 | // fichier .o | TmpDir/name.o | TmpDir/cxx_spiapp.o //
|
---|
460 | // fichier .so | TmpDir/name.so | TmpDir/cxx_spiapp.so //
|
---|
461 | //--------------------------------------------------------//
|
---|
462 | {
|
---|
463 | if(rootfilename.size()<1) rootfilename = mDefTmp + mDefRoot;
|
---|
464 | if(mPrtLevel) cout<<"Compile "<<rootfilename<<endl;
|
---|
465 |
|
---|
466 | int rc;
|
---|
467 | CxxCompilerLinker cxx;
|
---|
468 | if(mDefTmp.size()>0) cxx.SetTmpDir(mDefTmp);
|
---|
469 | cxx.SetVerbose(true);
|
---|
470 |
|
---|
471 | // Compilation
|
---|
472 | string fcc = rootfilename + ".cc";
|
---|
473 | string fo = "";
|
---|
474 | cxx.AddCompileOptions(mCompOpt);
|
---|
475 | rc = cxx.Compile(fcc,fo);
|
---|
476 | if(mPrtLevel) cout << "Compilation rc = "<<rc<< endl;
|
---|
477 | if(rc) return 1;
|
---|
478 |
|
---|
479 | // Fabrication Shared Lib.
|
---|
480 | string fso = "";
|
---|
481 | cxx.AddLinkOptions(mLinkOpt);
|
---|
482 | string sophlib = "-lPI";
|
---|
483 | cxx.AddLinkOptions(sophlib);
|
---|
484 | cxx.AddLinkOptions(mMyLibs);
|
---|
485 | rc = cxx.BuildSO(fo,fso);
|
---|
486 | if(mPrtLevel) cout << "Shared Library rc = "<<rc<< endl;
|
---|
487 | if(rc) return 2;
|
---|
488 |
|
---|
489 | return 0;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /* --Methode-- */
|
---|
493 | //int CxxExecutor::Compile(string rootfilename)
|
---|
494 | // Ne marche pas si TmpDir != ""
|
---|
495 | //{
|
---|
496 | //if(rootfilename.size()<1) rootfilename = mDefRoot;
|
---|
497 | //if(mPrtLevel) cout<<"Compile: "<<rootfilename<<endl;
|
---|
498 | //int rc = 0;
|
---|
499 | //rc = CrMakefile();
|
---|
500 | //if(rc) return(1);
|
---|
501 | //string make = "";
|
---|
502 | //make += "make -f " + mDefRoot + "_Makefile";
|
---|
503 | //make += " CXXFLAGS=\"" + mCompOpt + "\"";
|
---|
504 | //make += " LDFLAGS=\"" + mLinkOpt + "\"";
|
---|
505 | //make += " MYLIBS=\"" + mMyLibs + "\"";
|
---|
506 | //make += " " + rootfilename;
|
---|
507 | //rc = system(make.c_str());
|
---|
508 | //if(rc)
|
---|
509 | // {cout<<"CxxExecutor::Compile : \n"<<make<<" Failed"<<endl;
|
---|
510 | // return 1000+rc;}
|
---|
511 | //return 0;
|
---|
512 | //}
|
---|
513 |
|
---|
514 | /* --Methode-- */
|
---|
515 | //int CxxExecutor::CrMakefile(void)
|
---|
516 | // Ne marche pas si TmpDir != ""
|
---|
517 | //{
|
---|
518 | //string makename = mDefTmp + mDefRoot + "_Makefile";
|
---|
519 | //ofstream os(makename.c_str(),ios::out);
|
---|
520 | //if(!os)
|
---|
521 | // {cout<<"CxxExecutor::CrMakefile: unable to open file for Makefile"<<endl;
|
---|
522 | // return 1;}
|
---|
523 | //---------------------------------------------------------------------
|
---|
524 | //os<<"MODULEDECCXXFLAGS := -msg_quiet"<<endl;
|
---|
525 | //os<<"include $(DPCBASEREP)/Include/MakefileUser.h"<<endl;
|
---|
526 | //os<<"MYLIBS ="<<endl;
|
---|
527 | //os<<"LIBS = -L$(SLB) -lPI -lextsophya -lsophya -lm"<<endl;
|
---|
528 | //os<<"ifeq ($(MACHEROS),OSF1)"<<endl;
|
---|
529 | //os<<"LIBS := $(LIBS) -lfor"<<endl;
|
---|
530 | //os<<"endif"<<endl;
|
---|
531 | //os<<"ifeq ($(MACHEROS),Linux)"<<endl;
|
---|
532 | //os<<"LIBS := $(LIBS) -ldl -lf2c"<<endl;
|
---|
533 | //os<<"endif"<<endl;
|
---|
534 | //os<<"%.so:%.o"<<endl;
|
---|
535 | //os<<"%:%.cc"<<endl;
|
---|
536 | //os<<"%:%.o"<<endl;
|
---|
537 | //os<<"%.o:%.cc"<<endl;
|
---|
538 | //os<<"%.o:%.c"<<endl;
|
---|
539 | //os<<"%:%.c"<<endl;
|
---|
540 | //os<<endl;
|
---|
541 | //os<<".PRECIOUS: %.so"<<endl;
|
---|
542 | //os<<endl;
|
---|
543 | //os<<"%:%.so"<<endl;
|
---|
544 | //os<<"\t"<<"echo $@ \" made (.so) \""<<endl;
|
---|
545 | //os<<"%.so:%.o"<<endl;
|
---|
546 | //os<<"\t"<<"$(LINK.cc) -shared -o $@ $< $(LIBS) $(MYLIBS)"<<endl;
|
---|
547 | //os<<"%.o:%.cc"<<endl;
|
---|
548 | //os<<"\t"<<"$(COMPILE.cc) -o $@ $<"<<endl;
|
---|
549 | //os<<"%.o:%.c"<<endl;
|
---|
550 | //os<<"\t"<<"$(COMPILE.c) -c $(CFLAGS) $(USERFLAGS) -o $@ $<"<<endl;
|
---|
551 | //---------------------------------------------------------------------
|
---|
552 | //return 0;
|
---|
553 | //}
|
---|
554 |
|
---|
555 | /* --Methode-- */
|
---|
556 | int CxxExecutor::Link(string libname,string func)
|
---|
557 | {
|
---|
558 | if(libname.size()<1) libname = mDefTmp + mDefRoot + ".so";
|
---|
559 | else libname = mDefTmp + libname;
|
---|
560 | if(func.size()<1) func = mDefFunc;
|
---|
561 |
|
---|
562 | NamedObjMgr omg;
|
---|
563 | PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
|
---|
564 |
|
---|
565 | string key("linkff2");
|
---|
566 | vector<string> arg; arg.push_back(libname); arg.push_back(func);
|
---|
567 | string toks = libname + " " + func;
|
---|
568 | int rc = mpiac->ExecuteCommand(key,arg,toks);
|
---|
569 | if(mPrtLevel) cout<<"Link from "<<libname<<" for function "<<func
|
---|
570 | <<" (rc="<<rc<<")"<<endl;
|
---|
571 | return 0;
|
---|
572 | }
|
---|
573 |
|
---|
574 | /* --Methode-- */
|
---|
575 | int CxxExecutor::Call(string func)
|
---|
576 | {
|
---|
577 | if(func.size()<1) func = mDefFunc;
|
---|
578 |
|
---|
579 | NamedObjMgr omg;
|
---|
580 | PIACmd* mpiac = omg.GetImgApp()->CmdInterpreter();
|
---|
581 |
|
---|
582 | string key("call");
|
---|
583 | vector<string> arg; arg.push_back(func);
|
---|
584 | string toks = func;
|
---|
585 | if(mCallArgs.size()>0)
|
---|
586 | for(uint_4 i=0;i<mCallArgs.size();i++) arg.push_back(mCallArgs[i]);
|
---|
587 | mpiac->ExecuteCommand(key,arg,toks);
|
---|
588 | return 0;
|
---|
589 | }
|
---|
590 |
|
---|
591 | /* --Methode-- */
|
---|
592 | void CxxExecutor::FillArgs(vector<string>& args)
|
---|
593 | {
|
---|
594 | mCallArgs.resize(0);
|
---|
595 | if(args.size()<1) return;
|
---|
596 | for(uint_4 i=0;i<args.size();i++) mCallArgs.push_back(args[i]);
|
---|
597 | }
|
---|
598 |
|
---|
599 | void CxxExecutor::FillArgs(string& args)
|
---|
600 | {
|
---|
601 | mCallArgs.resize(0);
|
---|
602 | FillVStringFrString(args,mCallArgs,' ');
|
---|
603 | }
|
---|
604 |
|
---|
605 | string CxxExecutor::GetArgs(void)
|
---|
606 | {
|
---|
607 | string dum = "";
|
---|
608 | if(mCallArgs.size()<1) return dum;
|
---|
609 | for(uint_4 i=0;i<mCallArgs.size();i++) dum += mCallArgs[i] + " ";
|
---|
610 | return dum;
|
---|
611 | }
|
---|
612 |
|
---|
613 | /* --Methode-- */
|
---|
614 | void CxxExecutor::FillInclude(vector<string>& inc)
|
---|
615 | {
|
---|
616 | mIncList.resize(0);
|
---|
617 | if(inc.size()<1) return;
|
---|
618 | for(uint_4 i=0;i<inc.size();i++) mIncList.push_back(inc[i]);
|
---|
619 | }
|
---|
620 |
|
---|
621 | void CxxExecutor::FillInclude(string& inc)
|
---|
622 | {
|
---|
623 | mIncList.resize(0);
|
---|
624 | FillVStringFrString(inc,mIncList,' ');
|
---|
625 | }
|
---|
626 |
|
---|
627 | string CxxExecutor::GetInclude(void)
|
---|
628 | {
|
---|
629 | string dum = "";
|
---|
630 | if(mIncList.size()<1) return dum;
|
---|
631 | for(uint_4 i=0;i<mIncList.size();i++) dum += mIncList[i] + " ";
|
---|
632 | return dum;
|
---|
633 | }
|
---|
634 |
|
---|
635 | /* --Methode-- */
|
---|
636 | void CxxExecutor::FillCompileOpt(vector<string>& copt)
|
---|
637 | {
|
---|
638 | mCompOpt = "";
|
---|
639 | if(copt.size()<1) return;
|
---|
640 | for(uint_4 i=0;i<copt.size();i++) mCompOpt += copt[i] + " ";
|
---|
641 | }
|
---|
642 |
|
---|
643 | void CxxExecutor::FillCompileOpt(string& copt)
|
---|
644 | {
|
---|
645 | mCompOpt = copt;
|
---|
646 | }
|
---|
647 |
|
---|
648 | string CxxExecutor::GetCompileOpt(void)
|
---|
649 | {
|
---|
650 | return mCompOpt;
|
---|
651 | }
|
---|
652 |
|
---|
653 | /* --Methode-- */
|
---|
654 | void CxxExecutor::FillLinkOpt(vector<string>& lopt)
|
---|
655 | {
|
---|
656 | mLinkOpt = "";
|
---|
657 | if(lopt.size()<1) return;
|
---|
658 | for(uint_4 i=0;i<lopt.size();i++) mLinkOpt += lopt[i] + " ";
|
---|
659 | }
|
---|
660 |
|
---|
661 | void CxxExecutor::FillLinkOpt(string& lopt)
|
---|
662 | {
|
---|
663 | mLinkOpt = lopt;
|
---|
664 | }
|
---|
665 |
|
---|
666 | string CxxExecutor::GetLinkOpt(void)
|
---|
667 | {
|
---|
668 | return mLinkOpt;
|
---|
669 | }
|
---|
670 |
|
---|
671 | /* --Methode-- */
|
---|
672 | void CxxExecutor::FillLinkLibs(vector<string>& llibs)
|
---|
673 | {
|
---|
674 | mMyLibs = "";
|
---|
675 | if(llibs.size()<1) return;
|
---|
676 | for(uint_4 i=0;i<llibs.size();i++) mMyLibs += llibs[i] + " ";
|
---|
677 | }
|
---|
678 |
|
---|
679 | void CxxExecutor::FillLinkLibs(string& llibs)
|
---|
680 | {
|
---|
681 | mMyLibs = llibs;
|
---|
682 | }
|
---|
683 |
|
---|
684 | string CxxExecutor::GetLinkLibs(void)
|
---|
685 | {
|
---|
686 | return mMyLibs;
|
---|
687 | }
|
---|