| 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 | //
|
|---|
| 7 | // cfa.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Peter A. Buhr
|
|---|
| 10 | // Created On : Tue Aug 20 13:44:49 2002
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Thu Jul 20 15:54:45 2017
|
|---|
| 13 | // Update Count : 156
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <iostream>
|
|---|
| 17 | #include <cstdio> // perror
|
|---|
| 18 | #include <cstdlib> // putenv, exit
|
|---|
| 19 | #include <unistd.h> // execvp
|
|---|
| 20 | #include <string> // STL version
|
|---|
| 21 |
|
|---|
| 22 | #include "config.h" // configure info
|
|---|
| 23 |
|
|---|
| 24 | using std::cerr;
|
|---|
| 25 | using std::endl;
|
|---|
| 26 | using std::string;
|
|---|
| 27 | using std::to_string;
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | //#define __DEBUG_H__
|
|---|
| 31 |
|
|---|
| 32 |
|
|---|
| 33 | bool prefix( string arg, string pre ) {
|
|---|
| 34 | return arg.substr( 0, pre.size() ) == pre;
|
|---|
| 35 | } // prefix
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | void shuffle( const char *args[], int S, int E, int N ) {
|
|---|
| 39 | // S & E index 1 passed the end so adjust with -1
|
|---|
| 40 | #ifdef __DEBUG_H__
|
|---|
| 41 | cerr << "shuffle:" << S << " " << E << " " << N << endl;
|
|---|
| 42 | #endif // __DEBUG_H__
|
|---|
| 43 | for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
|
|---|
| 44 | #ifdef __DEBUG_H__
|
|---|
| 45 | cerr << "\t" << j << " " << j-N << endl;
|
|---|
| 46 | #endif // __DEBUG_H__
|
|---|
| 47 | args[j] = args[j-N];
|
|---|
| 48 | } // for
|
|---|
| 49 | } // shuffle
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | #define str(s) #s
|
|---|
| 53 |
|
|---|
| 54 | int main( int argc, char *argv[] ) {
|
|---|
| 55 | string Version( CFA_VERSION_LONG ); // current version number from CONFIG
|
|---|
| 56 | string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
|
|---|
| 57 |
|
|---|
| 58 | string installincdir( CFA_INCDIR ); // fixed location of include files
|
|---|
| 59 | string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands
|
|---|
| 60 |
|
|---|
| 61 | string heading; // banner printed at start of cfa compilation
|
|---|
| 62 | string arg; // current command-line argument during command-line parsing
|
|---|
| 63 | string Bprefix; // path where gcc looks for compiler command steps
|
|---|
| 64 | string langstd; // language standard
|
|---|
| 65 |
|
|---|
| 66 | string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
|
|---|
| 67 | string compiler_name; // name of C compiler
|
|---|
| 68 |
|
|---|
| 69 | bool nonoptarg = false; // indicates non-option argument specified
|
|---|
| 70 | bool link = true; // linking as well as compiling
|
|---|
| 71 | bool verbose = false; // -v flag
|
|---|
| 72 | bool quiet = false; // -quiet flag
|
|---|
| 73 | bool debug = true; // -debug flag
|
|---|
| 74 | bool help = false; // -help flag
|
|---|
| 75 | bool CFA_flag = false; // -CFA flag
|
|---|
| 76 | bool cpp_flag = false; // -E or -M flag, preprocessor only
|
|---|
| 77 | bool std_flag = false; // -std= flag
|
|---|
| 78 | bool debugging __attribute(( unused )) = false; // -g flag
|
|---|
| 79 |
|
|---|
| 80 | const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
|
|---|
| 81 | int sargs = 1; // starting location for arguments in args list
|
|---|
| 82 | int nargs = sargs; // number of arguments in args list; 0 => command name
|
|---|
| 83 |
|
|---|
| 84 | const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
|
|---|
| 85 | int nlibs = 0;
|
|---|
| 86 |
|
|---|
| 87 | #ifdef __DEBUG_H__
|
|---|
| 88 | cerr << "CFA:" << endl;
|
|---|
| 89 | #endif // __DEBUG_H__
|
|---|
| 90 |
|
|---|
| 91 | // process command-line arguments
|
|---|
| 92 |
|
|---|
| 93 | for ( int i = 1; i < argc; i += 1 ) {
|
|---|
| 94 | #ifdef __DEBUG_H__
|
|---|
| 95 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
|
|---|
| 96 | #endif // __DEBUG_H__
|
|---|
| 97 | arg = argv[i]; // convert to string value
|
|---|
| 98 | #ifdef __DEBUG_H__
|
|---|
| 99 | cerr << "arg:\"" << arg << "\"" << endl;
|
|---|
| 100 | #endif // __DEBUG_H__
|
|---|
| 101 | if ( prefix( arg, "-" ) ) {
|
|---|
| 102 | // pass through arguments
|
|---|
| 103 |
|
|---|
| 104 | if ( arg == "-Xlinker" || arg == "-o" ) {
|
|---|
| 105 | args[nargs] = argv[i]; // pass the argument along
|
|---|
| 106 | nargs += 1;
|
|---|
| 107 | i += 1;
|
|---|
| 108 | if ( i == argc ) continue; // next argument available ?
|
|---|
| 109 | args[nargs] = argv[i]; // pass the argument along
|
|---|
| 110 | nargs += 1;
|
|---|
| 111 | } else if ( arg == "-XCFA" ) { // CFA pass through
|
|---|
| 112 | i += 1;
|
|---|
| 113 | args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
|
|---|
| 114 | nargs += 1;
|
|---|
| 115 |
|
|---|
| 116 | // CFA specific arguments
|
|---|
| 117 |
|
|---|
| 118 | } else if ( arg == "-CFA" ) {
|
|---|
| 119 | CFA_flag = true; // strip the -CFA flag
|
|---|
| 120 | link = false;
|
|---|
| 121 | args[nargs] = "-E"; // replace the argument with -E
|
|---|
| 122 | nargs += 1;
|
|---|
| 123 | } else if ( arg == "-debug" ) {
|
|---|
| 124 | debug = true; // strip the debug flag
|
|---|
| 125 | } else if ( arg == "-nodebug" ) {
|
|---|
| 126 | debug = false; // strip the nodebug flag
|
|---|
| 127 | } else if ( arg == "-quiet" ) {
|
|---|
| 128 | quiet = true; // strip the quiet flag
|
|---|
| 129 | } else if ( arg == "-noquiet" ) {
|
|---|
| 130 | quiet = false; // strip the noquiet flag
|
|---|
| 131 | } else if ( arg == "-help" ) {
|
|---|
| 132 | help = true; // strip the help flag
|
|---|
| 133 | } else if ( arg == "-nohelp" ) {
|
|---|
| 134 | help = false; // strip the nohelp flag
|
|---|
| 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 |
|
|---|
| 145 | // C specific arguments
|
|---|
| 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;
|
|---|
| 155 | } else if ( prefix( arg, "-std=" ) ) {
|
|---|
| 156 | std_flag = true; // -std=XX provided
|
|---|
| 157 | args[nargs] = argv[i]; // pass the argument along
|
|---|
| 158 | nargs += 1;
|
|---|
| 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
|
|---|
| 216 |
|
|---|
| 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__
|
|---|
| 224 |
|
|---|
| 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
|
|---|
| 229 |
|
|---|
| 230 | // add the CFA include-library paths, which allow direct access to header files without directory qualification
|
|---|
| 231 | args[nargs] = "-I" CFA_INCDIR;
|
|---|
| 232 | nargs += 1;
|
|---|
| 233 | args[nargs] = "-I" CFA_INCDIR "/stdhdr";
|
|---|
| 234 | nargs += 1;
|
|---|
| 235 | args[nargs] = "-I" CFA_INCDIR "/concurrency";
|
|---|
| 236 | nargs += 1;
|
|---|
| 237 | args[nargs] = "-I" CFA_INCDIR "/containers";
|
|---|
| 238 | nargs += 1;
|
|---|
| 239 |
|
|---|
| 240 | #ifdef HAVE_LIBCFA
|
|---|
| 241 | if ( link ) {
|
|---|
| 242 | #if ! defined(HAVE_LIBCFA_RELEASE)
|
|---|
| 243 | if( !debug ) {
|
|---|
| 244 | cerr << "error: Option -nodebug is not available, libcfa was not installed." << endl;
|
|---|
| 245 | exit( EXIT_FAILURE );
|
|---|
| 246 | }
|
|---|
| 247 | #endif
|
|---|
| 248 | #if ! defined(HAVE_LIBCFA_DEBUG)
|
|---|
| 249 | if( debug ) {
|
|---|
| 250 | cerr << "error: Option -debug is not available, libcfa-d was not installed." << endl;
|
|---|
| 251 | exit( EXIT_FAILURE );
|
|---|
| 252 | }
|
|---|
| 253 | #endif
|
|---|
| 254 |
|
|---|
| 255 | // include the cfa library in case it's needed
|
|---|
| 256 | args[nargs] = "-L" CFA_LIBDIR;
|
|---|
| 257 | nargs += 1;
|
|---|
| 258 | if( debug ) {
|
|---|
| 259 | args[nargs] = "-lcfa-d";
|
|---|
| 260 | } else {
|
|---|
| 261 | args[nargs] = "-lcfa";
|
|---|
| 262 | }
|
|---|
| 263 | nargs += 1;
|
|---|
| 264 | args[nargs] = "-lpthread";
|
|---|
| 265 | nargs += 1;
|
|---|
| 266 | args[nargs] = "-ldl";
|
|---|
| 267 | nargs += 1;
|
|---|
| 268 | args[nargs] = "-lrt";
|
|---|
| 269 | nargs += 1;
|
|---|
| 270 | args[nargs] = "-Xlinker";
|
|---|
| 271 | nargs += 1;
|
|---|
| 272 | args[nargs] = "--undefined=__lib_debug_write";
|
|---|
| 273 | nargs += 1;
|
|---|
| 274 |
|
|---|
| 275 | } // if
|
|---|
| 276 | #endif //HAVE_LIBCFA
|
|---|
| 277 |
|
|---|
| 278 | // add the correct set of flags based on the type of compile this is
|
|---|
| 279 |
|
|---|
| 280 | args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
|
|---|
| 281 | nargs += 1;
|
|---|
| 282 | args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
|
|---|
| 283 | nargs += 1;
|
|---|
| 284 | args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
|
|---|
| 285 | nargs += 1;
|
|---|
| 286 | args[nargs] = "-D__CFA__";
|
|---|
| 287 | nargs += 1;
|
|---|
| 288 | args[nargs] = "-D__CFORALL__";
|
|---|
| 289 | nargs += 1;
|
|---|
| 290 | args[nargs] = "-D__cforall";
|
|---|
| 291 | nargs += 1;
|
|---|
| 292 |
|
|---|
| 293 | if ( cpp_flag ) {
|
|---|
| 294 | args[nargs] = "-D__CPP__";
|
|---|
| 295 | nargs += 1;
|
|---|
| 296 | } // if
|
|---|
| 297 |
|
|---|
| 298 | if ( CFA_flag ) {
|
|---|
| 299 | args[nargs] = "-D__CFA_PREPROCESS_";
|
|---|
| 300 | nargs += 1;
|
|---|
| 301 | } // if
|
|---|
| 302 |
|
|---|
| 303 | if ( debug ) {
|
|---|
| 304 | heading += " (debug)";
|
|---|
| 305 | args[nargs] = "-D__CFA_DEBUG__";
|
|---|
| 306 | nargs += 1;
|
|---|
| 307 | } else {
|
|---|
| 308 | heading += " (no debug)";
|
|---|
| 309 | } // if
|
|---|
| 310 |
|
|---|
| 311 | if ( Bprefix.length() == 0 ) {
|
|---|
| 312 | Bprefix = installlibdir;
|
|---|
| 313 | args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
|
|---|
| 314 | nargs += 1;
|
|---|
| 315 | } // if
|
|---|
| 316 |
|
|---|
| 317 | // execute the compilation command
|
|---|
| 318 |
|
|---|
| 319 | args[0] = compiler_path.c_str(); // set compiler command for exec
|
|---|
| 320 | // find actual name of the compiler independent of the path to it
|
|---|
| 321 | int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
|
|---|
| 322 | if ( p == -1 ) {
|
|---|
| 323 | compiler_name = compiler_path;
|
|---|
| 324 | } else {
|
|---|
| 325 | compiler_name = *new string( compiler_path.substr( p + 1 ) );
|
|---|
| 326 | } // if
|
|---|
| 327 |
|
|---|
| 328 | if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
|
|---|
| 329 | args[nargs] = "-no-integrated-cpp";
|
|---|
| 330 | nargs += 1;
|
|---|
| 331 | args[nargs] = "-Wno-deprecated";
|
|---|
| 332 | nargs += 1;
|
|---|
| 333 | if ( ! std_flag ) { // default c99, if none specified
|
|---|
| 334 | args[nargs] = "-std=gnu99";
|
|---|
| 335 | nargs += 1;
|
|---|
| 336 | } // if
|
|---|
| 337 | args[nargs] = "-fgnu89-inline";
|
|---|
| 338 | nargs += 1;
|
|---|
| 339 | args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
|
|---|
| 340 | nargs += 1;
|
|---|
| 341 | args[nargs] = "-lm";
|
|---|
| 342 | nargs += 1;
|
|---|
| 343 | } else {
|
|---|
| 344 | cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
|
|---|
| 345 | exit( EXIT_FAILURE );
|
|---|
| 346 | } // if
|
|---|
| 347 |
|
|---|
| 348 | for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
|
|---|
| 349 | args[nargs] = libs[i];
|
|---|
| 350 | nargs += 1;
|
|---|
| 351 | } // for
|
|---|
| 352 |
|
|---|
| 353 | args[nargs] = NULL; // terminate with NULL
|
|---|
| 354 |
|
|---|
| 355 | #ifdef __DEBUG_H__
|
|---|
| 356 | cerr << "nargs: " << nargs << endl;
|
|---|
| 357 | cerr << "args:" << endl;
|
|---|
| 358 | for ( int i = 0; args[i] != NULL; i += 1 ) {
|
|---|
| 359 | cerr << " \"" << args[i] << "\"" << endl;
|
|---|
| 360 | } // for
|
|---|
| 361 | #endif // __DEBUG_H__
|
|---|
| 362 |
|
|---|
| 363 | if ( ! quiet ) {
|
|---|
| 364 | cerr << "CFA " << "Version " << Version << heading << endl;
|
|---|
| 365 |
|
|---|
| 366 | if ( help ) {
|
|---|
| 367 | cerr <<
|
|---|
| 368 | "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
|
|---|
| 369 | "-help\t\t\t: print this help message" << endl <<
|
|---|
| 370 | "-quiet\t\t\t: print no messages from the cfa command" << endl <<
|
|---|
| 371 | "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
|
|---|
| 372 | "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
|
|---|
| 373 | "...\t\t\t: any other " << compiler_name << " flags" << endl;
|
|---|
| 374 | } // if
|
|---|
| 375 | } // if
|
|---|
| 376 |
|
|---|
| 377 | if ( verbose ) {
|
|---|
| 378 | if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
|
|---|
| 379 |
|
|---|
| 380 | for ( int i = 0; args[i] != NULL; i += 1 ) {
|
|---|
| 381 | cerr << args[i] << " ";
|
|---|
| 382 | } // for
|
|---|
| 383 | cerr << endl;
|
|---|
| 384 | } // if
|
|---|
| 385 |
|
|---|
| 386 | if ( ! nonoptarg ) {
|
|---|
| 387 | cerr << argv[0] << " error, no input files" << endl;
|
|---|
| 388 | exit( EXIT_FAILURE );
|
|---|
| 389 | } // if
|
|---|
| 390 |
|
|---|
| 391 | // execute the command and return the result
|
|---|
| 392 |
|
|---|
| 393 | execvp( args[0], (char *const *)args ); // should not return
|
|---|
| 394 | perror( "CFA Translator error: cfa level, execvp" );
|
|---|
| 395 | exit( EXIT_FAILURE );
|
|---|
| 396 | } // main
|
|---|
| 397 |
|
|---|
| 398 | // Local Variables: //
|
|---|
| 399 | // tab-width: 4 //
|
|---|
| 400 | // mode: c++ //
|
|---|
| 401 | // compile-command: "make install" //
|
|---|
| 402 | // End: //
|
|---|