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