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