source: src/driver/cfa.cc @ d9e2280

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since d9e2280 was d0542c4, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

add -no-include-std flag to fix recompilation issues

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