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