source: driver/cfa.cc @ b740f0b

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

remove double trialing slash for file names, fix -E flag, clean up suffix handling

  • Property mode set to 100644
File size: 18.3 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 Aug 22 17:26:19 2018
13// Update Count     : 265
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 <sys/types.h>
24#include <sys/stat.h>
25
26#include "Common/SemanticError.h"
27#include "config.h"                                                                             // configure info
28
29using std::cerr;
30using std::endl;
31using std::string;
32using std::to_string;
33
34
35//#define __DEBUG_H__
36
37
38bool prefix( string arg, string pre ) {
39        return arg.substr( 0, pre.size() ) == pre;
40} // prefix
41
42enum { NumSuffixes = 2 };
43const string suffixes[NumSuffixes] = { "cfa", "hfa", };
44
45void suffix( string arg, const char * args[], int & nargs ) {
46        //std::cerr << arg << std::endl;
47        size_t dot = arg.find_last_of( "." );
48        //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
49        if ( dot == string::npos ) return;
50        string sx = arg.substr( dot + 1 );
51        for ( int i = 0; i < NumSuffixes; i += 1 ) {
52                if ( sx == suffixes[i] ) {
53                        args[nargs] = "-x";
54                        nargs += 1;
55                        args[nargs] = "c";
56                        nargs += 1;
57                        return;
58                } // if
59        } // for
60} // suffix
61
62
63void shuffle( const char *args[], int S, int E, int N ) {
64        // S & E index 1 passed the end so adjust with -1
65        #ifdef __DEBUG_H__
66        cerr << "shuffle:" << S << " " << E << " " << N << endl;
67        #endif // __DEBUG_H__
68        for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
69                #ifdef __DEBUG_H__
70                cerr << "\t" << j << " " << j-N << endl;
71                #endif // __DEBUG_H__
72                args[j] = args[j-N];
73        } // for
74} // shuffle
75
76static inline bool dirExists(const string & path) {
77    struct stat info;
78    if(stat( path.c_str(), &info ) != 0)
79        return false;
80    else if(info.st_mode & S_IFDIR)
81        return true;
82    else
83        return false;
84} //dirExists
85
86
87#define str(s) #s
88
89int main( int argc, char *argv[] ) {
90        string Version( CFA_VERSION_LONG );                                     // current version number from CONFIG
91        string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
92
93        string installincdir( CFA_INCDIR );                         // fixed location of include files
94        string installlibdir( CFA_LIBDIR );                         // fixed location of cc1 and cfa-cpp commands when installed
95        string srcdriverdir ( TOP_BUILDDIR "driver");                // fixed location of cc1 and cfa-cpp commands when in tree
96
97        string heading;                                                                         // banner printed at start of cfa compilation
98        string arg;                                                                                     // current command-line argument during command-line parsing
99        string Bprefix;                                                                         // path where gcc looks for compiler command steps
100        string langstd;                                                                         // language standard
101
102        string compiler_path( CFA_BACKEND_CC );                         // path/name of C compiler
103        string compiler_name;                                                           // name of C compiler
104
105        bool nonoptarg = false;                                                         // indicates non-option argument specified
106        bool link = true;                                                                       // linking as well as compiling
107        bool verbose = false;                                                           // -v flag
108        bool quiet = false;                                                                     // -quiet flag
109        bool debug = true;                                                                      // -debug flag
110        bool help = false;                                                                      // -help flag
111        bool CFA_flag = false;                                                          // -CFA flag
112        bool cpp_flag = false;                                                          // -E or -M flag, preprocessor only
113        bool std_flag = false;                                                          // -std= flag
114        bool noincstd_flag = false;                                                     // -no-include-stdhdr= flag
115        bool xflag = false;                                                                     // user supplied -x flag
116        bool debugging __attribute(( unused )) = false;         // -g flag
117        bool m32 = false;                                    // -m32 flag
118        bool m64 = false;                                    // -m64 flag
119        bool intree = false;
120
121        const char *args[argc + 100];                                           // cfa command line values, plus some space for additional flags
122        int sargs = 1;                                                                          // starting location for arguments in args list
123        int nargs = sargs;                                                                      // number of arguments in args list; 0 => command name
124
125        const char *libs[argc + 20];                                            // non-user libraries must come separately, plus some added libraries and flags
126        int nlibs = 0;
127
128        #ifdef __DEBUG_H__
129        cerr << "CFA:" << endl;
130        #endif // __DEBUG_H__
131
132        // process command-line arguments
133
134        for ( int i = 1; i < argc; i += 1 ) {
135                #ifdef __DEBUG_H__
136                cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
137                #endif // __DEBUG_H__
138                arg = argv[i];                                                                  // convert to string value
139                #ifdef __DEBUG_H__
140                cerr << "arg:\"" << arg << "\"" << endl;
141                #endif // __DEBUG_H__
142                if ( prefix( arg, "-" ) ) {
143                        // pass through arguments
144
145                        if ( arg == "-Xlinker" || arg == "-o" ) {
146                                args[nargs] = argv[i];                                  // pass the argument along
147                                nargs += 1;
148                                i += 1;
149                                if ( i == argc ) continue;                              // next argument available ?
150                                args[nargs] = argv[i];                                  // pass the argument along
151                                nargs += 1;
152                        } else if ( arg == "-XCFA" ) {                          // CFA pass through
153                                i += 1;
154                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
155                                nargs += 1;
156
157                                // CFA specific arguments
158
159                        } else if ( arg == "-CFA" ) {
160                                CFA_flag = true;                                                // strip the -CFA flag
161                                link = false;
162                                args[nargs] = "-E";                                             // replace the argument with -E
163                                nargs += 1;
164                        } else if ( arg == "-debug" ) {
165                                debug = true;                                                   // strip the debug flag
166                        } else if ( arg == "-nodebug" ) {
167                                debug = false;                                                  // strip the nodebug flag
168                        } else if ( arg == "-quiet" ) {
169                                quiet = true;                                                   // strip the quiet flag
170                        } else if ( arg == "-noquiet" ) {
171                                quiet = false;                                                  // strip the noquiet flag
172                        } else if ( arg == "-help" ) {
173                                help = true;                                                    // strip the help flag
174                        } else if ( arg == "-nohelp" ) {
175                                help = false;                                                   // strip the nohelp flag
176                        } else if ( arg == "-no-include-stdhdr" ) {
177                                noincstd_flag = true;                                   // strip the no-include-stdhdr flag
178                        } else if ( arg == "-in-tree" ) {
179                                intree = true;
180                        } else if ( arg == "-compiler" ) {
181                                // use the user specified compiler
182                                i += 1;
183                                if ( i == argc ) continue;                              // next argument available ?
184                                compiler_path = argv[i];
185                                if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
186                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
187                                        exit( EXIT_FAILURE );
188                                } // if
189
190                                // C specific arguments
191
192                        } else if ( arg == "-v" ) {
193                                verbose = true;                                                 // verbosity required
194                                args[nargs] = argv[i];                                  // pass the argument along
195                                nargs += 1;
196                        } else if ( arg == "-g" ) {
197                                debugging = true;                                               // symbolic debugging required
198                                args[nargs] = argv[i];                                  // pass the argument along
199                                nargs += 1;
200                        } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
201                                std_flag = true;                                                // -std=XX provided
202                                args[nargs] = argv[i];                                  // pass the argument along
203                                nargs += 1;
204                        } else if ( arg == "-x" ) {
205                                xflag = true;
206                                args[nargs] = argv[i];                                  // pass the argument along
207                                nargs += 1;
208                                i += 1;                                                                 // advance to argument
209                                args[nargs] = argv[i];                                  // pass the argument along
210                                nargs += 1;
211                                // args[nargs] = ( *new string( string("-D__GCC_X__=") + argv[i] ) ).c_str(); // add the argument for -x
212                                // nargs += 1;
213                        } else if ( prefix( arg, "-x" ) ) {
214                                xflag = true;
215                                args[nargs] = argv[i];                                  // pass the argument along
216                                nargs += 1;
217                                // args[nargs] = ( *new string( string("-D__GCC_X__=") + arg.substr(2) ) ).c_str(); // add the argument for -x
218                                // nargs += 1;
219                        } else if ( arg == "-w" ) {
220                                args[nargs] = argv[i];                                  // pass the argument along
221                                nargs += 1;
222                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
223                                nargs += 1;
224                        } else if ( prefix( arg, "-W" ) ) {                     // check before next tests
225                                if ( arg == "-Werror" || arg == "-Wall" ) {
226                                        args[nargs] = argv[i];                          // pass the argument along
227                                        nargs += 1;
228                                        args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
229                                        nargs += 1;
230                                } else {
231                                        unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
232                                        args[nargs] = argv[i];                          // conditionally pass the argument along
233                                        const char * warning = argv[i] + adv;     // extract warning
234                                        if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
235                                                args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
236                                        } // if
237                                        nargs += 1;
238                                } // if
239                        } else if ( prefix( arg, "-B" ) ) {
240                                Bprefix = arg.substr(2);                                // strip the -B flag
241                                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
242                                nargs += 1;
243                        } else if ( prefix( arg, "-b" ) ) {
244                                if ( arg.length() == 2 ) {                              // separate argument ?
245                                        i += 1;
246                                        if ( i == argc ) continue;                      // next argument available ?
247                                        arg += argv[i];                                         // concatenate argument
248                                } // if
249                                // later versions of gcc require the -b option to appear at the start of the command line
250                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
251                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
252                                if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
253                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
254                                        exit( EXIT_FAILURE );
255                                } // if
256                                sargs += 1;
257                                nargs += 1;
258                        } else if ( prefix( arg, "-V" ) ) {
259                                if ( arg.length() == 2 ) {                              // separate argument ?
260                                        i += 1;
261                                        if ( i == argc ) continue;                      // next argument available ?
262                                        arg += argv[i];                                         // concatenate argument
263                                } // if
264                                // later versions of gcc require the -V option to appear at the start of the command line
265                                shuffle( args, sargs, nargs, 1 );               // make room at front of argument list
266                                args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
267                                if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
268                                        cerr << argv[0] << " error, cannot set environment variable." << endl;
269                                        exit( EXIT_FAILURE );
270                                } // if
271                                sargs += 1;
272                                nargs += 1;
273                        } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
274                                args[nargs] = argv[i];                                  // pass the argument along
275                                nargs += 1;
276                                if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
277                                        cpp_flag = true;                                        // cpp only
278                                } // if
279                                link = false;                           // no linkage required
280                        } else if ( arg[1] == 'l' ) {
281                                // if the user specifies a library, load it after user code
282                                libs[nlibs] = argv[i];
283                                nlibs += 1;
284                        } else if ( arg == "-m32" ) {
285                                m32 = true;
286                                m64 = false;
287                                args[nargs] = argv[i];
288                                nargs += 1;
289                        } else if ( arg == "-m64" ) {
290                                m64 = true;
291                                m32 = false;
292                                args[nargs] = argv[i];
293                                nargs += 1;
294                        } else {
295                                // concatenate any other arguments
296                                args[nargs] = argv[i];
297                                nargs += 1;
298                        } // if
299                } else {
300                        bool opt = false;
301                        if ( ! xflag ) {
302                                suffix( arg, args, nargs );                             // check suffix
303                                // args[nargs] = ( *new string( string("-D__GCC_X__=c") ) ).c_str(); // add the argument for -x
304                                // nargs += 1;
305                                opt = true;
306                        } // if
307                        // concatenate other arguments
308                        args[nargs] = argv[i];
309                        nargs += 1;
310                        if ( opt ) {
311                                args[nargs] = "-x";
312                                nargs += 1;
313                                args[nargs] = "none";
314                                nargs += 1;
315                                // args[nargs] = ( *new string( string("-D__GCC_X__=none") ) ).c_str(); // add the argument for -x
316                                // nargs += 1;
317                        } // if
318                        nonoptarg = true;
319                        xflag = false;
320                } // if
321        } // for
322
323        #ifdef __x86_64__
324        args[nargs] = "-mcx16";                                                         // allow double-wide CAA
325        nargs += 1;
326        #endif // __x86_64__
327
328        #ifdef __DEBUG_H__
329        cerr << "args:";
330        for ( int i = 1; i < nargs; i += 1 ) {
331                cerr << " " << args[i];
332        } // for
333        cerr << endl;
334        #endif // __DEBUG_H__
335
336        if ( cpp_flag && CFA_flag ) {
337                cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
338                exit( EXIT_FAILURE );
339        } // if
340
341        // add the CFA include-library paths, which allow direct access to header files without directory qualification
342        if( !intree ) {
343                args[nargs] = "-I" CFA_INCDIR;
344                nargs += 1;
345                if ( ! noincstd_flag ) {                                                        // do not use during build
346                        args[nargs] = "-I" CFA_INCDIR "stdhdr";
347                        nargs += 1;
348                } // if
349                args[nargs] = "-I" CFA_INCDIR "concurrency";
350                nargs += 1;
351                args[nargs] = "-I" CFA_INCDIR "containers";
352                nargs += 1;
353        } else {
354                args[nargs] = "-I" TOP_SRCDIR "libcfa/src";
355                nargs += 1;
356                if ( ! noincstd_flag ) {                                                        // do not use during build
357                        args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr";
358                        nargs += 1;
359                } // if
360                args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency";
361                nargs += 1;
362                args[nargs] = "-I" TOP_SRCDIR "libcfa/src" "/containers";
363                nargs += 1;
364        }
365
366        // add stdbool to get defines for bool/true/false
367        args[nargs] = "-imacros";
368        nargs += 1;
369        args[nargs] = "stdbool.h";
370        nargs += 1;
371
372        string libbase;
373        if( !intree ) {
374                libbase = CFA_LIBDIR;
375        } else {
376                libbase = TOP_BUILDDIR "libcfa/";
377                args[nargs] = "-D__CFA_FLAG__=-t";
378                nargs += 1;
379        }
380
381        const char * const arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
382        const char * config = debug ? "debug": "nodebug";
383        string libdir = libbase + arch + "-" + config;
384        if( !dirExists(libdir) ) {
385                cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl;
386                cerr << "Was looking for " << libdir << endl;
387                libdir = libbase + arch + "-" + "nolib";
388        }
389
390        if( !dirExists(libdir) ) {
391                cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
392                cerr << "Was looking for " << libdir << endl;
393                exit( EXIT_FAILURE );
394        }
395
396        args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir + (intree ? "/prelude" : "")) ).c_str();
397        nargs += 1;
398
399        if ( link ) {
400                args[nargs] = "-Xlinker";
401                nargs += 1;
402                args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
403                nargs += 1;
404                args[nargs] = "-Xlinker";
405                nargs += 1;
406                args[nargs] = "--undefined=__cfaabi_interpose_startup";
407                nargs += 1;
408                args[nargs] = "-Xlinker";
409                nargs += 1;
410                args[nargs] = "--undefined=__cfaabi_appready_startup";
411                nargs += 1;
412                args[nargs] = "-Xlinker";
413                nargs += 1;
414                args[nargs] = "--undefined=__cfaabi_dbg_record";
415                nargs += 1;
416
417                // include the cfa library in case it's needed
418                args[nargs] = ( *new string( string("-L" ) + libdir + (intree ? "/src" : "")) ).c_str();
419                nargs += 1;
420                args[nargs] = "-lcfa";
421                nargs += 1;
422                args[nargs] = "-lpthread";
423                nargs += 1;
424                args[nargs] = "-ldl";
425                nargs += 1;
426                args[nargs] = "-lrt";
427                nargs += 1;
428        } // if
429
430        // Add exception flags (unconditionally)
431        args[nargs] = "-fexceptions";
432        nargs += 1;
433
434        // add the correct set of flags based on the type of compile this is
435
436        args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
437        nargs += 1;
438        args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
439        nargs += 1;
440        args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
441        nargs += 1;
442        args[nargs] = "-D__CFA__";
443        nargs += 1;
444        args[nargs] = "-D__CFORALL__";
445        nargs += 1;
446        args[nargs] = "-D__cforall";
447        nargs += 1;
448
449        if ( cpp_flag ) {
450                args[nargs] = "-D__CPP__";
451                nargs += 1;
452        } // if
453
454        shuffle( args, sargs, nargs, 1 );                                       // make room at front of argument list
455        nargs += 1;
456        if ( CFA_flag ) {
457                args[sargs] = "-D__CFA_FLAG__=-N";
458                args[nargs] = "-D__CFA_PREPROCESS_";
459                nargs += 1;
460        } else {
461                args[sargs] = "-D__CFA_FLAG__=-L";
462        } // if
463        sargs += 1;
464
465        if ( debug ) {
466                heading += " (debug)";
467                args[nargs] = "-D__CFA_DEBUG__";
468                nargs += 1;
469        } else {
470                heading += " (no debug)";
471        } // if
472
473        if ( Bprefix.length() == 0 ) {
474                Bprefix = ! intree ? installlibdir : srcdriverdir;
475                if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
476                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
477                nargs += 1;
478        } // if
479
480        args[nargs] = "-Xlinker";                                                       // used by backtrace
481        nargs += 1;
482        args[nargs] = "-export-dynamic";
483        nargs += 1;
484
485        // execute the compilation command
486
487        args[0] = compiler_path.c_str();                                        // set compiler command for exec
488        // find actual name of the compiler independent of the path to it
489        int p = compiler_path.find_last_of( '/' );                      // scan r -> l for first '/'
490        if ( p == -1 ) {
491                compiler_name = compiler_path;
492        } else {
493                compiler_name = *new string( compiler_path.substr( p + 1 ) );
494        } // if
495
496        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
497                args[nargs] = "-no-integrated-cpp";
498                nargs += 1;
499                args[nargs] = "-Wno-deprecated";
500                nargs += 1;
501                if ( ! std_flag ) {                                                             // default c11, if none specified
502                        args[nargs] = "-std=gnu11";
503                        nargs += 1;
504                } // if
505                args[nargs] = "-fgnu89-inline";
506                nargs += 1;
507                args[nargs] = "-D__int8_t_defined";                             // prevent gcc type-size attributes
508                nargs += 1;
509                args[nargs] = ( *new string( string("-B") + Bprefix ) ).c_str();
510                nargs += 1;
511                args[nargs] = "-lm";
512                nargs += 1;
513        } else {
514                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
515                exit( EXIT_FAILURE );
516        } // if
517
518        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
519                args[nargs] = libs[i];
520                nargs += 1;
521        } // for
522
523        args[nargs] = NULL;                                                                     // terminate with NULL
524
525        #ifdef __DEBUG_H__
526        cerr << "nargs: " << nargs << endl;
527        cerr << "args:" << endl;
528        for ( int i = 0; args[i] != NULL; i += 1 ) {
529                cerr << " \"" << args[i] << "\"" << endl;
530        } // for
531        #endif // __DEBUG_H__
532
533        if ( ! quiet ) {
534                cerr << "CFA " << "Version " << Version << heading << endl;
535
536                if ( help ) {
537                        cerr <<
538                                "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
539                                "-help\t\t\t: print this help message" << endl <<
540                                "-quiet\t\t\t: print no messages from the cfa command" << endl <<
541                                "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
542                                "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
543                                "...\t\t\t: any other " << compiler_name << " flags" << endl;
544                } // if
545        } // if
546
547        if ( verbose ) {
548                if ( argc == 2 ) exit( EXIT_SUCCESS );                  // if only the -v flag is specified, do not invoke gcc
549
550                for ( int i = 0; args[i] != NULL; i += 1 ) {
551                        cerr << args[i] << " ";
552                } // for
553                cerr << endl;
554        } // if
555
556        if ( ! nonoptarg ) {
557                cerr << argv[0] << " error, no input files" << endl;
558                exit( EXIT_FAILURE );
559        } // if
560
561        // execute the command and return the result
562
563        execvp( args[0], (char *const *)args );                         // should not return
564        perror( "CFA Translator error: cfa level, execvp" );
565        exit( EXIT_FAILURE );
566} // main
567
568// Local Variables: //
569// tab-width: 4 //
570// mode: c++ //
571// compile-command: "make install" //
572// End: //
Note: See TracBrowser for help on using the repository browser.