source: src/driver/cc1.cc@ 1d386a7

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 1d386a7 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
RevLine 
[b87a5ed]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//
[51b73452]7// cc1.cc --
[b87a5ed]8//
9// Author : Peter A. Buhr
[51b73452]10// Created On : Fri Aug 26 14:23:51 2005
11// Last Modified By : Peter A. Buhr
[e3215c5]12// Last Modified On : Sat May 12 16:11:53 2018
13// Update Count : 94
[51b73452]14//
15
16#include <iostream>
17using std::cerr;
18using std::endl;
19#include <string>
20using std::string;
[b87a5ed]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
[51b73452]25
[b87a5ed]26#include "config.h" // configure info
[51b73452]27
28
29//#define __DEBUG_H__
30
31
[e24f13a]32string compiler_name( CFA_BACKEND_CC ); // path/name of C compiler
[51b73452]33
[e3215c5]34string D__GCC_X__( "-D__GCC_X__=" );
[51b73452]35string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
[d9a0e76]36string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
[51b73452]37
[bdd516a]38char tmpname[] = P_tmpdir "/CFAXXXXXX";
39int tmpfilefd = -1;
40
[51b73452]41
42bool prefix( string arg, string pre ) {
[b87a5ed]43 return arg.substr( 0, pre.size() ) == pre;
[51b73452]44} // prefix
45
46
47void checkEnv( const char *args[], int &nargs ) {
[b87a5ed]48 char *value;
[51b73452]49
[b87a5ed]50 value = getenv( "__COMPILER__" );
51 if ( value != NULL ) {
52 compiler_name = value;
[51b73452]53#ifdef __DEBUG_H__
[b87a5ed]54 cerr << "env arg:\"" << compiler_name << "\"" << endl;
[51b73452]55#endif // __DEBUG_H__
[b87a5ed]56 } // if
[51b73452]57
[b87a5ed]58 value = getenv( "__GCC_MACHINE__" );
59 if ( value != NULL ) {
60 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
[51b73452]61#ifdef __DEBUG_H__
[b87a5ed]62 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
[51b73452]63#endif // __DEBUG_H__
[b87a5ed]64 nargs += 1;
65 } // if
[51b73452]66
[b87a5ed]67 value = getenv( "__GCC_VERSION__" );
68 if ( value != NULL ) {
69 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
[51b73452]70#ifdef __DEBUG_H__
[b87a5ed]71 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
[51b73452]72#endif // __DEBUG_H__
[b87a5ed]73 nargs += 1;
74 } // if
[51b73452]75} // checkEnv
76
77
[bdd516a]78void rmtmpfile() {
[b87a5ed]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
[bdd516a]84} // rmtmpfile
85
86
[b3c36f4]87void sigTermHandler( __attribute__((unused)) int signal ) {
[b87a5ed]88 if ( tmpfilefd != -1 ) { // RACE, file created ?
89 rmtmpfile(); // remove
90 exit( EXIT_FAILURE ); // terminate
91 } // if
[bdd516a]92} // sigTermHandler
93
94
[51b73452]95void Stage1( const int argc, const char * const argv[] ) {
[b87a5ed]96 int code;
97 int i;
[51b73452]98
[b87a5ed]99 string arg;
100 string bprefix;
[51b73452]101
[b87a5ed]102 const char *cpp_in = NULL;
103 const char *cpp_out = NULL;
[51b73452]104
[b87a5ed]105 bool CFA_flag = false;
106 bool cpp_flag = false;
107 const char *o_name = NULL;
[51b73452]108
[b87a5ed]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
[7b937575]111 const char *cargs[20]; // leave space for 20 additional cfa-cpp command line values
112 int ncargs = 1; // 0 => command name
[51b73452]113
[b87a5ed]114 signal( SIGINT, sigTermHandler );
115 signal( SIGTERM, sigTermHandler );
[bdd516a]116
[7b937575]117#ifdef __DEBUG_H__
118 cerr << "Stage1" << endl;
119#endif // __DEBUG_H__
120
[b87a5ed]121 // process all the arguments
[51b73452]122
[b87a5ed]123 checkEnv( args, nargs ); // arguments passed via environment variables
[51b73452]124
[b87a5ed]125 for ( i = 1; i < argc; i += 1 ) {
[51b73452]126#ifdef __DEBUG_H__
[b87a5ed]127 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]128#endif // __DEBUG_H__
[b87a5ed]129 arg = argv[i];
[51b73452]130#ifdef __DEBUG_H__
[b87a5ed]131 cerr << "arg:\"" << arg << "\"" << endl;
[51b73452]132#endif // __DEBUG_H__
[b87a5ed]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;
[4c82a3c]154 } else if ( arg == "-D__CFA_PREPROCESS__" ) {
[b87a5ed]155 CFA_flag = true;
[4c82a3c]156 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) {
[b87a5ed]157 i += 1; // and the argument
158 CFA_flag = true;
159 } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {
[7b937575]160 cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();
161 ncargs += 1;
[b87a5ed]162 } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {
[7b937575]163 cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();
164 ncargs += 1;
[b87a5ed]165 i += 1; // and the argument
[e3215c5]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
[b87a5ed]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
[f8b6d921]183 // all other flags
[b87a5ed]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
[f8b6d921]192 if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
[b87a5ed]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;
[51b73452]198#ifdef __DEBUG_H__
[b87a5ed]199 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]200#endif // __DEBUG_H__
[b87a5ed]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;
[51b73452]207#ifdef __DEBUG_H__
[b87a5ed]208 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]209#endif // __DEBUG_H__
[b87a5ed]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];
[51b73452]220#ifdef __DEBUG_H__
[b87a5ed]221 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
[51b73452]222#endif // __DEBUG_H__
[b87a5ed]223 } else {
224 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
225 exit( EXIT_FAILURE );
226 } // if
227 } // if
228 } // for
229
[51b73452]230#ifdef __DEBUG_H__
[b87a5ed]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;
[51b73452]238#endif // __DEBUG_H__
[b87a5ed]239
240 if ( cpp_in == NULL ) {
[51b73452]241 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
242 exit( EXIT_FAILURE );
243 } // if
[b87a5ed]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
[51b73452]259
260#ifdef __DEBUG_H__
[b87a5ed]261 cerr << "nargs: " << nargs << endl;
262 for ( i = 0; args[i] != NULL; i += 1 ) {
263 cerr << args[i] << " ";
264 } // for
265 cerr << endl;
[51b73452]266#endif // __DEBUG_H__
267
[b87a5ed]268 execvp( args[0], (char *const *)args ); // should not return
269 perror( "CFA Translator error: cpp level, execvp" );
270 exit( EXIT_FAILURE );
271 } // if
[51b73452]272
[b87a5ed]273 // Create a temporary file to store output of the C preprocessor.
[51b73452]274
[b87a5ed]275 tmpfilefd = mkstemp( tmpname );
276 if ( tmpfilefd == -1 ) {
277 perror( "CFA Translator error: cpp level, mkstemp" );
278 exit( EXIT_FAILURE );
[51b73452]279 } // if
280
281#ifdef __DEBUG_H__
[b87a5ed]282 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
[51b73452]283#endif // __DEBUG_H__
284
[b87a5ed]285 // Run the C preprocessor and save the output in tmpfile.
[51b73452]286
[b87a5ed]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
[51b73452]295
[b87a5ed]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
[51b73452]300
301#ifdef __DEBUG_H__
[b87a5ed]302 cerr << "cpp nargs: " << nargs << endl;
303 for ( i = 0; args[i] != NULL; i += 1 ) {
304 cerr << args[i] << " ";
305 } // for
306 cerr << endl;
[51b73452]307#endif // __DEBUG_H__
308
[b87a5ed]309 execvp( args[0], (char *const *)args ); // should not return
310 perror( "CFA Translator error: cpp level, execvp" );
311 exit( EXIT_FAILURE );
[51b73452]312 } // if
313
[b87a5ed]314 wait( &code ); // wait for child to finish
[51b73452]315
316#ifdef __DEBUG_H__
[b87a5ed]317 cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
[51b73452]318#endif // __DEBUG_H__
319
[b87a5ed]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
[51b73452]325
[b87a5ed]326 if ( WEXITSTATUS(code) != 0 ) { // child error ?
327 rmtmpfile(); // remove tmpname
328 exit( WEXITSTATUS( code ) ); // do not continue
329 } // if
[51b73452]330
[b87a5ed]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.
[51b73452]333
[b87a5ed]334 if ( fork() == 0 ) { // child runs CFA
[7b937575]335 cargs[0] = ( *new string( bprefix + "/cfa-cpp" ) ).c_str();
[b87a5ed]336
[4acc87f]337 // Source file-name used to generate routine names containing global initializations for TU.
[7b937575]338 cargs[ncargs] = ( *new string( "-F" ) ).c_str();
339 ncargs += 1;
[4acc87f]340 cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str();
[7b937575]341 ncargs += 1;
342
343 cargs[ncargs] = tmpname;
344 ncargs += 1;
[b87a5ed]345 if ( o_name != NULL ) {
[7b937575]346 cargs[ncargs] = o_name;
347 ncargs += 1;
[b87a5ed]348 } else if ( ! CFA_flag ) { // run cfa-cpp ?
[7b937575]349 cargs[ncargs] = cpp_out;
350 ncargs += 1;
[b87a5ed]351 } // if
[7b937575]352 cargs[ncargs] = NULL; // terminate argument list
[51b73452]353
354#ifdef __DEBUG_H__
[7b937575]355 cerr << "cfa-cpp ncargs: " << o_name << " " << CFA_flag << " " << ncargs << endl;
356 for ( i = 0; cargs[i] != NULL; i += 1 ) {
357 cerr << cargs[i] << " ";
[b87a5ed]358 } // for
359 cerr << endl;
[51b73452]360#endif // __DEBUG_H__
361
[7b937575]362 execvp( cargs[0], (char * const *)cargs ); // should not return
[b87a5ed]363 perror( "CFA Translator error: cpp level, execvp" );
364 exit( EXIT_FAILURE );
365 } // if
[51b73452]366
[b87a5ed]367 wait( &code ); // wait for child to finish
[51b73452]368
369#ifdef __DEBUG_H__
[b87a5ed]370 cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
[51b73452]371#endif // __DEBUG_H__
372
[b87a5ed]373 // Must unlink here because file must exist across execvp.
374 rmtmpfile(); // remove tmpname
[51b73452]375
[b87a5ed]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
[51b73452]380
[b87a5ed]381 exit( WEXITSTATUS(code) );
[51b73452]382} // Stage1
383
384
385void Stage2( const int argc, const char * const * argv ) {
[b87a5ed]386 int i;
[51b73452]387
[b87a5ed]388 string arg;
[51b73452]389
[b87a5ed]390 const char *cpp_in = NULL;
[51b73452]391
[b87a5ed]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
[51b73452]394
[7b937575]395#ifdef __DEBUG_H__
396 cerr << "Stage2" << endl;
397#endif // __DEBUG_H__
398
[b87a5ed]399 // process all the arguments
[51b73452]400
[b87a5ed]401 checkEnv( args, nargs ); // arguments passed via environment variables
[51b73452]402
[b87a5ed]403 for ( i = 1; i < argc; i += 1 ) {
[51b73452]404#ifdef __DEBUG_H__
[b87a5ed]405 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]406#endif // __DEBUG_H__
[b87a5ed]407 arg = argv[i];
[51b73452]408#ifdef __DEBUG_H__
[b87a5ed]409 cerr << "arg:\"" << arg << "\"" << endl;
[51b73452]410#endif // __DEBUG_H__
[b87a5ed]411 if ( prefix( arg, "-" ) ) {
412 // strip inappropriate flags
[51b73452]413
[b87a5ed]414 if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
415 // Currently CFA does not suppose precompiled .h files.
416 prefix( arg, "--output-pch" ) ) {
[51b73452]417
[b87a5ed]418 // strip inappropriate flags with an argument
[51b73452]419
[b87a5ed]420 } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) {
421 i += 1;
[51b73452]422#ifdef __DEBUG_H__
[b87a5ed]423 cerr << "arg:\"" << argv[i] << "\"" << endl;
[51b73452]424#endif // __DEBUG_H__
425
[b87a5ed]426 // all other flags
[51b73452]427
[b87a5ed]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;
[51b73452]435#ifdef __DEBUG_H__
[b87a5ed]436 cerr << "arg:\"" << argv[i] << "\"" << endl;
[51b73452]437#endif // __DEBUG_H__
[b87a5ed]438 } // if
439 } // if
440 } else { // obtain input and possibly output files
441 if ( cpp_in == NULL ) {
442 cpp_in = argv[i];
[51b73452]443#ifdef __DEBUG_H__
[b87a5ed]444 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
[51b73452]445#endif // __DEBUG_H__
[b87a5ed]446 } else {
447 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
448 exit( EXIT_FAILURE );
449 } // if
450 } // if
451 } // for
[51b73452]452
453#ifdef __DEBUG_H__
[b87a5ed]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;
[51b73452]460#endif // __DEBUG_H__
461
[b87a5ed]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
[51b73452]468
469#ifdef __DEBUG_H__
[b87a5ed]470 cerr << "stage2 nargs: " << nargs << endl;
471 for ( i = 0; args[i] != NULL; i += 1 ) {
472 cerr << args[i] << " ";
473 } // for
474 cerr << endl;
[51b73452]475#endif // __DEBUG_H__
476
[b87a5ed]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
[51b73452]480} // Stage2
481
482
[b3c36f4]483int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
[51b73452]484#ifdef __DEBUG_H__
[b87a5ed]485 for ( int i = 0; env[i] != NULL; i += 1 ) {
486 cerr << env[i] << endl;
487 } // for
[51b73452]488#endif // __DEBUG_H__
489
[b87a5ed]490 string arg = argv[1];
[51b73452]491
[b87a5ed]492 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
[51b73452]493
[b87a5ed]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
[51b73452]502} // main
503
504// Local Variables: //
[b87a5ed]505// tab-width: 4 //
506// mode: c++ //
[51b73452]507// compile-command: "make install" //
508// End: //
Note: See TracBrowser for help on using the repository browser.