1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // cc1.cc -- |
---|
8 | // |
---|
9 | // Author : Peter A. Buhr |
---|
10 | // Created On : Fri Aug 26 14:23:51 2005 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Mon Aug 26 07:52:19 2019 |
---|
13 | // Update Count : 374 |
---|
14 | // |
---|
15 | |
---|
16 | #include <iostream> |
---|
17 | using std::cerr; |
---|
18 | using std::endl; |
---|
19 | #include <string> |
---|
20 | using std::string; |
---|
21 | #include <algorithm> // find |
---|
22 | #include <cstdio> // stderr, stdout, perror, fprintf |
---|
23 | #include <cstdlib> // getenv, exit, mkstemp |
---|
24 | #include <unistd.h> // execvp, fork, unlink |
---|
25 | #include <sys/wait.h> // wait |
---|
26 | #include <fcntl.h> |
---|
27 | |
---|
28 | |
---|
29 | #include "config.h" // configure info |
---|
30 | |
---|
31 | |
---|
32 | //#define __DEBUG_H__ |
---|
33 | |
---|
34 | |
---|
35 | static string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed |
---|
36 | static string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler |
---|
37 | static bool CFA_flag = false; // -CFA flag |
---|
38 | static bool save_temps = false; // -save-temps flag |
---|
39 | static string o_file; |
---|
40 | |
---|
41 | |
---|
42 | static bool prefix( const string & arg, const string & pre ) { |
---|
43 | return arg.substr( 0, pre.size() ) == pre; |
---|
44 | } // prefix |
---|
45 | |
---|
46 | static void suffix( const string & arg, const char * args[], int & nargs ) { |
---|
47 | enum { NumSuffixes = 3 }; |
---|
48 | static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" }; |
---|
49 | |
---|
50 | size_t dot = arg.find_last_of( "." ); |
---|
51 | if ( dot == string::npos ) return; |
---|
52 | const string * end = suffixes + NumSuffixes; |
---|
53 | if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) { |
---|
54 | args[nargs++] = "-x"; |
---|
55 | args[nargs++] = "c"; |
---|
56 | } // if |
---|
57 | } // suffix |
---|
58 | |
---|
59 | |
---|
60 | static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); |
---|
61 | |
---|
62 | static void checkEnv1( const char * args[], int & nargs ) { // stage 1 |
---|
63 | extern char ** environ; |
---|
64 | |
---|
65 | for ( int i = 0; environ[i]; i += 1 ) { |
---|
66 | string arg( environ[i] ); |
---|
67 | #ifdef __DEBUG_H__ |
---|
68 | cerr << "env arg:\"" << arg << "\"" << endl; |
---|
69 | #endif // __DEBUG_H__ |
---|
70 | |
---|
71 | if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) { |
---|
72 | string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ); |
---|
73 | if ( prefix( val, "-compiler=" ) ) { |
---|
74 | compiler_path = val.substr( 10 ); |
---|
75 | } // if |
---|
76 | } // if |
---|
77 | } // for |
---|
78 | } // checkEnv1 |
---|
79 | |
---|
80 | |
---|
81 | static void checkEnv2( const char * args[], int & nargs ) { // stage 2 |
---|
82 | extern char ** environ; |
---|
83 | |
---|
84 | for ( int i = 0; environ[i]; i += 1 ) { |
---|
85 | string arg( environ[i] ); |
---|
86 | #ifdef __DEBUG_H__ |
---|
87 | cerr << "env arg:\"" << arg << "\"" << endl; |
---|
88 | #endif // __DEBUG_H__ |
---|
89 | |
---|
90 | if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) { |
---|
91 | string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ); |
---|
92 | if ( prefix( val, "-compiler=" ) ) { |
---|
93 | compiler_path = val.substr( 10 ); |
---|
94 | } else if ( val == "-CFA" ) { |
---|
95 | CFA_flag = true; |
---|
96 | } else if ( val == "-save-temps" ) { |
---|
97 | save_temps = true; |
---|
98 | } else if ( prefix( val, "-o=" ) ) { // output file for -CFA |
---|
99 | o_file = val.substr( 3 ); |
---|
100 | } else { |
---|
101 | args[nargs++] = ( *new string( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ) ).c_str(); |
---|
102 | } // if |
---|
103 | } // if |
---|
104 | } // for |
---|
105 | } // checkEnv2 |
---|
106 | |
---|
107 | |
---|
108 | static char tmpname[] = P_tmpdir "/CFAXXXXXX.i"; |
---|
109 | static int tmpfilefd = -1; |
---|
110 | static bool startrm = false; |
---|
111 | |
---|
112 | static void rmtmpfile() { |
---|
113 | if ( tmpfilefd == -1 ) return; // RACE, file created ? |
---|
114 | |
---|
115 | startrm = true; // RACE with C-c C-c |
---|
116 | if ( unlink( tmpname ) == -1 ) { // remove tmpname |
---|
117 | perror ( "CC1 Translator error: failed, unlink" ); |
---|
118 | exit( EXIT_FAILURE ); |
---|
119 | } // if |
---|
120 | tmpfilefd = -1; // mark removed |
---|
121 | } // rmtmpfile |
---|
122 | |
---|
123 | |
---|
124 | static void sigTermHandler( int ) { // C-c C-c |
---|
125 | if ( startrm ) return; // return and let rmtmpfile finish, and then program finishes |
---|
126 | |
---|
127 | if ( tmpfilefd != -1 ) { // RACE, file created ? |
---|
128 | rmtmpfile(); // remove tmpname |
---|
129 | } // if |
---|
130 | exit( EXIT_FAILURE ); // terminate |
---|
131 | } // sigTermHandler |
---|
132 | |
---|
133 | |
---|
134 | static void Stage1( const int argc, const char * const argv[] ) { |
---|
135 | int code; |
---|
136 | string arg; |
---|
137 | |
---|
138 | const char * cpp_in = nullptr; |
---|
139 | const char * cpp_out = nullptr; |
---|
140 | |
---|
141 | bool cpp_flag = false; |
---|
142 | bool o_flag = false; |
---|
143 | |
---|
144 | const char * args[argc + 100]; // leave space for 100 additional cpp command line values |
---|
145 | int nargs = 1; // number of arguments in args list; 0 => command name |
---|
146 | |
---|
147 | #ifdef __DEBUG_H__ |
---|
148 | cerr << "Stage1" << endl; |
---|
149 | #endif // __DEBUG_H__ |
---|
150 | checkEnv1( args, nargs ); // arguments passed via environment variables |
---|
151 | #ifdef __DEBUG_H__ |
---|
152 | for ( int i = 1; i < argc; i += 1 ) { |
---|
153 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
154 | } // for |
---|
155 | #endif // __DEBUG_H__ |
---|
156 | |
---|
157 | // process all the arguments |
---|
158 | |
---|
159 | for ( int i = 1; i < argc; i += 1 ) { |
---|
160 | arg = argv[i]; |
---|
161 | if ( prefix( arg, "-" ) ) { |
---|
162 | // strip g++ flags that are inappropriate or cause duplicates in subsequent passes |
---|
163 | |
---|
164 | if ( arg == "-quiet" ) { |
---|
165 | } else if ( arg == "-imultilib" || arg == "-imultiarch" ) { |
---|
166 | i += 1; // and the argument |
---|
167 | } else if ( prefix( arg, "-A" ) ) { |
---|
168 | } else if ( prefix( arg, "-D__GNU" ) ) { |
---|
169 | //******** |
---|
170 | // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT! |
---|
171 | //******** |
---|
172 | } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) { |
---|
173 | i += 1; // and the argument |
---|
174 | |
---|
175 | // strip flags controlling cpp step |
---|
176 | |
---|
177 | } else if ( arg == "-D__CPP__" ) { |
---|
178 | cpp_flag = true; |
---|
179 | } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) { |
---|
180 | i += 1; // and the argument |
---|
181 | cpp_flag = true; |
---|
182 | |
---|
183 | // all other flags |
---|
184 | |
---|
185 | } else if ( arg == "-o" ) { |
---|
186 | i += 1; |
---|
187 | o_flag = true; |
---|
188 | cpp_out = argv[i]; |
---|
189 | } else { |
---|
190 | args[nargs++] = argv[i]; // pass the flag along |
---|
191 | // CPP flags with an argument |
---|
192 | if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" || |
---|
193 | arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" || |
---|
194 | arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) { |
---|
195 | i += 1; |
---|
196 | args[nargs++] = argv[i]; // pass the argument along |
---|
197 | #ifdef __DEBUG_H__ |
---|
198 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
199 | #endif // __DEBUG_H__ |
---|
200 | } else if ( arg == "-MD" || arg == "-MMD" ) { |
---|
201 | args[nargs++] = "-MF"; // insert before file |
---|
202 | i += 1; |
---|
203 | args[nargs++] = argv[i]; // pass the argument along |
---|
204 | #ifdef __DEBUG_H__ |
---|
205 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
206 | #endif // __DEBUG_H__ |
---|
207 | } // if |
---|
208 | } // if |
---|
209 | } else { // obtain input and possibly output files |
---|
210 | if ( cpp_in == nullptr ) { |
---|
211 | cpp_in = argv[i]; |
---|
212 | #ifdef __DEBUG_H__ |
---|
213 | cerr << "cpp_in:\"" << cpp_in << "\"" << endl; |
---|
214 | #endif // __DEBUG_H__ |
---|
215 | } else if ( cpp_out == nullptr ) { |
---|
216 | cpp_out = argv[i]; |
---|
217 | #ifdef __DEBUG_H__ |
---|
218 | cerr << "cpp_out:\"" << cpp_out << "\""<< endl; |
---|
219 | #endif // __DEBUG_H__ |
---|
220 | } else { |
---|
221 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; |
---|
222 | exit( EXIT_FAILURE ); |
---|
223 | } // if |
---|
224 | } // if |
---|
225 | } // for |
---|
226 | |
---|
227 | #ifdef __DEBUG_H__ |
---|
228 | cerr << "args:"; |
---|
229 | for ( int i = 1; i < nargs; i += 1 ) { |
---|
230 | cerr << " " << args[i]; |
---|
231 | } // for |
---|
232 | if ( cpp_in != nullptr ) cerr << " " << cpp_in; |
---|
233 | if ( cpp_out != nullptr ) cerr << " " << cpp_out; |
---|
234 | cerr << endl; |
---|
235 | #endif // __DEBUG_H__ |
---|
236 | |
---|
237 | if ( cpp_in == nullptr ) { |
---|
238 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; |
---|
239 | exit( EXIT_FAILURE ); |
---|
240 | } // if |
---|
241 | |
---|
242 | if ( cpp_flag ) { |
---|
243 | // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard |
---|
244 | // output or -o. The call to cfa has a -E so it does not have to be added to the argument list. |
---|
245 | |
---|
246 | args[0] = compiler_path.c_str(); |
---|
247 | suffix( cpp_in, args, nargs ); // check suffix |
---|
248 | args[nargs++] = cpp_in; |
---|
249 | if ( o_flag ) { // location for output |
---|
250 | args[nargs++] = "-o"; |
---|
251 | } // if |
---|
252 | args[nargs++] = cpp_out; |
---|
253 | args[nargs] = nullptr; // terminate argument list |
---|
254 | |
---|
255 | #ifdef __DEBUG_H__ |
---|
256 | cerr << "nargs: " << nargs << endl; |
---|
257 | for ( int i = 0; args[i] != nullptr; i += 1 ) { |
---|
258 | cerr << args[i] << " "; |
---|
259 | } // for |
---|
260 | cerr << endl; |
---|
261 | #endif // __DEBUG_H__ |
---|
262 | |
---|
263 | execvp( args[0], (char * const *)args ); // should not return |
---|
264 | perror( "CC1 Translator error: stage 1, execvp" ); |
---|
265 | exit( EXIT_FAILURE ); |
---|
266 | } // if |
---|
267 | |
---|
268 | // Run the C preprocessor and save the output in the given file. |
---|
269 | |
---|
270 | if ( fork() == 0 ) { // child process ? |
---|
271 | // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects |
---|
272 | // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error, |
---|
273 | // when cpp writes to stdout. Hence, stdout is redirected into the temporary file. |
---|
274 | if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file |
---|
275 | perror( "CC1 Translator error: stage 1, freopen" ); |
---|
276 | exit( EXIT_FAILURE ); |
---|
277 | } // if |
---|
278 | |
---|
279 | args[0] = compiler_path.c_str(); |
---|
280 | suffix( cpp_in, args, nargs ); // check suffix |
---|
281 | args[nargs++] = cpp_in; // input to cpp |
---|
282 | args[nargs] = nullptr; // terminate argument list |
---|
283 | |
---|
284 | #ifdef __DEBUG_H__ |
---|
285 | cerr << "cpp nargs: " << nargs << endl; |
---|
286 | for ( int i = 0; args[i] != nullptr; i += 1 ) { |
---|
287 | cerr << args[i] << " "; |
---|
288 | } // for |
---|
289 | cerr << endl; |
---|
290 | #endif // __DEBUG_H__ |
---|
291 | |
---|
292 | execvp( args[0], (char * const *)args ); // should not return |
---|
293 | perror( "CC1 Translator error: stage 1 cpp, execvp" ); |
---|
294 | cerr << " invoked " << args[0] << endl; |
---|
295 | exit( EXIT_FAILURE ); |
---|
296 | } // if |
---|
297 | |
---|
298 | wait( &code ); // wait for child to finish |
---|
299 | |
---|
300 | #ifdef __DEBUG_H__ |
---|
301 | cerr << "return code from cpp:" << WEXITSTATUS(code) << endl; |
---|
302 | #endif // __DEBUG_H__ |
---|
303 | |
---|
304 | if ( WIFSIGNALED(code) ) { // child failed ? |
---|
305 | cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl; |
---|
306 | exit( EXIT_FAILURE ); |
---|
307 | } // if |
---|
308 | |
---|
309 | exit( WEXITSTATUS(code) ); // bad cpp result stops top-level gcc |
---|
310 | } // Stage1 |
---|
311 | |
---|
312 | |
---|
313 | static void Stage2( const int argc, const char * const * argv ) { |
---|
314 | int code; |
---|
315 | string arg; |
---|
316 | |
---|
317 | const char * cpp_in = nullptr; |
---|
318 | const char * cpp_out = nullptr; |
---|
319 | |
---|
320 | const char * args[argc + 100]; // leave space for 100 additional cfa command line values |
---|
321 | int nargs = 1; // number of arguments in args list; 0 => command name |
---|
322 | const char * cargs[20]; // leave space for 20 additional cfa-cpp command line values |
---|
323 | int ncargs = 1; // 0 => command name |
---|
324 | |
---|
325 | #ifdef __DEBUG_H__ |
---|
326 | cerr << "Stage2" << endl; |
---|
327 | #endif // __DEBUG_H__ |
---|
328 | checkEnv2( cargs, ncargs ); // arguments passed via environment variables |
---|
329 | #ifdef __DEBUG_H__ |
---|
330 | for ( int i = 1; i < argc; i += 1 ) { |
---|
331 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
332 | } // for |
---|
333 | #endif // __DEBUG_H__ |
---|
334 | |
---|
335 | // process all the arguments |
---|
336 | |
---|
337 | for ( int i = 1; i < argc; i += 1 ) { |
---|
338 | arg = argv[i]; |
---|
339 | if ( prefix( arg, "-" ) ) { |
---|
340 | // strip inappropriate flags |
---|
341 | |
---|
342 | if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" || |
---|
343 | // Currently CFA does not suppose precompiled .h files. |
---|
344 | prefix( arg, "--output-pch" ) ) { |
---|
345 | |
---|
346 | // strip inappropriate flags with an argument |
---|
347 | |
---|
348 | } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) { |
---|
349 | i += 1; |
---|
350 | #ifdef __DEBUG_H__ |
---|
351 | cerr << "arg:\"" << argv[i] << "\"" << endl; |
---|
352 | #endif // __DEBUG_H__ |
---|
353 | |
---|
354 | // all other flags |
---|
355 | |
---|
356 | } else { |
---|
357 | args[nargs++] = argv[i]; // pass the flag along |
---|
358 | if ( arg == "-o" ) { |
---|
359 | i += 1; |
---|
360 | cpp_out = argv[i]; |
---|
361 | args[nargs++] = argv[i]; // pass the argument along |
---|
362 | #ifdef __DEBUG_H__ |
---|
363 | cerr << "arg:\"" << argv[i] << "\"" << endl; |
---|
364 | #endif // __DEBUG_H__ |
---|
365 | } // if |
---|
366 | } // if |
---|
367 | } else { // obtain input and possibly output files |
---|
368 | if ( cpp_in == nullptr ) { |
---|
369 | cpp_in = argv[i]; |
---|
370 | #ifdef __DEBUG_H__ |
---|
371 | cerr << "cpp_in:\"" << cpp_in << "\"" << endl; |
---|
372 | #endif // __DEBUG_H__ |
---|
373 | } else if ( cpp_out == nullptr ) { |
---|
374 | cpp_out = argv[i]; |
---|
375 | #ifdef __DEBUG_H__ |
---|
376 | cerr << "cpp_out:\"" << cpp_out << "\""<< endl; |
---|
377 | #endif // __DEBUG_H__ |
---|
378 | } else { |
---|
379 | cerr << "Usage: " << argv[0] << " more than two files specified" << endl; |
---|
380 | exit( EXIT_FAILURE ); |
---|
381 | } // if |
---|
382 | } // if |
---|
383 | } // for |
---|
384 | |
---|
385 | if ( cpp_in == nullptr ) { |
---|
386 | cerr << "Usage: " << argv[0] << " missing input file" << endl; |
---|
387 | exit( EXIT_FAILURE ); |
---|
388 | } // if |
---|
389 | if ( cpp_out == nullptr ) { |
---|
390 | cerr << "Usage: " << argv[0] << " missing output file" << endl; |
---|
391 | exit( EXIT_FAILURE ); |
---|
392 | } // if |
---|
393 | |
---|
394 | // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked |
---|
395 | // process because variables tmpname and tmpfilefd are cloned. |
---|
396 | |
---|
397 | string cfa_cpp_out; |
---|
398 | |
---|
399 | if ( ! CFA_flag ) { // run compiler ? |
---|
400 | if ( save_temps ) { |
---|
401 | cfa_cpp_out = cpp_in; |
---|
402 | size_t dot = cfa_cpp_out.find_last_of( "." ); |
---|
403 | if ( dot == string::npos ) { |
---|
404 | cerr << "CC1 Translator error: stage 2, bad file name " << endl; |
---|
405 | exit( EXIT_FAILURE ); |
---|
406 | } // if |
---|
407 | |
---|
408 | cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + ".ifa"; |
---|
409 | if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) { |
---|
410 | perror( "CC1 Translator error: stage 2, creat" ); |
---|
411 | exit( EXIT_FAILURE ); |
---|
412 | } // if |
---|
413 | } else { |
---|
414 | tmpfilefd = mkstemps( tmpname, 2 ); |
---|
415 | if ( tmpfilefd == -1 ) { |
---|
416 | perror( "CC1 Translator error: stage 2, mkstemp" ); |
---|
417 | exit( EXIT_FAILURE ); |
---|
418 | } // if |
---|
419 | cfa_cpp_out = tmpname; |
---|
420 | } // if |
---|
421 | #ifdef __DEBUG_H__ |
---|
422 | cerr << "cfa_cpp_out: " << cfa_cpp_out << endl; |
---|
423 | #endif // __DEBUG_H__ |
---|
424 | } // if |
---|
425 | |
---|
426 | // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard |
---|
427 | // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file. |
---|
428 | |
---|
429 | if ( fork() == 0 ) { // child runs CFA |
---|
430 | cargs[0] = ( *new string( installlibdir + "cfa-cpp" ) ).c_str(); |
---|
431 | cargs[ncargs++] = cpp_in; |
---|
432 | |
---|
433 | if ( CFA_flag ) { // run cfa-cpp ? |
---|
434 | if ( o_file.size() != 0 ) { // location for output |
---|
435 | cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str(); |
---|
436 | } // if |
---|
437 | } else { |
---|
438 | cargs[ncargs++] = cfa_cpp_out.c_str(); |
---|
439 | } // if |
---|
440 | cargs[ncargs] = nullptr; // terminate argument list |
---|
441 | |
---|
442 | #ifdef __DEBUG_H__ |
---|
443 | for ( int i = 0; cargs[i] != nullptr; i += 1 ) { |
---|
444 | cerr << cargs[i] << " "; |
---|
445 | } // for |
---|
446 | cerr << endl; |
---|
447 | #endif // __DEBUG_H__ |
---|
448 | |
---|
449 | execvp( cargs[0], (char * const *)cargs ); // should not return |
---|
450 | perror( "CC1 Translator error: stage 2 cfa-cpp, execvp" ); |
---|
451 | cerr << " invoked " << cargs[0] << endl; |
---|
452 | exit( EXIT_FAILURE ); |
---|
453 | } // if |
---|
454 | |
---|
455 | wait( &code ); // wait for child to finish |
---|
456 | |
---|
457 | if ( WIFSIGNALED(code) ) { // child failed ? |
---|
458 | rmtmpfile(); // remove tmpname |
---|
459 | cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl; |
---|
460 | exit( EXIT_FAILURE ); |
---|
461 | } // if |
---|
462 | |
---|
463 | if ( CFA_flag ) { // no tmpfile created |
---|
464 | exit( WEXITSTATUS( code ) ); // stop regardless of success or failure |
---|
465 | } // if |
---|
466 | |
---|
467 | #ifdef __DEBUG_H__ |
---|
468 | cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl; |
---|
469 | #endif // __DEBUG_H__ |
---|
470 | |
---|
471 | if ( WEXITSTATUS(code) ) { // child error ? |
---|
472 | rmtmpfile(); // remove tmpname |
---|
473 | exit( WEXITSTATUS( code ) ); // do not continue |
---|
474 | } // if |
---|
475 | |
---|
476 | #ifdef __DEBUG_H__ |
---|
477 | cerr << "args:"; |
---|
478 | for ( int i = 1; i < nargs; i += 1 ) { |
---|
479 | cerr << " " << args[i]; |
---|
480 | } // for |
---|
481 | cerr << " " << cpp_in << endl; |
---|
482 | #endif // __DEBUG_H__ |
---|
483 | |
---|
484 | if ( fork() == 0 ) { // child runs CFA |
---|
485 | args[0] = compiler_path.c_str(); |
---|
486 | args[nargs++] = "-S"; // only compile and put assembler output in specified file |
---|
487 | if ( save_temps ) { // make gcc accept .ifa suffix |
---|
488 | args[nargs++] = "-x"; |
---|
489 | args[nargs++] = "cpp-output"; |
---|
490 | } // if |
---|
491 | args[nargs++] = cfa_cpp_out.c_str(); |
---|
492 | args[nargs] = nullptr; // terminate argument list |
---|
493 | |
---|
494 | #ifdef __DEBUG_H__ |
---|
495 | cerr << "stage2 nargs: " << nargs << endl; |
---|
496 | for ( int i = 0; args[i] != nullptr; i += 1 ) { |
---|
497 | cerr << args[i] << " "; |
---|
498 | } // for |
---|
499 | cerr << endl; |
---|
500 | #endif // __DEBUG_H__ |
---|
501 | |
---|
502 | execvp( args[0], (char * const *)args ); // should not return |
---|
503 | perror( "CC1 Translator error: stage 2 cc1, execvp" ); |
---|
504 | cerr << " invoked " << cargs[0] << endl; |
---|
505 | exit( EXIT_FAILURE ); // tell gcc not to go any further |
---|
506 | } // if |
---|
507 | |
---|
508 | wait( &code ); // wait for child to finish |
---|
509 | |
---|
510 | if ( WIFSIGNALED(code) ) { // child failed ? |
---|
511 | rmtmpfile(); // remove tmpname |
---|
512 | cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl; |
---|
513 | exit( EXIT_FAILURE ); |
---|
514 | } // if |
---|
515 | |
---|
516 | #ifdef __DEBUG_H__ |
---|
517 | cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl; |
---|
518 | #endif // __DEBUG_H__ |
---|
519 | |
---|
520 | rmtmpfile(); // remove tmpname |
---|
521 | exit( WEXITSTATUS( code ) ); // stop regardless of success or failure |
---|
522 | } // Stage2 |
---|
523 | |
---|
524 | |
---|
525 | // This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first |
---|
526 | // command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call |
---|
527 | // is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the |
---|
528 | // doubly preprocessed program. |
---|
529 | |
---|
530 | int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) { |
---|
531 | #ifdef __DEBUG_H__ |
---|
532 | for ( int i = 0; env[i] != nullptr; i += 1 ) { |
---|
533 | cerr << env[i] << endl; |
---|
534 | } // for |
---|
535 | #endif // __DEBUG_H__ |
---|
536 | |
---|
537 | signal( SIGINT, sigTermHandler ); |
---|
538 | signal( SIGTERM, sigTermHandler ); |
---|
539 | |
---|
540 | string arg( argv[1] ); |
---|
541 | |
---|
542 | // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed. |
---|
543 | |
---|
544 | if ( arg == "-E" ) { |
---|
545 | Stage1( argc, argv ); |
---|
546 | } else if ( arg == "-fpreprocessed" ) { |
---|
547 | Stage2( argc, argv ); |
---|
548 | } else { |
---|
549 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; |
---|
550 | exit( EXIT_FAILURE ); |
---|
551 | } // if |
---|
552 | } // main |
---|
553 | |
---|
554 | // Local Variables: // |
---|
555 | // tab-width: 4 // |
---|
556 | // mode: c++ // |
---|
557 | // compile-command: "make install" // |
---|
558 | // End: // |
---|