source: driver/cfa.cc @ def9d4e

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

change linker-file order

  • Property mode set to 100644
File size: 17.7 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// cfa.cc --
8//
9// Author           : Peter A. Buhr
10// Created On       : Tue Aug 20 13:44:49 2002
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jan 15 20:56:03 2019
13// Update Count     : 280
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        for ( int i = 0; i < nlibs; i += 1 ) {                          // copy non-user libraries after all user libraries
387                args[nargs] = libs[i];
388                nargs += 1;
389        } // for
390
391        if ( link ) {
392                args[nargs] = "-Xlinker";
393                nargs += 1;
394                args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
395                nargs += 1;
396                args[nargs] = "-Xlinker";
397                nargs += 1;
398                args[nargs] = "--undefined=__cfaabi_interpose_startup";
399                nargs += 1;
400                args[nargs] = "-Xlinker";
401                nargs += 1;
402                args[nargs] = "--undefined=__cfaabi_appready_startup";
403                nargs += 1;
404                args[nargs] = "-Xlinker";
405                nargs += 1;
406                args[nargs] = "--undefined=__cfaabi_dbg_record";
407                nargs += 1;
408
409                // include the cfa library in case it's needed
410                args[nargs] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
411                nargs += 1;
412                args[nargs] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str();
413                nargs += 1;
414                args[nargs] = "-lcfa";
415                nargs += 1;
416                args[nargs] = "-lpthread";
417                nargs += 1;
418                args[nargs] = "-ldl";
419                nargs += 1;
420                args[nargs] = "-lrt";
421                nargs += 1;
422                args[nargs] = "-lm";
423                nargs += 1;
424        } // if
425
426        // Add exception flags (unconditionally)
427        args[nargs] = "-fexceptions";
428        nargs += 1;
429
430        // add the correct set of flags based on the type of compile this is
431
432        args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
433        nargs += 1;
434        args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
435        nargs += 1;
436        args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
437        nargs += 1;
438        args[nargs] = "-D__CFA__";
439        nargs += 1;
440        args[nargs] = "-D__CFORALL__";
441        nargs += 1;
442        args[nargs] = "-D__cforall";
443        nargs += 1;
444
445        if ( cpp_flag ) {
446                args[nargs] = "-D__CPP__";
447                nargs += 1;
448        } // if
449
450        shuffle( args, sargs, nargs, 1 );                                       // make room at front of argument list
451        nargs += 1;
452        if ( CFA_flag ) {
453                args[sargs] = "-D__CFA_FLAG__=-N";
454                args[nargs] = "-D__CFA_PREPROCESS_";
455                nargs += 1;
456        } else {
457                args[sargs] = "-D__CFA_FLAG__=-L";
458        } // if
459        sargs += 1;
460
461        if ( debug ) {
462                heading += " (debug)";
463                args[nargs] = "-D__CFA_DEBUG__";
464                nargs += 1;
465        } else {
466                heading += " (no debug)";
467        } // if
468
469        if ( Bprefix.length() == 0 ) {
470                Bprefix = ! intree ? installlibdir : srcdriverdir;
471                if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/';
472                args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
473                nargs += 1;
474        } // if
475
476        args[nargs] = "-Xlinker";                                                       // used by backtrace
477        nargs += 1;
478        args[nargs] = "-export-dynamic";
479        nargs += 1;
480
481        // execute the compilation command
482
483        args[0] = compiler_path.c_str();                                        // set compiler command for exec
484        // find actual name of the compiler independent of the path to it
485        int p = compiler_path.find_last_of( '/' );                      // scan r -> l for first '/'
486        if ( p == -1 ) {
487                compiler_name = compiler_path;
488        } else {
489                compiler_name = *new string( compiler_path.substr( p + 1 ) );
490        } // if
491
492        if ( prefix( compiler_name, "gcc" ) ) {                         // allow suffix on gcc name
493                args[nargs] = "-no-integrated-cpp";
494                nargs += 1;
495                args[nargs] = "-Wno-deprecated";
496                nargs += 1;
497                if ( ! std_flag ) {                                                             // default c11, if none specified
498                        args[nargs] = "-std=gnu11";
499                        nargs += 1;
500                } // if
501                args[nargs] = "-fgnu89-inline";
502                nargs += 1;
503                args[nargs] = "-D__int8_t_defined";                             // prevent gcc type-size attributes
504                nargs += 1;
505                args[nargs] = ( *new string( string("-B") + Bprefix ) ).c_str();
506                nargs += 1;
507        } else {
508                cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
509                exit( EXIT_FAILURE );
510        } // if
511
512        args[nargs] = NULL;                                                                     // terminate with NULL
513
514        #ifdef __DEBUG_H__
515        cerr << "nargs: " << nargs << endl;
516        cerr << "args:" << endl;
517        for ( int i = 0; args[i] != NULL; i += 1 ) {
518                cerr << " \"" << args[i] << "\"" << endl;
519        } // for
520        #endif // __DEBUG_H__
521
522        if ( ! quiet ) {
523                cerr << "CFA " << "Version " << Version << heading << endl;
524
525                if ( help ) {
526                        cerr <<
527                                "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
528                                "-help\t\t\t: print this help message" << endl <<
529                                "-quiet\t\t\t: print no messages from the cfa command" << endl <<
530                                "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
531                                "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
532                                "...\t\t\t: any other " << compiler_name << " flags" << endl;
533                } // if
534        } // if
535
536        if ( verbose ) {
537                if ( argc == 2 ) exit( EXIT_SUCCESS );                  // if only the -v flag is specified, do not invoke gcc
538
539                for ( int i = 0; args[i] != NULL; i += 1 ) {
540                        cerr << args[i] << " ";
541                } // for
542                cerr << endl;
543        } // if
544
545        if ( ! nonoptarg ) {
546                cerr << argv[0] << " error, no input files" << endl;
547                exit( EXIT_FAILURE );
548        } // if
549
550        // execute the command and return the result
551
552        execvp( args[0], (char *const *)args );                         // should not return
553        perror( "CFA Translator error: cfa level, execvp" );
554        exit( EXIT_FAILURE );
555} // main
556
557// Local Variables: //
558// tab-width: 4 //
559// mode: c++ //
560// compile-command: "make install" //
561// End: //
Note: See TracBrowser for help on using the repository browser.