source: driver/cfa.cc @ ef22ad6

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since ef22ad6 was 2c60af75, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

move cfa-cpp into stage 2 of cc1, and update cfa to facilitate the change

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