source: driver/cc1.cc @ 3b4750d

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3b4750d was db62eef, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

formatting

  • Property mode set to 100644
File size: 18.2 KB
RevLine 
[b87a5ed]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
[6dc19544]7// cc1.cc --
[b87a5ed]8//
9// Author           : Peter A. Buhr
[51b7345]10// Created On       : Fri Aug 26 14:23:51 2005
11// Last Modified By : Peter A. Buhr
[db62eef]12// Last Modified On : Thu Aug 13 21:03:15 2020
13// Update Count     : 407
[51b7345]14//
15
16#include <iostream>
17using std::cerr;
18using std::endl;
19#include <string>
20using std::string;
[2c60af75]21#include <algorithm>                                                                    // find
[b87a5ed]22#include <cstdio>                                                                               // stderr, stdout, perror, fprintf
23#include <cstdlib>                                                                              // getenv, exit, mkstemp
24#include <unistd.h>                                                                             // execvp, fork, unlink
25#include <sys/wait.h>                                                                   // wait
[bbb1b35]26#include <fcntl.h>
27
[51b7345]28
[b87a5ed]29#include "config.h"                                                                             // configure info
[51b7345]30
31
32//#define __DEBUG_H__
33
34
[33c849e]35static string compiler_path( CFA_BACKEND_CC );                  // C compiler path/name
[2c60af75]36static bool CFA_flag = false;                                                   // -CFA flag
[bbb1b35]37static bool save_temps = false;                                                 // -save-temps flag
[2c60af75]38static string o_file;
[417a630]39static string bprefix;
[0163d3e]40static string lang;                                                                             // -x flag
[51b7345]41
[bbb1b35]42
[2c60af75]43static bool prefix( const string & arg, const string & pre ) {
[b87a5ed]44        return arg.substr( 0, pre.size() ) == pre;
[51b7345]45} // prefix
46
[2c60af75]47static void suffix( const string & arg, const char * args[], int & nargs ) {
48        enum { NumSuffixes = 3 };
49        static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" };
[dffaeac]50
51        size_t dot = arg.find_last_of( "." );
[b740f0b]52        if ( dot == string::npos ) return;
[2c60af75]53        const string * end = suffixes + NumSuffixes;
54        if ( std::find( suffixes, end, arg.substr( dot + 1 ) ) != end ) {
55                args[nargs++] = "-x";
56                args[nargs++] = "c";
57        } // if
[dffaeac]58} // suffix
59
[51b7345]60
[0bf5340]61static string __CFA_FLAGPREFIX__( "__CFA_FLAG" );               // "N__=" suffix
[51b7345]62
[2c60af75]63static void checkEnv1( const char * args[], int & nargs ) { // stage 1
64        extern char ** environ;
[51b7345]65
[2c60af75]66        for ( int i = 0; environ[i]; i += 1 ) {
[bbb1b35]67                string arg( environ[i] );
[dffaeac]68                #ifdef __DEBUG_H__
[2c60af75]69                cerr << "env arg:\"" << arg << "\"" << endl;
[dffaeac]70                #endif // __DEBUG_H__
[51b7345]71
[2c60af75]72                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
[0bf5340]73                        string val( arg.substr( arg.find_first_of( "=" ) + 1 ) );
[2c60af75]74                        if ( prefix( val, "-compiler=" ) ) {
75                                compiler_path = val.substr( 10 );
[0163d3e]76                        } else if ( prefix( val, "-x=" ) ) {
77                                lang = val.substr( 3 );
[2c60af75]78                        } // if
79                } // if
80        } // for
81} // checkEnv1
82
83
84static void checkEnv2( const char * args[], int & nargs ) { // stage 2
85        extern char ** environ;
86
87        for ( int i = 0; environ[i]; i += 1 ) {
[bbb1b35]88                string arg( environ[i] );
[dffaeac]89                #ifdef __DEBUG_H__
[2c60af75]90                cerr << "env arg:\"" << arg << "\"" << endl;
[dffaeac]91                #endif // __DEBUG_H__
[2c60af75]92
93                if ( prefix( arg, __CFA_FLAGPREFIX__ ) ) {
[0bf5340]94                        string val( arg.substr( arg.find_first_of( "=" ) + 1 ) );
[2c60af75]95                        if ( prefix( val, "-compiler=" ) ) {
96                                compiler_path = val.substr( 10 );
[bbb1b35]97                        } else if ( val == "-CFA" ) {
[2c60af75]98                                CFA_flag = true;
[bbb1b35]99                        } else if ( val == "-save-temps" ) {
100                                save_temps = true;
[2c60af75]101                        } else if ( prefix( val, "-o=" ) ) {            // output file for -CFA
102                                o_file = val.substr( 3 );
[417a630]103                        } else if ( prefix( val, "-B=" ) ) {            // location of cfa-cpp
104                                bprefix = val.substr( 3 );
[0163d3e]105                        } else if ( prefix( val, "-x=" ) ) {            // ignore
[33c849e]106                        } else {                                                                        // normal flag for cfa-cpp
[0bf5340]107                                args[nargs++] = ( *new string( arg.substr( arg.find_first_of( "=" ) + 1 ) ) ).c_str();
[2c60af75]108                        } // if
109                } // if
110        } // for
111} // checkEnv2
[51b7345]112
113
[0bf5340]114static char tmpname[] = P_tmpdir "/CFAXXXXXX.ifa";
[2c60af75]115static int tmpfilefd = -1;
116static bool startrm = false;
117
118static void rmtmpfile() {
[bbb1b35]119        if ( tmpfilefd == -1 ) return;                                          // RACE, file created ?
120
[2c60af75]121        startrm = true;                                                                         // RACE with C-c C-c
[b87a5ed]122        if ( unlink( tmpname ) == -1 ) {                                        // remove tmpname
[2c60af75]123                perror ( "CC1 Translator error: failed, unlink" );
[b87a5ed]124                exit( EXIT_FAILURE );
125        } // if
[2c60af75]126        tmpfilefd = -1;                                                                         // mark removed
[bdd516a]127} // rmtmpfile
128
129
[2c60af75]130static void sigTermHandler( int ) {                                             // C-c C-c
131        if ( startrm ) return;                                                          // return and let rmtmpfile finish, and then program finishes
132
[b87a5ed]133        if ( tmpfilefd != -1 ) {                                                        // RACE, file created ?
[2c60af75]134                rmtmpfile();                                                                    // remove tmpname
[b87a5ed]135        } // if
[2c60af75]136        exit( EXIT_FAILURE );                                                           // terminate
[bdd516a]137} // sigTermHandler
138
139
[2c60af75]140static void Stage1( const int argc, const char * const argv[] ) {
[b87a5ed]141        int code;
142        string arg;
[51b7345]143
[bbb1b35]144        const char * cpp_in = nullptr;
145        const char * cpp_out = nullptr;
[51b7345]146
[b87a5ed]147        bool cpp_flag = false;
[2c60af75]148        bool o_flag = false;
[51b7345]149
[bbb1b35]150        const char * args[argc + 100];                                          // leave space for 100 additional cpp command line values
[b87a5ed]151        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
[bdd516a]152
[dffaeac]153        #ifdef __DEBUG_H__
[7b937575]154        cerr << "Stage1" << endl;
[dffaeac]155        #endif // __DEBUG_H__
[2c60af75]156        checkEnv1( args, nargs );                                                       // arguments passed via environment variables
[bec4d24]157        #ifdef __DEBUG_H__
158        for ( int i = 1; i < argc; i += 1 ) {
159                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
160        } // for
161        #endif // __DEBUG_H__
[7b937575]162
[b87a5ed]163        // process all the arguments
[51b7345]164
[bec4d24]165        for ( int i = 1; i < argc; i += 1 ) {
[b87a5ed]166                arg = argv[i];
167                if ( prefix( arg, "-" ) ) {
168                        // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
169
170                        if ( arg == "-quiet" ) {
171                        } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {
[db62eef]172                                i += 1;                                                                 // and argument
[b87a5ed]173                        } else if ( prefix( arg, "-A" ) ) {
174                        } else if ( prefix( arg, "-D__GNU" ) ) {
175                                //********
176                                // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!
177                                //********
178                        } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {
[db62eef]179                                i += 1;                                                                 // and argument
[b87a5ed]180
181                                // strip flags controlling cpp step
182
183                        } else if ( arg == "-D__CPP__" ) {
184                                cpp_flag = true;
185                        } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {
[db62eef]186                                i += 1;                                                                 // and argument
[b87a5ed]187                                cpp_flag = true;
188
[2c60af75]189                                // all other flags
[b87a5ed]190
191                        } else if ( arg == "-o" ) {
192                                i += 1;
[2c60af75]193                                o_flag = true;
194                                cpp_out = argv[i];
[b87a5ed]195                        } else {
[db62eef]196                                args[nargs++] = argv[i];                                // pass flag along
[b87a5ed]197                                // CPP flags with an argument
[f8b6d921]198                                if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
[b87a5ed]199                                         arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
200                                         arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
201                                        i += 1;
[db62eef]202                                        args[nargs++] = argv[i];                        // pass argument along
[dffaeac]203                                        #ifdef __DEBUG_H__
[b87a5ed]204                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[dffaeac]205                                        #endif // __DEBUG_H__
[b87a5ed]206                                } else if ( arg == "-MD" || arg == "-MMD" ) {
[db62eef]207                                        // gcc frontend generates the dependency file-name after the -MD/-MMD flag, but it is necessary to
208                                        // prefix that file name with -MF.
[2c60af75]209                                        args[nargs++] = "-MF";                          // insert before file
[b87a5ed]210                                        i += 1;
[db62eef]211                                        args[nargs++] = argv[i];                        // pass argument along
[dffaeac]212                                        #ifdef __DEBUG_H__
[b87a5ed]213                                        cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[dffaeac]214                                        #endif // __DEBUG_H__
[b87a5ed]215                                } // if
216                        } // if
217                } else {                                                                                // obtain input and possibly output files
[bbb1b35]218                        if ( cpp_in == nullptr ) {
[b87a5ed]219                                cpp_in = argv[i];
[dffaeac]220                                #ifdef __DEBUG_H__
[b87a5ed]221                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
[dffaeac]222                                #endif // __DEBUG_H__
[bbb1b35]223                        } else if ( cpp_out == nullptr ) {
[b87a5ed]224                                cpp_out = argv[i];
[dffaeac]225                                #ifdef __DEBUG_H__
[b87a5ed]226                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
[dffaeac]227                                #endif // __DEBUG_H__
[b87a5ed]228                        } else {
229                                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
230                                exit( EXIT_FAILURE );
231                        } // if
232                } // if
233        } // for
234
[dffaeac]235        #ifdef __DEBUG_H__
[b87a5ed]236        cerr << "args:";
[bec4d24]237        for ( int i = 1; i < nargs; i += 1 ) {
[b87a5ed]238                cerr << " " << args[i];
239        } // for
[bbb1b35]240        if ( cpp_in != nullptr ) cerr << " " << cpp_in;
241        if ( cpp_out != nullptr ) cerr << " " << cpp_out;
[b87a5ed]242        cerr << endl;
[dffaeac]243        #endif // __DEBUG_H__
[b87a5ed]244
[bbb1b35]245        if ( cpp_in == nullptr ) {
[51b7345]246                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
247                exit( EXIT_FAILURE );
248        } // if
[b87a5ed]249
250        if ( cpp_flag ) {
251                // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard
252                // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
253
[2c60af75]254                args[0] = compiler_path.c_str();
[0163d3e]255                if ( lang.size() == 0 ) {
256                        suffix( cpp_in, args, nargs );                          // check suffix
257                } else {
258                        args[nargs++] = "-x";
259                        args[nargs++] = ( *new string( lang.c_str() ) ).c_str();
260                } // if
[2c60af75]261                args[nargs++] = cpp_in;
262                if ( o_flag ) {                                                                 // location for output
263                        args[nargs++] = "-o";
[b87a5ed]264                } // if
[2c60af75]265                args[nargs++] = cpp_out;
[bbb1b35]266                args[nargs] = nullptr;                                                  // terminate argument list
[51b7345]267
[dffaeac]268                #ifdef __DEBUG_H__
[b87a5ed]269                cerr << "nargs: " << nargs << endl;
[bbb1b35]270                for ( int i = 0; args[i] != nullptr; i += 1 ) {
[b87a5ed]271                        cerr << args[i] << " ";
272                } // for
273                cerr << endl;
[dffaeac]274                #endif // __DEBUG_H__
[51b7345]275
[bbb1b35]276                execvp( args[0], (char * const *)args );                // should not return
[2c60af75]277                perror( "CC1 Translator error: stage 1, execvp" );
[b87a5ed]278                exit( EXIT_FAILURE );
[51b7345]279        } // if
280
[2c60af75]281        // Run the C preprocessor and save the output in the given file.
[51b7345]282
[db62eef]283        if ( fork() == 0 ) {                                                            // child process ?
[b87a5ed]284                // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects
285                // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
286                // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
[bbb1b35]287                if ( freopen( cpp_out, "w", stdout ) == nullptr ) { // redirect stdout to output file
[2c60af75]288                        perror( "CC1 Translator error: stage 1, freopen" );
[b87a5ed]289                        exit( EXIT_FAILURE );
290                } // if
[51b7345]291
[2c60af75]292                args[0] = compiler_path.c_str();
[0163d3e]293                if ( lang.size() == 0 ) {
294                        suffix( cpp_in, args, nargs );                          // check suffix
295                } else {
296                        args[nargs++] = "-x";
297                        args[nargs++] = ( *new string( lang.c_str() ) ).c_str();
298                } // if
[2c60af75]299                args[nargs++] = cpp_in;                                                 // input to cpp
[bbb1b35]300                args[nargs] = nullptr;                                                  // terminate argument list
[51b7345]301
[dffaeac]302                #ifdef __DEBUG_H__
[b87a5ed]303                cerr << "cpp nargs: " << nargs << endl;
[bbb1b35]304                for ( int i = 0; args[i] != nullptr; i += 1 ) {
[b87a5ed]305                        cerr << args[i] << " ";
306                } // for
307                cerr << endl;
[dffaeac]308                #endif // __DEBUG_H__
[51b7345]309
[bbb1b35]310                execvp( args[0], (char * const *)args );                // should not return
[5a43ab8]311                perror( "CC1 Translator error: stage 1 cpp, execvp" );
312                cerr << " invoked " << args[0] << endl;
[b87a5ed]313                exit( EXIT_FAILURE );
[51b7345]314        } // if
315
[b87a5ed]316        wait( &code );                                                                          // wait for child to finish
[51b7345]317
[dffaeac]318        #ifdef __DEBUG_H__
[b87a5ed]319        cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
[dffaeac]320        #endif // __DEBUG_H__
[51b7345]321
[b87a5ed]322        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
[2c60af75]323                cerr << "CC1 Translator error: stage 1, child failed " << WTERMSIG(code) << endl;
[b87a5ed]324                exit( EXIT_FAILURE );
325        } // if
[51b7345]326
[2c60af75]327        exit( WEXITSTATUS(code) );                                                      // bad cpp result stops top-level gcc
[51b7345]328} // Stage1
329
330
[2c60af75]331static void Stage2( const int argc, const char * const * argv ) {
332        int code;
[b87a5ed]333        string arg;
[51b7345]334
[bbb1b35]335        const char * cpp_in = nullptr;
336        const char * cpp_out = nullptr;
[51b7345]337
[bbb1b35]338        const char * args[argc + 100];                                          // leave space for 100 additional cfa command line values
[b87a5ed]339        int nargs = 1;                                                                          // number of arguments in args list; 0 => command name
[bbb1b35]340        const char * cargs[20];                                                         // leave space for 20 additional cfa-cpp command line values
[2c60af75]341        int ncargs = 1;                                                                         // 0 => command name
[51b7345]342
[dffaeac]343        #ifdef __DEBUG_H__
[7b937575]344        cerr << "Stage2" << endl;
[dffaeac]345        #endif // __DEBUG_H__
[2c60af75]346        checkEnv2( cargs, ncargs );                                                     // arguments passed via environment variables
[bec4d24]347        #ifdef __DEBUG_H__
348        for ( int i = 1; i < argc; i += 1 ) {
349                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
350        } // for
351        #endif // __DEBUG_H__
[7b937575]352
[49d3128]353        enum {
354                Color_Auto   = 0,
355                Color_Always = 1,
356                Color_Never  = 2,
357        } color_arg = Color_Auto;
358
359        const char * color_names[3] = { "--colors=auto", "--colors=always", "--colors=never" };
360
[b87a5ed]361        // process all the arguments
[51b7345]362
[bec4d24]363        for ( int i = 1; i < argc; i += 1 ) {
[b87a5ed]364                arg = argv[i];
365                if ( prefix( arg, "-" ) ) {
366                        // strip inappropriate flags
[51b7345]367
[49d3128]368                        if ( prefix( arg, "-fdiagnostics-color=" ) ) {
369                                string choice = arg.substr(20);
370                                     if(choice == "always") color_arg = Color_Always;
371                                else if(choice == "never" ) color_arg = Color_Never;
372                                else if(choice == "auto"  ) color_arg = Color_Auto;
373                        } else if ( arg == "-fno-diagnostics-color" ) {
374                                color_arg = Color_Auto;
375                        }
376
[b87a5ed]377                        if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
[dffaeac]378                                // Currently CFA does not suppose precompiled .h files.
379                                prefix( arg, "--output-pch" ) ) {
[51b7345]380
[b87a5ed]381                                // strip inappropriate flags with an argument
[51b7345]382
[b87a5ed]383                        } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) {
384                                i += 1;
[dffaeac]385                                #ifdef __DEBUG_H__
[b87a5ed]386                                cerr << "arg:\"" << argv[i] << "\"" << endl;
[dffaeac]387                                #endif // __DEBUG_H__
[51b7345]388
[b87a5ed]389                                // all other flags
[51b7345]390
[b87a5ed]391                        } else {
[db62eef]392                                args[nargs++] = argv[i];                                // pass flag along
[b87a5ed]393                                if ( arg == "-o" ) {
394                                        i += 1;
[2c60af75]395                                        cpp_out = argv[i];
[db62eef]396                                        args[nargs++] = argv[i];                        // pass argument along
[68bceeb]397                                        #ifdef __DEBUG_H__
[b87a5ed]398                                        cerr << "arg:\"" << argv[i] << "\"" << endl;
[68bceeb]399                                        #endif // __DEBUG_H__
[b87a5ed]400                                } // if
401                        } // if
402                } else {                                                                                // obtain input and possibly output files
[bbb1b35]403                        if ( cpp_in == nullptr ) {
[b87a5ed]404                                cpp_in = argv[i];
[dffaeac]405                                #ifdef __DEBUG_H__
[b87a5ed]406                                cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
[dffaeac]407                                #endif // __DEBUG_H__
[bbb1b35]408                        } else if ( cpp_out == nullptr ) {
[2c60af75]409                                cpp_out = argv[i];
410                                #ifdef __DEBUG_H__
411                                cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
412                                #endif // __DEBUG_H__
[b87a5ed]413                        } else {
[bbb1b35]414                                cerr << "Usage: " << argv[0] << " more than two files specified" << endl;
[b87a5ed]415                                exit( EXIT_FAILURE );
416                        } // if
417                } // if
418        } // for
[51b7345]419
[bbb1b35]420        if ( cpp_in == nullptr ) {
421                cerr << "Usage: " << argv[0] << " missing input file" << endl;
422                exit( EXIT_FAILURE );
423        } // if
424        if ( cpp_out == nullptr ) {
425                cerr << "Usage: " << argv[0] << " missing output file" << endl;
426                exit( EXIT_FAILURE );
427        } // if
428
[2c60af75]429        // Create a temporary file, if needed, to store output of the cfa-cpp preprocessor. Cannot be created in forked
430        // process because variables tmpname and tmpfilefd are cloned.
431
[bbb1b35]432        string cfa_cpp_out;
433
434        if ( ! CFA_flag ) {                                                                     // run compiler ?
435                if ( save_temps ) {
436                        cfa_cpp_out = cpp_in;
437                        size_t dot = cfa_cpp_out.find_last_of( "." );
438                        if ( dot == string::npos ) {
439                                cerr << "CC1 Translator error: stage 2, bad file name " << endl;
440                                exit( EXIT_FAILURE );
441                        } // if
[2c60af75]442
[bbb1b35]443                        cfa_cpp_out = cfa_cpp_out.substr( 0, dot ) + ".ifa";
444                        if ( creat( cfa_cpp_out.c_str(), 0666 ) == -1 ) {
445                                perror( "CC1 Translator error: stage 2, creat" );
446                                exit( EXIT_FAILURE );
447                        } // if
448                } else {
[0bf5340]449                        tmpfilefd = mkstemps( tmpname, 4 );
[bbb1b35]450                        if ( tmpfilefd == -1 ) {
451                                perror( "CC1 Translator error: stage 2, mkstemp" );
452                                exit( EXIT_FAILURE );
453                        } // if
454                        cfa_cpp_out = tmpname;
455                } // if
[2c60af75]456                #ifdef __DEBUG_H__
[bbb1b35]457                cerr << "cfa_cpp_out: " << cfa_cpp_out << endl;
[2c60af75]458                #endif // __DEBUG_H__
459        } // if
460
461        // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
462        // output.  Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
463
464        if ( fork() == 0 ) {                                                            // child runs CFA
[7c8246d]465                cargs[0] = ( *new string( bprefix + "cfa-cpp" ) ).c_str();
[2c60af75]466                cargs[ncargs++] = cpp_in;
467
468                if ( CFA_flag ) {                                                               // run cfa-cpp ?
469                        if ( o_file.size() != 0 ) {                                     // location for output
470                                cargs[ncargs++] = ( *new string( o_file.c_str() ) ).c_str();
471                        } // if
472                } else {
[bbb1b35]473                        cargs[ncargs++] = cfa_cpp_out.c_str();
[2c60af75]474                } // if
[49d3128]475
476                cargs[ncargs++] = color_names[color_arg];
477
[5a43ab8]478                cargs[ncargs] = nullptr;                                                // terminate argument list
[2c60af75]479
480                #ifdef __DEBUG_H__
[bbb1b35]481                for ( int i = 0; cargs[i] != nullptr; i += 1 ) {
[2c60af75]482                        cerr << cargs[i] << " ";
483                } // for
484                cerr << endl;
485                #endif // __DEBUG_H__
486
487                execvp( cargs[0], (char * const *)cargs );              // should not return
[5a43ab8]488                perror( "CC1 Translator error: stage 2 cfa-cpp, execvp" );
489                cerr << " invoked " << cargs[0] << endl;
[2c60af75]490                exit( EXIT_FAILURE );
491        } // if
492
493        wait( &code );                                                                          // wait for child to finish
494
495        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
496                rmtmpfile();                                                                    // remove tmpname
497                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
498                exit( EXIT_FAILURE );
499        } // if
500
501        if ( CFA_flag ) {                                                                       // no tmpfile created
502                exit( WEXITSTATUS( code ) );                                    // stop regardless of success or failure
503        } // if
504
505        #ifdef __DEBUG_H__
506        cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
507        #endif // __DEBUG_H__
508
509        if ( WEXITSTATUS(code) ) {                                                      // child error ?
510                rmtmpfile();                                                                    // remove tmpname
511                exit( WEXITSTATUS( code ) );                                    // do not continue
512        } // if
513
[dffaeac]514        #ifdef __DEBUG_H__
[b87a5ed]515        cerr << "args:";
[bec4d24]516        for ( int i = 1; i < nargs; i += 1 ) {
[b87a5ed]517                cerr << " " << args[i];
518        } // for
[bbb1b35]519        cerr << " " << cpp_in << endl;
[dffaeac]520        #endif // __DEBUG_H__
[51b7345]521
[2c60af75]522        if ( fork() == 0 ) {                                                            // child runs CFA
523                args[0] = compiler_path.c_str();
524                args[nargs++] = "-S";                                                   // only compile and put assembler output in specified file
[0bf5340]525                args[nargs++] = "-x";
526                args[nargs++] = "cpp-output";
527
[bbb1b35]528                args[nargs++] = cfa_cpp_out.c_str();
529                args[nargs] = nullptr;                                                  // terminate argument list
[2c60af75]530
531                #ifdef __DEBUG_H__
532                cerr << "stage2 nargs: " << nargs << endl;
[bbb1b35]533                for ( int i = 0; args[i] != nullptr; i += 1 ) {
[2c60af75]534                        cerr << args[i] << " ";
535                } // for
536                cerr << endl;
537                #endif // __DEBUG_H__
538
539                execvp( args[0], (char * const *)args );                // should not return
[5a43ab8]540                perror( "CC1 Translator error: stage 2 cc1, execvp" );
[c5c0148]541                cerr << " invoked " << args[0] << endl;
[2c60af75]542                exit( EXIT_FAILURE );                                                   // tell gcc not to go any further
543        } // if
544
545        wait( &code );                                                                          // wait for child to finish
[0bf5340]546        rmtmpfile();                                                                            // remove tmpname
[2c60af75]547
548        if ( WIFSIGNALED(code) ) {                                                      // child failed ?
549                cerr << "CC1 Translator error: stage 2, child failed " << WTERMSIG(code) << endl;
550                exit( EXIT_FAILURE );
551        } // if
[51b7345]552
[dffaeac]553        #ifdef __DEBUG_H__
[2c60af75]554        cerr << "return code from gcc cc1:" << WEXITSTATUS(code) << endl;
[dffaeac]555        #endif // __DEBUG_H__
[51b7345]556
[2c60af75]557        exit( WEXITSTATUS( code ) );                                            // stop regardless of success or failure
[51b7345]558} // Stage2
559
560
[2c60af75]561// This program is called twice because of the -no-integrated-cpp. The calls are differentiated by the first
562// command-line argument. The first call replaces the traditional cpp pass to preprocess the C program. The second call
563// is to the compiler, which is broken into two steps: preprocess again with cfa-cpp and then call gcc to compile the
564// doubly preprocessed program.
565
[b3c36f4]566int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
[dffaeac]567        #ifdef __DEBUG_H__
[bbb1b35]568        for ( int i = 0; env[i] != nullptr; i += 1 ) {
[b87a5ed]569                cerr << env[i] << endl;
570        } // for
[dffaeac]571        #endif // __DEBUG_H__
[51b7345]572
[2c60af75]573        signal( SIGINT,  sigTermHandler );
574        signal( SIGTERM, sigTermHandler );
575
[bbb1b35]576        string arg( argv[1] );
[51b7345]577
[b87a5ed]578        // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
[51b7345]579
[b87a5ed]580        if ( arg == "-E" ) {
581                Stage1( argc, argv );
582        } else if ( arg == "-fpreprocessed" ) {
583                Stage2( argc, argv );
584        } else {
585                cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
586                exit( EXIT_FAILURE );
587        } // if
[51b7345]588} // main
589
590// Local Variables: //
[b87a5ed]591// tab-width: 4 //
592// mode: c++ //
[51b7345]593// compile-command: "make install" //
594// End: //
Note: See TracBrowser for help on using the repository browser.