source: src/driver/cfa.cc @ 3f0c6a5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 3f0c6a5 was 47a8d17, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

cfa version is now based on file of the same name

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