source: src/driver/cfa.cc @ ec129c4

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

add additional #defines for CFA version numbers

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