source: driver/cc1.cc @ 33c849e

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 33c849e was 33c849e, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

temporarily use library path versus bprefix to locate cfa-cpp

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