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