source: driver/cfa.cc @ 134b86a

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 134b86a was 134b86a, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add compiler flag to driver, update examples, fix unnamed bit fields

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