Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • driver/cfa.cc

    r9b34766 r406970b1  
    1010// Created On       : Tue Aug 20 13:44:49 2002
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Aug 18 16:40:22 2020
    13 // Update Count     : 435
     12// Last Modified On : Wed Sep  2 17:59:20 2020
     13// Update Count     : 438
    1414//
    1515
    1616#include <iostream>
    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 
     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
    2524#include <sys/types.h>
    2625#include <sys/stat.h>
     
    3433using std::to_string;
    3534
    36 // #define __DEBUG_H__
    37 
    38 // "N__=" suffix
    39 static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );
    40 
    41 void Putenv( char * argv[], string arg ) {
     35//#define __DEBUG_H__
     36
     37#define xstr(s) str(s)
     38#define str(s) #s
     39
     40static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );               // "__CFA_FLAG__=" suffix
     41
     42static void Putenv( char * argv[], string arg ) {
    4243        // environment variables must have unique names
    4344        static int flags = 0;
     
    4950} // Putenv
    5051
    51 // check if string has prefix
    52 bool prefix( const string & arg, const string & pre ) {
     52static bool prefix( const string & arg, const string & pre ) { // check if string has prefix
    5353        return arg.substr( 0, pre.size() ) == pre;
    5454} // prefix
    5555
    56 inline bool ends_with(const string & str, const string & sfix) {
     56static inline 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 bool suffix( const string & arg ) {
     62static bool suffix( const string & arg ) {
    6363        enum { NumSuffixes = 3 };
    6464        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
     
    7070} // suffix
    7171
    72 
    7372static inline bool dirExists( const string & path ) {   // check if directory exists
    7473    struct stat info;
     
    7978static inline string dir(const string & path) {
    8079        return path.substr(0, path.find_last_of('/'));
    81 }
     80} // dir
    8281
    8382// Different path modes
     
    118117}
    119118
    120 
    121 #define xstr(s) str(s)
    122 #define str(s) #s
    123119
    124120int main( int argc, char * argv[] ) {
     
    158154        PathMode path = FromProc();
    159155
    160         const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
     156        const char * args[argc + 100];                                          // cfa command line values, plus some space for additional flags
    161157        int sargs = 1;                                                                          // starting location for arguments in args list
    162158        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
    163159
    164         const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
     160        const char * libs[argc + 20];                                           // non-user libraries must come separately, plus some added libraries and flags
    165161        int nlibs = 0;
    166162
     
    180176
    181177                        if ( arg == "-Xlinker" || arg == "-o" ) {
    182                                 args[nargs++] = argv[i];                                // pass argument along
     178                                args[nargs++] = argv[i];                                // pass flag along
    183179                                i += 1;
    184180                                if ( i == argc ) continue;                              // next argument available ?
    185181                                args[nargs++] = argv[i];                                // pass argument along
    186182                                if ( arg == "-o" ) o_file = i;                  // remember file
    187                         } else if ( strncmp(arg.c_str(), "-XCFA", 5) == 0 ) {                           // CFA pass through
    188                                 if(arg.size() == 5) {
     183
     184                                // CFA specific arguments
     185
     186                        } else if ( strncmp(arg.c_str(), "-XCFA", 5) == 0 ) { // CFA pass through
     187                                if ( arg.size() == 5 ) {
    189188                                        i += 1;
    190                                         if ( i == argc ) continue;                              // next argument available ?
     189                                        if ( i == argc ) continue;                      // next argument available ?
    191190                                        Putenv( argv, argv[i] );
    192 
    193                                         // CFA specific arguments
    194                                 }
    195                                 else if(arg[5] == ',') {
     191                                } else if ( arg[5] == ',' ) {                   // CFA specific arguments
    196192                                        Putenv( argv, argv[i] + 6 );
    197 
    198                                         // CFA specific arguments
    199                                 }
    200                                 else {
     193                                } else {                                                                // CFA specific arguments
    201194                                        args[nargs++] = argv[i];
    202                                 }
    203 
     195                                } // if
    204196                        } else if ( arg == "-CFA" ) {
    205197                                CFA_flag = true;                                                // strip the -CFA flag
     
    210202                        } else if ( arg == "-nodebug" ) {
    211203                                debug = false;                                                  // strip the nodebug flag
    212                         } else if ( arg == "-nolib" ) {
    213                                 nolib = true;                                                   // strip the nodebug flag
    214204                        } else if ( arg == "-quiet" ) {
    215205                                quiet = true;                                                   // strip the quiet flag
    216206                        } else if ( arg == "-noquiet" ) {
    217207                                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
    218212                        } else if ( arg == "-help" ) {
    219213                                help = true;                                                    // strip the help flag
    220214                        } else if ( arg == "-nohelp" ) {
    221215                                help = false;                                                   // strip the nohelp flag
    222                         } else if ( arg == "-no-include-stdhdr" ) {
    223                                 noincstd_flag = true;                                   // strip the no-include-stdhdr flag
    224216                        } else if ( arg == "-cfalib") {
    225217                                compiling_libs = true;
     
    235227                        } else if ( arg == "-v" ) {
    236228                                verbose = true;                                                 // verbosity required
    237                                 args[nargs++] = argv[i];                                // pass argument along
     229                                args[nargs++] = argv[i];                                // pass flag along
    238230                        } else if ( arg == "-g" ) {
    239231                                debugging = true;                                               // symbolic debugging required
    240                                 args[nargs++] = argv[i];                                // pass argument along
     232                                args[nargs++] = argv[i];                                // pass flag along
    241233                        } else if ( arg == "-save-temps" ) {
    242                                 args[nargs++] = argv[i];                                // pass argument along
     234                                args[nargs++] = argv[i];                                // pass flag along
    243235                                Putenv( argv, arg );                                    // save cfa-cpp output
    244236                        } else if ( prefix( arg, "-x" ) ) {                     // file suffix ?
    245237                                string lang;
    246                                 args[nargs++] = argv[i];                                // pass argument along
     238                                args[nargs++] = argv[i];                                // pass flag along
    247239                                if ( arg.length() == 2 ) {                              // separate argument ?
    248240                                        i += 1;
     
    261253                        } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
    262254                                std_flag = true;                                                // -std=XX provided
    263                                 args[nargs++] = argv[i];                                // pass argument along
     255                                args[nargs++] = argv[i];                                // pass flag along
    264256                        } else if ( arg == "-w" ) {
    265                                 args[nargs++] = argv[i];                                // pass argument along
     257                                args[nargs++] = argv[i];                                // pass flag along
    266258                                Putenv( argv, arg );
    267259                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
    268260                                if ( arg == "-Werror" || arg == "-Wall" ) {
    269                                         args[nargs++] = argv[i];                        // pass argument along
     261                                        args[nargs++] = argv[i];                        // pass flag along
    270262                                        Putenv( argv, argv[i] );
    271263                                } else {
     
    281273                                bprefix = arg.substr(2);                                // strip the -B flag
    282274                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
    283                                 args[nargs++] = argv[i];                                // pass argument along
     275                                args[nargs++] = argv[i];                                // pass flag along
    284276                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
    285277                                        cpp_flag = true;                                        // cpp only
    286278                                } // if
    287279                                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
    288286                        } else if ( arg[1] == 'l' ) {
    289287                                // if the user specifies a library, load it after user code
     
    337335        string libbase;
    338336        switch(path) {
    339         case Installed:
     337          case Installed:
    340338                args[nargs++] = "-I" CFA_INCDIR;
    341339                // do not use during build
     
    347345                libbase = CFA_LIBDIR;
    348346                break;
    349         case BuildTree:
    350         case Distributed:
     347          case BuildTree:
     348          case Distributed:
    351349                args[nargs++] = "-I" TOP_SRCDIR "libcfa/src";
    352350                // do not use during build
     
    382380        string libdir = libbase + arch + "-" + config;
    383381
    384         if (path != Distributed) {
     382        if ( path != Distributed ) {
    385383                if ( ! nolib && ! dirExists( libdir ) ) {
    386384                        cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
     
    402400        string preludedir;
    403401        switch(path) {
    404         case Installed   : preludedir = libdir; break;
    405         case BuildTree   : preludedir = libdir + "/prelude"; break;
    406         case Distributed : preludedir = dir(argv[0]); break;
    407         }
     402          case Installed   : preludedir = libdir; break;
     403          case BuildTree   : preludedir = libdir + "/prelude"; break;
     404          case Distributed : preludedir = dir(argv[0]); break;
     405        } // switch
    408406
    409407        Putenv( argv, "--prelude-dir=" + preludedir );
     
    439437                #endif // __x86_64__
    440438                args[nargs++] = "-ldl";
    441                 args[nargs++] = "-lrt";
    442439                args[nargs++] = "-lm";
    443440        } // if
     
    477474        if ( bprefix.length() == 0 ) {
    478475                switch(path) {
    479                 case Installed   : bprefix = installlibdir; break;
    480                 case BuildTree   : bprefix = srcdriverdir ; break;
    481                 case Distributed : bprefix = dir(argv[0]) ; break;
    482                 }
    483                 if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/';
    484                 Putenv( argv, string("-B=") + bprefix );
    485         } // if
     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 );
    486483
    487484        args[nargs++] = "-Xlinker";                                                     // used by backtrace
     
    505502                args[nargs++] = "-Wno-cast-function-type";
    506503                #endif // HAVE_CAST_FUNCTION_TYPE
    507                 if ( ! std_flag ) {                                                             // default c11, if none specified
    508                         args[nargs++] = "-std=gnu11";
     504                if ( ! std_flag && ! x_flag ) {
     505                        args[nargs++] = "-std=gnu11";                           // default c11, if none specified
    509506                } // if
    510507                args[nargs++] = "-fgnu89-inline";
     
    556553        // execute the command and return the result
    557554
    558         execvp( args[0], (char *const *)args );                         // should not return
     555        execvp( args[0], (char * const *)args );                        // should not return
    559556        perror( "CFA Translator error: execvp" );
    560557        exit( EXIT_FAILURE );
Note: See TracChangeset for help on using the changeset viewer.