source: src/driver/cfa.cc @ e3215c5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since e3215c5 was e3215c5, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add gcc -x flag

  • Property mode set to 100644
File size: 15.2 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// cfa.cc --
8//
9// Author           : Peter A. Buhr
10// Created On       : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Sat May 12 17:07:53 2018
13// Update Count     : 240
14//
15
16#include <iostream>
17#include <cstdio>                                                                               // perror
18#include <cstdlib>                                                                              // putenv, exit
19#include <unistd.h>                                                                             // execvp
20#include <string>                                                                               // STL version
21#include <string.h>                                                                             // strcmp
22
23#include "Common/SemanticError.h"
24#include "config.h"                                                                             // configure info
25
26using std::cerr;
27using std::endl;
28using std::string;
29using std::to_string;
30
31
32//#define __DEBUG_H__
33
34
35bool prefix( string arg, string pre ) {
36        return arg.substr( 0, pre.size() ) == pre;
37} // prefix
38
39
40void shuffle( const char *args[], int S, int E, int N ) {
41        // S & E index 1 passed the end so adjust with -1
42#ifdef __DEBUG_H__
43        cerr << "shuffle:" << S << " " << E << " " << N << endl;
44#endif // __DEBUG_H__
45        for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
46#ifdef __DEBUG_H__
47                cerr << "\t" << j << " " << j-N << endl;
48#endif // __DEBUG_H__
49                args[j] = args[j-N];
50        } // for
51} // shuffle
52
53
54#define str(s) #s
55
56int main( int argc, char *argv[] ) {
57        string Version( CFA_VERSION_LONG );                                     // current version number from CONFIG
58        string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
59
60        string installincdir( CFA_INCDIR );                                     // fixed location of include files
61        string installlibdir( CFA_LIBDIR );                                     // fixed location of cc1 and cfa-cpp commands
62
63        string heading;                                                                         // banner printed at start of cfa compilation
64        string arg;                                                                                     // current command-line argument during command-line parsing
65        string Bprefix;                                                                         // path where gcc looks for compiler command steps
66        string langstd;                                                                         // language standard
67
68        string compiler_path( CFA_BACKEND_CC );                         // path/name of C compiler
69        string compiler_name;                                                           // name of C compiler
70
71        bool nonoptarg = false;                                                         // indicates non-option argument specified
72        bool link = true;                                                                       // linking as well as compiling
73        bool verbose = false;                                                           // -v flag
74        bool quiet = false;                                                                     // -quiet flag
75        bool debug = true;                                                                      // -debug flag
76        bool help = false;                                                                      // -help flag
77        bool CFA_flag = false;                                                          // -CFA flag
78        bool cpp_flag = false;                                                          // -E or -M flag, preprocessor only
79        bool std_flag = false;                                                          // -std= flag
80        bool noincstd_flag = false;                                                     // -no-include-stdhdr= flag
81        bool debugging __attribute(( unused )) = false;         // -g flag
82
83        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
84        int sargs = 1;                                                                          // starting location for arguments in args list
85        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
86
87        const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
88        int nlibs = 0;
89
90#ifdef __DEBUG_H__
91        cerr << "CFA:" << endl;
92#endif // __DEBUG_H__
93
94        // process command-line arguments
95
96        for ( int i = 1; i < argc; i += 1 ) {
97#ifdef __DEBUG_H__
98                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
99#endif // __DEBUG_H__
100                arg = argv[i];                                                                  // convert to string value
101#ifdef __DEBUG_H__
102                cerr << "arg:\"" << arg << "\"" << endl;
103#endif // __DEBUG_H__
104                if ( prefix( arg, "-" ) ) {
105                        // pass through arguments
106
107                        if ( arg == "-Xlinker" || arg == "-o" ) {
108                                args[nargs] = argv[i];                                  // pass the argument along
109                                nargs += 1;
110                                i += 1;
111                                if ( i == argc ) continue;                              // next argument available ?
112                                args[nargs] = argv[i];                                  // pass the argument along
113                                nargs += 1;
114                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
115                                i += 1;
116                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
117                                nargs += 1;
118
119                                // CFA specific arguments
120
121                        } else if ( arg == "-CFA" ) {
122                                CFA_flag = true;                                                // strip the -CFA flag
123                                link = false;
124                                args[nargs] = "-E";                                             // replace the argument with -E
125                                nargs += 1;
126                        } else if ( arg == "-debug" ) {
127                                debug = true;                                                   // strip the debug flag
128                        } else if ( arg == "-nodebug" ) {
129                                debug = false;                                                  // strip the nodebug flag
130                        } else if ( arg == "-quiet" ) {
131                                quiet = true;                                                   // strip the quiet flag
132                        } else if ( arg == "-noquiet" ) {
133                                quiet = false;                                                  // strip the noquiet flag
134                        } else if ( arg == "-help" ) {
135                                help = true;                                                    // strip the help flag
136                        } else if ( arg == "-nohelp" ) {
137                                help = false;                                                   // strip the nohelp flag
138                        } else if ( arg == "-no-include-stdhdr" ) {
139                                noincstd_flag = true;                                   // strip the no-include-stdhdr flag
140                        } else if ( arg == "-compiler" ) {
141                                // use the user specified compiler
142                                i += 1;
143                                if ( i == argc ) continue;                              // next argument available ?
144                                compiler_path = argv[i];
145                                if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
146                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
147                                        exit( EXIT_FAILURE );
148                                } // if
149
150                                // C specific arguments
151
152                        } else if ( arg == "-v" ) {
153                                verbose = true;                                                 // verbosity required
154                                args[nargs] = argv[i];                                  // pass the argument along
155                                nargs += 1;
156                        } else if ( arg == "-g" ) {
157                                debugging = true;                                               // symbolic debugging required
158                                args[nargs] = argv[i];                                  // pass the argument along
159                                nargs += 1;
160                        } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
161                                std_flag = true;                                                // -std=XX provided
162                                args[nargs] = argv[i];                                  // pass the argument along
163                                nargs += 1;
164                        } else if ( arg == "-x" ) {                                     // lost so force along
165                                args[nargs] = argv[i];                                  // pass the argument along
166                                nargs += 1;
167                                i += 1;                                                                 // advance to argument
168                                args[nargs] = argv[i];                                  // pass the argument along
169                                nargs += 1;
170                                args[nargs] = ( *new string( string("-D__GCC_X__=") + argv[i] ) ).c_str(); // add the argument for -x
171                                nargs += 1;
172                        } else if ( prefix( arg, "-x" ) ) {                     // lost so force along
173                                args[nargs] = argv[i];                                  // pass the argument along
174                                nargs += 1;
175                                args[nargs] = ( *new string( string("-D__GCC_X__=") + arg.substr(2) ) ).c_str(); // add the argument for -x
176                                nargs += 1;
177                        } else if ( arg == "-w" ) {
178                                args[nargs] = argv[i];                                  // pass the argument along
179                                nargs += 1;
180                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
181                                nargs += 1;
182                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
183                                if ( arg == "-Werror" || arg == "-Wall" ) {
184                                        args[nargs] = argv[i];                          // pass the argument along
185                                        nargs += 1;
186                                        args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
187                                        nargs += 1;
188                                } else {
189                                        unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
190                                        args[nargs] = argv[i];                          // conditionally pass the argument along
191                                        const char * warning = argv[i] + adv;     // extract warning
192                                        if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
193                                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
194                                        } // if
195                                        nargs += 1;
196                                } // if
197                        } else if ( prefix( arg, "-B" ) ) {
198                                Bprefix = arg.substr(2);                                // strip the -B flag
199                                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
200                                nargs += 1;
201                        } else if ( prefix( arg, "-b" ) ) {
202                                if ( arg.length() == 2 ) {                              // separate argument ?
203                                        i += 1;
204                                        if ( i == argc ) continue;                      // next argument available ?
205                                        arg += argv[i];                                         // concatenate argument
206                                } // if
207                                // later versions of gcc require the -b option to appear at the start of the command line
208                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
209                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
210                                if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
211                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
212                                        exit( EXIT_FAILURE );
213                                } // if
214                                sargs += 1;
215                                nargs += 1;
216                        } else if ( prefix( arg, "-V" ) ) {
217                                if ( arg.length() == 2 ) {                              // separate argument ?
218                                        i += 1;
219                                        if ( i == argc ) continue;                      // next argument available ?
220                                        arg += argv[i];                                         // concatenate argument
221                                } // if
222                                // later versions of gcc require the -V option to appear at the start of the command line
223                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
224                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
225                                if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
226                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
227                                        exit( EXIT_FAILURE );
228                                } // if
229                                sargs += 1;
230                                nargs += 1;
231                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
232                                args[nargs] = argv[i];                                  // pass the argument along
233                                nargs += 1;
234                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
235                                        cpp_flag = true;                                        // cpp only
236                                } // if
237                                link = false;                           // no linkage required
238                        } else if ( arg[1] == 'l' ) {
239                                // if the user specifies a library, load it after user code
240                                libs[nlibs] = argv[i];
241                                nlibs += 1;
242                        } else {
243                                // concatenate any other arguments
244                                args[nargs] = argv[i];
245                                nargs += 1;
246                        } // if
247                } else {
248                        // concatenate other arguments
249                        args[nargs] = argv[i];
250                        nargs += 1;
251                        nonoptarg = true;
252                } // if
253        } // for
254
255//    if ( MACHINE_TYPE == "x86_64" ) {
256//              args[nargs] = "-mcx16";                                                 // allow double-wide CAA
257//              nargs += 1;
258//    } // if
259
260#ifdef __DEBUG_H__
261        cerr << "args:";
262        for ( int i = 1; i < nargs; i += 1 ) {
263                cerr << " " << args[i];
264        } // for
265        cerr << endl;
266#endif // __DEBUG_H__
267
268        if ( cpp_flag && CFA_flag ) {
269                cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
270                exit( EXIT_FAILURE );
271        } // if
272
273        // add the CFA include-library paths, which allow direct access to header files without directory qualification
274        args[nargs] = "-I" CFA_INCDIR;
275        nargs += 1;
276        if ( ! noincstd_flag ) {                                                        // do not use during build
277                args[nargs] = "-I" CFA_INCDIR "/stdhdr";
278                nargs += 1;
279        } // if
280        args[nargs] = "-I" CFA_INCDIR "/concurrency";
281        nargs += 1;
282        args[nargs] = "-I" CFA_INCDIR "/containers";
283        nargs += 1;
284
285#ifdef HAVE_LIBCFA
286        if ( link ) {
287                #if ! defined(HAVE_LIBCFA_RELEASE)
288                        if ( ! debug ) {
289                                cerr << "error: Option -nodebug is unavailable, libcfa was not installed." << endl;
290                                exit( EXIT_FAILURE );
291                        } // if
292                #endif
293                #if ! defined(HAVE_LIBCFA_DEBUG)
294                        if ( debug ) {
295                                cerr << "error: Option -debug is unavailable, libcfa-d was not installed." << endl;
296                                exit( EXIT_FAILURE );
297                        } // if
298                #endif
299
300                args[nargs] = "-Xlinker";
301                nargs += 1;
302                args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
303                nargs += 1;
304                args[nargs] = "-Xlinker";
305                nargs += 1;
306                args[nargs] = "--undefined=__cfaabi_interpose_startup";
307                nargs += 1;
308
309                // include the cfa library in case it's needed
310                args[nargs] = "-L" CFA_LIBDIR;
311                nargs += 1;
312                if ( debug ) {
313                        args[nargs] = "-lcfa-d";
314                } else {
315                        args[nargs] = "-lcfa";
316                } // if
317                nargs += 1;
318                args[nargs] = "-lpthread";
319                nargs += 1;
320                args[nargs] = "-ldl";
321                nargs += 1;
322                args[nargs] = "-lrt";
323                nargs += 1;
324        } // if
325#endif // HAVE_LIBCFA
326
327        // Add exception flags (unconditionally)
328        args[nargs] = "-fexceptions";
329        nargs += 1;
330
331        // add the correct set of flags based on the type of compile this is
332
333        args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
334        nargs += 1;
335        args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
336        nargs += 1;
337        args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
338        nargs += 1;
339        args[nargs] = "-D__CFA__";
340        nargs += 1;
341        args[nargs] = "-D__CFORALL__";
342        nargs += 1;
343        args[nargs] = "-D__cforall";
344        nargs += 1;
345
346        if ( cpp_flag ) {
347                args[nargs] = "-D__CPP__";
348                nargs += 1;
349        } // if
350
351        shuffle( args, sargs, nargs, 1 );                                       // make room at front of argument list
352        nargs += 1;
353        if ( CFA_flag ) {
354                args[sargs] = "-D__CFA_FLAG__=-N";
355                args[nargs] = "-D__CFA_PREPROCESS_";
356                nargs += 1;
357        } else {
358                args[sargs] = "-D__CFA_FLAG__=-L";
359        } // if
360        sargs += 1;
361
362        if ( debug ) {
363                heading += " (debug)";
364                args[nargs] = "-D__CFA_DEBUG__";
365                nargs += 1;
366        } else {
367                heading += " (no debug)";
368        } // if
369
370        if ( Bprefix.length() == 0 ) {
371                Bprefix = installlibdir;
372                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
373                nargs += 1;
374        } // if
375
376    args[nargs] = "-Xlinker";                                                   // used by backtrace
377    nargs += 1;
378    args[nargs] = "-export-dynamic";
379    nargs += 1;
380
381        // execute the compilation command
382
383        args[0] = compiler_path.c_str();                                        // set compiler command for exec
384        // find actual name of the compiler independent of the path to it
385        int p = compiler_path.find_last_of( '/' );                      // scan r -> l for first '/'
386        if ( p == -1 ) {
387                compiler_name = compiler_path;
388        } else {
389                compiler_name = *new string( compiler_path.substr( p + 1 ) );
390        } // if
391
392        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
393                args[nargs] = "-no-integrated-cpp";
394                nargs += 1;
395                args[nargs] = "-Wno-deprecated";
396                nargs += 1;
397                if ( ! std_flag ) {                                                             // default c99, if none specified
398                        args[nargs] = "-std=gnu99";
399                        nargs += 1;
400                } // if
401                args[nargs] = "-fgnu89-inline";
402                nargs += 1;
403                args[nargs] = "-D__int8_t_defined";                             // prevent gcc type-size attributes
404                nargs += 1;
405                args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
406                nargs += 1;
407                args[nargs] = "-lm";
408                nargs += 1;
409        } else {
410                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
411                exit( EXIT_FAILURE );
412        } // if
413
414        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
415                args[nargs] = libs[i];
416                nargs += 1;
417        } // for
418
419        args[nargs] = NULL;                                                                     // terminate with NULL
420
421#ifdef __DEBUG_H__
422        cerr << "nargs: " << nargs << endl;
423        cerr << "args:" << endl;
424        for ( int i = 0; args[i] != NULL; i += 1 ) {
425                cerr << " \"" << args[i] << "\"" << endl;
426        } // for
427#endif // __DEBUG_H__
428
429        if ( ! quiet ) {
430                cerr << "CFA " << "Version " << Version << heading << endl;
431
432                if ( help ) {
433                        cerr <<
434                                "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
435                                "-help\t\t\t: print this help message" << endl <<
436                                "-quiet\t\t\t: print no messages from the cfa command" << endl <<
437                                "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
438                                "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
439                                "...\t\t\t: any other " << compiler_name << " flags" << endl;
440                } // if
441        } // if
442
443        if ( verbose ) {
444                if ( argc == 2 ) exit( EXIT_SUCCESS );                  // if only the -v flag is specified, do not invoke gcc
445
446                for ( int i = 0; args[i] != NULL; i += 1 ) {
447                        cerr << args[i] << " ";
448                } // for
449                cerr << endl;
450        } // if
451
452        if ( ! nonoptarg ) {
453                cerr << argv[0] << " error, no input files" << endl;
454                exit( EXIT_FAILURE );
455        } // if
456
457        // execute the command and return the result
458
459        execvp( args[0], (char *const *)args );                         // should not return
460        perror( "CFA Translator error: cfa level, execvp" );
461        exit( EXIT_FAILURE );
462} // main
463
464// Local Variables: //
465// tab-width: 4 //
466// mode: c++ //
467// compile-command: "make install" //
468// End: //
Note: See TracBrowser for help on using the repository browser.