Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • driver/cc1.cc

    r6dc19544 r2c60af75  
    1010// Created On       : Fri Aug 26 14:23:51 2005
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep  3 16:57:05 2018
    13 // Update Count     : 125
     12// Last Modified On : Thu Aug 22 22:29:52 2019
     13// Update Count     : 328
    1414//
    1515
     
    1919#include <string>
    2020using std::string;
     21#include <algorithm>                                                                    // find
    2122#include <cstdio>                                                                               // stderr, stdout, perror, fprintf
    2223#include <cstdlib>                                                                              // getenv, exit, mkstemp
     
    3031
    3132
    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 ) {
     33static string installlibdir( CFA_LIBDIR );                              // fixed location of cc1 and cfa-cpp commands when installed
     34static string compiler_path( CFA_BACKEND_CC );                  // path/name of C compiler
     35static bool CFA_flag = false;                                                   // -CFA flag
     36static string o_file;
     37
     38static bool prefix( const string & arg, const string & pre ) {
    4239        return arg.substr( 0, pre.size() ) == pre;
    4340} // prefix
    4441
    45 enum { NumSuffixes = 2 };
    46 const string suffixes[NumSuffixes] = { "cfa", "hfa", };
    47 
    48 void suffix( string arg, const char * args[], int & nargs ) {
     42static void suffix( const string & arg, const char * args[], int & nargs ) {
     43        enum { NumSuffixes = 3 };
     44        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
     45
    4946        //std::cerr << arg << std::endl;
    5047        size_t dot = arg.find_last_of( "." );
    5148        //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
    5249        if ( dot == string::npos ) return;
    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
     50        const string * end = suffixes + NumSuffixes;
     51        if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) {
     52                args[nargs++] = "-x";
     53                args[nargs++] = "c";
     54        } // if
    6355} // suffix
    6456
    6557
    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;
    72                 #ifdef __DEBUG_H__
    73                 cerr << "env arg:\"" << compiler_name << "\"" << endl;
    74                 #endif // __DEBUG_H__
    75         } // if
    76 
    77         value = getenv( "__GCC_MACHINE__" );
    78         if ( value != NULL ) {
    79                 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
    80                 #ifdef __DEBUG_H__
    81                 cerr << "env arg:\"" << args[nargs] << "\"" << endl;
    82                 #endif // __DEBUG_H__
    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() {
     58static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );
     59
     60static void checkEnv1( const char * args[], int & nargs ) { // stage 1
     61        extern char ** environ;
     62
     63        for ( int i = 0; environ[i]; i += 1 ) {
     64                string arg = environ[i];
     65                #ifdef __DEBUG_H__
     66                cerr << "env arg:\"" << arg << "\"" << endl;
     67                #endif // __DEBUG_H__
     68
     69                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
     70                        string val = arg.substr( __CFA_FLAGPREFIX__.size() + 4 );
     71                        if ( prefix( val, "-compiler=" ) ) {
     72                                compiler_path = val.substr( 10 );
     73                        } // if
     74                } // if
     75        } // for
     76} // checkEnv1
     77
     78
     79static void checkEnv2( const char * args[], int & nargs ) { // stage 2
     80        extern char ** environ;
     81
     82        for ( int i = 0; environ[i]; i += 1 ) {
     83                string arg = environ[i];
     84                #ifdef __DEBUG_H__
     85                cerr << "env arg:\"" << arg << "\"" << endl;
     86                #endif // __DEBUG_H__
     87
     88                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
     89                        string val = arg.substr( __CFA_FLAGPREFIX__.size() + 4 );
     90                        if ( prefix( val, "-compiler=" ) ) {
     91                                compiler_path = val.substr( 10 );
     92                        } else if ( prefix( val, "-CFA" ) ) {
     93                                CFA_flag = true;
     94                        } else if ( prefix( val, "-o=" ) ) {            // output file for -CFA
     95                                o_file = val.substr( 3 );
     96                        } else {
     97                                args[nargs++] = ( *new string( arg.substr( __CFA_FLAGPREFIX__.size() + 4 ) ) ).c_str();
     98                        } // if
     99                } // if
     100        } // for
     101} // checkEnv2
     102
     103
     104static char tmpname[] = P_tmpdir "/CFAXXXXXX.i";
     105static int tmpfilefd = -1;
     106static bool startrm = false;
     107
     108static void rmtmpfile() {
     109        startrm = true;                                                                         // RACE with C-c C-c
    98110        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
    99                 perror ( "CFA Translator error: cpp failed" );
    100                 exit( EXIT_FAILURE );
    101         } // if
    102         tmpfilefd = -1;                                                                         // mark closed
     111                perror ( "CC1 Translator error: failed, unlink" );
     112                exit( EXIT_FAILURE );
     113        } // if
     114        tmpfilefd = -1;                                                                         // mark removed
    103115} // rmtmpfile
    104116
    105117
    106 void sigTermHandler( __attribute__((unused)) int signal ) {
     118static void sigTermHandler( int ) {                                             // C-c C-c
     119        if ( startrm ) return;                                                          // return and let rmtmpfile finish, and then program finishes
     120
    107121        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
    108                 rmtmpfile();                                                                    // remove
    109                 exit( EXIT_FAILURE );                                                   // terminate
    110         } // if
     122                rmtmpfile();                                                                    // remove tmpname
     123        } // if
     124        exit( EXIT_FAILURE );                                                           // terminate
    111125} // sigTermHandler
    112126
    113127
    114 void Stage1( const int argc, const char * const argv[] ) {
     128static void Stage1( const int argc, const char * const argv[] ) {
    115129        int code;
    116 
    117130        string arg;
    118         string bprefix;
    119131
    120132        const char *cpp_in = NULL;
    121133        const char *cpp_out = NULL;
    122134
    123         bool CFA_flag = false;
    124135        bool cpp_flag = false;
    125         const char *o_name = NULL;
     136        bool o_flag = false;
    126137
    127138        const char *args[argc + 100];                                           // leave space for 100 additional cpp command line values
    128139        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 );
    134140
    135141        #ifdef __DEBUG_H__
    136142        cerr << "Stage1" << endl;
    137143        #endif // __DEBUG_H__
    138         checkEnv( args, nargs );                                                        // arguments passed via environment variables
     144        checkEnv1( args, nargs );                                                       // arguments passed via environment variables
    139145        #ifdef __DEBUG_H__
    140146        for ( int i = 1; i < argc; i += 1 ) {
     
    168174                                i += 1;                                                                 // and the argument
    169175                                cpp_flag = true;
    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
     176
     177                                // all other flags
    189178
    190179                        } else if ( arg == "-o" ) {
    191180                                i += 1;
    192                                 o_name = argv[i];
     181                                o_flag = true;
     182                                cpp_out = argv[i];
    193183                        } else {
    194                                 args[nargs] = argv[i];                                  // pass the flag along
    195                                 nargs += 1;
     184                                args[nargs++] = argv[i];                                // pass the flag along
    196185                                // CPP flags with an argument
    197186                                if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
     
    199188                                         arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
    200189                                        i += 1;
    201                                         args[nargs] = argv[i];                          // pass the argument along
    202                                         nargs += 1;
     190                                        args[nargs++] = argv[i];                        // pass the argument along
    203191                                        #ifdef __DEBUG_H__
    204192                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
    205193                                        #endif // __DEBUG_H__
    206194                                } else if ( arg == "-MD" || arg == "-MMD" ) {
    207                                         args[nargs] = "-MF";                            // insert before file
    208                                         nargs += 1;
     195                                        args[nargs++] = "-MF";                          // insert before file
    209196                                        i += 1;
    210                                         args[nargs] = argv[i];                          // pass the argument along
    211                                         nargs += 1;
     197                                        args[nargs++] = argv[i];                        // pass the argument along
    212198                                        #ifdef __DEBUG_H__
    213199                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
     
    252238                // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
    253239
    254                 args[0] = compiler_name.c_str();
     240                args[0] = compiler_path.c_str();
    255241                suffix( cpp_in, args, nargs );                                  // check suffix
    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;
    263                 } // if
     242                args[nargs++] = cpp_in;
     243                if ( o_flag ) {                                                                 // location for output
     244                        args[nargs++] = "-o";
     245                } // if
     246                args[nargs++] = cpp_out;
    264247                args[nargs] = NULL;                                                             // terminate argument list
    265248
     
    273256
    274257                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.
     258                perror( "CC1 Translator error: stage 1, execvp" );
     259                exit( EXIT_FAILURE );
     260        } // if
     261
     262        // Run the C preprocessor and save the output in the given file.
    292263
    293264        if ( fork() == 0 ) {                                                             // child process ?
     
    295266                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
    296267                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
    297                 if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
    298                         perror( "CFA Translator error: cpp level, freopen" );
     268                if ( freopen( cpp_out, "w", stdout ) == NULL ) { // redirect stdout to output file
     269                        perror( "CC1 Translator error: stage 1, freopen" );
    299270                        exit( EXIT_FAILURE );
    300271                } // if
    301272
    302                 args[0] = compiler_name.c_str();
     273                args[0] = compiler_path.c_str();
    303274                suffix( cpp_in, args, nargs );                                  // check suffix
    304                 args[nargs] = cpp_in;                                                   // input to cpp
    305                 nargs += 1;
     275                args[nargs++] = cpp_in;                                                 // input to cpp
    306276                args[nargs] = NULL;                                                             // terminate argument list
    307277
     
    315285
    316286                execvp( args[0], (char *const *)args );                 // should not return
    317                 perror( "CFA Translator error: cpp level, execvp" );
     287                perror( "CC1 Translator error: stage 1, execvp" );
    318288                exit( EXIT_FAILURE );
    319289        } // if
     
    325295        #endif // __DEBUG_H__
    326296
    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 
    383297        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
    384                 cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
    385                 exit( EXIT_FAILURE );
    386         } // if
    387 
    388         exit( WEXITSTATUS(code) );
     298                cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl;
     299                exit( EXIT_FAILURE );
     300        } // if
     301
     302        exit( WEXITSTATUS(code) );                                                      // bad cpp result stops top-level gcc
    389303} // Stage1
    390304
    391305
    392 void Stage2( const int argc, const char * const * argv ) {
     306static void Stage2( const int argc, const char * const * argv ) {
     307        int code;
    393308        string arg;
    394309
    395         const char *cpp_in = NULL;
     310        const char * cpp_in = NULL;
     311        const char * cpp_out = NULL;
    396312
    397313        const char *args[argc + 100];                                           // leave space for 100 additional cfa command line values
    398314        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
     315        const char *cargs[20];                                                          // leave space for 20 additional cfa-cpp command line values
     316        int ncargs = 1;                                                                         // 0 => command name
    399317
    400318        #ifdef __DEBUG_H__
    401319        cerr << "Stage2" << endl;
    402320        #endif // __DEBUG_H__
    403         checkEnv( args, nargs );                                                        // arguments passed via environment variables
     321        checkEnv2( cargs, ncargs );                                                     // arguments passed via environment variables
    404322        #ifdef __DEBUG_H__
    405323        for ( int i = 1; i < argc; i += 1 ) {
     
    430348
    431349                        } else {
    432                                 args[nargs] = argv[i];                                  // pass the flag along
    433                                 nargs += 1;
     350                                args[nargs++] = argv[i];                                // pass the flag along
    434351                                if ( arg == "-o" ) {
    435352                                        i += 1;
    436                                         args[nargs] = argv[i];                          // pass the argument along
    437                                         nargs += 1;
     353                                        cpp_out = argv[i];
     354                                        args[nargs++] = argv[i];                        // pass the argument along
    438355                                        #ifdef __DEBUG_H__
    439356                                        cerr << "arg:\"" << argv[i] << "\"" << endl;
     
    447364                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
    448365                                #endif // __DEBUG_H__
     366                        } else if ( cpp_out == NULL ) {
     367                                cpp_out = argv[i];
     368                                #ifdef __DEBUG_H__
     369                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
     370                                #endif // __DEBUG_H__
    449371                        } else {
    450372                                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
     
    454376        } // for
    455377
     378        // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked
     379        // process because variables tmpname and tmpfilefd are cloned.
     380
     381        if ( ! CFA_flag ) {                                                             // run cfa-cpp ?
     382                tmpfilefd = mkstemps( tmpname, 2 );
     383                if ( tmpfilefd == -1 ) {
     384                        perror( "CC1 Translator error: stage 2, mkstemp" );
     385                        exit( EXIT_FAILURE );
     386                } // if
     387
     388                #ifdef __DEBUG_H__
     389                cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
     390                #endif // __DEBUG_H__
     391        } // if
     392
     393        // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
     394        // output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
     395
     396        if ( fork() == 0 ) {                                                            // child runs CFA
     397                cargs[0] = ( *new string( installlibdir + "cfa-cpp" ) ).c_str();
     398
     399                cargs[ncargs++] = cpp_in;
     400
     401                if ( CFA_flag ) {                                                               // run cfa-cpp ?
     402                        if ( o_file.size() != 0 ) {                                     // location for output
     403                                cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str();
     404                        } // if
     405                } else {
     406                        cargs[ncargs++] = tmpname;
     407                } // if
     408                cargs[ncargs] = NULL;                                                   // terminate argument list
     409
     410                #ifdef __DEBUG_H__
     411                for ( int i = 0; cargs[i] != NULL; i += 1 ) {
     412                        cerr << cargs[i] << " ";
     413                } // for
     414                cerr << endl;
     415                #endif // __DEBUG_H__
     416
     417                execvp( cargs[0], (char * const *)cargs );              // should not return
     418                perror( "CC1 Translator error: stage 2, execvp" );
     419                exit( EXIT_FAILURE );
     420        } // if
     421
     422        wait( &code );                                                                          // wait for child to finish
     423
     424        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
     425                rmtmpfile();                                                                    // remove tmpname
     426                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
     427                exit( EXIT_FAILURE );
     428        } // if
     429
     430        if ( CFA_flag ) {                                                                       // no tmpfile created
     431                exit( WEXITSTATUS( code ) );                                    // stop regardless of success or failure
     432        } // if
     433
     434        #ifdef __DEBUG_H__
     435        cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
     436        #endif // __DEBUG_H__
     437
     438        if ( WEXITSTATUS(code) ) {                                                      // child error ?
     439                rmtmpfile();                                                                    // remove tmpname
     440                exit( WEXITSTATUS( code ) );                                    // do not continue
     441        } // if
     442
    456443        #ifdef __DEBUG_H__
    457444        cerr << "args:";
     
    463450        #endif // __DEBUG_H__
    464451
    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
     452        if ( fork() == 0 ) {                                                            // child runs CFA
     453                args[0] = compiler_path.c_str();
     454                args[nargs++] = "-S";                                                   // only compile and put assembler output in specified file
     455                args[nargs++] = tmpname;
     456                args[nargs] = NULL;                                                             // terminate argument list
     457
     458                #ifdef __DEBUG_H__
     459                cerr << "stage2 nargs: " << nargs << endl;
     460                for ( int i = 0; args[i] != NULL; i += 1 ) {
     461                        cerr << args[i] << " ";
     462                } // for
     463                cerr << endl;
     464                #endif // __DEBUG_H__
     465
     466                execvp( args[0], (char * const *)args );                // should not return
     467                perror( "CC1 Translator error: stage 2, execvp" );
     468                exit( EXIT_FAILURE );                                                   // tell gcc not to go any further
     469        } // if
     470
     471        wait( &code );                                                                          // wait for child to finish
     472
     473        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
     474                rmtmpfile();                                                                    // remove tmpname
     475                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
     476                exit( EXIT_FAILURE );
     477        } // if
     478
     479        #ifdef __DEBUG_H__
     480        cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
     481        #endif // __DEBUG_H__
     482
     483        rmtmpfile();                                                                            // remove tmpname
     484        exit( WEXITSTATUS( code ) );                                            // stop regardless of success or failure
    483485} // Stage2
    484486
     487
     488// This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first
     489// command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call
     490// is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the
     491// doubly preprocessed program.
    485492
    486493int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
     
    490497        } // for
    491498        #endif // __DEBUG_H__
     499
     500        signal( SIGINT,  sigTermHandler );
     501        signal( SIGTERM, sigTermHandler );
    492502
    493503        string arg = argv[1];
Note: See TracChangeset for help on using the changeset viewer.