1 | // -*- Mode: C++ -*- |
---|
2 | // |
---|
3 | // CForall Version 1.0, Copyright (C) Peter A. Buhr 2002 |
---|
4 | // |
---|
5 | // cpp.cc -- |
---|
6 | // |
---|
7 | // Author : Peter A. Buhr |
---|
8 | // Created On : Thu Aug 29 12:24:06 2002 |
---|
9 | // Last Modified By : Peter A. Buhr |
---|
10 | // Last Modified On : Sat Dec 6 08:31:49 2014 |
---|
11 | // Update Count : 51 |
---|
12 | // |
---|
13 | |
---|
14 | #include <iostream> |
---|
15 | #include <string> |
---|
16 | #include <stdio.h> // tempnam, freopen, perror |
---|
17 | #include <unistd.h> // execvp, fork, unlink |
---|
18 | #include <sys/wait.h> // wait |
---|
19 | |
---|
20 | #include "../config.h" // configure info |
---|
21 | |
---|
22 | using namespace std; |
---|
23 | |
---|
24 | |
---|
25 | //#define __DEBUG_H__ |
---|
26 | |
---|
27 | |
---|
28 | int main( int argc, char *argv[] ) { |
---|
29 | int code; |
---|
30 | int i; |
---|
31 | |
---|
32 | string arg; |
---|
33 | string bprefix; |
---|
34 | |
---|
35 | string cpp_in; |
---|
36 | string cpp_out; |
---|
37 | string cpp; |
---|
38 | string cpp_name( "gcc" ); |
---|
39 | |
---|
40 | bool CFA_flag = false; |
---|
41 | bool cpp_flag = false; |
---|
42 | bool gnu = false; |
---|
43 | |
---|
44 | const char *args[argc + 100]; // leave space for 100 additional cfa command line values |
---|
45 | int nargs = 1; // 0 => command name |
---|
46 | const char *argsCppOnly[argc]; // cpp only arguments |
---|
47 | int nargsCppOnly = 0; |
---|
48 | const char *argsCFAOnly[argc]; // CFA only arguments |
---|
49 | int nargsCFAOnly = 0; |
---|
50 | |
---|
51 | // get a name of a temporary file |
---|
52 | |
---|
53 | char *tmpfile = tempnam( NULL, "CFA" ); // storage is not freed |
---|
54 | |
---|
55 | #ifdef __DEBUG_H__ |
---|
56 | cerr << "CPP:" << endl; |
---|
57 | #endif // __DEBUG_H__ |
---|
58 | |
---|
59 | // process all the arguments |
---|
60 | |
---|
61 | for ( i = 1; i < argc; i += 1 ) { |
---|
62 | #ifdef __DEBUG_H__ |
---|
63 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
64 | #endif // __DEBUG_H__ |
---|
65 | arg = argv[i]; |
---|
66 | #ifdef __DEBUG_H__ |
---|
67 | cerr << "arg:\"" << arg << "\"" << endl; |
---|
68 | #endif // __DEBUG_H__ |
---|
69 | if ( arg.substr(0,1) == "-" ) { |
---|
70 | if ( arg == "-D__CFA__" ) { |
---|
71 | CFA_flag = true; // strip -D__CFA__ flag |
---|
72 | } else if ( arg == "-D__CPP__" ) { |
---|
73 | cpp_flag = true; // strip -D__CPP__ flag |
---|
74 | } else if ( arg.substr(0,sizeof("-D__GNU")-1) == "-D__GNU" ) { |
---|
75 | gnu = true; // strip -D__GNUxxx flags to remove duplication |
---|
76 | } else if ( arg == "-lang-c" ) { // strip -lang-c flag |
---|
77 | } else if ( arg.substr(0,sizeof("-A")-1) == "-A" ) { // strip -A flags |
---|
78 | } else if ( arg.substr(0,sizeof("-D__STDC_HOSTED__")-1) == "-D__STDC_HOSTED__" ) { // strip this define: causes conflict |
---|
79 | } else if ( arg.substr(0,sizeof("-o")-1) == "-o" ) { |
---|
80 | i += 1; // strip -o flag and its argument |
---|
81 | } else if ( arg.substr(0,sizeof("-D__CFA_FLAG__")-1) == "-D__CFA_FLAG__" ) { |
---|
82 | argsCFAOnly[nargsCFAOnly] = ( *new string( arg.substr(sizeof("-D__CFA_FLAG__")) ) ).c_str(); // pass argument along |
---|
83 | nargsCFAOnly += 1; |
---|
84 | } else if ( arg == "-v" ) { |
---|
85 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
86 | nargsCppOnly += 1; |
---|
87 | } else if ( arg.substr(0,sizeof("-I")-1) == "-I" ) { |
---|
88 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
89 | nargsCppOnly += 1; |
---|
90 | } else if ( arg == "-C" || arg == "-P" || arg == "-H" ) { |
---|
91 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
92 | nargsCppOnly += 1; |
---|
93 | } else if ( arg.substr(0,sizeof("-W")-1) == "-W" ) { |
---|
94 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
95 | nargsCppOnly += 1; |
---|
96 | } else if ( arg == "-lint" ) { |
---|
97 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
98 | nargsCppOnly += 1; |
---|
99 | } else if ( arg == "-pedantic" || arg == "-pedantic-errors" ) { |
---|
100 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
101 | nargsCppOnly += 1; |
---|
102 | } else if ( arg == "-traditional" || arg == "-trigraphs" ) { |
---|
103 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
104 | nargsCppOnly += 1; |
---|
105 | } else if ( arg == "-dM" || arg == "-dD" ) { |
---|
106 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
107 | nargsCppOnly += 1; |
---|
108 | } else if ( arg == "-M" || arg == "-MG" || arg == "-MM" ) { |
---|
109 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
110 | nargsCppOnly += 1; |
---|
111 | } else if ( arg == "-MD" || arg == "-MMD" ) { |
---|
112 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
113 | nargsCppOnly += 1; |
---|
114 | i += 1; |
---|
115 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
116 | nargsCppOnly += 1; |
---|
117 | #ifdef __DEBUG_H__ |
---|
118 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
119 | #endif // __DEBUG_H__ |
---|
120 | } else if ( arg == "-imacros" ) { |
---|
121 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
122 | nargsCppOnly += 1; |
---|
123 | i += 1; |
---|
124 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
125 | nargsCppOnly += 1; |
---|
126 | #ifdef __DEBUG_H__ |
---|
127 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
128 | #endif // __DEBUG_H__ |
---|
129 | } else if ( arg == "-include" ) { |
---|
130 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
131 | nargsCppOnly += 1; |
---|
132 | i += 1; |
---|
133 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
134 | nargsCppOnly += 1; |
---|
135 | #ifdef __DEBUG_H__ |
---|
136 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
137 | #endif // __DEBUG_H__ |
---|
138 | } else if ( arg == "-idirafter" ) { |
---|
139 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
140 | nargsCppOnly += 1; |
---|
141 | i += 1; |
---|
142 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
143 | nargsCppOnly += 1; |
---|
144 | #ifdef __DEBUG_H__ |
---|
145 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
146 | #endif // __DEBUG_H__ |
---|
147 | } else if ( arg == "-iprefix" ) { |
---|
148 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
149 | nargsCppOnly += 1; |
---|
150 | i += 1; |
---|
151 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
152 | nargsCppOnly += 1; |
---|
153 | #ifdef __DEBUG_H__ |
---|
154 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
155 | #endif // __DEBUG_H__ |
---|
156 | } else if ( arg == "-iwithprefix" ) { |
---|
157 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
158 | nargsCppOnly += 1; |
---|
159 | i += 1; |
---|
160 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
161 | nargsCppOnly += 1; |
---|
162 | #ifdef __DEBUG_H__ |
---|
163 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
164 | #endif // __DEBUG_H__ |
---|
165 | } else if ( arg == "-isystem" ) { |
---|
166 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
167 | nargsCppOnly += 1; |
---|
168 | i += 1; |
---|
169 | argsCppOnly[nargsCppOnly] = argv[i]; // pass argument along |
---|
170 | nargsCppOnly += 1; |
---|
171 | #ifdef __DEBUG_H__ |
---|
172 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
173 | #endif // __DEBUG_H__ |
---|
174 | } else if ( arg.substr(0,sizeof("-D__GCC_BPREFIX__")-1) == "-D__GCC_BPREFIX__" ) { |
---|
175 | bprefix = arg.substr(sizeof("-D__GCC_BPREFIX__")); |
---|
176 | } else if ( arg.substr(0,sizeof("-D__GCC_MACHINE__")-1) == "-D__GCC_MACHINE__" ) { |
---|
177 | argsCppOnly[nargsCppOnly] = "-b"; // pass argument along |
---|
178 | nargsCppOnly += 1; |
---|
179 | argsCppOnly[nargsCppOnly] = ( *new string(arg.substr(sizeof("-D__GCC_MACHINE__")) ) ).c_str(); // pass argument along |
---|
180 | nargsCppOnly += 1; |
---|
181 | } else if ( arg.substr(0,sizeof("-D__GCC_VERSION__")-1) == "-D__GCC_VERSION__" ) { |
---|
182 | argsCppOnly[nargsCppOnly] = "-V"; // pass argument along |
---|
183 | nargsCppOnly += 1; |
---|
184 | argsCppOnly[nargsCppOnly] = ( *new string( arg.substr(sizeof("-D__GCC_VERSION__")) ) ).c_str(); // pass argument along |
---|
185 | nargsCppOnly += 1; |
---|
186 | } else if ( arg.substr(0,sizeof("-D__CPP_NAME__")-1) == "-D__CPP_NAME__" ) { |
---|
187 | cpp_name = arg.substr(sizeof("-D__CPP_NAME__")); |
---|
188 | } else if ( arg.substr(1,1) != "+" ) { |
---|
189 | args[nargs] = argv[i]; // pass argument along |
---|
190 | nargs += 1; |
---|
191 | } // if |
---|
192 | } else { |
---|
193 | if ( cpp_in.length() == 0 ) { |
---|
194 | cpp_in = arg; |
---|
195 | #ifdef __DEBUG_H__ |
---|
196 | cerr << "cpp_in:\"" << cpp_in << "\"" << endl; |
---|
197 | #endif // __DEBUG_H__ |
---|
198 | } else if ( cpp_out.length() == 0 ) { |
---|
199 | cpp_out = arg; |
---|
200 | #ifdef __DEBUG_H__ |
---|
201 | cerr << "cpp_out:\"" << cpp_out << "\""<< endl; |
---|
202 | #endif // __DEBUG_H__ |
---|
203 | } else { |
---|
204 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; |
---|
205 | exit( 1 ); |
---|
206 | } // if |
---|
207 | } // if |
---|
208 | } // for |
---|
209 | |
---|
210 | argsCppOnly[nargsCppOnly] = "-D__cplusplus"; |
---|
211 | nargsCppOnly += 1; |
---|
212 | |
---|
213 | if ( cpp_in.length() == 0 ) { |
---|
214 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; |
---|
215 | exit( 1 ); |
---|
216 | } // if |
---|
217 | |
---|
218 | if ( cpp_flag && CFA_flag ) { |
---|
219 | cerr << argv[0] << " Error cannot use -E and -CFA flags together." << endl; |
---|
220 | exit( -1 ); |
---|
221 | } // if |
---|
222 | |
---|
223 | // The -E flag is specified so only run the preprocessor and output is |
---|
224 | // written to standard output. |
---|
225 | // |
---|
226 | // OR |
---|
227 | // |
---|
228 | // The preprocessor is called internally during compilation, probably by |
---|
229 | // "collect" during linking to compile some ctor/dtor code. In this case, |
---|
230 | // the cfa-cpp preprocessor is not run. Output is redirected to cpp_out |
---|
231 | |
---|
232 | if ( cpp_flag || bprefix.length() == 0 ) { |
---|
233 | if ( ! cpp_flag ) { |
---|
234 | if ( freopen( cpp_out.c_str(), "w", stdout ) == NULL ) { // redirect stdout if not -E |
---|
235 | cerr << argv[0] << ": Error can't write to " << cpp_out << endl; |
---|
236 | exit( -1 ); |
---|
237 | } // if |
---|
238 | |
---|
239 | // If called by collect, must prevent the compiler from recursively |
---|
240 | // calling this cpp through the -B path. To stop the recursion, |
---|
241 | // sent the COMPILER_PATH environment variable to the NULL string, |
---|
242 | // which removes the -B path supplied on the initial cfa command. |
---|
243 | |
---|
244 | if ( gnu ) { |
---|
245 | putenv( "COMPILER_PATH=" ); |
---|
246 | } // if |
---|
247 | } // if |
---|
248 | |
---|
249 | if ( bprefix.length() == 0 ) { // collect ? |
---|
250 | args[0] = "gcc"; // use gcc |
---|
251 | } else { |
---|
252 | args[0] = cpp_name.c_str(); |
---|
253 | } // if |
---|
254 | args[nargs] = "-E"; |
---|
255 | nargs += 1; |
---|
256 | |
---|
257 | for ( i = 0; i < nargsCppOnly; i += 1 ) { // copy cpp only arguments |
---|
258 | args[nargs] = argsCppOnly[i]; |
---|
259 | nargs += 1; |
---|
260 | } // if |
---|
261 | |
---|
262 | args[nargs] = cpp_in.c_str(); |
---|
263 | nargs += 1; |
---|
264 | args[nargs] = NULL; // terminate argument list |
---|
265 | |
---|
266 | #ifdef __DEBUG_H__ |
---|
267 | cerr << "nargs: " << nargs << endl; |
---|
268 | for ( i = 0; args[i] != NULL; i += 1 ) { |
---|
269 | cerr << args[i] << " "; |
---|
270 | } // for |
---|
271 | cerr << endl; |
---|
272 | #endif // __DEBUG_H__ |
---|
273 | |
---|
274 | execvp( args[0], (char *const *)args ); // should not return |
---|
275 | perror( "CFA translator error: cpp level, exec" ); |
---|
276 | exit( -1 ); |
---|
277 | } // if |
---|
278 | |
---|
279 | // Run the C preprocessor and save the output in tmpfile. |
---|
280 | |
---|
281 | if ( fork() == 0 ) { // child process ? |
---|
282 | if ( freopen( tmpfile, "w", stdout ) == NULL) { // redirect output to tmpfile |
---|
283 | cerr << argv[0] << ": Error can't write to " << tmpfile << endl; |
---|
284 | exit( -1 ); |
---|
285 | } // if |
---|
286 | |
---|
287 | args[0] = cpp_name.c_str(); |
---|
288 | args[nargs] = "-E"; |
---|
289 | nargs += 1; |
---|
290 | |
---|
291 | for ( i = 0; i < nargsCppOnly; i += 1 ) { // copy cpp only arguments |
---|
292 | args[nargs] = argsCppOnly[i]; |
---|
293 | nargs += 1; |
---|
294 | } // if |
---|
295 | |
---|
296 | args[nargs] = cpp_in.c_str(); |
---|
297 | nargs += 1; |
---|
298 | args[nargs] = NULL; // terminate argument list |
---|
299 | |
---|
300 | #ifdef __DEBUG_H__ |
---|
301 | cerr << "cpp nargs: " << nargs << endl; |
---|
302 | for ( i = 0; args[i] != NULL; i += 1 ) { |
---|
303 | cerr << args[i] << " "; |
---|
304 | } // for |
---|
305 | cerr << endl; |
---|
306 | #endif // __DEBUG_H__ |
---|
307 | |
---|
308 | execvp( args[0], (char *const *)args ); // should not return |
---|
309 | perror( "CFA translator error: cpp level, exec" ); |
---|
310 | exit( -1 ); |
---|
311 | } // if |
---|
312 | |
---|
313 | wait( &code ); // wait for child to finish |
---|
314 | |
---|
315 | if ( WIFSIGNALED(code) != 0 ) { // child completed successfully ? |
---|
316 | unlink( tmpfile ); |
---|
317 | cerr << "CFA translator error: cpp failed with signal " << WTERMSIG(code) << endl; |
---|
318 | exit( -1 ); |
---|
319 | } // if |
---|
320 | |
---|
321 | // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary |
---|
322 | // file, and output is written to standard output. Otherwise, run the |
---|
323 | // cfa-cpp preprocessor on the temporary file and save the result into the |
---|
324 | // output file. |
---|
325 | |
---|
326 | if ( fork() == 0 ) { // child process ? |
---|
327 | args[0] = ( *new string( bprefix + "/cfa-cpp" ) ).c_str(); |
---|
328 | |
---|
329 | for ( i = 0; i < nargsCFAOnly; i += 1 ) { // copy CFA only arguments |
---|
330 | args[nargs] = argsCFAOnly[i]; |
---|
331 | nargs += 1; |
---|
332 | } // if |
---|
333 | args[nargs] = "-p"; |
---|
334 | nargs += 1; |
---|
335 | |
---|
336 | args[nargs] = tmpfile; |
---|
337 | nargs += 1; |
---|
338 | |
---|
339 | if ( ! CFA_flag ) { // run cfa-cpp ? |
---|
340 | args[nargs] = cpp_out.c_str(); |
---|
341 | nargs += 1; |
---|
342 | } // if |
---|
343 | args[nargs] = NULL; // terminate argument list |
---|
344 | |
---|
345 | #ifdef __DEBUG_H__ |
---|
346 | cerr << "cfa-cpp nargs: " << nargs << endl; |
---|
347 | for ( i = 0; args[i] != NULL; i += 1 ) { |
---|
348 | cerr << args[i] << " "; |
---|
349 | } // for |
---|
350 | cerr << endl; |
---|
351 | #endif // __DEBUG_H__ |
---|
352 | |
---|
353 | execvp( args[0], (char *const *)args ); // should not return |
---|
354 | perror( "CFA translator error: cpp level, exec" ); |
---|
355 | } // if |
---|
356 | |
---|
357 | wait( &code ); // wait for child to finish |
---|
358 | |
---|
359 | unlink( tmpfile ); |
---|
360 | |
---|
361 | if ( WIFSIGNALED(code) != 0 ) { // child completed successfully ? |
---|
362 | cerr << "CFA translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl; |
---|
363 | exit( -1 ); |
---|
364 | } // if |
---|
365 | |
---|
366 | if ( CFA_flag ) { // -CFA flag ? |
---|
367 | exit( -1 ); // tell gcc not to go any further |
---|
368 | } else { |
---|
369 | exit( WEXITSTATUS(code) ); |
---|
370 | } // if |
---|
371 | } // main |
---|
372 | |
---|
373 | |
---|
374 | // Local Variables: // |
---|
375 | // compile-command: "gmake" // |
---|
376 | // End: // |
---|