source: driver/cfa.cc @ 096a2ff

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

Merge branch 'master' of plg2:software/cfa/cfa-cc

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