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