Changes in / [ef22ad6:5d00425]


Ignore:
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • driver/cc1.cc

    ref22ad6 r5d00425  
    1010// Created On       : Fri Aug 26 14:23:51 2005
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 22 22:29:52 2019
    13 // Update Count     : 328
     12// Last Modified On : Mon Sep  3 16:57:05 2018
     13// Update Count     : 125
    1414//
    1515
     
    1919#include <string>
    2020using std::string;
    21 #include <algorithm>                                                                    // find
    2221#include <cstdio>                                                                               // stderr, stdout, perror, fprintf
    2322#include <cstdlib>                                                                              // getenv, exit, mkstemp
     
    3130
    3231
    33 static string installlibdir( CFA_LIBDIR );                              // fixed location of cc1 and cfa-cpp commands when installed
    34 static string compiler_path( CFA_BACKEND_CC );                  // path/name of C compiler
    35 static bool CFA_flag = false;                                                   // -CFA flag
    36 static string o_file;
    37 
    38 static bool prefix( const string & arg, const string & pre ) {
     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 ) {
    3942        return arg.substr( 0, pre.size() ) == pre;
    4043} // prefix
    4144
    42 static void suffix( const string & arg, const char * args[], int & nargs ) {
    43         enum { NumSuffixes = 3 };
    44         static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
    45 
     45enum { NumSuffixes = 2 };
     46const string suffixes[NumSuffixes] = { "cfa", "hfa", };
     47
     48void suffix( string arg, const char * args[], int & nargs ) {
    4649        //std::cerr << arg << std::endl;
    4750        size_t dot = arg.find_last_of( "." );
    4851        //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
    4952        if ( dot == string::npos ) return;
    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
     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
    5563} // suffix
    5664
    5765
    58 static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );
    59 
    60 static 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];
     66void checkEnv( const char * args[], int & nargs ) {
     67        char *value;
     68
     69        value = getenv( "__CFA_COMPILER__" );
     70        if ( value != NULL ) {
     71                compiler_name = value;
    6572                #ifdef __DEBUG_H__
    66                 cerr << "env arg:\"" << arg << "\"" << endl;
     73                cerr << "env arg:\"" << compiler_name << "\"" << endl;
    6774                #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 
    79 static 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];
     75        } // if
     76
     77        value = getenv( "__GCC_MACHINE__" );
     78        if ( value != NULL ) {
     79                args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
    8480                #ifdef __DEBUG_H__
    85                 cerr << "env arg:\"" << arg << "\"" << endl;
     81                cerr << "env arg:\"" << args[nargs] << "\"" << endl;
    8682                #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 
    104 static char tmpname[] = P_tmpdir "/CFAXXXXXX.i";
    105 static int tmpfilefd = -1;
    106 static bool startrm = false;
    107 
    108 static void rmtmpfile() {
    109         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
     97void rmtmpfile() {
    11098        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
    111                 perror ( "CC1 Translator error: failed, unlink" );
    112                 exit( EXIT_FAILURE );
    113         } // if
    114         tmpfilefd = -1;                                                                         // mark removed
     99                perror ( "CFA Translator error: cpp failed" );
     100                exit( EXIT_FAILURE );
     101        } // if
     102        tmpfilefd = -1;                                                                         // mark closed
    115103} // rmtmpfile
    116104
    117105
    118 static void sigTermHandler( int ) {                                             // C-c C-c
    119         if ( startrm ) return;                                                          // return and let rmtmpfile finish, and then program finishes
    120 
     106void sigTermHandler( __attribute__((unused)) int signal ) {
    121107        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
    122                 rmtmpfile();                                                                    // remove tmpname
    123         } // if
    124         exit( EXIT_FAILURE );                                                           // terminate
     108                rmtmpfile();                                                                    // remove
     109                exit( EXIT_FAILURE );                                                   // terminate
     110        } // if
    125111} // sigTermHandler
    126112
    127113
    128 static void Stage1( const int argc, const char * const argv[] ) {
     114void Stage1( const int argc, const char * const argv[] ) {
    129115        int code;
     116
    130117        string arg;
     118        string bprefix;
    131119
    132120        const char *cpp_in = NULL;
    133121        const char *cpp_out = NULL;
    134122
     123        bool CFA_flag = false;
    135124        bool cpp_flag = false;
    136         bool o_flag = false;
     125        const char *o_name = NULL;
    137126
    138127        const char *args[argc + 100];                                           // leave space for 100 additional cpp command line values
    139128        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 );
    140134
    141135        #ifdef __DEBUG_H__
    142136        cerr << "Stage1" << endl;
    143137        #endif // __DEBUG_H__
    144         checkEnv1( args, nargs );                                                       // arguments passed via environment variables
     138        checkEnv( args, nargs );                                                        // arguments passed via environment variables
    145139        #ifdef __DEBUG_H__
    146140        for ( int i = 1; i < argc; i += 1 ) {
     
    174168                                i += 1;                                                                 // and the argument
    175169                                cpp_flag = true;
    176 
    177                                 // 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
    178189
    179190                        } else if ( arg == "-o" ) {
    180191                                i += 1;
    181                                 o_flag = true;
    182                                 cpp_out = argv[i];
     192                                o_name = argv[i];
    183193                        } else {
    184                                 args[nargs++] = argv[i];                                // pass the flag along
     194                                args[nargs] = argv[i];                                  // pass the flag along
     195                                nargs += 1;
    185196                                // CPP flags with an argument
    186197                                if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
     
    188199                                         arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
    189200                                        i += 1;
    190                                         args[nargs++] = argv[i];                        // pass the argument along
     201                                        args[nargs] = argv[i];                          // pass the argument along
     202                                        nargs += 1;
    191203                                        #ifdef __DEBUG_H__
    192204                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
    193205                                        #endif // __DEBUG_H__
    194206                                } else if ( arg == "-MD" || arg == "-MMD" ) {
    195                                         args[nargs++] = "-MF";                          // insert before file
     207                                        args[nargs] = "-MF";                            // insert before file
     208                                        nargs += 1;
    196209                                        i += 1;
    197                                         args[nargs++] = argv[i];                        // pass the argument along
     210                                        args[nargs] = argv[i];                          // pass the argument along
     211                                        nargs += 1;
    198212                                        #ifdef __DEBUG_H__
    199213                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
     
    238252                // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
    239253
    240                 args[0] = compiler_path.c_str();
     254                args[0] = compiler_name.c_str();
    241255                suffix( cpp_in, args, nargs );                                  // check suffix
    242                 args[nargs++] = cpp_in;
    243                 if ( o_flag ) {                                                                 // location for output
    244                         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;
    245263                } // if
    246                 args[nargs++] = cpp_out;
    247264                args[nargs] = NULL;                                                             // terminate argument list
    248265
     
    256273
    257274                execvp( args[0], (char *const *)args );                 // should not return
    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.
     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.
    263292
    264293        if ( fork() == 0 ) {                                                             // child process ?
     
    266295                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
    267296                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
    268                 if ( freopen( cpp_out, "w", stdout ) == NULL ) { // redirect stdout to output file
    269                         perror( "CC1 Translator error: stage 1, freopen" );
     297                if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
     298                        perror( "CFA Translator error: cpp level, freopen" );
    270299                        exit( EXIT_FAILURE );
    271300                } // if
    272301
    273                 args[0] = compiler_path.c_str();
     302                args[0] = compiler_name.c_str();
    274303                suffix( cpp_in, args, nargs );                                  // check suffix
    275                 args[nargs++] = cpp_in;                                                 // input to cpp
     304                args[nargs] = cpp_in;                                                   // input to cpp
     305                nargs += 1;
    276306                args[nargs] = NULL;                                                             // terminate argument list
    277307
     
    285315
    286316                execvp( args[0], (char *const *)args );                 // should not return
    287                 perror( "CC1 Translator error: stage 1, execvp" );
     317                perror( "CFA Translator error: cpp level, execvp" );
    288318                exit( EXIT_FAILURE );
    289319        } // if
     
    295325        #endif // __DEBUG_H__
    296326
     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
    297383        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
    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
     384                cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
     385                exit( EXIT_FAILURE );
     386        } // if
     387
     388        exit( WEXITSTATUS(code) );
    303389} // Stage1
    304390
    305391
    306 static void Stage2( const int argc, const char * const * argv ) {
    307         int code;
     392void Stage2( const int argc, const char * const * argv ) {
    308393        string arg;
    309394
    310         const char * cpp_in = NULL;
    311         const char * cpp_out = NULL;
     395        const char *cpp_in = NULL;
    312396
    313397        const char *args[argc + 100];                                           // leave space for 100 additional cfa command line values
    314398        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
    317399
    318400        #ifdef __DEBUG_H__
    319401        cerr << "Stage2" << endl;
    320402        #endif // __DEBUG_H__
    321         checkEnv2( cargs, ncargs );                                                     // arguments passed via environment variables
     403        checkEnv( args, nargs );                                                        // arguments passed via environment variables
    322404        #ifdef __DEBUG_H__
    323405        for ( int i = 1; i < argc; i += 1 ) {
     
    348430
    349431                        } else {
    350                                 args[nargs++] = argv[i];                                // pass the flag along
     432                                args[nargs] = argv[i];                                  // pass the flag along
     433                                nargs += 1;
    351434                                if ( arg == "-o" ) {
    352435                                        i += 1;
    353                                         cpp_out = argv[i];
    354                                         args[nargs++] = argv[i];                        // pass the argument along
     436                                        args[nargs] = argv[i];                          // pass the argument along
     437                                        nargs += 1;
    355438                                        #ifdef __DEBUG_H__
    356439                                        cerr << "arg:\"" << argv[i] << "\"" << endl;
     
    364447                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
    365448                                #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__
    371449                        } else {
    372450                                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
     
    376454        } // for
    377455
    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 
    443456        #ifdef __DEBUG_H__
    444457        cerr << "args:";
     
    450463        #endif // __DEBUG_H__
    451464
    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
     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
    485483} // Stage2
    486484
    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.
    492485
    493486int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
     
    497490        } // for
    498491        #endif // __DEBUG_H__
    499 
    500         signal( SIGINT,  sigTermHandler );
    501         signal( SIGTERM, sigTermHandler );
    502492
    503493        string arg = argv[1];
  • driver/cfa.cc

    ref22ad6 r5d00425  
    1010// Created On       : Tue Aug 20 13:44:49 2002
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu Aug 22 22:29:50 2019
    13 // Update Count     : 403
     12// Last Modified On : Sat Aug 10 08:44:15 2019
     13// Update Count     : 311
    1414//
    1515
     
    2828#include "config.h"                                                                             // configure info
    2929
     30
    3031using std::cerr;
    3132using std::endl;
     
    3334using std::to_string;
    3435
     36
    3537//#define __DEBUG_H__
    3638
    3739
    38 void Putenv( char * argv[], string arg ) {
    39         static int flags = 0;                                                           // environment variables must have unique names
    40 
    41         if ( putenv( (char *)( *new string( string( "__CFA_FLAG" + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) != 0 ) {
    42                 cerr << argv[0] << " error, cannot set environment variable." << endl;
    43                 exit( EXIT_FAILURE );
    44         } // if
    45 } // Putenv
    46 
    47 
    48 bool prefix( const string & arg, const string & pre ) { // check if string has prefix
     40bool prefix( string arg, string pre ) {
    4941        return arg.substr( 0, pre.size() ) == pre;
    5042} // prefix
    5143
    52 bool suffix( const string & arg ) {                                             // check if string has suffix
     44bool suffix( string arg ) {
    5345        enum { NumSuffixes = 3 };
    5446        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
    55 
     47        //std::cerr << arg << std::endl;
    5648        size_t dot = arg.find_last_of( "." );
     49        //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
    5750        if ( dot == string::npos ) return false;
    5851        const string * end = suffixes + NumSuffixes;
     
    6154
    6255
    63 static inline bool dirExists( const string & path ) {   // check if directory exists
     56void shuffle( const char * args[], int S, int E, int N ) {
     57        // S & E index 1 passed the end so adjust with -1
     58        #ifdef __DEBUG_H__
     59        cerr << "shuffle:" << S << " " << E << " " << N << endl;
     60        #endif // __DEBUG_H__
     61        for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
     62                #ifdef __DEBUG_H__
     63                cerr << "\t" << j << " " << j-N << endl;
     64                #endif // __DEBUG_H__
     65                args[j] = args[j-N];
     66        } // for
     67} // shuffle
     68
     69static inline bool dirExists( const string & path ) {
    6470    struct stat info;
    65     if ( stat( path.c_str(), &info ) != 0 ) return false;
    66     if ( info.st_mode & S_IFDIR ) return true;
    67         return false;
    68 } // dirExists
    69 
    70 
    71 #define xstr(s) str(s)
     71    if(stat( path.c_str(), &info ) != 0)
     72        return false;
     73    else if(info.st_mode & S_IFDIR)
     74        return true;
     75    else
     76        return false;
     77} //dirExists
     78
     79
    7280#define str(s) #s
    7381
    7482int main( int argc, char * argv[] ) {
    7583        string Version( CFA_VERSION_LONG );                                     // current version number from CONFIG
    76         string Major( xstr( CFA_VERSION_MAJOR ) ), Minor( xstr( CFA_VERSION_MINOR ) ), Patch( xstr( CFA_VERSION_PATCH ) );
     84        string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
    7785
    7886        string installincdir( CFA_INCDIR );                                     // fixed location of include files
     
    104112        bool m64 = false;                                                                       // -m64 flag
    105113        bool intree = false;
    106         int o_file = 0;                                                                         // -o filename position
    107114
    108115        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
     
    128135
    129136                        if ( arg == "-Xlinker" || arg == "-o" ) {
    130                                 args[nargs++] = argv[i];                                // pass argument along
     137                                args[nargs] = argv[i];                                  // pass the argument along
     138                                nargs += 1;
    131139                                i += 1;
    132140                                if ( i == argc ) continue;                              // next argument available ?
    133                                 args[nargs++] = argv[i];                                // pass argument along
    134                                 if ( arg == "-o" ) o_file = i;                  // remember file
     141                                args[nargs] = argv[i];                                  // pass the argument along
     142                                nargs += 1;
    135143                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
    136144                                i += 1;
    137                                 Putenv( argv, argv[i] );
     145                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
     146                                nargs += 1;
    138147
    139148                                // CFA specific arguments
     
    142151                                CFA_flag = true;                                                // strip the -CFA flag
    143152                                link = false;
    144                                 args[nargs++] = "-fsyntax-only";                // stop after stage 2
     153                                args[nargs] = "-E";                                             // replace the argument with -E
     154                                nargs += 1;
    145155                        } else if ( arg == "-debug" ) {
    146156                                debug = true;                                                   // strip the debug flag
    147157                        } else if ( arg == "-nodebug" ) {
    148                                 debug = false;                                                  // strip the nodebug flag
     158                                debug = false;                                                  // strip the debug flag
    149159                        } else if ( arg == "-nolib" ) {
    150160                                nolib = true;                                                   // strip the nodebug flag
     
    166176                                if ( i == argc ) continue;                              // next argument available ?
    167177                                compiler_path = argv[i];
    168                                 Putenv( argv, arg + "=" + argv[i] );
     178                                if ( putenv( (char *)( *new string( string( "__CFA_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
     179                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
     180                                        exit( EXIT_FAILURE );
     181                                } // if
    169182
    170183                                // C specific arguments
     
    172185                        } else if ( arg == "-v" ) {
    173186                                verbose = true;                                                 // verbosity required
    174                                 args[nargs++] = argv[i];                                // pass argument along
     187                                args[nargs] = argv[i];                                  // pass the argument along
     188                                nargs += 1;
    175189                        } else if ( arg == "-g" ) {
    176190                                debugging = true;                                               // symbolic debugging required
    177                                 args[nargs++] = argv[i];                                // pass argument along
     191                                args[nargs] = argv[i];                                  // pass the argument along
     192                                nargs += 1;
    178193                        } else if ( prefix( arg, "-x" ) ) {                     // file suffix ?
    179194                                string lang;
    180                                 args[nargs++] = argv[i];                                // pass argument along
     195                                args[nargs] = argv[i];                                  // pass the argument along
     196                                nargs += 1;
    181197                                if ( arg.length() == 2 ) {                              // separate argument ?
    182198                                        i += 1;
    183199                                        if ( i == argc ) continue;                      // next argument available ?
    184200                                        lang = argv[i];
    185                                         args[nargs++] = argv[i];                        // pass argument along
     201                                        args[nargs] = argv[i];                          // pass the argument along
     202                                        nargs += 1;
    186203                                } else {
    187204                                        lang = arg.substr( 2 );
     
    190207                        } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
    191208                                std_flag = true;                                                // -std=XX provided
    192                                 args[nargs++] = argv[i];                                // pass argument along
     209                                args[nargs] = argv[i];                                  // pass the argument along
     210                                nargs += 1;
    193211                        } else if ( arg == "-w" ) {
    194                                 args[nargs++] = argv[i];                                // pass argument along
    195                                 Putenv( argv, arg );
     212                                args[nargs] = argv[i];                                  // pass the argument along
     213                                nargs += 1;
     214                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
     215                                nargs += 1;
    196216                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
    197217                                if ( arg == "-Werror" || arg == "-Wall" ) {
    198                                         args[nargs++] = argv[i];                        // pass argument along
    199                                         Putenv( argv, argv[i] );
     218                                        args[nargs] = argv[i];                          // pass the argument along
     219                                        nargs += 1;
     220                                        args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
     221                                        nargs += 1;
    200222                                } else {
    201223                                        unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
    202                                         args[nargs] = argv[i];                          // conditionally pass argument along
    203                                         const char * warning = argv[i] + adv; // extract warning
     224                                        args[nargs] = argv[i];                          // conditionally pass the argument along
     225                                        const char * warning = argv[i] + adv;    // extract warning
    204226                                        if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
    205                                                 Putenv( argv, arg );
     227                                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
    206228                                        } // if
    207229                                        nargs += 1;
     
    209231                        } else if ( prefix( arg, "-B" ) ) {
    210232                                Bprefix = arg.substr(2);                                // strip the -B flag
    211                                 args[nargs++] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
     233                                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
     234                                nargs += 1;
     235                        } else if ( prefix( arg, "-b" ) ) {
     236                                if ( arg.length() == 2 ) {                              // separate argument ?
     237                                        i += 1;
     238                                        if ( i == argc ) continue;                      // next argument available ?
     239                                        arg += argv[i];                                         // concatenate argument
     240                                } // if
     241                                // later versions of gcc require the -b option to appear at the start of the command line
     242                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
     243                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
     244                                if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
     245                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
     246                                        exit( EXIT_FAILURE );
     247                                } // if
     248                                sargs += 1;
     249                                nargs += 1;
     250                        } else if ( prefix( arg, "-V" ) ) {
     251                                if ( arg.length() == 2 ) {                              // separate argument ?
     252                                        i += 1;
     253                                        if ( i == argc ) continue;                      // next argument available ?
     254                                        arg += argv[i];                                         // concatenate argument
     255                                } // if
     256                                // later versions of gcc require the -V option to appear at the start of the command line
     257                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
     258                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
     259                                if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
     260                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
     261                                        exit( EXIT_FAILURE );
     262                                } // if
     263                                sargs += 1;
     264                                nargs += 1;
    212265                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
    213                                 args[nargs++] = argv[i];                                // pass argument along
     266                                args[nargs] = argv[i];                                  // pass the argument along
     267                                nargs += 1;
    214268                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
    215269                                        cpp_flag = true;                                        // cpp only
     
    218272                        } else if ( arg[1] == 'l' ) {
    219273                                // if the user specifies a library, load it after user code
    220                                 libs[nlibs++] = argv[i];
     274                                libs[nlibs] = argv[i];
     275                                nlibs += 1;
    221276                        } else if ( arg == "-m32" ) {
    222277                                m32 = true;
    223278                                m64 = false;
    224                                 args[nargs++] = argv[i];
     279                                args[nargs] = argv[i];
     280                                nargs += 1;
    225281                        } else if ( arg == "-m64" ) {
    226282                                m64 = true;
    227283                                m32 = false;
    228                                 args[nargs++] = argv[i];
     284                                args[nargs] = argv[i];
     285                                nargs += 1;
    229286                        } else {
    230287                                // concatenate any other arguments
    231                                 args[nargs++] = argv[i];
     288                                args[nargs] = argv[i];
     289                                nargs += 1;
    232290                        } // if
    233291                } else {
    234292                        bool cfa = suffix( arg );                                       // check suffix
    235293                        if ( ! x_flag && cfa ) {                                        // no explicit suffix and cfa suffix ?
    236                                 args[nargs++] = "-x";
    237                                 args[nargs++] = "c";
     294                                args[nargs] = "-x";
     295                                nargs += 1;
     296                                args[nargs] = "c";
     297                                nargs += 1;
    238298                        } // if
    239                         args[nargs++] = argv[i];                                        // concatenate files
     299                        args[nargs] = argv[i];                                          // concatenate file
     300                        nargs += 1;
    240301                        if ( ! x_flag && cfa ) {                                        // no explicit suffix and cfa suffix ?
    241                                 args[nargs++] = "-x";
    242                                 args[nargs++] = "none";
     302                                args[nargs] = "-x";
     303                                nargs += 1;
     304                                args[nargs] = "none";
     305                                nargs += 1;
    243306                        } // if
    244307                        nonoptarg = true;
     
    247310
    248311        #ifdef __x86_64__
    249         args[nargs++] = "-mcx16";                                                       // allow double-wide CAA
     312        args[nargs] = "-mcx16";                                                         // allow double-wide CAA
     313        nargs += 1;
    250314        #endif // __x86_64__
    251315
     
    258322        #endif // __DEBUG_H__
    259323
    260         // if ( cpp_flag && CFA_flag ) {
    261         //      cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
    262         //      exit( EXIT_FAILURE );
    263         // } // if
     324        if ( cpp_flag && CFA_flag ) {
     325                cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
     326                exit( EXIT_FAILURE );
     327        } // if
    264328
    265329        // add the CFA include-library paths, which allow direct access to header files without directory qualification
    266         if ( ! intree ) {
    267                 args[nargs++] = "-I" CFA_INCDIR;
     330        if( !intree ) {
     331                args[nargs] = "-I" CFA_INCDIR;
     332                nargs += 1;
    268333                if ( ! noincstd_flag ) {                                                // do not use during build
    269                         args[nargs++] = "-I" CFA_INCDIR "stdhdr";
     334                        args[nargs] = "-I" CFA_INCDIR "stdhdr";
     335                        nargs += 1;
    270336                } // if
    271                 args[nargs++] = "-I" CFA_INCDIR "concurrency";
    272                 args[nargs++] = "-I" CFA_INCDIR "containers";
     337                args[nargs] = "-I" CFA_INCDIR "concurrency";
     338                nargs += 1;
     339                args[nargs] = "-I" CFA_INCDIR "containers";
     340                nargs += 1;
    273341        } else {
    274                 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src";
     342                args[nargs] = "-I" TOP_SRCDIR "libcfa/src";
     343                nargs += 1;
    275344                if ( ! noincstd_flag ) {                                                // do not use during build
    276                         args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
     345                        args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
     346                        nargs += 1;
    277347                } // if
    278                 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
    279                 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
    280         } // if
     348                args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
     349                nargs += 1;
     350                args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
     351                nargs += 1;
     352        }
    281353
    282354        // add stdbool to get defines for bool/true/false
    283         args[nargs++] = "-imacros";
    284         args[nargs++] = "stdbool.h";
     355        args[nargs] = "-imacros";
     356        nargs += 1;
     357        args[nargs] = "stdbool.h";
     358        nargs += 1;
    285359
    286360        string libbase;
    287         if ( ! intree ) {
     361        if( !intree ) {
    288362                libbase = CFA_LIBDIR;
    289363        } else {
    290364                libbase = TOP_BUILDDIR "libcfa/";
    291                 Putenv( argv, "-t" );
    292         } // if
     365                args[nargs] = "-D__CFA_FLAG__=-t";
     366                nargs += 1;
     367        }
    293368
    294369        string arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
    295370        if ( ! m32 && ! m64 ) {
    296371                if ( arch == "x86" ) {
    297                         args[nargs++] = "-m32";
     372                        args[nargs] = "-m32";
     373                        nargs += 1;
    298374                } else if ( arch == "x64" ) {
    299                         args[nargs++] = "-m64";
     375                        args[nargs] = "-m64";
     376                        nargs += 1;
    300377                }  // if
    301378        } // if
    302 
    303         string libdir = libbase + arch + "-" + (nolib ? "nolib" : (debug ? "debug": "nodebug"));
     379        const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug");
     380        string libdir = libbase + arch + "-" + config;
     381
     382        if ( ! nolib && ! dirExists( libdir ) ) {
     383                cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
     384                cerr << "Was looking for " << libdir << endl;
     385                libdir = libbase + arch + "-" + "nolib";
     386        } // if
     387
    304388        if ( ! dirExists( libdir ) ) {
    305                 cerr << argv[0] << " internal error, cannot find prelude directory " << libdir << endl;
     389                cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
     390                cerr << "Was looking for " << libdir << endl;
    306391                exit( EXIT_FAILURE );
    307392        } // if
    308393
     394        args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir + (intree ? "/prelude" : "")) ).c_str();
     395        nargs += 1;
     396
    309397        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
    310                 args[nargs++] = libs[i];
     398                args[nargs] = libs[i];
     399                nargs += 1;
    311400        } // for
    312401
    313402        if ( link ) {
    314                 args[nargs++] = "-Xlinker";
    315                 args[nargs++] = "--undefined=__cfaabi_dbg_bits_write";
    316                 args[nargs++] = "-Xlinker";
    317                 args[nargs++] = "--undefined=__cfaabi_interpose_startup";
    318                 args[nargs++] = "-Xlinker";
    319                 args[nargs++] = "--undefined=__cfaabi_appready_startup";
     403                args[nargs] = "-Xlinker";
     404                nargs += 1;
     405                args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
     406                nargs += 1;
     407                args[nargs] = "-Xlinker";
     408                nargs += 1;
     409                args[nargs] = "--undefined=__cfaabi_interpose_startup";
     410                nargs += 1;
     411                args[nargs] = "-Xlinker";
     412                nargs += 1;
     413                args[nargs] = "--undefined=__cfaabi_appready_startup";
     414                nargs += 1;
    320415
    321416                // include the cfa library in case it's needed
    322                 args[nargs++] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
    323                 args[nargs++] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
    324                 args[nargs++] = "-Wl,--push-state,--as-needed";
    325                 args[nargs++] = "-lcfathread";
    326                 args[nargs++] = "-Wl,--pop-state";
    327                 args[nargs++] = "-lcfa";
    328                 args[nargs++] = "-lpthread";
    329                 args[nargs++] = "-ldl";
    330                 args[nargs++] = "-lrt";
    331                 args[nargs++] = "-lm";
    332         } // if
    333 
    334         args[nargs++] = "-fexceptions";                                         // add exception flags (unconditionally)
    335 
    336         // add flags based on the type of compile
    337 
    338         args[nargs++] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
    339         args[nargs++] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
    340         args[nargs++] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
    341         args[nargs++] = "-D__CFA__";
    342         args[nargs++] = "-D__CFORALL__";
    343         args[nargs++] = "-D__cforall";
     417                args[nargs] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
     418                nargs += 1;
     419                args[nargs] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
     420                nargs += 1;
     421                args[nargs] = "-Wl,--push-state,--as-needed";
     422                nargs += 1;
     423                args[nargs] = "-lcfathread";
     424                nargs += 1;
     425                args[nargs] = "-Wl,--pop-state";
     426                nargs += 1;
     427                args[nargs] = "-lcfa";
     428                nargs += 1;
     429                args[nargs] = "-lpthread";
     430                nargs += 1;
     431                args[nargs] = "-ldl";
     432                nargs += 1;
     433                args[nargs] = "-lrt";
     434                nargs += 1;
     435                args[nargs] = "-lm";
     436                nargs += 1;
     437        } // if
     438
     439        // Add exception flags (unconditionally)
     440        args[nargs] = "-fexceptions";
     441        nargs += 1;
     442
     443        // add the correct set of flags based on the type of compile this is
     444
     445        args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
     446        nargs += 1;
     447        args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
     448        nargs += 1;
     449        args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
     450        nargs += 1;
     451        args[nargs] = "-D__CFA__";
     452        nargs += 1;
     453        args[nargs] = "-D__CFORALL__";
     454        nargs += 1;
     455        args[nargs] = "-D__cforall";
     456        nargs += 1;
    344457
    345458        if ( cpp_flag ) {
    346                 args[nargs++] = "-D__CPP__";
    347         } // if
    348 
     459                args[nargs] = "-D__CPP__";
     460                nargs += 1;
     461        } // if
     462
     463        shuffle( args, sargs, nargs, 1 );                                       // make room at front of argument list
     464        nargs += 1;
    349465        if ( CFA_flag ) {
    350                 Putenv( argv, "-N" );
    351                 Putenv( argv, "-CFA" );
    352                 if ( o_file ) Putenv( argv, string( "-o=" ) + argv[o_file] );
     466                args[sargs] = "-D__CFA_FLAG__=-N";
     467                args[nargs] = "-D__CFA_PREPROCESS_";
     468                nargs += 1;
    353469        } else {
    354                 Putenv( argv, "-L" );
    355         } // if
    356 
    357         Putenv( argv, "--prelude-dir=" + libdir + (intree ? "/prelude" : "") );
     470                args[sargs] = "-D__CFA_FLAG__=-L";
     471        } // if
     472        sargs += 1;
    358473
    359474        if ( debug ) {
    360475                heading += " (debug)";
    361                 args[nargs++] = "-D__CFA_DEBUG__";
     476                args[nargs] = "-D__CFA_DEBUG__";
     477                nargs += 1;
    362478        } else {
    363479                heading += " (no debug)";
     
    367483                Bprefix = ! intree ? installlibdir : srcdriverdir;
    368484                if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
    369                 args[nargs++] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
    370         } // if
    371 
    372         args[nargs++] = "-Xlinker";                                                     // used by backtrace
    373         args[nargs++] = "-export-dynamic";
     485                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
     486                nargs += 1;
     487        } // if
     488
     489        args[nargs] = "-Xlinker";                                                       // used by backtrace
     490        nargs += 1;
     491        args[nargs] = "-export-dynamic";
     492        nargs += 1;
    374493
    375494        // execute the compilation command
     
    385504
    386505        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
    387                 args[nargs++] = "-no-integrated-cpp";
    388                 args[nargs++] = "-Wno-deprecated";
    389                 #ifdef HAVE_CAST_FUNCTION_TYPE
    390                 args[nargs++] = "-Wno-cast-function-type";
    391                 #endif // HAVE_CAST_FUNCTION_TYPE
     506                args[nargs] = "-no-integrated-cpp";
     507                nargs += 1;
     508                args[nargs] = "-Wno-deprecated";
     509                nargs += 1;
     510#ifdef HAVE_CAST_FUNCTION_TYPE
     511                args[nargs] = "-Wno-cast-function-type";
     512                nargs += 1;
     513#endif // HAVE_CAST_FUNCTION_TYPE
    392514                if ( ! std_flag ) {                                                             // default c11, if none specified
    393                         args[nargs++] = "-std=gnu11";
     515                        args[nargs] = "-std=gnu11";
     516                        nargs += 1;
    394517                } // if
    395                 args[nargs++] = "-fgnu89-inline";
    396                 args[nargs++] = "-D__int8_t_defined";                   // prevent gcc type-size attributes
    397                 args[nargs++] = ( *new string( string("-B") + Bprefix ) ).c_str();
     518                args[nargs] = "-fgnu89-inline";
     519                nargs += 1;
     520                args[nargs] = "-D__int8_t_defined";                             // prevent gcc type-size attributes
     521                nargs += 1;
     522                args[nargs] = ( *new string( string("-B") + Bprefix ) ).c_str();
     523                nargs += 1;
    398524        } else {
    399525                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
     
    401527        } // if
    402528
    403         args[nargs] = NULL;                                                                     // terminate
     529        args[nargs] = NULL;                                                                     // terminate with NULL
    404530
    405531        #ifdef __DEBUG_H__
     
    442568
    443569        execvp( args[0], (char *const *)args );                         // should not return
    444         perror( "CFA Translator error: execvp" );
     570        perror( "CFA Translator error: cfa level, execvp" );
    445571        exit( EXIT_FAILURE );
    446572} // main
  • src/main.cc

    ref22ad6 r5d00425  
    1010// Created On       : Fri May 15 23:12:02 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Aug 23 06:50:08 2019
    13 // Update Count     : 607
     12// Last Modified On : Wed Jun  5 20:35:13 2019
     13// Update Count     : 601
    1414//
    1515
     
    9696DeclarationNode * parseTree = nullptr;                                  // program parse tree
    9797
    98 static bool waiting_for_gdb = false;                                    // flag to set cfa-cpp to wait for gdb on start
     98static bool waiting_for_gdb = false; // flag to set cfa-cpp to wait for gdb on start
    9999
    100100static std::string PreludeDirector = "";
    101101
    102 static void parse_cmdline( int argc, char *argv[] );
     102static void parse_cmdline( int argc, char *argv[], const char *& filename );
    103103static void parse( FILE * input, LinkageSpec::Spec linkage, bool shouldExit = false );
    104104static void dump( list< Declaration * > & translationUnit, ostream & out = cout );
     
    172172        FILE * input;                                                                           // use FILE rather than istream because yyin is FILE
    173173        ostream * output = & cout;
     174        const char * filename = nullptr;
    174175        list< Declaration * > translationUnit;
    175176
     
    183184        // } // for
    184185
    185         parse_cmdline( argc, argv );                                            // process command-line arguments
     186        parse_cmdline( argc, argv, filename );                          // process command-line arguments
    186187        CodeGen::FixMain::setReplaceMain( !nomainp );
    187188
    188         if ( waiting_for_gdb ) {
     189        if(waiting_for_gdb) {
    189190                std::cerr << "Waiting for gdb" << std::endl;
    190191                std::cerr << "run :" << std::endl;
    191192                std::cerr << "  gdb attach " << getpid() << std::endl;
    192193                raise(SIGSTOP);
    193         } // if
     194        }
    194195
    195196        try {
     
    197198                if ( optind < argc ) {                                                  // any commands after the flags ? => input file name
    198199                        input = fopen( argv[ optind ], "r" );
    199                         assertf( input, "cannot open %s because %s\n", argv[ optind ], strerror( errno ) );
     200                        assertf( input, "cannot open %s\n", argv[ optind ] );
     201                        // if running cfa-cpp directly, might forget to pass -F option (and really shouldn't have to)
     202                        if ( filename == nullptr ) filename = argv[ optind ];
     203                        // prelude filename comes in differently
     204                        if ( libcfap ) filename = "prelude.cfa";
    200205                        optind += 1;
    201206                } else {                                                                                // no input file name
    202207                        input = stdin;
     208                        std::cerr << "Input from stdin" << std::endl;
     209                        // if running cfa-cpp directly, might forget to pass -F option. Since this takes from stdin, pass
     210                        // a fake name along
     211                        if ( filename == nullptr ) filename = "stdin";
    203212                } // if
    204213
     
    438447
    439448
    440 static const char optstring[] = ":hlLmNnpP:S:twW:D:";
     449static const char optstring[] = ":hlLmNnpP:S:tgwW:D:F:";
    441450
    442451enum { PreludeDir = 128 };
     
    457466        { "", no_argument, nullptr, 0 },                                        // -W
    458467        { "", no_argument, nullptr, 0 },                                        // -D
     468        { "", no_argument, nullptr, 0 },                                        // -F
    459469        { nullptr, 0, nullptr, 0 }
    460470}; // long_opts
     
    476486        "",                                                                                                     // -W
    477487        "",                                                                                                     // -D
     488        "",                                                                                                     // -F
    478489}; // description
    479490
     
    510521
    511522static void usage( char *argv[] ) {
    512     cout << "Usage: " << argv[0] << " [options] [input-file (default stdin)] [output-file (default stdout)], where options are:" << endl;
     523    cout << "Usage: " << argv[0] << " options are:" << endl;
    513524        int i = 0, j = 1;                                                                       // j skips starting colon
    514525        for ( ; long_opts[i].name != 0 && optstring[j] != '\0'; i += 1, j += 1 ) {
     
    536547} // usage
    537548
    538 static void parse_cmdline( int argc, char * argv[] ) {
     549static void parse_cmdline( int argc, char * argv[], const char *& filename ) {
    539550        opterr = 0;                                                                                     // (global) prevent getopt from printing error messages
    540551
     
    610621                  case 'D':                                                                             // ignore -Dxxx, forwarded by cpp, hidden
    611622                        break;
     623                  case 'F':                                                                             // source file-name without suffix, hidden
     624                        filename = optarg;
     625                        break;
    612626                  case '?':                                                                             // unknown option
    613627                        if ( optopt ) {                                                         // short option ?
Note: See TracChangeset for help on using the changeset viewer.