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

Last change on this file since 3637 was 3619, checked in by cmv, 16 years ago

add various #include<> for g++ 4.3 (jaunty 9.04), cmv 05/05/2009

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