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