source: driver/cc1.cc @ 36de20d

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

formatting, update how bprefix is handled from the command line versus default value

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