source: driver/cfa.cc @ 9705ffe

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 9705ffe was 9b34766, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

add command-line flag -latomic for ARM

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