Changes in driver/cfa.cc [92a9768:bbb1b35]
- File:
-
- 1 edited
-
driver/cfa.cc (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
driver/cfa.cc
r92a9768 rbbb1b35 10 10 // Created On : Tue Aug 20 13:44:49 2002 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jan 31 16:48:03 202013 // Update Count : 4 2112 // Last Modified On : Fri Aug 23 16:27:07 2019 13 // Update Count : 411 14 14 // 15 15 16 16 #include <iostream> 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 17 #include <cstdio> // perror 18 #include <cstdlib> // putenv, exit 19 #include <unistd.h> // execvp 20 #include <string> // STL version 21 #include <string.h> // strcmp 22 #include <algorithm> // find 24 23 25 24 #include <sys/types.h> … … 34 33 using std::to_string; 35 34 36 // #define __DEBUG_H__ 37 38 // "N__=" suffix 39 static string __CFA_FLAGPREFIX__( "__CFA_FLAG" ); 35 //#define __DEBUG_H__ 36 40 37 41 38 void Putenv( char * argv[], string arg ) { 42 // environment variables must have unique names 43 static int flags = 0; 44 45 if ( putenv( (char *)( *new string( string( __CFA_FLAGPREFIX__ + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) ) { 39 static int flags = 0; // environment variables must have unique names 40 41 if ( putenv( (char *)( *new string( string( "__CFA_FLAG" + to_string( flags++ ) + "__=" ) + arg ) ).c_str() ) ) { 46 42 cerr << argv[0] << " error, cannot set environment variable." << endl; 47 43 exit( EXIT_FAILURE ); … … 49 45 } // Putenv 50 46 51 // check if string has prefix 52 bool prefix( const string & arg, const string & pre ) { 47 48 bool prefix( const string & arg, const string & pre ) { // check if string has prefix 53 49 return arg.substr( 0, pre.size() ) == pre; 54 50 } // prefix 55 51 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 61 // check if string has suffix 62 bool suffix( const string & arg ) { 52 bool suffix( const string & arg ) { // check if string has suffix 63 53 enum { NumSuffixes = 3 }; 64 54 static const string suffixes[NumSuffixes] = { "cfa", "hfa", "ifa" }; … … 74 64 struct stat info; 75 65 if ( stat( path.c_str(), &info ) != 0 ) return false; 76 return (info.st_mode & S_IFDIR) != 0; 66 if ( info.st_mode & S_IFDIR ) return true; 67 return false; 77 68 } // dirExists 78 79 static inline string dir(const string & path) {80 return path.substr(0, path.find_last_of('/'));81 }82 83 // Different path modes84 enum PathMode {85 Installed, // cfa is installed, use prefix86 BuildTree, // cfa is in the tree, use source and build tree87 Distributed // cfa is distributed, use build tree for includes and executable directory for .cfs88 };89 90 // Get path mode from /proc91 PathMode FromProc() {92 std::string abspath;93 abspath.resize(PATH_MAX);94 95 // get executable path from /proc/self/exe96 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 characters104 abspath.resize(size);105 106 // Are we installed107 if(abspath.rfind(CFA_BINDIR , 0) == 0) { return Installed; }108 109 // Is this the build tree110 if(abspath.rfind(TOP_BUILDDIR, 0) == 0) { return BuildTree; }111 112 // Does this look like distcc113 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 are116 std::cerr << "Cannot find required files from excutable path " << abspath << std::endl;117 std::exit(1);118 }119 69 120 70 … … 132 82 string heading; // banner printed at start of cfa compilation 133 83 string arg; // current command-line argument during command-line parsing 134 string bprefix; // path where gcc looks for compilersteps84 string Bprefix; // path where gcc looks for compiler command steps 135 85 string langstd; // language standard 136 86 … … 153 103 bool m32 = false; // -m32 flag 154 104 bool m64 = false; // -m64 flag 155 bool compiling_libs = false;105 bool intree = false; // build in tree 156 106 int o_file = 0; // -o filename position 157 158 PathMode path = FromProc();159 107 160 108 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags … … 187 135 } else if ( arg == "-XCFA" ) { // CFA pass through 188 136 i += 1; 189 if ( i == argc ) continue; // next argument available ?190 137 Putenv( argv, argv[i] ); 191 138 … … 212 159 } else if ( arg == "-no-include-stdhdr" ) { 213 160 noincstd_flag = true; // strip the no-include-stdhdr flag 214 } else if ( arg == "- cfalib") {215 compiling_libs= true;161 } else if ( arg == "-in-tree" ) { 162 intree = true; 216 163 } else if ( arg == "-compiler" ) { 217 164 // use the user specified compiler … … 264 211 } // if 265 212 } else if ( prefix( arg, "-B" ) ) { 266 bprefix = arg.substr(2); // strip the -B flag 213 Bprefix = arg.substr(2); // strip the -B flag 214 args[nargs++] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); 267 215 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) { 268 216 args[nargs++] = argv[i]; // pass argument along … … 315 263 // -E flag stops at cc1 stage 1, so cfa-cpp in cc1 stage 2 is never executed. 316 264 if ( cpp_flag && CFA_flag ) { 317 CFA_flag = false;318 cerr << argv[0] << " warning, both -E and -CFA flags specified, using -E and ignoring -CFA." << endl;265 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl; 266 exit( EXIT_FAILURE ); 319 267 } // if 320 268 321 269 // add the CFA include-library paths, which allow direct access to header files without directory qualification 322 string libbase; 323 switch(path) { 324 case Installed: 270 if ( ! intree ) { 325 271 args[nargs++] = "-I" CFA_INCDIR; 326 // do not use during build 327 if ( ! noincstd_flag ) { 272 if ( ! noincstd_flag ) { // do not use during build 328 273 args[nargs++] = "-I" CFA_INCDIR "stdhdr"; 329 274 } // if 330 275 args[nargs++] = "-I" CFA_INCDIR "concurrency"; 331 276 args[nargs++] = "-I" CFA_INCDIR "containers"; 332 libbase = CFA_LIBDIR; 333 break; 334 case BuildTree: 335 case Distributed: 277 } else { 336 278 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src"; 337 // do not use during build 338 if ( ! noincstd_flag ) { 279 if ( ! noincstd_flag ) { // do not use during build 339 280 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/stdhdr"; 340 281 } // if 341 282 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/concurrency"; 342 283 args[nargs++] = "-I" TOP_SRCDIR "libcfa/src" "/containers"; 343 344 libbase = TOP_BUILDDIR "libcfa/";345 346 break;347 284 } // if 348 285 … … 351 288 args[nargs++] = "stdbool.h"; 352 289 353 if( compiling_libs ) { 290 string libbase; 291 if ( ! intree ) { 292 libbase = CFA_LIBDIR; 293 } else { 294 libbase = TOP_BUILDDIR "libcfa/"; 354 295 Putenv( argv, "-t" ); 355 296 } // if … … 364 305 } // if 365 306 366 const char * config = nolib ? "nolib" : (debug ? "debug": "nodebug"); 367 string libdir = libbase + arch + "-" + config; 368 369 if (path != Distributed) { 370 if ( ! nolib && ! dirExists( libdir ) ) { 371 cerr << argv[0] << " internal error, configuration " << config << " not installed." << endl; 372 cerr << "Was looking for " << libdir << endl; 373 for(int i = 1; i < argc; i++) { 374 cerr << argv[i] << " "; 375 } 376 cerr << endl; 377 libdir = libbase + arch + "-" + "nolib"; 378 } // if 379 380 if ( ! dirExists( libdir ) ) { 381 cerr << argv[0] << " internal error, cannot find prelude directory." << endl; 382 cerr << "Was looking for " << libdir << endl; 383 exit( EXIT_FAILURE ); 384 } // if 385 } // if 386 387 switch(path) { 388 case Installed : Putenv( argv, "--prelude-dir=" + libdir ); break; 389 case BuildTree : Putenv( argv, "--prelude-dir=" + libdir + "/prelude" ); break; 390 case Distributed : Putenv( argv, "--prelude-dir=" + dir(argv[0]) ); break; 391 } 307 string libdir( libbase + arch + "-" + (nolib ? "nolib" : (debug ? "debug": "nodebug")) ); 308 if ( ! dirExists( libdir ) ) { 309 cerr << argv[0] << " internal error, cannot find prelude directory " << libdir << endl; 310 exit( EXIT_FAILURE ); 311 } // if 392 312 393 313 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries … … 402 322 args[nargs++] = "-Xlinker"; 403 323 args[nargs++] = "--undefined=__cfaabi_appready_startup"; 404 args[nargs++] = "-z"; 405 args[nargs++] = "execstack"; 406 407 // include the cfa library in case it is needed 408 args[nargs++] = ( *new string( string("-L" ) + libdir + (path != Installed ? "/src/.libs" : "")) ).c_str(); 409 args[nargs++] = ( *new string( string("-Wl,-rpath," ) + libdir + (path != Installed ? "/src/.libs" : "")) ).c_str(); 324 325 // include the cfa library in case it's needed 326 args[nargs++] = ( *new string( string("-L" ) + libdir + (intree ? "/src/.libs" : "")) ).c_str(); 327 args[nargs++] = ( *new string( string("-Wl,-rpath," ) + libdir + (intree ? "/src/.libs" : "")) ).c_str(); 410 328 args[nargs++] = "-Wl,--push-state,--as-needed"; 411 329 args[nargs++] = "-lcfathread"; 412 330 args[nargs++] = "-Wl,--pop-state"; 413 args[nargs++] = "-Wl,--push-state,--no-as-needed";414 331 args[nargs++] = "-lcfa"; 415 args[nargs++] = "-Wl,--pop-state"; 416 args[nargs++] = "-pthread"; 332 args[nargs++] = "-lpthread"; 417 333 args[nargs++] = "-ldl"; 418 334 args[nargs++] = "-lrt"; … … 445 361 } // if 446 362 363 Putenv( argv, "--prelude-dir=" + libdir + (intree ? "/prelude" : "") ); 364 447 365 if ( debug ) { 448 366 heading += " (debug)"; … … 452 370 } // if 453 371 454 if ( bprefix.length() == 0 ) { 455 switch(path) { 456 case Installed : bprefix = installlibdir; break; 457 case BuildTree : bprefix = srcdriverdir ; break; 458 case Distributed : bprefix = dir(argv[0]) ; break; 459 } 460 if ( bprefix[bprefix.length() - 1] != '/' ) bprefix += '/'; 461 Putenv( argv, string("-B=") + bprefix ); 372 if ( Bprefix.length() == 0 ) { 373 Bprefix = ! intree ? installlibdir : srcdriverdir; 374 if ( Bprefix[Bprefix.length() - 1] != '/' ) Bprefix += '/'; 375 args[nargs++] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); 462 376 } // if 463 377 … … 487 401 args[nargs++] = "-fgnu89-inline"; 488 402 args[nargs++] = "-D__int8_t_defined"; // prevent gcc type-size attributes 489 args[nargs++] = ( *new string( string("-B") + bprefix ) ).c_str();403 args[nargs++] = ( *new string( string("-B") + Bprefix ) ).c_str(); 490 404 } else { 491 405 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl; … … 501 415 cerr << " \"" << args[i] << "\"" << endl; 502 416 } // for 503 cerr << endl;504 417 #endif // __DEBUG_H__ 505 418 506 419 if ( ! quiet ) { 507 420 cerr << "CFA " << "Version " << Version << heading << endl; 421 508 422 if ( help ) { 509 423 cerr <<
Note:
See TracChangeset
for help on using the changeset viewer.