Changes in driver/cc1.cc [bbb1b35:6dc19544]
- File:
-
- 1 edited
-
driver/cc1.cc (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
driver/cc1.cc
rbbb1b35 r6dc19544 10 10 // Created On : Fri Aug 26 14:23:51 2005 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Aug 23 15:06:27 201913 // Update Count : 37112 // Last Modified On : Mon Sep 3 16:57:05 2018 13 // Update Count : 125 14 14 // 15 15 … … 19 19 #include <string> 20 20 using std::string; 21 #include <algorithm> // find22 21 #include <cstdio> // stderr, stdout, perror, fprintf 23 22 #include <cstdlib> // getenv, exit, mkstemp 24 23 #include <unistd.h> // execvp, fork, unlink 25 24 #include <sys/wait.h> // wait 26 #include <fcntl.h>27 28 25 29 26 #include "config.h" // configure info … … 33 30 34 31 35 static string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands when installed 36 static string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler 37 static bool CFA_flag = false; // -CFA flag 38 static bool save_temps = false; // -save-temps flag 39 static string o_file; 40 41 42 static bool prefix( const string & arg, const string & pre ) { 32 string compiler_name( CFA_BACKEND_CC ); // path/name of C compiler 33 34 string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" ); 35 string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" ); 36 37 char tmpname[] = P_tmpdir "/CFAXXXXXX"; 38 int tmpfilefd = -1; 39 40 41 bool prefix( string arg, string pre ) { 43 42 return arg.substr( 0, pre.size() ) == pre; 44 43 } // prefix 45 44 46 static void suffix( const string & arg, const char * args[], int & nargs ) { 47 enum { NumSuffixes = 3 }; 48 static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" }; 49 45 enum { NumSuffixes = 2 }; 46 const string suffixes[NumSuffixes] = { "cfa", "hfa", }; 47 48 void suffix( string arg, const char * args[], int & nargs ) { 49 //std::cerr << arg << std::endl; 50 50 size_t dot = arg.find_last_of( "." ); 51 //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl; 51 52 if ( dot == string::npos ) return; 52 const string * end = suffixes + NumSuffixes; 53 if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) { 54 args[nargs++] = "-x"; 55 args[nargs++] = "c"; 56 } // if 53 string sx = arg.substr( dot + 1 ); 54 for ( int i = 0; i < NumSuffixes; i += 1 ) { 55 if ( sx == suffixes[i] ) { 56 args[nargs] = "-x"; 57 nargs += 1; 58 args[nargs] = "c"; 59 nargs += 1; 60 return; 61 } // if 62 } // for 57 63 } // suffix 58 64 59 65 60 static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); 61 62 static void checkEnv1( const char * args[], int & nargs ) { // stage 1 63 extern char ** environ; 64 65 for ( int i = 0; environ[i]; i += 1 ) { 66 string arg( environ[i] ); 66 void checkEnv( const char * args[], int & nargs ) { 67 char *value; 68 69 value = getenv( "__CFA_COMPILER__" ); 70 if ( value != NULL ) { 71 compiler_name = value; 67 72 #ifdef __DEBUG_H__ 68 cerr << "env arg:\"" << arg<< "\"" << endl;73 cerr << "env arg:\"" << compiler_name << "\"" << endl; 69 74 #endif // __DEBUG_H__ 70 71 if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) { 72 string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ); 73 if ( prefix( val, "-compiler=" ) ) { 74 compiler_path = val.substr( 10 ); 75 } // if 76 } // if 77 } // for 78 } // checkEnv1 79 80 81 static void checkEnv2( const char * args[], int & nargs ) { // stage 2 82 extern char ** environ; 83 84 for ( int i = 0; environ[i]; i += 1 ) { 85 string arg( environ[i] ); 75 } // if 76 77 value = getenv( "__GCC_MACHINE__" ); 78 if ( value != NULL ) { 79 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along 86 80 #ifdef __DEBUG_H__ 87 cerr << "env arg:\"" << arg << "\"" << endl;81 cerr << "env arg:\"" << args[nargs] << "\"" << endl; 88 82 #endif // __DEBUG_H__ 89 90 if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) { 91 string val( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ); 92 if ( prefix( val, "-compiler=" ) ) { 93 compiler_path = val.substr( 10 ); 94 } else if ( val == "-CFA" ) { 95 CFA_flag = true; 96 } else if ( val == "-save-temps" ) { 97 save_temps = true; 98 } else if ( prefix( val, "-o=" ) ) { // output file for -CFA 99 o_file = val.substr( 3 ); 100 } else { 101 args[nargs++] = ( *new string( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ) ).c_str(); 102 } // if 103 } // if 104 } // for 105 } // checkEnv2 106 107 108 static char tmpname[] = P_tmpdir "/CFAXXXXXX.i"; 109 static int tmpfilefd = -1; 110 static bool startrm = false; 111 112 static void rmtmpfile() { 113 if ( tmpfilefd == -1 ) return; // RACE, file created ? 114 115 startrm = true; // RACE with C-c C-c 83 nargs += 1; 84 } // if 85 86 value = getenv( "__GCC_VERSION__" ); 87 if ( value != NULL ) { 88 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along 89 #ifdef __DEBUG_H__ 90 cerr << "env arg:\"" << args[nargs] << "\"" << endl; 91 #endif // __DEBUG_H__ 92 nargs += 1; 93 } // if 94 } // checkEnv 95 96 97 void rmtmpfile() { 116 98 if ( unlink( tmpname ) == -1 ) { // remove tmpname 117 perror ( "C C1 Translator error: failed, unlink" );118 exit( EXIT_FAILURE ); 119 } // if 120 tmpfilefd = -1; // mark removed99 perror ( "CFA Translator error: cpp failed" ); 100 exit( EXIT_FAILURE ); 101 } // if 102 tmpfilefd = -1; // mark closed 121 103 } // rmtmpfile 122 104 123 105 124 static void sigTermHandler( int ) { // C-c C-c 125 if ( startrm ) return; // return and let rmtmpfile finish, and then program finishes 126 106 void sigTermHandler( __attribute__((unused)) int signal ) { 127 107 if ( tmpfilefd != -1 ) { // RACE, file created ? 128 rmtmpfile(); // remove tmpname129 } // if130 exit( EXIT_FAILURE ); // terminate108 rmtmpfile(); // remove 109 exit( EXIT_FAILURE ); // terminate 110 } // if 131 111 } // sigTermHandler 132 112 133 113 134 staticvoid Stage1( const int argc, const char * const argv[] ) {114 void Stage1( const int argc, const char * const argv[] ) { 135 115 int code; 116 136 117 string arg; 137 138 const char * cpp_in = nullptr; 139 const char * cpp_out = nullptr; 140 118 string bprefix; 119 120 const char *cpp_in = NULL; 121 const char *cpp_out = NULL; 122 123 bool CFA_flag = false; 141 124 bool cpp_flag = false; 142 bool o_flag = false;143 144 const char * args[argc + 100]; // leave space for 100 additional cpp command line values125 const char *o_name = NULL; 126 127 const char *args[argc + 100]; // leave space for 100 additional cpp command line values 145 128 int nargs = 1; // number of arguments in args list; 0 => command name 129 const char *cargs[20]; // leave space for 20 additional cfa-cpp command line values 130 int ncargs = 1; // 0 => command name 131 132 signal( SIGINT, sigTermHandler ); 133 signal( SIGTERM, sigTermHandler ); 146 134 147 135 #ifdef __DEBUG_H__ 148 136 cerr << "Stage1" << endl; 149 137 #endif // __DEBUG_H__ 150 checkEnv 1( args, nargs ); // arguments passed via environment variables138 checkEnv( args, nargs ); // arguments passed via environment variables 151 139 #ifdef __DEBUG_H__ 152 140 for ( int i = 1; i < argc; i += 1 ) { … … 180 168 i += 1; // and the argument 181 169 cpp_flag = true; 182 183 // all other flags 170 } else if ( arg == "-D__CFA_PREPROCESS__" ) { 171 CFA_flag = true; 172 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) { 173 i += 1; // and the argument 174 CFA_flag = true; 175 } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) { 176 cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str(); 177 ncargs += 1; 178 } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) { 179 cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str(); 180 ncargs += 1; 181 i += 1; // and the argument 182 } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) { 183 bprefix = arg.substr( D__GCC_BPREFIX__.size() ); 184 } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) { 185 bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 ); 186 i += 1; // and the argument 187 188 // all other flags 184 189 185 190 } else if ( arg == "-o" ) { 186 191 i += 1; 187 o_flag = true; 188 cpp_out = argv[i]; 192 o_name = argv[i]; 189 193 } else { 190 args[nargs++] = argv[i]; // pass the flag along 194 args[nargs] = argv[i]; // pass the flag along 195 nargs += 1; 191 196 // CPP flags with an argument 192 197 if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" || … … 194 199 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) { 195 200 i += 1; 196 args[nargs++] = argv[i]; // pass the argument along 201 args[nargs] = argv[i]; // pass the argument along 202 nargs += 1; 197 203 #ifdef __DEBUG_H__ 198 204 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 199 205 #endif // __DEBUG_H__ 200 206 } else if ( arg == "-MD" || arg == "-MMD" ) { 201 args[nargs++] = "-MF"; // insert before file 207 args[nargs] = "-MF"; // insert before file 208 nargs += 1; 202 209 i += 1; 203 args[nargs++] = argv[i]; // pass the argument along 210 args[nargs] = argv[i]; // pass the argument along 211 nargs += 1; 204 212 #ifdef __DEBUG_H__ 205 213 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; … … 208 216 } // if 209 217 } else { // obtain input and possibly output files 210 if ( cpp_in == nullptr) {218 if ( cpp_in == NULL ) { 211 219 cpp_in = argv[i]; 212 220 #ifdef __DEBUG_H__ 213 221 cerr << "cpp_in:\"" << cpp_in << "\"" << endl; 214 222 #endif // __DEBUG_H__ 215 } else if ( cpp_out == nullptr) {223 } else if ( cpp_out == NULL ) { 216 224 cpp_out = argv[i]; 217 225 #ifdef __DEBUG_H__ … … 230 238 cerr << " " << args[i]; 231 239 } // for 232 if ( cpp_in != nullptr) cerr << " " << cpp_in;233 if ( cpp_out != nullptr) cerr << " " << cpp_out;240 if ( cpp_in != NULL ) cerr << " " << cpp_in; 241 if ( cpp_out != NULL ) cerr << " " << cpp_out; 234 242 cerr << endl; 235 243 #endif // __DEBUG_H__ 236 244 237 if ( cpp_in == nullptr) {245 if ( cpp_in == NULL ) { 238 246 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 239 247 exit( EXIT_FAILURE ); … … 244 252 // output or -o. The call to cfa has a -E so it does not have to be added to the argument list. 245 253 246 args[0] = compiler_ path.c_str();254 args[0] = compiler_name.c_str(); 247 255 suffix( cpp_in, args, nargs ); // check suffix 248 args[nargs++] = cpp_in; 249 if ( o_flag ) { // location for output 250 args[nargs++] = "-o"; 256 args[nargs] = cpp_in; 257 nargs += 1; 258 if ( o_name != NULL ) { // location for output 259 args[nargs] = "-o"; 260 nargs += 1; 261 args[nargs] = o_name; 262 nargs += 1; 251 263 } // if 252 args[nargs++] = cpp_out; 253 args[nargs] = nullptr; // terminate argument list 264 args[nargs] = NULL; // terminate argument list 254 265 255 266 #ifdef __DEBUG_H__ 256 267 cerr << "nargs: " << nargs << endl; 257 for ( int i = 0; args[i] != nullptr; i += 1 ) {268 for ( int i = 0; args[i] != NULL; i += 1 ) { 258 269 cerr << args[i] << " "; 259 270 } // for … … 261 272 #endif // __DEBUG_H__ 262 273 263 execvp( args[0], (char * const *)args ); // should not return 264 perror( "CC1 Translator error: stage 1, execvp" ); 265 exit( EXIT_FAILURE ); 266 } // if 267 268 // Run the C preprocessor and save the output in the given file. 274 execvp( args[0], (char *const *)args ); // should not return 275 perror( "CFA Translator error: cpp level, execvp" ); 276 exit( EXIT_FAILURE ); 277 } // if 278 279 // Create a temporary file to store output of the C preprocessor. 280 281 tmpfilefd = mkstemp( tmpname ); 282 if ( tmpfilefd == -1 ) { 283 perror( "CFA Translator error: cpp level, mkstemp" ); 284 exit( EXIT_FAILURE ); 285 } // if 286 287 #ifdef __DEBUG_H__ 288 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl; 289 #endif // __DEBUG_H__ 290 291 // Run the C preprocessor and save the output in tmpfile. 269 292 270 293 if ( fork() == 0 ) { // child process ? … … 272 295 // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error, 273 296 // when cpp writes to stdout. Hence, stdout is redirected into the temporary file. 274 if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file275 perror( "C C1 Translator error: stage 1, freopen" );297 if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname 298 perror( "CFA Translator error: cpp level, freopen" ); 276 299 exit( EXIT_FAILURE ); 277 300 } // if 278 301 279 args[0] = compiler_ path.c_str();302 args[0] = compiler_name.c_str(); 280 303 suffix( cpp_in, args, nargs ); // check suffix 281 args[nargs++] = cpp_in; // input to cpp 282 args[nargs] = nullptr; // terminate argument list 304 args[nargs] = cpp_in; // input to cpp 305 nargs += 1; 306 args[nargs] = NULL; // terminate argument list 283 307 284 308 #ifdef __DEBUG_H__ 285 309 cerr << "cpp nargs: " << nargs << endl; 286 for ( int i = 0; args[i] != nullptr; i += 1 ) {310 for ( int i = 0; args[i] != NULL; i += 1 ) { 287 311 cerr << args[i] << " "; 288 312 } // for … … 290 314 #endif // __DEBUG_H__ 291 315 292 execvp( args[0], (char * const *)args );// should not return293 perror( "C C1 Translator error: stage 1, execvp" );316 execvp( args[0], (char *const *)args ); // should not return 317 perror( "CFA Translator error: cpp level, execvp" ); 294 318 exit( EXIT_FAILURE ); 295 319 } // if … … 301 325 #endif // __DEBUG_H__ 302 326 327 if ( WIFSIGNALED(code) != 0 ) { // child failed ? 328 rmtmpfile(); // remove tmpname 329 cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl; 330 exit( EXIT_FAILURE ); 331 } // if 332 333 if ( WEXITSTATUS(code) != 0 ) { // child error ? 334 rmtmpfile(); // remove tmpname 335 exit( WEXITSTATUS( code ) ); // do not continue 336 } // if 337 338 // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard 339 // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file. 340 341 if ( fork() == 0 ) { // child runs CFA 342 cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str(); 343 344 // Source file-name used to generate routine names containing global initializations for TU. 345 cargs[ncargs] = ( *new string( "-F" ) ).c_str(); 346 ncargs += 1; 347 cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str(); 348 ncargs += 1; 349 350 cargs[ncargs] = tmpname; 351 ncargs += 1; 352 if ( o_name != NULL ) { 353 cargs[ncargs] = o_name; 354 ncargs += 1; 355 } else if ( ! CFA_flag ) { // run cfa-cpp ? 356 cargs[ncargs] = cpp_out; 357 ncargs += 1; 358 } // if 359 cargs[ncargs] = NULL; // terminate argument list 360 361 #ifdef __DEBUG_H__ 362 cerr << "cfa-cpp ncargs: " << (o_name ? o_name : "No -o") << " " << CFA_flag << " " << ncargs << endl; 363 for ( int i = 0; cargs[i] != NULL; i += 1 ) { 364 cerr << cargs[i] << " "; 365 } // for 366 cerr << endl; 367 #endif // __DEBUG_H__ 368 369 execvp( cargs[0], (char * const *)cargs ); // should not return 370 perror( "CFA Translator error: cpp level, execvp" ); 371 exit( EXIT_FAILURE ); 372 } // if 373 374 wait( &code ); // wait for child to finish 375 376 #ifdef __DEBUG_H__ 377 cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl; 378 #endif // __DEBUG_H__ 379 380 // Must unlink here because file must exist across execvp. 381 rmtmpfile(); // remove tmpname 382 303 383 if ( WIFSIGNALED(code) ) { // child failed ? 304 cerr << "C C1 Translator error: stage 1, child failed" << WTERMSIG(code) << endl;305 exit( EXIT_FAILURE ); 306 } // if 307 308 exit( WEXITSTATUS(code) ); // bad cpp result stops top-level gcc384 cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl; 385 exit( EXIT_FAILURE ); 386 } // if 387 388 exit( WEXITSTATUS(code) ); 309 389 } // Stage1 310 390 311 391 312 static void Stage2( const int argc, const char * const * argv ) { 313 int code; 392 void Stage2( const int argc, const char * const * argv ) { 314 393 string arg; 315 394 316 const char * cpp_in = nullptr; 317 const char * cpp_out = nullptr; 318 319 const char * args[argc + 100]; // leave space for 100 additional cfa command line values 395 const char *cpp_in = NULL; 396 397 const char *args[argc + 100]; // leave space for 100 additional cfa command line values 320 398 int nargs = 1; // number of arguments in args list; 0 => command name 321 const char * cargs[20]; // leave space for 20 additional cfa-cpp command line values322 int ncargs = 1; // 0 => command name323 399 324 400 #ifdef __DEBUG_H__ 325 401 cerr << "Stage2" << endl; 326 402 #endif // __DEBUG_H__ 327 checkEnv 2( cargs, ncargs ); // arguments passed via environment variables403 checkEnv( args, nargs ); // arguments passed via environment variables 328 404 #ifdef __DEBUG_H__ 329 405 for ( int i = 1; i < argc; i += 1 ) { … … 354 430 355 431 } else { 356 args[nargs++] = argv[i]; // pass the flag along 432 args[nargs] = argv[i]; // pass the flag along 433 nargs += 1; 357 434 if ( arg == "-o" ) { 358 435 i += 1; 359 cpp_out = argv[i];360 args[nargs++] = argv[i]; // pass the argument along436 args[nargs] = argv[i]; // pass the argument along 437 nargs += 1; 361 438 #ifdef __DEBUG_H__ 362 439 cerr << "arg:\"" << argv[i] << "\"" << endl; … … 365 442 } // if 366 443 } else { // obtain input and possibly output files 367 if ( cpp_in == nullptr) {444 if ( cpp_in == NULL ) { 368 445 cpp_in = argv[i]; 369 446 #ifdef __DEBUG_H__ 370 447 cerr << "cpp_in:\"" << cpp_in << "\"" << endl; 371 448 #endif // __DEBUG_H__ 372 } else if ( cpp_out == nullptr ) {373 cpp_out = argv[i];374 #ifdef __DEBUG_H__375 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;376 #endif // __DEBUG_H__377 449 } else { 378 cerr << "Usage: " << argv[0] << " more than two files specified" << endl;450 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 379 451 exit( EXIT_FAILURE ); 380 452 } // if … … 382 454 } // for 383 455 384 if ( cpp_in == nullptr ) {385 cerr << "Usage: " << argv[0] << " missing input file" << endl;386 exit( EXIT_FAILURE );387 } // if388 if ( cpp_out == nullptr ) {389 cerr << "Usage: " << argv[0] << " missing output file" << endl;390 exit( EXIT_FAILURE );391 } // if392 393 // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked394 // process because variables tmpname and tmpfilefd are cloned.395 396 string cfa_cpp_out;397 398 if ( ! CFA_flag ) { // run compiler ?399 if ( save_temps ) {400 cfa_cpp_out = cpp_in;401 size_t dot = cfa_cpp_out.find_last_of( "." );402 if ( dot == string::npos ) {403 cerr << "CC1 Translator error: stage 2, bad file name " << endl;404 exit( EXIT_FAILURE );405 } // if406 407 cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + ".ifa";408 if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) {409 perror( "CC1 Translator error: stage 2, creat" );410 exit( EXIT_FAILURE );411 } // if412 } else {413 tmpfilefd = mkstemps( tmpname, 2 );414 if ( tmpfilefd == -1 ) {415 perror( "CC1 Translator error: stage 2, mkstemp" );416 exit( EXIT_FAILURE );417 } // if418 cfa_cpp_out = tmpname;419 } // if420 #ifdef __DEBUG_H__421 cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;422 #endif // __DEBUG_H__423 } // if424 425 // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard426 // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.427 428 if ( fork() == 0 ) { // child runs CFA429 cargs[0] = ( *new string( installlibdir + "cfa-cpp" ) ).c_str();430 431 cargs[ncargs++] = cpp_in;432 433 if ( CFA_flag ) { // run cfa-cpp ?434 if ( o_file.size() != 0 ) { // location for output435 cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str();436 } // if437 } else {438 cargs[ncargs++] = cfa_cpp_out.c_str();439 } // if440 cargs[ncargs] = nullptr; // terminate argument list441 442 #ifdef __DEBUG_H__443 for ( int i = 0; cargs[i] != nullptr; i += 1 ) {444 cerr << cargs[i] << " ";445 } // for446 cerr << endl;447 #endif // __DEBUG_H__448 449 execvp( cargs[0], (char * const *)cargs ); // should not return450 perror( "CC1 Translator error: stage 2, execvp" );451 exit( EXIT_FAILURE );452 } // if453 454 wait( &code ); // wait for child to finish455 456 if ( WIFSIGNALED(code) ) { // child failed ?457 rmtmpfile(); // remove tmpname458 cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;459 exit( EXIT_FAILURE );460 } // if461 462 if ( CFA_flag ) { // no tmpfile created463 exit( WEXITSTATUS( code ) ); // stop regardless of success or failure464 } // if465 466 #ifdef __DEBUG_H__467 cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;468 #endif // __DEBUG_H__469 470 if ( WEXITSTATUS(code) ) { // child error ?471 rmtmpfile(); // remove tmpname472 exit( WEXITSTATUS( code ) ); // do not continue473 } // if474 475 456 #ifdef __DEBUG_H__ 476 457 cerr << "args:"; … … 478 459 cerr << " " << args[i]; 479 460 } // for 480 cerr << " " << cpp_in << endl; 481 #endif // __DEBUG_H__ 482 483 if ( fork() == 0 ) { // child runs CFA 484 args[0] = compiler_path.c_str(); 485 args[nargs++] = "-S"; // only compile and put assembler output in specified file 486 if ( save_temps ) { // make gcc accept .ifa suffix 487 args[nargs++] = "-x"; 488 args[nargs++] = "cpp-output"; 489 } // if 490 args[nargs++] = cfa_cpp_out.c_str(); 491 args[nargs] = nullptr; // terminate argument list 492 493 #ifdef __DEBUG_H__ 494 cerr << "stage2 nargs: " << nargs << endl; 495 for ( int i = 0; args[i] != nullptr; i += 1 ) { 496 cerr << args[i] << " "; 497 } // for 498 cerr << endl; 499 #endif // __DEBUG_H__ 500 501 execvp( args[0], (char * const *)args ); // should not return 502 perror( "CC1 Translator error: stage 2, execvp" ); 503 exit( EXIT_FAILURE ); // tell gcc not to go any further 504 } // if 505 506 wait( &code ); // wait for child to finish 507 508 if ( WIFSIGNALED(code) ) { // child failed ? 509 rmtmpfile(); // remove tmpname 510 cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl; 511 exit( EXIT_FAILURE ); 512 } // if 513 514 #ifdef __DEBUG_H__ 515 cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl; 516 #endif // __DEBUG_H__ 517 518 rmtmpfile(); // remove tmpname 519 exit( WEXITSTATUS( code ) ); // stop regardless of success or failure 461 cerr << endl; 462 if ( cpp_in != NULL ) cerr << " " << cpp_in; 463 #endif // __DEBUG_H__ 464 465 args[0] = compiler_name.c_str(); 466 args[nargs] = "-S"; // only compile and put assembler output in specified file 467 nargs += 1; 468 args[nargs] = cpp_in; 469 nargs += 1; 470 args[nargs] = NULL; // terminate argument list 471 472 #ifdef __DEBUG_H__ 473 cerr << "stage2 nargs: " << nargs << endl; 474 for ( int i = 0; args[i] != NULL; i += 1 ) { 475 cerr << args[i] << " "; 476 } // for 477 cerr << endl; 478 #endif // __DEBUG_H__ 479 480 execvp( args[0], (char * const *)args ); // should not return 481 perror( "CFA Translator error: cpp level, execvp" ); 482 exit( EXIT_FAILURE ); // tell gcc not to go any further 520 483 } // Stage2 521 484 522 485 523 // This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first524 // command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call525 // is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the526 // doubly preprocessed program.527 528 486 int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) { 529 487 #ifdef __DEBUG_H__ 530 for ( int i = 0; env[i] != nullptr; i += 1 ) {488 for ( int i = 0; env[i] != NULL; i += 1 ) { 531 489 cerr << env[i] << endl; 532 490 } // for 533 491 #endif // __DEBUG_H__ 534 492 535 signal( SIGINT, sigTermHandler ); 536 signal( SIGTERM, sigTermHandler ); 537 538 string arg( argv[1] ); 493 string arg = argv[1]; 539 494 540 495 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
Note:
See TracChangeset
for help on using the changeset viewer.