source: src/driver/cfa.cc @ af39199d

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since af39199d was af39199d, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

add and use search routine

  • Property mode set to 100644
File size: 14.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 : Wed May  2 17:57:43 2018
13// Update Count     : 224
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#include <string.h>                                                                             // strcmp
22
23#include "Common/SemanticError.h"
24#include "config.h"                                                                             // configure info
25
26using std::cerr;
27using std::endl;
28using std::string;
29using std::to_string;
30
31
32//#define __DEBUG_H__
33
34
35bool prefix( string arg, string pre ) {
36        return arg.substr( 0, pre.size() ) == pre;
37} // prefix
38
39
40void shuffle( const char *args[], int S, int E, int N ) {
41        // S & E index 1 passed the end so adjust with -1
42#ifdef __DEBUG_H__
43        cerr << "shuffle:" << S << " " << E << " " << N << endl;
44#endif // __DEBUG_H__
45        for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
46#ifdef __DEBUG_H__
47                cerr << "\t" << j << " " << j-N << endl;
48#endif // __DEBUG_H__
49                args[j] = args[j-N];
50        } // for
51} // shuffle
52
53
54#define str(s) #s
55
56int main( int argc, char *argv[] ) {
57        string Version( CFA_VERSION_LONG );                                     // current version number from CONFIG
58        string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
59
60        string installincdir( CFA_INCDIR );                                     // fixed location of include files
61        string installlibdir( CFA_LIBDIR );                                     // fixed location of cc1 and cfa-cpp commands
62
63        string heading;                                                                         // banner printed at start of cfa compilation
64        string arg;                                                                                     // current command-line argument during command-line parsing
65        string Bprefix;                                                                         // path where gcc looks for compiler command steps
66        string langstd;                                                                         // language standard
67
68        string compiler_path( CFA_BACKEND_CC );                         // path/name of C compiler
69        string compiler_name;                                                           // name of C compiler
70
71        bool nonoptarg = false;                                                         // indicates non-option argument specified
72        bool link = true;                                                                       // linking as well as compiling
73        bool verbose = false;                                                           // -v flag
74        bool quiet = false;                                                                     // -quiet flag
75        bool debug = true;                                                                      // -debug flag
76        bool help = false;                                                                      // -help flag
77        bool CFA_flag = false;                                                          // -CFA flag
78        bool cpp_flag = false;                                                          // -E or -M flag, preprocessor only
79        bool std_flag = false;                                                          // -std= flag
80        bool noincstd_flag = false;                                                     // -no-include-stdhdr= flag
81        bool debugging __attribute(( unused )) = false;         // -g flag
82
83        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
84        int sargs = 1;                                                                          // starting location for arguments in args list
85        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
86
87        const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
88        int nlibs = 0;
89
90#ifdef __DEBUG_H__
91        cerr << "CFA:" << endl;
92#endif // __DEBUG_H__
93
94        // process command-line arguments
95
96        for ( int i = 1; i < argc; i += 1 ) {
97#ifdef __DEBUG_H__
98                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
99#endif // __DEBUG_H__
100                arg = argv[i];                                                                  // convert to string value
101#ifdef __DEBUG_H__
102                cerr << "arg:\"" << arg << "\"" << endl;
103#endif // __DEBUG_H__
104                if ( prefix( arg, "-" ) ) {
105                        // pass through arguments
106
107                        if ( arg == "-Xlinker" || arg == "-o" ) {
108                                args[nargs] = argv[i];                                  // pass the argument along
109                                nargs += 1;
110                                i += 1;
111                                if ( i == argc ) continue;                              // next argument available ?
112                                args[nargs] = argv[i];                                  // pass the argument along
113                                nargs += 1;
114                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
115                                i += 1;
116                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
117                                nargs += 1;
118
119                                // CFA specific arguments
120
121                        } else if ( arg == "-CFA" ) {
122                                CFA_flag = true;                                                // strip the -CFA flag
123                                link = false;
124                                args[nargs] = "-E";                                             // replace the argument with -E
125                                nargs += 1;
126                        } else if ( arg == "-debug" ) {
127                                debug = true;                                                   // strip the debug flag
128                        } else if ( arg == "-nodebug" ) {
129                                debug = false;                                                  // strip the nodebug flag
130                        } else if ( arg == "-quiet" ) {
131                                quiet = true;                                                   // strip the quiet flag
132                        } else if ( arg == "-noquiet" ) {
133                                quiet = false;                                                  // strip the noquiet flag
134                        } else if ( arg == "-help" ) {
135                                help = true;                                                    // strip the help flag
136                        } else if ( arg == "-nohelp" ) {
137                                help = false;                                                   // strip the nohelp flag
138                        } else if ( arg == "-no-include-stdhdr" ) {
139                                noincstd_flag = true;                                   // strip the no-include-stdhdr flag
140                        } else if ( arg == "-compiler" ) {
141                                // use the user specified compiler
142                                i += 1;
143                                if ( i == argc ) continue;                              // next argument available ?
144                                compiler_path = argv[i];
145                                if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
146                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
147                                        exit( EXIT_FAILURE );
148                                } // if
149
150                                // C specific arguments
151
152                        } else if ( arg == "-v" ) {
153                                verbose = true;                                                 // verbosity required
154                                args[nargs] = argv[i];                                  // pass the argument along
155                                nargs += 1;
156                        } else if ( arg == "-g" ) {
157                                debugging = true;                                               // symbolic debugging required
158                                args[nargs] = argv[i];                                  // pass the argument along
159                                nargs += 1;
160                        } else if ( prefix( arg, "-std=" ) ) {
161                                std_flag = true;                                                // -std=XX provided
162                                args[nargs] = argv[i];                                  // pass the argument along
163                                nargs += 1;
164                        } else if ( arg == "-w" ) {
165                                args[nargs] = argv[i];                                  // pass the argument along
166                                nargs += 1;
167                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
168                                nargs += 1;
169                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
170                                if ( arg == "-Werror" || arg == "-Wall" ) {
171                                        args[nargs] = argv[i];                          // pass the argument along
172                                        nargs += 1;
173                                        args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
174                                        nargs += 1;
175                                } else {
176                                        unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
177                                        args[nargs] = argv[i];                          // conditionally pass the argument along
178                                        const char * warning = argv[i] + adv;     // extract warning
179                                        if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
180                                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
181                                        } // if
182                                        nargs += 1;
183                                } // if
184                        } else if ( prefix( arg, "-B" ) ) {
185                                Bprefix = arg.substr(2);                                // strip the -B flag
186                                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
187                                nargs += 1;
188                        } else if ( prefix( arg, "-b" ) ) {
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 -b 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_MACHINE__=" ) + 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 ( prefix( arg, "-V" ) ) {
204                                if ( arg.length() == 2 ) {                              // separate argument ?
205                                        i += 1;
206                                        if ( i == argc ) continue;                      // next argument available ?
207                                        arg += argv[i];                                         // concatenate argument
208                                } // if
209                                // later versions of gcc require the -V option to appear at the start of the command line
210                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
211                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
212                                if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
213                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
214                                        exit( EXIT_FAILURE );
215                                } // if
216                                sargs += 1;
217                                nargs += 1;
218                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
219                                args[nargs] = argv[i];                                  // pass the argument along
220                                nargs += 1;
221                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
222                                        cpp_flag = true;                                        // cpp only
223                                } // if
224                                link = false;                           // no linkage required
225                        } else if ( arg[1] == 'l' ) {
226                                // if the user specifies a library, load it after user code
227                                libs[nlibs] = argv[i];
228                                nlibs += 1;
229                        } else {
230                                // concatenate any other arguments
231                                args[nargs] = argv[i];
232                                nargs += 1;
233                        } // if
234                } else {
235                        // concatenate other arguments
236                        args[nargs] = argv[i];
237                        nargs += 1;
238                        nonoptarg = true;
239                } // if
240        } // for
241
242#ifdef __DEBUG_H__
243        cerr << "args:";
244        for ( int i = 1; i < nargs; i += 1 ) {
245                cerr << " " << args[i];
246        } // for
247        cerr << endl;
248#endif // __DEBUG_H__
249
250        if ( cpp_flag && CFA_flag ) {
251                cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
252                exit( EXIT_FAILURE );
253        } // if
254
255        // add the CFA include-library paths, which allow direct access to header files without directory qualification
256        args[nargs] = "-I" CFA_INCDIR;
257        nargs += 1;
258        if ( ! noincstd_flag ) {                                                        // do not use during build
259                args[nargs] = "-I" CFA_INCDIR "/stdhdr";
260                nargs += 1;
261        } // if
262        args[nargs] = "-I" CFA_INCDIR "/concurrency";
263        nargs += 1;
264        args[nargs] = "-I" CFA_INCDIR "/containers";
265        nargs += 1;
266
267#ifdef HAVE_LIBCFA
268        if ( link ) {
269                #if ! defined(HAVE_LIBCFA_RELEASE)
270                        if( !debug ) {
271                                cerr << "error: Option -nodebug is unavailable, libcfa was not installed." << endl;
272                                exit( EXIT_FAILURE );
273                                }
274                #endif
275                #if ! defined(HAVE_LIBCFA_DEBUG)
276                        if( debug ) {
277                                cerr << "error: Option -debug is unavailable, libcfa-d was not installed." << endl;
278                                exit( EXIT_FAILURE );
279                                }
280                #endif
281
282                args[nargs] = "-Xlinker";
283                nargs += 1;
284                args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
285                nargs += 1;
286                args[nargs] = "-Xlinker";
287                nargs += 1;
288                args[nargs] = "--undefined=__cfaabi_interpose_startup";
289                nargs += 1;
290
291                // include the cfa library in case it's needed
292                args[nargs] = "-L" CFA_LIBDIR;
293                nargs += 1;
294                if( debug ) {
295                        args[nargs] = "-lcfa-d";
296                } else {
297                        args[nargs] = "-lcfa";
298                }
299                nargs += 1;
300                args[nargs] = "-lpthread";
301                nargs += 1;
302                args[nargs] = "-ldl";
303                nargs += 1;
304                args[nargs] = "-lrt";
305                nargs += 1;
306        } // if
307#endif // HAVE_LIBCFA
308
309        // Add exception flags (unconditionally)
310        args[nargs] = "-fexceptions";
311        nargs += 1;
312
313        // add the correct set of flags based on the type of compile this is
314
315        args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
316        nargs += 1;
317        args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
318        nargs += 1;
319        args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
320        nargs += 1;
321        args[nargs] = "-D__CFA__";
322        nargs += 1;
323        args[nargs] = "-D__CFORALL__";
324        nargs += 1;
325        args[nargs] = "-D__cforall";
326        nargs += 1;
327
328        if ( cpp_flag ) {
329                args[nargs] = "-D__CPP__";
330                nargs += 1;
331        } // if
332
333        shuffle( args, sargs, nargs, 1 );                                       // make room at front of argument list
334        nargs += 1;
335        if ( CFA_flag ) {
336                args[sargs] = "-D__CFA_FLAG__=-N";
337                args[nargs] = "-D__CFA_PREPROCESS_";
338                nargs += 1;
339        } else {
340                args[sargs] = "-D__CFA_FLAG__=-L";
341        } // if
342        sargs += 1;
343
344        if ( debug ) {
345                heading += " (debug)";
346                args[nargs] = "-D__CFA_DEBUG__";
347                nargs += 1;
348        } else {
349                heading += " (no debug)";
350        } // if
351
352        if ( Bprefix.length() == 0 ) {
353                Bprefix = installlibdir;
354                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
355                nargs += 1;
356        } // if
357
358    args[nargs] = "-Xlinker";                                                   // used by backtrace
359    nargs += 1;
360    args[nargs] = "-export-dynamic";
361    nargs += 1;
362
363        // execute the compilation command
364
365        args[0] = compiler_path.c_str();                                        // set compiler command for exec
366        // find actual name of the compiler independent of the path to it
367        int p = compiler_path.find_last_of( '/' );                      // scan r -> l for first '/'
368        if ( p == -1 ) {
369                compiler_name = compiler_path;
370        } else {
371                compiler_name = *new string( compiler_path.substr( p + 1 ) );
372        } // if
373
374        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
375                args[nargs] = "-no-integrated-cpp";
376                nargs += 1;
377                args[nargs] = "-Wno-deprecated";
378                nargs += 1;
379                if ( ! std_flag ) {                                                             // default c99, if none specified
380                        args[nargs] = "-std=gnu99";
381                        nargs += 1;
382                } // if
383                args[nargs] = "-fgnu89-inline";
384                nargs += 1;
385                args[nargs] = "-D__int8_t_defined";                             // prevent gcc type-size attributes
386                nargs += 1;
387                args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
388                nargs += 1;
389                args[nargs] = "-lm";
390                nargs += 1;
391        } else {
392                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
393                exit( EXIT_FAILURE );
394        } // if
395
396        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
397                args[nargs] = libs[i];
398                nargs += 1;
399        } // for
400
401        args[nargs] = NULL;                                                                     // terminate with NULL
402
403#ifdef __DEBUG_H__
404        cerr << "nargs: " << nargs << endl;
405        cerr << "args:" << endl;
406        for ( int i = 0; args[i] != NULL; i += 1 ) {
407                cerr << " \"" << args[i] << "\"" << endl;
408        } // for
409#endif // __DEBUG_H__
410
411        if ( ! quiet ) {
412                cerr << "CFA " << "Version " << Version << heading << endl;
413
414                if ( help ) {
415                        cerr <<
416                                "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
417                                "-help\t\t\t: print this help message" << endl <<
418                                "-quiet\t\t\t: print no messages from the cfa command" << endl <<
419                                "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
420                                "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
421                                "...\t\t\t: any other " << compiler_name << " flags" << endl;
422                } // if
423        } // if
424
425        if ( verbose ) {
426                if ( argc == 2 ) exit( EXIT_SUCCESS );                  // if only the -v flag is specified, do not invoke gcc
427
428                for ( int i = 0; args[i] != NULL; i += 1 ) {
429                        cerr << args[i] << " ";
430                } // for
431                cerr << endl;
432        } // if
433
434        if ( ! nonoptarg ) {
435                cerr << argv[0] << " error, no input files" << endl;
436                exit( EXIT_FAILURE );
437        } // if
438
439        // execute the command and return the result
440
441        execvp( args[0], (char *const *)args );                         // should not return
442        perror( "CFA Translator error: cfa level, execvp" );
443        exit( EXIT_FAILURE );
444} // main
445
446// Local Variables: //
447// tab-width: 4 //
448// mode: c++ //
449// compile-command: "make install" //
450// End: //
Note: See TracBrowser for help on using the repository browser.