source: src/driver/cfa.cc @ 82dd287

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 82dd287 was de62360d, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

fix computed goto, fixed -std=, implicit typedefs for enum and aggregates, add _Noreturn _Thread_local

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