Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • driver/cfa.cc

    rb4130f9 rc334ecd  
    1010// Created On       : Tue Aug 20 13:44:49 2002
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Nov 17 14:27:28 2020
    13 // Update Count     : 440
     12// Last Modified On : Fri Jan 31 16:48:03 2020
     13// Update Count     : 421
    1414//
    1515
    1616#include <iostream>
    17 #include <cstdio>                                                                               // perror
    18 #include <cstdlib>                                                                              // putenv, exit
    19 #include <climits>                                                                              // PATH_MAX
    20 #include <string>                                                                               // STL version
    21 #include <algorithm>                                                                    // find
    22 
    23 #include <unistd.h>                                                                             // execvp
     17#include <cstdio>      // perror
     18#include <cstdlib>     // putenv, exit
     19#include <climits>     // PATH_MAX
     20#include <unistd.h>    // execvp
     21#include <string>      // STL version
     22#include <string.h>    // strcmp
     23#include <algorithm>   // find
     24
    2425#include <sys/types.h>
    2526#include <sys/stat.h>
     
    3334using std::to_string;
    3435
    35 //#define __DEBUG_H__
    36 
    37 #define xstr(s) str(s)
    38 #define str(s) #s
    39 
    40 static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );               // "__CFA_FLAG__=" suffix
    41 
    42 static void Putenv( char * argv[], string arg ) {
     36// #define __DEBUG_H__
     37
     38// "N__=" suffix
     39static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );
     40
     41void Putenv( char * argv[], string arg ) {
    4342        // environment variables must have unique names
    4443        static int flags = 0;
     
    5049} // Putenv
    5150
    52 static bool prefix( const string & arg, const string & pre ) { // check if string has prefix
     51// check if string has prefix
     52bool prefix( const string & arg, const string & pre ) {
    5353        return arg.substr( 0, pre.size() ) == pre;
    5454} // prefix
    5555
    56 static inline bool ends_with(const string & str, const string & sfix) {
     56inline bool ends_with(const string & str, const string & sfix) {
    5757        if (sfix.size() > str.size()) return false;
    5858        return std::equal(str.rbegin(), str.rbegin() + sfix.size(), sfix.rbegin(), sfix.rend());
     
    6060
    6161// check if string has suffix
    62 static bool suffix( const string & arg ) {
     62bool suffix( const string & arg ) {
    6363        enum { NumSuffixes = 3 };
    6464        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
     
    7070} // suffix
    7171
     72
    7273static inline bool dirExists( const string & path ) {   // check if directory exists
    7374    struct stat info;
     
    7879static inline string dir(const string & path) {
    7980        return path.substr(0, path.find_last_of('/'));
    80 } // dir
     81}
    8182
    8283// Different path modes
     
    117118}
    118119
     120
     121#define xstr(s) str(s)
     122#define str(s) #s
    119123
    120124int main( int argc, char * argv[] ) {
     
    154158        PathMode path = FromProc();
    155159
    156         const char * args[argc + 100];                                          // cfa command line values, plus some space for additional flags
     160        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
    157161        int sargs = 1;                                                                          // starting location for arguments in args list
    158162        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
    159163
    160         const char * libs[argc + 20];                                           // non-user libraries must come separately, plus some added libraries and flags
     164        const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
    161165        int nlibs = 0;
    162166
     
    176180
    177181                        if ( arg == "-Xlinker" || arg == "-o" ) {
    178                                 args[nargs++] = argv[i];                                // pass flag along
     182                                args[nargs++] = argv[i];                                // pass argument along
    179183                                i += 1;
    180184                                if ( i == argc ) continue;                              // next argument available ?
    181185                                args[nargs++] = argv[i];                                // pass argument along
    182186                                if ( arg == "-o" ) o_file = i;                  // remember file
     187                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
     188                                i += 1;
     189                                if ( i == argc ) continue;                              // next argument available ?
     190                                Putenv( argv, argv[i] );
    183191
    184192                                // CFA specific arguments
    185193
    186                         } else if ( strncmp(arg.c_str(), "-XCFA", 5) == 0 ) { // CFA pass through
    187                                 if ( arg.size() == 5 ) {
    188                                         i += 1;
    189                                         if ( i == argc ) continue;                      // next argument available ?
    190                                         Putenv( argv, argv[i] );
    191                                 } else if ( arg[5] == ',' ) {                   // CFA specific arguments
    192                                         Putenv( argv, argv[i] + 6 );
    193                                 } else {                                                                // CFA specific arguments
    194                                         args[nargs++] = argv[i];
    195                                 } // if
    196194                        } else if ( arg == "-CFA" ) {
    197195                                CFA_flag = true;                                                // strip the -CFA flag
     
    202200                        } else if ( arg == "-nodebug" ) {
    203201                                debug = false;                                                  // strip the nodebug flag
     202                        } else if ( arg == "-nolib" ) {
     203                                nolib = true;                                                   // strip the nodebug flag
    204204                        } else if ( arg == "-quiet" ) {
    205205                                quiet = true;                                                   // strip the quiet flag
    206206                        } else if ( arg == "-noquiet" ) {
    207207                                quiet = false;                                                  // strip the noquiet flag
    208                         } else if ( arg == "-no-include-stdhdr" ) {
    209                                 noincstd_flag = true;                                   // strip the no-include-stdhdr flag
    210                         } else if ( arg == "-nolib" ) {
    211                                 nolib = true;                                                   // strip the nolib flag
    212208                        } else if ( arg == "-help" ) {
    213209                                help = true;                                                    // strip the help flag
    214210                        } else if ( arg == "-nohelp" ) {
    215211                                help = false;                                                   // strip the nohelp flag
     212                        } else if ( arg == "-no-include-stdhdr" ) {
     213                                noincstd_flag = true;                                   // strip the no-include-stdhdr flag
    216214                        } else if ( arg == "-cfalib") {
    217215                                compiling_libs = true;
     
    227225                        } else if ( arg == "-v" ) {
    228226                                verbose = true;                                                 // verbosity required
    229                                 args[nargs++] = argv[i];                                // pass flag along
     227                                args[nargs++] = argv[i];                                // pass argument along
    230228                        } else if ( arg == "-g" ) {
    231229                                debugging = true;                                               // symbolic debugging required
    232                                 args[nargs++] = argv[i];                                // pass flag along
    233                         } else if ( arg == "-save-temps" || arg == "--save-temps" ) {
    234                                 args[nargs++] = argv[i];                                // pass flag along
     230                                args[nargs++] = argv[i];                                // pass argument along
     231                        } else if ( arg == "-save-temps" ) {
     232                                args[nargs++] = argv[i];                                // pass argument along
    235233                                Putenv( argv, arg );                                    // save cfa-cpp output
    236234                        } else if ( prefix( arg, "-x" ) ) {                     // file suffix ?
    237235                                string lang;
    238                                 args[nargs++] = argv[i];                                // pass flag along
     236                                args[nargs++] = argv[i];                                // pass argument along
    239237                                if ( arg.length() == 2 ) {                              // separate argument ?
    240238                                        i += 1;
     
    245243                                        lang = arg.substr( 2 );
    246244                                } // if
    247                                 if ( x_flag ) {
    248                                         cerr << argv[0] << " warning, only one -x flag per compile, ignoring subsequent flag." << endl;
    249                                 } else {
    250                                         x_flag = true;
    251                                         Putenv( argv, string( "-x=" ) + lang );
    252                                 } // if
     245                                x_flag = lang != "none";
    253246                        } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
    254247                                std_flag = true;                                                // -std=XX provided
    255                                 args[nargs++] = argv[i];                                // pass flag along
     248                                args[nargs++] = argv[i];                                // pass argument along
    256249                        } else if ( arg == "-w" ) {
    257                                 args[nargs++] = argv[i];                                // pass flag along
     250                                args[nargs++] = argv[i];                                // pass argument along
    258251                                Putenv( argv, arg );
    259252                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
    260253                                if ( arg == "-Werror" || arg == "-Wall" ) {
    261                                         args[nargs++] = argv[i];                        // pass flag along
     254                                        args[nargs++] = argv[i];                        // pass argument along
    262255                                        Putenv( argv, argv[i] );
    263256                                } else {
     
    273266                                bprefix = arg.substr(2);                                // strip the -B flag
    274267                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
    275                                 args[nargs++] = argv[i];                                // pass flag along
     268                                args[nargs++] = argv[i];                                // pass argument along
    276269                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
    277270                                        cpp_flag = true;                                        // cpp only
    278271                                } // if
    279272                                link = false;                           // no linkage required
    280                         } else if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
    281                                                 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
    282                                                 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
    283                                 args[nargs++] = argv[i];                                // pass flag along
    284                                 i += 1;
    285                                 args[nargs++] = argv[i];                                // pass argument along
    286273                        } else if ( arg[1] == 'l' ) {
    287274                                // if the user specifies a library, load it after user code
     
    315302
    316303        #ifdef __x86_64__
    317         args[nargs++] = "-mcx16";                                                       // allow double-wide CAS
     304        args[nargs++] = "-mcx16";                                                       // allow double-wide CAA
    318305        #endif // __x86_64__
    319306
     
    335322        string libbase;
    336323        switch(path) {
    337           case Installed:
     324        case Installed:
    338325                args[nargs++] = "-I" CFA_INCDIR;
    339326                // do not use during build
     
    345332                libbase = CFA_LIBDIR;
    346333                break;
    347           case BuildTree:
    348           case Distributed:
     334        case BuildTree:
     335        case Distributed:
    349336                args[nargs++] = "-I" TOP_SRCDIR "libcfa/src";
    350337                // do not use during build
     
    380367        string libdir = libbase + arch + "-" + config;
    381368
    382         if ( path != Distributed ) {
     369        if (path != Distributed) {
    383370                if ( ! nolib && ! dirExists( libdir ) ) {
    384371                        cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
     
    398385        } // if
    399386
    400         string preludedir;
    401387        switch(path) {
    402           case Installed   : preludedir = libdir; break;
    403           case BuildTree   : preludedir = libdir + "/prelude"; break;
    404           case Distributed : preludedir = dir(argv[0]); break;
    405         } // switch
    406 
    407         Putenv( argv, "--prelude-dir=" + preludedir );
    408         args[nargs++] = "-include";
    409         args[nargs++] = (*new string(preludedir + "/defines.hfa")).c_str();
     388        case Installed   : Putenv( argv, "--prelude-dir=" + libdir ); break;
     389        case BuildTree   : Putenv( argv, "--prelude-dir=" + libdir + "/prelude" ); break;
     390        case Distributed : Putenv( argv, "--prelude-dir=" + dir(argv[0]) ); break;
     391        }
    410392
    411393        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
     
    429411                args[nargs++] = "-lcfathread";
    430412                args[nargs++] = "-Wl,--pop-state";
    431                 args[nargs++] = "-Wl,--push-state,--no-as-needed";
    432413                args[nargs++] = "-lcfa";
    433                 args[nargs++] = "-Wl,--pop-state";
    434414                args[nargs++] = "-pthread";
    435                 #if defined(  __x86_64__ ) || defined( __ARM_ARCH )
    436                 args[nargs++] = "-latomic";                                             // allow double-wide CAS
    437                 #endif // __x86_64__
    438415                args[nargs++] = "-ldl";
     416                args[nargs++] = "-lrt";
    439417                args[nargs++] = "-lm";
    440418        } // if
     
    474452        if ( bprefix.length() == 0 ) {
    475453                switch(path) {
    476                   case Installed   : bprefix = installlibdir; break;
    477                   case BuildTree   : bprefix = srcdriverdir ; break;
    478                   case Distributed : bprefix = dir(argv[0]) ; break;
    479                 } // switch
    480         } // if
    481         if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/';
    482         Putenv( argv, string("-B=") + bprefix );
     454                case Installed   : bprefix = installlibdir; break;
     455                case BuildTree   : bprefix = srcdriverdir ; break;
     456                case Distributed : bprefix = dir(argv[0]) ; break;
     457                }
     458                if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/';
     459                Putenv( argv, string("-B=") + bprefix );
     460        } // if
    483461
    484462        args[nargs++] = "-Xlinker";                                                     // used by backtrace
     
    502480                args[nargs++] = "-Wno-cast-function-type";
    503481                #endif // HAVE_CAST_FUNCTION_TYPE
    504                 if ( ! std_flag && ! x_flag ) {
    505                         args[nargs++] = "-std=gnu11";                           // default c11, if none specified
     482                if ( ! std_flag ) {                                                             // default c11, if none specified
     483                        args[nargs++] = "-std=gnu11";
    506484                } // if
    507485                args[nargs++] = "-fgnu89-inline";
     
    553531        // execute the command and return the result
    554532
    555         execvp( args[0], (char * const *)args );                        // should not return
     533        execvp( args[0], (char *const *)args );                         // should not return
    556534        perror( "CFA Translator error: execvp" );
    557535        exit( EXIT_FAILURE );
Note: See TracChangeset for help on using the changeset viewer.