[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 |
---|
| 11 | // Last Modified By : Peter A. Buhr |
---|
[ec129c4] | 12 | // Last Modified On : Tue Oct 25 21:29:48 2016 |
---|
| 13 | // Update Count : 152 |
---|
[b87a5ed] | 14 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include <iostream> |
---|
[b87a5ed] | 17 | #include <cstdio> // perror |
---|
| 18 | #include <cstdlib> // putenv, exit |
---|
| 19 | #include <unistd.h> // execvp |
---|
| 20 | #include <string> // STL version |
---|
[51b7345] | 21 | |
---|
[b87a5ed] | 22 | #include "config.h" // configure info |
---|
[51b7345] | 23 | |
---|
| 24 | using std::cerr; |
---|
| 25 | using std::endl; |
---|
| 26 | using std::string; |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | //#define __DEBUG_H__ |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | bool prefix( string arg, string pre ) { |
---|
[b87a5ed] | 33 | return arg.substr( 0, pre.size() ) == pre; |
---|
[51b7345] | 34 | } // prefix |
---|
| 35 | |
---|
| 36 | |
---|
| 37 | void shuffle( const char *args[], int S, int E, int N ) { |
---|
[b87a5ed] | 38 | // S & E index 1 passed the end so adjust with -1 |
---|
[51b7345] | 39 | #ifdef __DEBUG_H__ |
---|
[b87a5ed] | 40 | cerr << "shuffle:" << S << " " << E << " " << N << endl; |
---|
[51b7345] | 41 | #endif // __DEBUG_H__ |
---|
[b87a5ed] | 42 | for ( int j = E-1 + N; j > S-1 + N; j -=1 ) { |
---|
[51b7345] | 43 | #ifdef __DEBUG_H__ |
---|
[b87a5ed] | 44 | cerr << "\t" << j << " " << j-N << endl; |
---|
[51b7345] | 45 | #endif // __DEBUG_H__ |
---|
[b87a5ed] | 46 | args[j] = args[j-N]; |
---|
| 47 | } // for |
---|
[51b7345] | 48 | } // shuffle |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | int main( int argc, char *argv[] ) { |
---|
[b87a5ed] | 52 | string Version( VERSION ); // current version number from CONFIG |
---|
[ec129c4] | 53 | string Major( CFA_VERSION_MAJOR ), Minor( CFA_VERSION_MINOR ), Patch( CFA_VERSION_MINOR ); |
---|
[51b7345] | 54 | |
---|
[00cc023] | 55 | string installincdir( CFA_INCDIR ); // fixed location of include files |
---|
| 56 | string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands |
---|
[51b7345] | 57 | |
---|
[b87a5ed] | 58 | string heading; // banner printed at start of cfa compilation |
---|
| 59 | string arg; // current command-line argument during command-line parsing |
---|
| 60 | string Bprefix; // path where gcc looks for compiler command steps |
---|
| 61 | string langstd; // language standard |
---|
[51b7345] | 62 | |
---|
[e24f13a] | 63 | string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler |
---|
[b87a5ed] | 64 | string compiler_name; // name of C compiler |
---|
[51b7345] | 65 | |
---|
[b87a5ed] | 66 | bool nonoptarg = false; // indicates non-option argument specified |
---|
| 67 | bool link = true; // linking as well as compiling |
---|
| 68 | bool verbose = false; // -v flag |
---|
| 69 | bool quiet = false; // -quiet flag |
---|
| 70 | bool debug = true; // -debug flag |
---|
| 71 | bool help = false; // -help flag |
---|
| 72 | bool CFA_flag = false; // -CFA flag |
---|
| 73 | bool cpp_flag = false; // -E or -M flag, preprocessor only |
---|
[de62360d] | 74 | bool std_flag = false; // -std= flag |
---|
[35f9114] | 75 | bool noincstd_flag = false; // -no-include-stdhdr= flag |
---|
[6e4b913] | 76 | bool debugging __attribute(( unused )) = false; // -g flag |
---|
[51b7345] | 77 | |
---|
[b87a5ed] | 78 | const char *args[argc + 100]; // cfa command line values, plus some space for additional flags |
---|
| 79 | int sargs = 1; // starting location for arguments in args list |
---|
| 80 | int nargs = sargs; // number of arguments in args list; 0 => command name |
---|
[51b7345] | 81 | |
---|
[b87a5ed] | 82 | const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags |
---|
| 83 | int nlibs = 0; |
---|
[51b7345] | 84 | |
---|
| 85 | #ifdef __DEBUG_H__ |
---|
[b87a5ed] | 86 | cerr << "CFA:" << endl; |
---|
[51b7345] | 87 | #endif // __DEBUG_H__ |
---|
| 88 | |
---|
[b87a5ed] | 89 | // process command-line arguments |
---|
[51b7345] | 90 | |
---|
[b87a5ed] | 91 | for ( int i = 1; i < argc; i += 1 ) { |
---|
[51b7345] | 92 | #ifdef __DEBUG_H__ |
---|
[b87a5ed] | 93 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; |
---|
[51b7345] | 94 | #endif // __DEBUG_H__ |
---|
[b87a5ed] | 95 | arg = argv[i]; // convert to string value |
---|
[51b7345] | 96 | #ifdef __DEBUG_H__ |
---|
[b87a5ed] | 97 | cerr << "arg:\"" << arg << "\"" << endl; |
---|
[51b7345] | 98 | #endif // __DEBUG_H__ |
---|
[b87a5ed] | 99 | if ( prefix( arg, "-" ) ) { |
---|
| 100 | // pass through arguments |
---|
| 101 | |
---|
| 102 | if ( arg == "-Xlinker" || arg == "-o" ) { |
---|
| 103 | args[nargs] = argv[i]; // pass the argument along |
---|
| 104 | nargs += 1; |
---|
| 105 | i += 1; |
---|
| 106 | if ( i == argc ) continue; // next argument available ? |
---|
| 107 | args[nargs] = argv[i]; // pass the argument along |
---|
| 108 | nargs += 1; |
---|
| 109 | } else if ( arg == "-XCFA" ) { // CFA pass through |
---|
| 110 | i += 1; |
---|
| 111 | args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str(); |
---|
| 112 | nargs += 1; |
---|
| 113 | |
---|
| 114 | // CFA specific arguments |
---|
| 115 | |
---|
| 116 | } else if ( arg == "-CFA" ) { |
---|
| 117 | CFA_flag = true; // strip the -CFA flag |
---|
| 118 | link = false; |
---|
| 119 | args[nargs] = "-E"; // replace the argument with -E |
---|
| 120 | nargs += 1; |
---|
| 121 | } else if ( arg == "-debug" ) { |
---|
| 122 | debug = true; // strip the debug flag |
---|
| 123 | } else if ( arg == "-nodebug" ) { |
---|
| 124 | debug = false; // strip the nodebug flag |
---|
| 125 | } else if ( arg == "-quiet" ) { |
---|
| 126 | quiet = true; // strip the quiet flag |
---|
| 127 | } else if ( arg == "-noquiet" ) { |
---|
| 128 | quiet = false; // strip the noquiet flag |
---|
| 129 | } else if ( arg == "-help" ) { |
---|
| 130 | help = true; // strip the help flag |
---|
| 131 | } else if ( arg == "-nohelp" ) { |
---|
| 132 | help = false; // strip the nohelp flag |
---|
[35f9114] | 133 | } else if ( arg == "-no-include-stdhdr" ) { |
---|
| 134 | noincstd_flag = true; // strip the no-include-stdhdr flag |
---|
[b87a5ed] | 135 | } else if ( arg == "-compiler" ) { |
---|
| 136 | // use the user specified compiler |
---|
| 137 | i += 1; |
---|
| 138 | if ( i == argc ) continue; // next argument available ? |
---|
| 139 | compiler_path = argv[i]; |
---|
| 140 | if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) { |
---|
| 141 | cerr << argv[0] << " error, cannot set environment variable." << endl; |
---|
| 142 | exit( EXIT_FAILURE ); |
---|
| 143 | } // if |
---|
| 144 | |
---|
[de62360d] | 145 | // C specific arguments |
---|
[b87a5ed] | 146 | |
---|
| 147 | } else if ( arg == "-v" ) { |
---|
| 148 | verbose = true; // verbosity required |
---|
| 149 | args[nargs] = argv[i]; // pass the argument along |
---|
| 150 | nargs += 1; |
---|
| 151 | } else if ( arg == "-g" ) { |
---|
| 152 | debugging = true; // symbolic debugging required |
---|
| 153 | args[nargs] = argv[i]; // pass the argument along |
---|
| 154 | nargs += 1; |
---|
[de62360d] | 155 | } else if ( prefix( arg, "-std=" ) ) { |
---|
[53ba273] | 156 | std_flag = true; // -std=XX provided |
---|
[de62360d] | 157 | args[nargs] = argv[i]; // pass the argument along |
---|
| 158 | nargs += 1; |
---|
[b87a5ed] | 159 | } else if ( prefix( arg, "-B" ) ) { |
---|
| 160 | Bprefix = arg.substr(2); // strip the -B flag |
---|
| 161 | args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); |
---|
| 162 | nargs += 1; |
---|
| 163 | } else if ( prefix( arg, "-b" ) ) { |
---|
| 164 | if ( arg.length() == 2 ) { // separate argument ? |
---|
| 165 | i += 1; |
---|
| 166 | if ( i == argc ) continue; // next argument available ? |
---|
| 167 | arg += argv[i]; // concatenate argument |
---|
| 168 | } // if |
---|
| 169 | // later versions of gcc require the -b option to appear at the start of the command line |
---|
| 170 | shuffle( args, sargs, nargs, 1 ); // make room at front of argument list |
---|
| 171 | args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along |
---|
| 172 | if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) { |
---|
| 173 | cerr << argv[0] << " error, cannot set environment variable." << endl; |
---|
| 174 | exit( EXIT_FAILURE ); |
---|
| 175 | } // if |
---|
| 176 | sargs += 1; |
---|
| 177 | nargs += 1; |
---|
| 178 | } else if ( prefix( arg, "-V" ) ) { |
---|
| 179 | if ( arg.length() == 2 ) { // separate argument ? |
---|
| 180 | i += 1; |
---|
| 181 | if ( i == argc ) continue; // next argument available ? |
---|
| 182 | arg += argv[i]; // concatenate argument |
---|
| 183 | } // if |
---|
| 184 | // later versions of gcc require the -V option to appear at the start of the command line |
---|
| 185 | shuffle( args, sargs, nargs, 1 ); // make room at front of argument list |
---|
| 186 | args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along |
---|
| 187 | if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) { |
---|
| 188 | cerr << argv[0] << " error, cannot set environment variable." << endl; |
---|
| 189 | exit( EXIT_FAILURE ); |
---|
| 190 | } // if |
---|
| 191 | sargs += 1; |
---|
| 192 | nargs += 1; |
---|
| 193 | } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) { |
---|
| 194 | args[nargs] = argv[i]; // pass the argument along |
---|
| 195 | nargs += 1; |
---|
| 196 | if ( arg == "-E" || arg == "-M" || arg == "-MM" ) { |
---|
| 197 | cpp_flag = true; // cpp only |
---|
| 198 | } // if |
---|
| 199 | link = false; // no linkage required |
---|
| 200 | } else if ( arg[1] == 'l' ) { |
---|
| 201 | // if the user specifies a library, load it after user code |
---|
| 202 | libs[nlibs] = argv[i]; |
---|
| 203 | nlibs += 1; |
---|
| 204 | } else { |
---|
| 205 | // concatenate any other arguments |
---|
| 206 | args[nargs] = argv[i]; |
---|
| 207 | nargs += 1; |
---|
| 208 | } // if |
---|
| 209 | } else { |
---|
| 210 | // concatenate other arguments |
---|
| 211 | args[nargs] = argv[i]; |
---|
| 212 | nargs += 1; |
---|
| 213 | nonoptarg = true; |
---|
| 214 | } // if |
---|
| 215 | } // for |
---|
[51b7345] | 216 | |
---|
[b87a5ed] | 217 | #ifdef __DEBUG_H__ |
---|
| 218 | cerr << "args:"; |
---|
| 219 | for ( int i = 1; i < nargs; i += 1 ) { |
---|
| 220 | cerr << " " << args[i]; |
---|
| 221 | } // for |
---|
| 222 | cerr << endl; |
---|
| 223 | #endif // __DEBUG_H__ |
---|
[51b7345] | 224 | |
---|
[b87a5ed] | 225 | if ( cpp_flag && CFA_flag ) { |
---|
| 226 | cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl; |
---|
| 227 | exit( EXIT_FAILURE ); |
---|
| 228 | } // if |
---|
[51b7345] | 229 | |
---|
[b87a5ed] | 230 | string d; |
---|
| 231 | if ( debug ) { |
---|
| 232 | d = "-d"; |
---|
| 233 | } // if |
---|
[51b7345] | 234 | |
---|
[6e4b913] | 235 | // add the CFA include-library paths, which allow direct access to header files without directory qualification |
---|
| 236 | |
---|
[b87a5ed] | 237 | args[nargs] = "-I" CFA_INCDIR; |
---|
| 238 | nargs += 1; |
---|
[d0542c4] | 239 | if ( ! noincstd_flag ) { // do not use during build |
---|
| 240 | args[nargs] = "-I" CFA_INCDIR "/stdhdr"; |
---|
| 241 | nargs += 1; |
---|
| 242 | } // if |
---|
[76c7f65e] | 243 | args[nargs] = "-I" CFA_INCDIR "/containers"; |
---|
| 244 | nargs += 1; |
---|
| 245 | |
---|
[b87a5ed] | 246 | if ( link ) { |
---|
| 247 | // include the cfa library in case it's needed |
---|
| 248 | args[nargs] = "-L" CFA_LIBDIR; |
---|
[51b7345] | 249 | nargs += 1; |
---|
[b87a5ed] | 250 | args[nargs] = "-lcfa"; |
---|
[51b7345] | 251 | nargs += 1; |
---|
| 252 | } // if |
---|
| 253 | |
---|
[b87a5ed] | 254 | // add the correct set of flags based on the type of compile this is |
---|
[8c17ab0] | 255 | |
---|
[ec129c4] | 256 | args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str(); |
---|
[51b7345] | 257 | nargs += 1; |
---|
[b87a5ed] | 258 | args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str(); |
---|
[51b7345] | 259 | nargs += 1; |
---|
[ec129c4] | 260 | args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str(); |
---|
[1db21619] | 261 | nargs += 1; |
---|
[4c82a3c] | 262 | args[nargs] = "-D__CFA__"; |
---|
| 263 | nargs += 1; |
---|
| 264 | args[nargs] = "-D__CFORALL__"; |
---|
[02e5ab6] | 265 | nargs += 1; |
---|
[51b7345] | 266 | |
---|
[b87a5ed] | 267 | if ( cpp_flag ) { |
---|
| 268 | args[nargs] = "-D__CPP__"; |
---|
| 269 | nargs += 1; |
---|
| 270 | } // if |
---|
[51b7345] | 271 | |
---|
[b87a5ed] | 272 | if ( CFA_flag ) { |
---|
[4c82a3c] | 273 | args[nargs] = "-D__CFA_PREPROCESS_"; |
---|
[b87a5ed] | 274 | nargs += 1; |
---|
| 275 | } // if |
---|
[51b7345] | 276 | |
---|
[b87a5ed] | 277 | if ( debug ) { |
---|
| 278 | heading += " (debug)"; |
---|
| 279 | args[nargs] = "-D__CFA_DEBUG__"; |
---|
| 280 | nargs += 1; |
---|
| 281 | } else { |
---|
| 282 | heading += " (no debug)"; |
---|
| 283 | } // if |
---|
[51b7345] | 284 | |
---|
[b87a5ed] | 285 | if ( Bprefix.length() == 0 ) { |
---|
| 286 | Bprefix = installlibdir; |
---|
| 287 | args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); |
---|
| 288 | nargs += 1; |
---|
| 289 | } // if |
---|
[51b7345] | 290 | |
---|
[b87a5ed] | 291 | // execute the compilation command |
---|
[51b7345] | 292 | |
---|
[b87a5ed] | 293 | args[0] = compiler_path.c_str(); // set compiler command for exec |
---|
| 294 | // find actual name of the compiler independent of the path to it |
---|
| 295 | int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/' |
---|
| 296 | if ( p == -1 ) { |
---|
| 297 | compiler_name = compiler_path; |
---|
| 298 | } else { |
---|
| 299 | compiler_name = *new string( compiler_path.substr( p + 1 ) ); |
---|
| 300 | } // if |
---|
[51b7345] | 301 | |
---|
[b87a5ed] | 302 | if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name |
---|
| 303 | args[nargs] = "-no-integrated-cpp"; |
---|
| 304 | nargs += 1; |
---|
[76c7f65e] | 305 | args[nargs] = "-Wno-deprecated"; |
---|
[b87a5ed] | 306 | nargs += 1; |
---|
[de62360d] | 307 | if ( ! std_flag ) { // default c99, if none specified |
---|
[53ba273] | 308 | args[nargs] = "-std=gnu99"; |
---|
[de62360d] | 309 | nargs += 1; |
---|
| 310 | } // if |
---|
[76c7f65e] | 311 | args[nargs] = "-fgnu89-inline"; |
---|
[6e991d6] | 312 | nargs += 1; |
---|
[b87a5ed] | 313 | args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str(); |
---|
| 314 | nargs += 1; |
---|
[d3b7937] | 315 | args[nargs] = "-lm"; |
---|
| 316 | nargs += 1; |
---|
[b87a5ed] | 317 | } else { |
---|
[e24f13a] | 318 | cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl; |
---|
[b87a5ed] | 319 | exit( EXIT_FAILURE ); |
---|
| 320 | } // if |
---|
[51b7345] | 321 | |
---|
[b87a5ed] | 322 | for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries |
---|
| 323 | args[nargs] = libs[i]; |
---|
| 324 | nargs += 1; |
---|
| 325 | } // for |
---|
[51b7345] | 326 | |
---|
[b87a5ed] | 327 | args[nargs] = NULL; // terminate with NULL |
---|
[51b7345] | 328 | |
---|
| 329 | #ifdef __DEBUG_H__ |
---|
[b87a5ed] | 330 | cerr << "nargs: " << nargs << endl; |
---|
| 331 | cerr << "args:" << endl; |
---|
| 332 | for ( int i = 0; args[i] != NULL; i += 1 ) { |
---|
| 333 | cerr << " \"" << args[i] << "\"" << endl; |
---|
| 334 | } // for |
---|
[51b7345] | 335 | #endif // __DEBUG_H__ |
---|
| 336 | |
---|
[b87a5ed] | 337 | if ( ! quiet ) { |
---|
| 338 | cerr << "CFA " << "Version " << Version << heading << endl; |
---|
| 339 | |
---|
| 340 | if ( help ) { |
---|
| 341 | cerr << |
---|
| 342 | "-debug\t\t\t: use cfa runtime with debug checking" << endl << |
---|
| 343 | "-help\t\t\t: print this help message" << endl << |
---|
| 344 | "-quiet\t\t\t: print no messages from the cfa command" << endl << |
---|
| 345 | "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl << |
---|
| 346 | "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl << |
---|
| 347 | "...\t\t\t: any other " << compiler_name << " flags" << endl; |
---|
| 348 | } // if |
---|
[51b7345] | 349 | } // if |
---|
| 350 | |
---|
[b87a5ed] | 351 | if ( verbose ) { |
---|
| 352 | if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc |
---|
[51b7345] | 353 | |
---|
[b87a5ed] | 354 | for ( int i = 0; args[i] != NULL; i += 1 ) { |
---|
| 355 | cerr << args[i] << " "; |
---|
| 356 | } // for |
---|
| 357 | cerr << endl; |
---|
| 358 | } // if |
---|
[51b7345] | 359 | |
---|
[b87a5ed] | 360 | if ( ! nonoptarg ) { |
---|
| 361 | cerr << argv[0] << " error, no input files" << endl; |
---|
| 362 | exit( EXIT_FAILURE ); |
---|
| 363 | } // if |
---|
[51b7345] | 364 | |
---|
[b87a5ed] | 365 | // execute the command and return the result |
---|
[51b7345] | 366 | |
---|
[b87a5ed] | 367 | execvp( args[0], (char *const *)args ); // should not return |
---|
| 368 | perror( "CFA Translator error: cfa level, execvp" ); |
---|
| 369 | exit( EXIT_FAILURE ); |
---|
[51b7345] | 370 | } // main |
---|
| 371 | |
---|
| 372 | // Local Variables: // |
---|
[b87a5ed] | 373 | // tab-width: 4 // |
---|
| 374 | // mode: c++ // |
---|
[51b7345] | 375 | // compile-command: "make install" // |
---|
| 376 | // End: // |
---|