source: driver/cfa.cc @ 0bf5340

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

updates after code review, use suffix .ifa for cc1 stage1 temp-file

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