source: src/driver/cc1.cc@ f8b6d921

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 resolv-new with_gc
Last change on this file since f8b6d921 was f8b6d921, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

handle cpp -U command-line option

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