source: src/driver/cfa.cc @ 3354e6d

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

remove -no-include-stdhdr command-line flag

  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[b87a5ed]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//
[76c7f65e]7// cfa.cc --
[b87a5ed]8//
[51b7345]9// Author           : Peter A. Buhr
10// Created On       : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
[3354e6d]12// Last Modified On : Thu Jul 20 15:54:45 2017
13// Update Count     : 156
[b87a5ed]14//
[51b7345]15
16#include <iostream>
[b87a5ed]17#include <cstdio>                                                                               // perror
18#include <cstdlib>                                                                              // putenv, exit
19#include <unistd.h>                                                                             // execvp
20#include <string>                                                                               // STL version
[51b7345]21
[b87a5ed]22#include "config.h"                                                                             // configure info
[51b7345]23
24using std::cerr;
25using std::endl;
26using std::string;
[47a8d17]27using std::to_string;
[51b7345]28
29
30//#define __DEBUG_H__
31
32
33bool prefix( string arg, string pre ) {
[b87a5ed]34        return arg.substr( 0, pre.size() ) == pre;
[51b7345]35} // prefix
36
37
38void shuffle( const char *args[], int S, int E, int N ) {
[b87a5ed]39        // S & E index 1 passed the end so adjust with -1
[51b7345]40#ifdef __DEBUG_H__
[b87a5ed]41        cerr << "shuffle:" << S << " " << E << " " << N << endl;
[51b7345]42#endif // __DEBUG_H__
[b87a5ed]43        for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
[51b7345]44#ifdef __DEBUG_H__
[b87a5ed]45                cerr << "\t" << j << " " << j-N << endl;
[51b7345]46#endif // __DEBUG_H__
[b87a5ed]47                args[j] = args[j-N];
48        } // for
[51b7345]49} // shuffle
50
51
[4b1afb6]52#define str(s) #s
53
[51b7345]54int main( int argc, char *argv[] ) {
[4b1afb6]55        string Version( CFA_VERSION_LONG );                                     // current version number from CONFIG
56        string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
[51b7345]57
[00cc023]58        string installincdir( CFA_INCDIR );                                     // fixed location of include files
59        string installlibdir( CFA_LIBDIR );                                     // fixed location of cc1 and cfa-cpp commands
[51b7345]60
[b87a5ed]61        string heading;                                                                         // banner printed at start of cfa compilation
62        string arg;                                                                                     // current command-line argument during command-line parsing
63        string Bprefix;                                                                         // path where gcc looks for compiler command steps
64        string langstd;                                                                         // language standard
[51b7345]65
[e24f13a]66        string compiler_path( CFA_BACKEND_CC );                         // path/name of C compiler
[b87a5ed]67        string compiler_name;                                                           // name of C compiler
[51b7345]68
[b87a5ed]69        bool nonoptarg = false;                                                         // indicates non-option argument specified
70        bool link = true;                                                                       // linking as well as compiling
71        bool verbose = false;                                                           // -v flag
72        bool quiet = false;                                                                     // -quiet flag
73        bool debug = true;                                                                      // -debug flag
74        bool help = false;                                                                      // -help flag
75        bool CFA_flag = false;                                                          // -CFA flag
76        bool cpp_flag = false;                                                          // -E or -M flag, preprocessor only
[de62360d]77        bool std_flag = false;                                                          // -std= flag
[6e4b913]78        bool debugging __attribute(( unused )) = false;         // -g flag
[51b7345]79
[b87a5ed]80        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
81        int sargs = 1;                                                                          // starting location for arguments in args list
82        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
[51b7345]83
[b87a5ed]84        const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
85        int nlibs = 0;
[51b7345]86
87#ifdef __DEBUG_H__
[b87a5ed]88        cerr << "CFA:" << endl;
[51b7345]89#endif // __DEBUG_H__
90
[b87a5ed]91        // process command-line arguments
[51b7345]92
[b87a5ed]93        for ( int i = 1; i < argc; i += 1 ) {
[51b7345]94#ifdef __DEBUG_H__
[b87a5ed]95                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b7345]96#endif // __DEBUG_H__
[b87a5ed]97                arg = argv[i];                                                                  // convert to string value
[51b7345]98#ifdef __DEBUG_H__
[b87a5ed]99                cerr << "arg:\"" << arg << "\"" << endl;
[51b7345]100#endif // __DEBUG_H__
[b87a5ed]101                if ( prefix( arg, "-" ) ) {
102                        // pass through arguments
103
104                        if ( arg == "-Xlinker" || arg == "-o" ) {
105                                args[nargs] = argv[i];                                  // pass the argument along
106                                nargs += 1;
107                                i += 1;
108                                if ( i == argc ) continue;                              // next argument available ?
109                                args[nargs] = argv[i];                                  // pass the argument along
110                                nargs += 1;
111                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
112                                i += 1;
113                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
114                                nargs += 1;
115
116                                // CFA specific arguments
117
118                        } else if ( arg == "-CFA" ) {
119                                CFA_flag = true;                                                // strip the -CFA flag
120                                link = false;
121                                args[nargs] = "-E";                                             // replace the argument with -E
122                                nargs += 1;
123                        } else if ( arg == "-debug" ) {
124                                debug = true;                                                   // strip the debug flag
125                        } else if ( arg == "-nodebug" ) {
126                                debug = false;                                                  // strip the nodebug flag
127                        } else if ( arg == "-quiet" ) {
128                                quiet = true;                                                   // strip the quiet flag
129                        } else if ( arg == "-noquiet" ) {
130                                quiet = false;                                                  // strip the noquiet flag
131                        } else if ( arg == "-help" ) {
132                                help = true;                                                    // strip the help flag
133                        } else if ( arg == "-nohelp" ) {
134                                help = false;                                                   // strip the nohelp 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
[de62360d]145                                // C specific arguments
[b87a5ed]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;
[de62360d]155                        } else if ( prefix( arg, "-std=" ) ) {
[53ba273]156                                std_flag = true;                                                // -std=XX provided
[de62360d]157                                args[nargs] = argv[i];                                  // pass the argument along
158                                nargs += 1;
[b87a5ed]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
[51b7345]216
[b87a5ed]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__
[51b7345]224
[b87a5ed]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
[51b7345]229
[6e4b913]230        // add the CFA include-library paths, which allow direct access to header files without directory qualification
[b87a5ed]231        args[nargs] = "-I" CFA_INCDIR;
232        nargs += 1;
[3354e6d]233        args[nargs] = "-I" CFA_INCDIR "/stdhdr";
234        nargs += 1;
[0e76cf4f]235        args[nargs] = "-I" CFA_INCDIR "/concurrency";
236        nargs += 1;
[76c7f65e]237        args[nargs] = "-I" CFA_INCDIR "/containers";
238        nargs += 1;
239
[0edebf8]240#ifdef HAVE_LIBCFA
[b87a5ed]241        if ( link ) {
[0edebf8]242                #if ! defined(HAVE_LIBCFA_RELEASE)
[ff2d7341]243                        if( !debug ) {
[0edebf8]244                                cerr << "error: Option -nodebug is not available, libcfa was not installed." << endl;
245                                exit( EXIT_FAILURE );
246                                }
247                #endif
248                #if ! defined(HAVE_LIBCFA_DEBUG)
[ff2d7341]249                        if( debug ) {
[0edebf8]250                                cerr << "error: Option -debug is not available, libcfa-d was not installed." << endl;
251                                exit( EXIT_FAILURE );
252                                }
253                #endif
254
[b87a5ed]255                // include the cfa library in case it's needed
256                args[nargs] = "-L" CFA_LIBDIR;
[51b7345]257                nargs += 1;
[0edebf8]258                if( debug ) {
259                        args[nargs] = "-lcfa-d";
260                } else {
261                        args[nargs] = "-lcfa";
262                }
[51b7345]263                nargs += 1;
[63f78f0]264                args[nargs] = "-lpthread";
265                nargs += 1;
[9d944b2]266                args[nargs] = "-ldl";
267                nargs += 1;
[c5ac6d5]268                args[nargs] = "-lrt";
269                nargs += 1;
[9d944b2]270                args[nargs] = "-Xlinker";
271                nargs += 1;
272                args[nargs] = "--undefined=__lib_debug_write";
273                nargs += 1;
274
[51b7345]275        } // if
[0edebf8]276#endif //HAVE_LIBCFA
[51b7345]277
[b87a5ed]278        // add the correct set of flags based on the type of compile this is
[8c17ab0]279
[ec129c4]280        args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
[51b7345]281        nargs += 1;
[b87a5ed]282        args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
[51b7345]283        nargs += 1;
[ec129c4]284        args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
[1db21619]285        nargs += 1;
[4c82a3c]286        args[nargs] = "-D__CFA__";
287        nargs += 1;
288        args[nargs] = "-D__CFORALL__";
[02e5ab6]289        nargs += 1;
[6acb935]290        args[nargs] = "-D__cforall";
291        nargs += 1;
[51b7345]292
[b87a5ed]293        if ( cpp_flag ) {
294                args[nargs] = "-D__CPP__";
295                nargs += 1;
296        } // if
[51b7345]297
[b87a5ed]298        if ( CFA_flag ) {
[4c82a3c]299                args[nargs] = "-D__CFA_PREPROCESS_";
[b87a5ed]300                nargs += 1;
301        } // if
[51b7345]302
[b87a5ed]303        if ( debug ) {
304                heading += " (debug)";
305                args[nargs] = "-D__CFA_DEBUG__";
306                nargs += 1;
307        } else {
308                heading += " (no debug)";
309        } // if
[51b7345]310
[b87a5ed]311        if ( Bprefix.length() == 0 ) {
312                Bprefix = installlibdir;
313                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
314                nargs += 1;
315        } // if
[51b7345]316
[b87a5ed]317        // execute the compilation command
[51b7345]318
[b87a5ed]319        args[0] = compiler_path.c_str();                                        // set compiler command for exec
320        // find actual name of the compiler independent of the path to it
321        int p = compiler_path.find_last_of( '/' );                      // scan r -> l for first '/'
322        if ( p == -1 ) {
323                compiler_name = compiler_path;
324        } else {
325                compiler_name = *new string( compiler_path.substr( p + 1 ) );
326        } // if
[51b7345]327
[b87a5ed]328        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
329                args[nargs] = "-no-integrated-cpp";
330                nargs += 1;
[76c7f65e]331                args[nargs] = "-Wno-deprecated";
[b87a5ed]332                nargs += 1;
[de62360d]333                if ( ! std_flag ) {                                                             // default c99, if none specified
[53ba273]334                        args[nargs] = "-std=gnu99";
[de62360d]335                        nargs += 1;
336                } // if
[76c7f65e]337                args[nargs] = "-fgnu89-inline";
[6e991d6]338                nargs += 1;
[b87a5ed]339                args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
340                nargs += 1;
[d3b7937]341                args[nargs] = "-lm";
342                nargs += 1;
[b87a5ed]343        } else {
[e24f13a]344                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
[b87a5ed]345                exit( EXIT_FAILURE );
346        } // if
[51b7345]347
[b87a5ed]348        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
349                args[nargs] = libs[i];
350                nargs += 1;
351        } // for
[51b7345]352
[b87a5ed]353        args[nargs] = NULL;                                                                     // terminate with NULL
[51b7345]354
355#ifdef __DEBUG_H__
[b87a5ed]356        cerr << "nargs: " << nargs << endl;
357        cerr << "args:" << endl;
358        for ( int i = 0; args[i] != NULL; i += 1 ) {
359                cerr << " \"" << args[i] << "\"" << endl;
360        } // for
[51b7345]361#endif // __DEBUG_H__
362
[b87a5ed]363        if ( ! quiet ) {
364                cerr << "CFA " << "Version " << Version << heading << endl;
365
366                if ( help ) {
367                        cerr <<
368                                "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
369                                "-help\t\t\t: print this help message" << endl <<
370                                "-quiet\t\t\t: print no messages from the cfa command" << endl <<
371                                "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
372                                "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
373                                "...\t\t\t: any other " << compiler_name << " flags" << endl;
374                } // if
[51b7345]375        } // if
376
[b87a5ed]377        if ( verbose ) {
378                if ( argc == 2 ) exit( EXIT_SUCCESS );                  // if only the -v flag is specified, do not invoke gcc
[51b7345]379
[b87a5ed]380                for ( int i = 0; args[i] != NULL; i += 1 ) {
381                        cerr << args[i] << " ";
382                } // for
383                cerr << endl;
384        } // if
[51b7345]385
[b87a5ed]386        if ( ! nonoptarg ) {
387                cerr << argv[0] << " error, no input files" << endl;
388                exit( EXIT_FAILURE );
389        } // if
[51b7345]390
[b87a5ed]391        // execute the command and return the result
[51b7345]392
[b87a5ed]393        execvp( args[0], (char *const *)args );                         // should not return
394        perror( "CFA Translator error: cfa level, execvp" );
395        exit( EXIT_FAILURE );
[51b7345]396} // main
397
398// Local Variables: //
[b87a5ed]399// tab-width: 4 //
400// mode: c++ //
[51b7345]401// compile-command: "make install" //
402// End: //
Note: See TracBrowser for help on using the repository browser.