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