source: driver/cfa.cc @ 265e460

ADTast-experimental
Last change on this file since 265e460 was 09f34a84, checked in by Thierry Delisle <tdelisle@…>, 22 months ago

Remove some of the warnings on the new clang

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