source: Sophya/trunk/SophyaProg/PrgUtil/runcxx.cc@ 2411

Last change on this file since 2411 was 2411, checked in by ansari, 22 years ago

Correction bug gestion include pour -import / Reza 21/07/03

File size: 9.0 KB
Line 
1#include <stdio.h>
2#include <stdlib.h>
3#include <math.h>
4#include <iostream>
5#include <fstream>
6
7#include <string>
8#include <vector>
9
10#include <typeinfo>
11
12#include "sambainit.h"
13#include "skyinit.h"
14#include "tarrinit.h"
15
16#include "pexceptions.h"
17#include "cxxcmplnk.h"
18#include "pdlmgr.h"
19#include "timing.h"
20
21/*!
22 \ingroup PrgUtil
23 \file runcxx.cc
24 \brief \b runcxx: Compile and run simple C++ code using SOPHYA
25
26 \verbatim
27 csh> runcxx -h
28 PIOPersist::Initialize() Starting Sophya Persistence management service
29 SOPHYA Version 1.6 Revision 6 (V_Mai2003) -- Jun 25 2003 12:01:04 cxx
30 runcxx : compiling and running of a piece of C++ code
31 Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions]
32 [-tmpdir TmpDirectory] [-f C++CodeFileName]
33 [-inc includefile] [-inc includefile ...]
34 [-import modulename] [-import modulename ...]
35 [-uarg UserArg1 UserArg2 ...]
36 if no file name is specified, read from standard input
37 modulenames: SkyMap, Samba, SkyT, FitsIOServer,
38 LinAlg, IFFTW, XAstroPack
39 \endverbatim
40*/
41
42typedef void (* DlFunctionOfVecStr) (vector<string> & args, int & rc);
43
44static void Usage(bool fgerr);
45static int GetInput(string & codeflnm);
46static int FillCxxFile(string & codeflnm, string & cxxfile,
47 vector<string> & inc1, vector<string> & inc2);
48
49/* --Fonction-- */
50void Usage(bool fgerr)
51{
52 if (fgerr)
53 cout << " runcxx : Argument Error ! " << endl;
54 else
55 cout << " runcxx : compiling and running of a piece of C++ code " << endl;
56
57 cout << " Usage: runcxx [-compopt CompileOptions] [-linkopt LinkOptions] \n"
58 << " [-tmpdir TmpDirectory] [-f C++CodeFileName] \n"
59 << " [-inc includefile] [-inc includefile ...] \n"
60 << " [-import modulename] [-import modulename ...] \n"
61 << " [-uarg UserArg1 UserArg2 ...] \n"
62 << " if no file name is specified, read from standard input \n"
63 << " modulenames: SkyMap, Samba, SkyT, FitsIOServer, \n"
64 << " LinAlg, IFFTW, XAstroPack \n"
65 << endl;
66
67 exit(0);
68}
69
70
71
72/* --Main-- */
73int main(int narg, char *arg[])
74{
75 if ((narg > 1) && (strcmp(arg[1],"-h") == 0) ) Usage(false);
76
77 // Decoding options
78 string codefile;
79 string compoption;
80 string linkoption;
81 string tmpdir;
82
83 bool fgco = false;
84 bool fglo = false;
85 bool fgcofn = false;
86 bool fgtmp = false;
87
88 vector<string> uarg;
89 vector<string> inc1;
90 vector<string> inc2;
91
92 cout << " runcxx: Decoding command line options ... " << endl;
93 for (int k=1; k<narg; k++) {
94 if (strcmp(arg[k], "-compopt") == 0) {
95 if (k == narg-1) Usage(true);
96 fgco = true; k++;
97 compoption = arg[k];
98 }
99 else if (strcmp(arg[k], "-linkopt") == 0) {
100 if (k == narg-1) Usage(true);
101 fglo = true; k++;
102 linkoption = arg[k];
103 }
104 else if (strcmp(arg[k], "-tmpdir") == 0) {
105 if (k == narg-1) Usage(true);
106 fgtmp = true; k++;
107 tmpdir = arg[k];
108 }
109 else if (strcmp(arg[k], "-f") == 0) {
110 if (k == narg-1) Usage(true);
111 fgcofn = true; k++;
112 codefile = arg[k];
113 }
114 else if (strcmp(arg[k], "-inc") == 0) {
115 if (k == narg-1) Usage(true);
116 k++; inc2.push_back(arg[k]);
117 }
118 else if (strcmp(arg[k], "-import") == 0) {
119 if (k == narg-1) Usage(true);
120 k++;
121 if (strcmp(arg[k], "SkyMap") == 0) inc1.push_back("skymap.h");
122 else if (strcmp(arg[k], "Samba") == 0) inc1.push_back("samba.h");
123 else if (strcmp(arg[k], "SkyT") == 0) inc1.push_back("skyt.h");
124 else if (strcmp(arg[k], "FitsIOServer") == 0) inc1.push_back("fitsioserver.h");
125 else if (strcmp(arg[k], "LinAlg") == 0 ) inc1.push_back("intflapack.h");
126 else if (strcmp(arg[k], "IFFTW") == 0) inc1.push_back("fftwserver.h");
127 else if (strcmp(arg[k], "XAstroPack") == 0) inc1.push_back("xastropack.h");
128 }
129
130 else if (strcmp(arg[k], "-uarg") == 0) {
131 if (k == narg-1) Usage(true);
132 for(int kk=k+1; kk<narg; kk++)
133 uarg.push_back(arg[kk]);
134 break;
135 }
136 else Usage(true);
137 }
138
139 // Checking TMPDIR
140 if (!fgtmp) { // TMPDIR pas defini par option de ligne de commande
141 char* varenv;
142 if ( (varenv=getenv("TMPDIR")) != NULL ) {
143 tmpdir = varenv;
144 fgtmp = true;
145 }
146 }
147 if (tmpdir.length() > 0)
148 if (tmpdir[tmpdir.length()-1] != '/') tmpdir += '/';
149
150 int rc = 0;
151
152 try {
153 SophyaInit();
154 InitTim();
155
156 if (!fgcofn) { // Pas de fichier specifie - Lecture de fichier sur stdin
157 codefile = tmpdir + "aucode.icc";
158 rc = GetInput(codefile);
159 }
160 string flnm;
161 if (rc == 0) {
162 flnm = tmpdir + "acxxrun.cc";
163 rc = FillCxxFile(codefile, flnm, inc1, inc2);
164 }
165
166 PrtTim(" End of FillCxxFile ");
167
168 string oname, soname;
169 if (rc == 0) {
170 CxxCompilerLinker cxx;
171 if (fgtmp) cxx.SetTmpDir(tmpdir);
172 if (fgco) cxx.AddCompileOptions(compoption);
173 if (fglo) cxx.AddLinkOptions(linkoption);
174 cxx.SetVerbose(true);
175
176 rc = cxx.Compile(flnm, oname);
177 cout << " RC from cxx.Compile() = " << rc << " oname= " << oname << endl;
178 if (rc == 0) {
179 rc = cxx.BuildSO(oname, soname);
180 cout << " RC from cxx.BuildSO() = " << rc << " soname= " << soname << endl;
181 }
182 PrtTim(" End of Compile-Link ");
183 }
184 if (rc == 0) {
185 string funcname = "runcxx_usercode";
186 if (fgtmp) PDynLinkMgr::SetTmpDir(tmpdir);
187 PDynLinkMgr dyl(soname);
188 DlFunction f = dyl.GetFunction(funcname);
189 if (f != NULL) {
190 cout << "DlFunctionOfVecStr f linked OK - calling f(uarg) ... \n" << endl;
191 DlFunctionOfVecStr fvs = (DlFunctionOfVecStr)f;
192 int rcu = 0;
193 fvs(uarg, rcu);
194 cout << "\n RC from UserCode() = " << rcu << endl;
195 rc = rcu;
196 }
197 else {
198 cout << " ERROR linking DlFunction f !!! " << endl;
199 }
200 }
201 }
202
203 catch (PThrowable & exc) {
204 cerr << " Catched Exception " << (string)typeid(exc).name()
205 << " - Msg= " << exc.Msg() << endl;
206 }
207 catch (...) {
208 cerr << " some other exception was caught ! " << endl;
209 }
210
211 PrtTim(" End of runcxx ");
212 return(rc);
213}
214
215/* --Fonction-- */
216int GetInput(string & codeflnm)
217{
218 cout << " runcxx/GetInput() : Type in your C++ code, \n"
219 << " end with a blanck line OR <Cntl>D " << endl;
220 int line = 0;
221 ofstream os(codeflnm.c_str(),ios::out);
222 os << "\n" ;
223 bool fg = true;
224 char buff[512];
225 char * ret;
226 while (fg) {
227 printf("L%d ? ", line+1);
228 fflush(stdout);
229 buff[0] = '\0';
230 ret = fgets(buff, 512, stdin);
231 buff[511] = '\0';
232 if (ret && ( (buff[0] != '\0') && (buff[0] != '\n') && (buff[0] != '\r')) ) {
233 os << buff << endl; line++;
234 }
235 else fg = false;
236 }
237 os << "\n" ;
238 cout << " \n Total " << line << " lines copied to file " << codeflnm << endl;
239 if (line > 0) return(0);
240 else return(1);
241}
242
243/* --Fonction-- */
244int FillCxxFile(string & code, string & cxxfile,
245 vector<string>& inc1, vector<string>& inc2)
246{
247 ofstream os(cxxfile.c_str(),ios::out);
248
249 os<<"#include \"machdefs.h\" \n"<<endl;
250
251 os<<"//---- System et stdc++ include files \n"
252 <<"#include <stdio.h> \n"
253 <<"#include <stdlib.h> \n"
254 <<"#include <math.h> \n"
255 <<"#include <ctype.h> \n"
256 <<"#include <string.h> \n"
257 <<"#include <iostream> \n"
258 <<"#include <fstream> \n"
259 <<"#include <complex> \n"
260 <<endl
261
262 <<"#include <typeinfo> \n"
263 <<"#include <string> \n"
264 <<"#include <vector> \n"
265 <<"#include <map> \n"
266 <<"#include <functional> \n"
267 <<"#include <list> \n"
268 <<endl
269
270 <<"//---- Sophya include files \n"
271 <<"#include \"basetools.h\" \n"
272 <<"#include \"systools.h\" \n"
273 <<"#include \"sutils.h\" \n"
274 <<"#include \"ntools.h\" \n"
275 <<"#include \"array.h\" \n"
276 <<"#include \"histats.h\" \n"
277 <<endl;
278
279 int ki;
280 for(ki=0; ki<inc1.size(); ki++)
281 os << "#include \"" << inc1[ki] << "\" \n" ;
282 os << endl;
283 for(ki=0; ki<inc2.size(); ki++)
284 os << "#include \"" << inc2[ki] << "\" \n" ;
285 os << endl;
286
287 os<<"//---- function for Adding and displaying Objects " << endl;
288 os<<"//---- compatibility with piapp cxxexecutor " << endl;
289 os<<"void Keep_Object(AnyDataObj & obj, string const & nom) \n{ \n"
290 <<" POutPersist pos(nom); \n"
291 <<" pos.PutObject(obj); \n"
292 <<"} \n" << endl;
293
294 os<<"//---- Objects and variables saving"<<endl
295 <<"#define KeepObj(obj) Keep_Object(obj, #obj);"<<endl
296 <<endl;
297
298 os<<"//---- Displaying objects and command execution"<<endl;
299 os<<"#define DisplayObj(obj, att) "
300 <<" cout << \"DisplayObj() available only in piapp \" << endl; " << endl;
301 os<<"#define ExecuteCommand(cmd) "
302 <<" cout << \"ExecuteCommand() available only in piapp \" << endl; " << endl;
303
304
305 os << "extern \"C\" { \n"
306 << " void runcxx_usercode(vector<string>& arg, int & rc); \n"
307 // << " void runcxx_usercode(); \n"
308 << " } \n " << endl;
309
310 os << "// ---- User Code function ----- \n"
311 << " void runcxx_usercode(vector<string>& arg, int & rc) \n"
312 // << " void runcxx_usercode() \n"
313 << "{ \n"
314 << "#include \"" << code << "\" \n"
315 << "} \n" << endl;
316
317 return(0);
318}
319
Note: See TracBrowser for help on using the repository browser.