source: src/driver/cc1.cc@ a1c9ddd

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since a1c9ddd was e3215c5, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add gcc -x flag

  • Property mode set to 100644
File size: 15.0 KB
Line 
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 12 16:11:53 2018
13// Update Count : 94
14//
15
16#include <iostream>
17using std::cerr;
18using std::endl;
19#include <string>
20using std::string;
21#include <cstdio> // stderr, stdout, perror, fprintf
22#include <cstdlib> // getenv, exit, mkstemp
23#include <unistd.h> // execvp, fork, unlink
24#include <sys/wait.h> // wait
25
26#include "config.h" // configure info
27
28
29//#define __DEBUG_H__
30
31
32string compiler_name( CFA_BACKEND_CC ); // path/name of C compiler
33
34string D__GCC_X__( "-D__GCC_X__=" );
35string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
36string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
37
38char tmpname[] = P_tmpdir "/CFAXXXXXX";
39int tmpfilefd = -1;
40
41
42bool prefix( string arg, string pre ) {
43 return arg.substr( 0, pre.size() ) == pre;
44} // prefix
45
46
47void checkEnv( const char *args[], int &nargs ) {
48 char *value;
49
50 value = getenv( "__COMPILER__" );
51 if ( value != NULL ) {
52 compiler_name = value;
53#ifdef __DEBUG_H__
54 cerr << "env arg:\"" << compiler_name << "\"" << endl;
55#endif // __DEBUG_H__
56 } // if
57
58 value = getenv( "__GCC_MACHINE__" );
59 if ( value != NULL ) {
60 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
61#ifdef __DEBUG_H__
62 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
63#endif // __DEBUG_H__
64 nargs += 1;
65 } // if
66
67 value = getenv( "__GCC_VERSION__" );
68 if ( value != NULL ) {
69 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
70#ifdef __DEBUG_H__
71 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
72#endif // __DEBUG_H__
73 nargs += 1;
74 } // if
75} // checkEnv
76
77
78void rmtmpfile() {
79 if ( unlink( tmpname ) == -1 ) { // remove tmpname
80 perror ( "CFA Translator error: cpp failed" );
81 exit( EXIT_FAILURE );
82 } // if
83 tmpfilefd = -1; // mark closed
84} // rmtmpfile
85
86
87void sigTermHandler( __attribute__((unused)) int signal ) {
88 if ( tmpfilefd != -1 ) { // RACE, file created ?
89 rmtmpfile(); // remove
90 exit( EXIT_FAILURE ); // terminate
91 } // if
92} // sigTermHandler
93
94
95void Stage1( const int argc, const char * const argv[] ) {
96 int code;
97 int i;
98
99 string arg;
100 string bprefix;
101
102 const char *cpp_in = NULL;
103 const char *cpp_out = NULL;
104
105 bool CFA_flag = false;
106 bool cpp_flag = false;
107 const char *o_name = NULL;
108
109 const char *args[argc + 100]; // leave space for 100 additional cpp command line values
110 int nargs = 1; // number of arguments in args list; 0 => command name
111 const char *cargs[20]; // leave space for 20 additional cfa-cpp command line values
112 int ncargs = 1; // 0 => command name
113
114 signal( SIGINT, sigTermHandler );
115 signal( SIGTERM, sigTermHandler );
116
117#ifdef __DEBUG_H__
118 cerr << "Stage1" << endl;
119#endif // __DEBUG_H__
120
121 // process all the arguments
122
123 checkEnv( args, nargs ); // arguments passed via environment variables
124
125 for ( i = 1; i < argc; i += 1 ) {
126#ifdef __DEBUG_H__
127 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
128#endif // __DEBUG_H__
129 arg = argv[i];
130#ifdef __DEBUG_H__
131 cerr << "arg:\"" << arg << "\"" << endl;
132#endif // __DEBUG_H__
133 if ( prefix( arg, "-" ) ) {
134 // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
135
136 if ( arg == "-quiet" ) {
137 } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {
138 i += 1; // and the argument
139 } else if ( prefix( arg, "-A" ) ) {
140 } else if ( prefix( arg, "-D__GNU" ) ) {
141 //********
142 // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!
143 //********
144 } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {
145 i += 1; // and the argument
146
147 // strip flags controlling cpp step
148
149 } else if ( arg == "-D__CPP__" ) {
150 cpp_flag = true;
151 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {
152 i += 1; // and the argument
153 cpp_flag = true;
154 } else if ( arg == "-D__CFA_PREPROCESS__" ) {
155 CFA_flag = true;
156 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) {
157 i += 1; // and the argument
158 CFA_flag = true;
159 } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {
160 cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();
161 ncargs += 1;
162 } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {
163 cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();
164 ncargs += 1;
165 i += 1; // and the argument
166 } else if ( prefix( arg, D__GCC_X__ ) ) {
167 args[nargs] = "-x";
168 nargs += 1;
169 args[nargs] = ( *new string( arg.substr( D__GCC_X__.size() ) ) ).c_str(); // pass the flag along
170 nargs += 1;
171 } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_X__.substr(2) ) ) {
172 args[nargs] = "-x";
173 nargs += 1;
174 args[nargs] = ( *new string( string( argv[i + 1] ).substr( D__GCC_X__.size() - 2 ) ) ).c_str(); // pass the flag along
175 nargs += 1;
176 i += 1; // and the argument
177 } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {
178 bprefix = arg.substr( D__GCC_BPREFIX__.size() );
179 } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) {
180 bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 );
181 i += 1; // and the argument
182
183 // all other flags
184
185 } else if ( arg == "-o" ) {
186 i += 1;
187 o_name = argv[i];
188 } else {
189 args[nargs] = argv[i]; // pass the flag along
190 nargs += 1;
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 nargs += 1;
198#ifdef __DEBUG_H__
199 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
200#endif // __DEBUG_H__
201 } else if ( arg == "-MD" || arg == "-MMD" ) {
202 args[nargs] = "-MF"; // insert before file
203 nargs += 1;
204 i += 1;
205 args[nargs] = argv[i]; // pass the argument along
206 nargs += 1;
207#ifdef __DEBUG_H__
208 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
209#endif // __DEBUG_H__
210 } // if
211 } // if
212 } else { // obtain input and possibly output files
213 if ( cpp_in == NULL ) {
214 cpp_in = argv[i];
215#ifdef __DEBUG_H__
216 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
217#endif // __DEBUG_H__
218 } else if ( cpp_out == NULL ) {
219 cpp_out = argv[i];
220#ifdef __DEBUG_H__
221 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
222#endif // __DEBUG_H__
223 } else {
224 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
225 exit( EXIT_FAILURE );
226 } // if
227 } // if
228 } // for
229
230#ifdef __DEBUG_H__
231 cerr << "args:";
232 for ( i = 1; i < nargs; i += 1 ) {
233 cerr << " " << args[i];
234 } // for
235 if ( cpp_in != NULL ) cerr << " " << cpp_in;
236 if ( cpp_out != NULL ) cerr << " " << cpp_out;
237 cerr << endl;
238#endif // __DEBUG_H__
239
240 if ( cpp_in == NULL ) {
241 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
242 exit( EXIT_FAILURE );
243 } // if
244
245 if ( cpp_flag ) {
246 // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard
247 // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
248
249 args[0] = compiler_name.c_str();
250 args[nargs] = cpp_in;
251 nargs += 1;
252 if ( o_name != NULL ) { // location for output
253 args[nargs] = "-o";
254 nargs += 1;
255 args[nargs] = o_name;
256 nargs += 1;
257 } // if
258 args[nargs] = NULL; // terminate argument list
259
260#ifdef __DEBUG_H__
261 cerr << "nargs: " << nargs << endl;
262 for ( i = 0; args[i] != NULL; i += 1 ) {
263 cerr << args[i] << " ";
264 } // for
265 cerr << endl;
266#endif // __DEBUG_H__
267
268 execvp( args[0], (char *const *)args ); // should not return
269 perror( "CFA Translator error: cpp level, execvp" );
270 exit( EXIT_FAILURE );
271 } // if
272
273 // Create a temporary file to store output of the C preprocessor.
274
275 tmpfilefd = mkstemp( tmpname );
276 if ( tmpfilefd == -1 ) {
277 perror( "CFA Translator error: cpp level, mkstemp" );
278 exit( EXIT_FAILURE );
279 } // if
280
281#ifdef __DEBUG_H__
282 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
283#endif // __DEBUG_H__
284
285 // Run the C preprocessor and save the output in tmpfile.
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( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
292 perror( "CFA Translator error: cpp level, freopen" );
293 exit( EXIT_FAILURE );
294 } // if
295
296 args[0] = compiler_name.c_str();
297 args[nargs] = cpp_in; // input to cpp
298 nargs += 1;
299 args[nargs] = NULL; // terminate argument list
300
301#ifdef __DEBUG_H__
302 cerr << "cpp nargs: " << nargs << endl;
303 for ( i = 0; args[i] != NULL; i += 1 ) {
304 cerr << args[i] << " ";
305 } // for
306 cerr << endl;
307#endif // __DEBUG_H__
308
309 execvp( args[0], (char *const *)args ); // should not return
310 perror( "CFA Translator error: cpp level, execvp" );
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) != 0 ) { // child failed ?
321 rmtmpfile(); // remove tmpname
322 cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl;
323 exit( EXIT_FAILURE );
324 } // if
325
326 if ( WEXITSTATUS(code) != 0 ) { // child error ?
327 rmtmpfile(); // remove tmpname
328 exit( WEXITSTATUS( code ) ); // do not continue
329 } // if
330
331 // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
332 // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
333
334 if ( fork() == 0 ) { // child runs CFA
335 cargs[0] = ( *new string( bprefix + "/cfa-cpp" ) ).c_str();
336
337 // Source file-name used to generate routine names containing global initializations for TU.
338 cargs[ncargs] = ( *new string( "-F" ) ).c_str();
339 ncargs += 1;
340 cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str();
341 ncargs += 1;
342
343 cargs[ncargs] = tmpname;
344 ncargs += 1;
345 if ( o_name != NULL ) {
346 cargs[ncargs] = o_name;
347 ncargs += 1;
348 } else if ( ! CFA_flag ) { // run cfa-cpp ?
349 cargs[ncargs] = cpp_out;
350 ncargs += 1;
351 } // if
352 cargs[ncargs] = NULL; // terminate argument list
353
354#ifdef __DEBUG_H__
355 cerr << "cfa-cpp ncargs: " << o_name << " " << CFA_flag << " " << ncargs << endl;
356 for ( i = 0; cargs[i] != NULL; i += 1 ) {
357 cerr << cargs[i] << " ";
358 } // for
359 cerr << endl;
360#endif // __DEBUG_H__
361
362 execvp( cargs[0], (char * const *)cargs ); // should not return
363 perror( "CFA Translator error: cpp level, execvp" );
364 exit( EXIT_FAILURE );
365 } // if
366
367 wait( &code ); // wait for child to finish
368
369#ifdef __DEBUG_H__
370 cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
371#endif // __DEBUG_H__
372
373 // Must unlink here because file must exist across execvp.
374 rmtmpfile(); // remove tmpname
375
376 if ( WIFSIGNALED(code) ) { // child failed ?
377 cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
378 exit( EXIT_FAILURE );
379 } // if
380
381 exit( WEXITSTATUS(code) );
382} // Stage1
383
384
385void Stage2( const int argc, const char * const * argv ) {
386 int i;
387
388 string arg;
389
390 const char *cpp_in = NULL;
391
392 const char *args[argc + 100]; // leave space for 100 additional cfa command line values
393 int nargs = 1; // number of arguments in args list; 0 => command name
394
395#ifdef __DEBUG_H__
396 cerr << "Stage2" << endl;
397#endif // __DEBUG_H__
398
399 // process all the arguments
400
401 checkEnv( args, nargs ); // arguments passed via environment variables
402
403 for ( i = 1; i < argc; i += 1 ) {
404#ifdef __DEBUG_H__
405 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
406#endif // __DEBUG_H__
407 arg = argv[i];
408#ifdef __DEBUG_H__
409 cerr << "arg:\"" << arg << "\"" << endl;
410#endif // __DEBUG_H__
411 if ( prefix( arg, "-" ) ) {
412 // strip inappropriate flags
413
414 if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
415 // Currently CFA does not suppose precompiled .h files.
416 prefix( arg, "--output-pch" ) ) {
417
418 // strip inappropriate flags with an argument
419
420 } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) {
421 i += 1;
422#ifdef __DEBUG_H__
423 cerr << "arg:\"" << argv[i] << "\"" << endl;
424#endif // __DEBUG_H__
425
426 // all other flags
427
428 } else {
429 args[nargs] = argv[i]; // pass the flag along
430 nargs += 1;
431 if ( arg == "-o" ) {
432 i += 1;
433 args[nargs] = argv[i]; // pass the argument along
434 nargs += 1;
435#ifdef __DEBUG_H__
436 cerr << "arg:\"" << argv[i] << "\"" << endl;
437#endif // __DEBUG_H__
438 } // if
439 } // if
440 } else { // obtain input and possibly output files
441 if ( cpp_in == NULL ) {
442 cpp_in = argv[i];
443#ifdef __DEBUG_H__
444 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
445#endif // __DEBUG_H__
446 } else {
447 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
448 exit( EXIT_FAILURE );
449 } // if
450 } // if
451 } // for
452
453#ifdef __DEBUG_H__
454 cerr << "args:";
455 for ( i = 1; i < nargs; i += 1 ) {
456 cerr << " " << args[i];
457 } // for
458 cerr << endl;
459 if ( cpp_in != NULL ) cerr << " " << cpp_in;
460#endif // __DEBUG_H__
461
462 args[0] = compiler_name.c_str();
463 args[nargs] = "-S"; // only compile and put assembler output in specified file
464 nargs += 1;
465 args[nargs] = cpp_in;
466 nargs += 1;
467 args[nargs] = NULL; // terminate argument list
468
469#ifdef __DEBUG_H__
470 cerr << "stage2 nargs: " << nargs << endl;
471 for ( i = 0; args[i] != NULL; i += 1 ) {
472 cerr << args[i] << " ";
473 } // for
474 cerr << endl;
475#endif // __DEBUG_H__
476
477 execvp( args[0], (char * const *)args ); // should not return
478 perror( "CFA Translator error: cpp level, execvp" );
479 exit( EXIT_FAILURE ); // tell gcc not to go any further
480} // Stage2
481
482
483int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
484#ifdef __DEBUG_H__
485 for ( int i = 0; env[i] != NULL; i += 1 ) {
486 cerr << env[i] << endl;
487 } // for
488#endif // __DEBUG_H__
489
490 string arg = argv[1];
491
492 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
493
494 if ( arg == "-E" ) {
495 Stage1( argc, argv );
496 } else if ( arg == "-fpreprocessed" ) {
497 Stage2( argc, argv );
498 } else {
499 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
500 exit( EXIT_FAILURE );
501 } // if
502} // main
503
504// Local Variables: //
505// tab-width: 4 //
506// mode: c++ //
507// compile-command: "make install" //
508// End: //
Note: See TracBrowser for help on using the repository browser.