1
|
1 /****************************************************************************
|
|
2 **
|
|
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
|
|
4 ** Copyright 2005 Roberto Raggi <roberto@kdevelop.org>
|
|
5 **
|
|
6 ** This file is part of Qt Jambi.
|
|
7 **
|
|
8 ** * Commercial Usage
|
|
9 * Licensees holding valid Qt Commercial licenses may use this file in
|
|
10 * accordance with the Qt Commercial License Agreement provided with the
|
|
11 * Software or, alternatively, in accordance with the terms contained in
|
|
12 * a written agreement between you and Nokia.
|
|
13 *
|
|
14 *
|
|
15 * GNU General Public License Usage
|
|
16 * Alternatively, this file may be used under the terms of the GNU
|
|
17 * General Public License versions 2.0 or 3.0 as published by the Free
|
|
18 * Software Foundation and appearing in the file LICENSE.GPL included in
|
|
19 * the packaging of this file. Please review the following information
|
|
20 * to ensure GNU General Public Licensing requirements will be met:
|
|
21 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
|
|
22 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
|
|
23 * exception, Nokia gives you certain additional rights. These rights
|
|
24 * are described in the Nokia Qt GPL Exception version 1.2, included in
|
|
25 * the file GPL_EXCEPTION.txt in this package.
|
|
26 *
|
|
27 * Qt for Windows(R) Licensees
|
|
28 * As a special exception, Nokia, as the sole copyright holder for Qt
|
|
29 * Designer, grants users of the Qt/Eclipse Integration plug-in the
|
|
30 * right for the Qt/Eclipse Integration to link to functionality
|
|
31 * provided by Qt Designer and its related libraries.
|
|
32 *
|
|
33 *
|
|
34 * If you are unsure which license is appropriate for your use, please
|
|
35 * contact the sales department at qt-sales@nokia.com.
|
|
36
|
|
37 **
|
|
38 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
|
39 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
|
40 **
|
|
41 ****************************************************************************/
|
|
42
|
|
43 #include <fstream>
|
|
44 #include "pp.h"
|
|
45
|
|
46 using namespace rpp;
|
|
47
|
|
48 #ifndef GCC_MACHINE
|
|
49 # define GCC_MACHINE "i386-redhat-linux"
|
|
50 #endif
|
|
51
|
|
52 #ifndef GCC_VERSION
|
|
53 # define GCC_VERSION "4.1.1"
|
|
54 #endif
|
|
55
|
|
56 void usage ()
|
|
57 {
|
|
58 std::cerr << "usage: rpp file.cpp" << std::endl;
|
|
59 ::exit (EXIT_FAILURE);
|
|
60 }
|
|
61
|
|
62 void dump_macros (pp_environment &env, pp &, std::ostream &__out)
|
|
63 {
|
|
64 for (pp_environment::const_iterator it = env.first_macro (); it != env.last_macro (); ++it)
|
|
65 {
|
|
66 pp_macro const *m = *it;
|
|
67
|
|
68 if (m->hidden)
|
|
69 continue;
|
|
70
|
|
71 std::string id (m->name->begin (), m->name->end ());
|
|
72 __out << "#define " << id;
|
|
73
|
|
74 if (m->function_like)
|
|
75 {
|
|
76 __out << "(";
|
|
77
|
|
78 for (std::size_t i = 0; i < m->formals.size (); ++i)
|
|
79 {
|
|
80 if (i != 0)
|
|
81 __out << ", ";
|
|
82
|
|
83 pp_fast_string const *f = m->formals [i];
|
|
84 std::string name (f->begin (), f->end ());
|
|
85 __out << name;
|
|
86 }
|
|
87
|
|
88 if (m->variadics)
|
|
89 __out << "...";
|
|
90
|
|
91 __out << ")";
|
|
92 }
|
|
93
|
|
94 __out << "\t";
|
|
95 if (m->definition)
|
|
96 {
|
|
97 std::string def (m->definition->begin (), m->definition->end ());
|
|
98 __out << def;
|
|
99 }
|
|
100
|
|
101 __out << std::endl;
|
|
102 }
|
|
103 }
|
|
104
|
|
105 int main (int, char *argv [])
|
|
106 {
|
|
107 char const *input_file = 0;
|
|
108 char const *output_file = 0;
|
|
109 char const *include_pch_file = 0;
|
|
110 bool opt_help = false;
|
|
111 bool opt_dump_macros = false;
|
|
112 bool opt_pch = false;
|
|
113
|
|
114 pp_environment env;
|
|
115 pp preprocess(env);
|
|
116
|
|
117 std::string result;
|
|
118 result.reserve (20 * 1024); // 20K
|
|
119
|
|
120 pp_output_iterator<std::string> out (result);
|
|
121 pp_null_output_iterator null_out;
|
|
122
|
|
123 preprocess.push_include_path ("/usr/include");
|
|
124 preprocess.push_include_path ("/usr/lib/gcc/" GCC_MACHINE "/" GCC_VERSION "/include");
|
|
125
|
|
126 preprocess.push_include_path ("/usr/include/c++/" GCC_VERSION);
|
|
127 preprocess.push_include_path ("/usr/include/c++/" GCC_VERSION "/" GCC_MACHINE);
|
|
128
|
|
129 std::string extra_args;
|
|
130
|
|
131 while (const char *arg = *++argv)
|
|
132 {
|
|
133 if (arg [0] != '-')
|
|
134 input_file = arg;
|
|
135
|
|
136 else if (! strcmp (arg, "-help"))
|
|
137 opt_help = true;
|
|
138
|
|
139 else if (! strcmp (arg, "-dM"))
|
|
140 opt_dump_macros = true;
|
|
141
|
|
142 else if (! strcmp (arg, "-pch"))
|
|
143 opt_pch = true;
|
|
144
|
|
145 else if (! strcmp (arg, "-msse"))
|
|
146 {
|
|
147 pp_macro __macro;
|
|
148 __macro.name = pp_symbol::get ("__SSE__", 7);
|
|
149 env.bind (__macro.name, __macro);
|
|
150
|
|
151 __macro.name = pp_symbol::get ("__MMX__", 7);
|
|
152 env.bind (__macro.name, __macro);
|
|
153 }
|
|
154
|
|
155 else if (! strcmp (arg, "-include"))
|
|
156 {
|
|
157 if (argv [1])
|
|
158 include_pch_file = *++argv;
|
|
159 }
|
|
160
|
|
161 else if (! strncmp (arg, "-o", 2))
|
|
162 {
|
|
163 arg += 2;
|
|
164
|
|
165 if (! arg [0] && argv [1])
|
|
166 arg = *++argv;
|
|
167
|
|
168 if (arg)
|
|
169 output_file = arg;
|
|
170 }
|
|
171
|
|
172 else if (! strncmp (arg, "-conf", 8))
|
|
173 {
|
|
174 if (argv [1])
|
|
175 preprocess.file (*++argv, null_out);
|
|
176 }
|
|
177
|
|
178 else if (! strncmp (arg, "-I", 2))
|
|
179 {
|
|
180 arg += 2;
|
|
181
|
|
182 if (! arg [0] && argv [1])
|
|
183 arg = *++argv;
|
|
184
|
|
185 if (arg)
|
|
186 preprocess.push_include_path (arg);
|
|
187 }
|
|
188
|
|
189 else if (! strncmp (arg, "-U", 2))
|
|
190 {
|
|
191 arg += 2;
|
|
192
|
|
193 if (! arg [0] && argv [1])
|
|
194 arg = *++argv;
|
|
195
|
|
196 if (arg)
|
|
197 {
|
|
198 env.unbind (arg, strlen (arg));
|
|
199 }
|
|
200 }
|
|
201
|
|
202 else if (! strncmp (arg, "-D", 2))
|
|
203 {
|
|
204 arg += 2;
|
|
205
|
|
206 if (! arg [0] && argv [1])
|
|
207 arg = *++argv;
|
|
208
|
|
209 if (arg)
|
|
210 {
|
|
211 pp_macro __macro;
|
|
212
|
|
213 char const *end = arg;
|
|
214 char const *eq = 0;
|
|
215
|
|
216 for (; *end; ++end)
|
|
217 {
|
|
218 if (*end == '=')
|
|
219 eq = end;
|
|
220 }
|
|
221
|
|
222 if (eq != 0)
|
|
223 {
|
|
224 __macro.name = pp_symbol::get (arg, eq - arg);
|
|
225 __macro.definition = pp_symbol::get (eq + 1, end - (eq + 1));
|
|
226 }
|
|
227
|
|
228 else
|
|
229 {
|
|
230 __macro.name = pp_symbol::get (arg, end - arg);
|
|
231 __macro.definition = 0;
|
|
232 }
|
|
233
|
|
234 env.bind (__macro.name, __macro);
|
|
235 }
|
|
236 }
|
|
237 else
|
|
238 {
|
|
239 extra_args += " ";
|
|
240 extra_args += arg;
|
|
241 }
|
|
242 }
|
|
243
|
|
244 if (! input_file || opt_help)
|
|
245 {
|
|
246 usage ();
|
|
247 return EXIT_FAILURE;
|
|
248 }
|
|
249
|
|
250 std::string __ifile (input_file);
|
|
251 bool is_c_file = false;
|
|
252 if (__ifile.size () > 2 && __ifile [__ifile.size () - 1] == 'c' && __ifile [__ifile.size () - 2] == '.')
|
|
253 {
|
|
254 is_c_file = true;
|
|
255 env.unbind ("__cplusplus", 11);
|
|
256
|
|
257 pp_macro __macro;
|
|
258 __macro.name = pp_symbol::get ("__null");
|
|
259 __macro.definition = pp_symbol::get ("((void*) 0)");
|
|
260 env.bind (__macro.name, __macro);
|
|
261
|
|
262 // turn off the pch
|
|
263 include_pch_file = 0;
|
|
264 }
|
|
265 else if (include_pch_file)
|
|
266 {
|
|
267 std::string __pch (include_pch_file);
|
|
268 __pch += ".gch/c++.conf";
|
|
269
|
|
270 //std::cerr << "*** pch file " << __pch << std::endl;
|
|
271 preprocess.file (__pch, null_out);
|
|
272 }
|
|
273
|
|
274 if (opt_dump_macros)
|
|
275 {
|
|
276 preprocess.file (input_file, null_out);
|
|
277 dump_macros (env, preprocess, std::cout);
|
|
278 return EXIT_SUCCESS;
|
|
279 }
|
|
280
|
|
281 preprocess.file (input_file, out);
|
|
282
|
|
283 if (opt_pch)
|
|
284 {
|
|
285 if (! output_file)
|
|
286 {
|
|
287 std::cerr << "*** WARNING expected a file name" << std::endl;
|
|
288 return EXIT_FAILURE;
|
|
289 }
|
|
290
|
|
291 std::string __conf_file (output_file);
|
|
292 __conf_file += ".conf";
|
|
293
|
|
294 std::ofstream __out;
|
|
295 __out.open (__conf_file.c_str ());
|
|
296 dump_macros (env, preprocess, __out);
|
|
297 __out.close ();
|
|
298
|
|
299 std::string __pp_file (output_file);
|
|
300 __pp_file += ".i";
|
|
301
|
|
302 __out.open (__pp_file.c_str ());
|
|
303 __out.write (result.c_str (), result.size ());
|
|
304 __out.close ();
|
|
305 return EXIT_SUCCESS;
|
|
306 }
|
|
307
|
|
308 std::ostream *__out = &std::cout;
|
|
309 std::ofstream __ofile;
|
|
310
|
|
311 if (output_file)
|
|
312 {
|
|
313 std::string __output_file_name (output_file);
|
|
314 __ofile.open (output_file);
|
|
315 __out = &__ofile;
|
|
316 }
|
|
317
|
|
318 if (include_pch_file)
|
|
319 {
|
|
320 std::string __pch (include_pch_file);
|
|
321 __pch += ".gch/c++.i";
|
|
322
|
|
323 std::ifstream __in (__pch.c_str ());
|
|
324
|
|
325 char buffer [1024];
|
|
326 while (__in.read (buffer, 1024))
|
|
327 __out->write (buffer, 1024);
|
|
328
|
|
329 __in.close ();
|
|
330 }
|
|
331
|
|
332 __out->write (result.c_str (), result.size ());
|
|
333
|
|
334 if (output_file)
|
|
335 __ofile.close ();
|
|
336
|
|
337 return EXIT_SUCCESS;
|
|
338 }
|
|
339
|
|
340 // kate: space-indent on; indent-width 2; replace-tabs on;
|
|
341
|