source: src/driver/cfa.cc @ d3b7937

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

building runtime library (first attempt)

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