source: driver/cc1.cc @ 3d5701e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 3d5701e was 49d3128, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

cfa command now properly supports -fdiagnostics-color and -fno-diagnostics-color

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