source: driver/cc1.cc@ f277633e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since f277633e was 0163d3e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

make -x flag work for one input file

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