source: driver/cfa.cc @ 3ce2425

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

move bprefix to environment variable for cc1

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