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