- Timestamp:
- May 16, 2015, 3:36:19 PM (10 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- a32b204
- Parents:
- b8508a2
- Location:
- driver
- Files:
-
- 1 deleted
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
driver/Makefile.in
rb8508a2 rb87a5ed 1 ### 2 ### This file is part of the Cforall project 3 ### 4 ### $Id: Makefile.in,v 1.5 2005/08/26 19:16:54 rcbilson Exp $ 5 ### 1 ######################### -*- Mode: Makefile-Gmake -*- ########################$ 2 ## 3 ## Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo 4 ## 5 ## The contents of this file are covered under the licence agreement in the 6 ## file "LICENCE" distributed with Cforall. 7 ## 8 ## Makefile.in -- 9 ## 10 ## Author : Peter A. Buhr 11 ## Created On : Sat May 16 07:50:15 2015 12 ## Last Modified By : Peter A. Buhr 13 ## Last Modified On : Sat May 16 08:34:55 2015 14 ## Update Count : 4 15 ############################################################################### 6 16 7 17 CXX=@CXX@ … … 11 21 12 22 SRCS:=cfa.cc cc1.cc 13 OBJECTS:=$ (SRCS:.cc=.o)14 DEPS:=$ (SRCS:.cc=.d)23 OBJECTS:=${SRCS:.cc=.o} 24 DEPS:=${SRCS:.cc=.d} 15 25 16 26 all: cfa cc1 17 27 18 28 cfa: cfa.o 19 $ (CXX)$< -o $@29 ${CXX} $< -o $@ 20 30 21 31 cc1 : cc1.o 22 $ (CXX)$< -o $@32 ${CXX} $< -o $@ 23 33 24 34 install: cfa cc1 25 $ (INSTALL)-d @CFA_BINDIR@26 $ (INSTALL)-d @CFA_LIBDIR@27 $ (INSTALL)cc1 @CFA_LIBDIR@28 $ (INSTALL)cfa @CFA_BINDIR@35 ${INSTALL} -d @CFA_BINDIR@ 36 ${INSTALL} -d @CFA_LIBDIR@ 37 ${INSTALL} cc1 @CFA_LIBDIR@ 38 ${INSTALL} cfa @CFA_BINDIR@ 29 39 30 40 clean: 31 rm -f cfa cc1 $ (OBJECTS) $(DEPS)core41 rm -f cfa cc1 ${OBJECTS} ${DEPS} core -
driver/cc1.cc
rb8508a2 rb87a5ed 1 // -*- Mode: C++ -*- 2 // 3 // CForall Version 1.0, Copyright (C) Peter A. Buhr 2005 4 // 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 // 5 7 // cc1.cc -- 6 // 7 // Author : Richard C. Bilson8 // 9 // Author : Peter A. Buhr 8 10 // Created On : Fri Aug 26 14:23:51 2005 9 11 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Thu May 14 13:24:45201511 // Update Count : 4 312 // Last Modified On : Sat May 16 07:42:14 2015 13 // Update Count : 49 12 14 // 13 14 15 15 16 #include <iostream> … … 18 19 #include <string> 19 20 using std::string; 20 #include <cstdio> // stderr, stdout, perror, fprintf21 #include <cstdlib> // getenv, exit, mkstemp22 #include <unistd.h> // execvp, fork, unlink23 #include <sys/wait.h> // wait24 25 #include "config.h" // configure info21 #include <cstdio> // stderr, stdout, perror, fprintf 22 #include <cstdlib> // getenv, exit, mkstemp 23 #include <unistd.h> // execvp, fork, unlink 24 #include <sys/wait.h> // wait 25 26 #include "config.h" // configure info 26 27 27 28 … … 29 30 30 31 31 string compiler_name( GCC_PATH ); 32 string compiler_name( GCC_PATH ); // path/name of C compiler 32 33 33 34 string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" ); … … 39 40 40 41 bool prefix( string arg, string pre ) { 41 42 return arg.substr( 0, pre.size() ) == pre; 42 43 } // prefix 43 44 44 45 45 46 void checkEnv( const char *args[], int &nargs ) { 46 char *value; 47 48 value = getenv( "__COMPILER__" ); 49 if ( value != NULL ) { 50 compiler_name = value; 51 #ifdef __DEBUG_H__ 52 cerr << "env arg:\"" << compiler_name << "\"" << endl; 53 #endif // __DEBUG_H__ 54 } // if 55 56 value = getenv( "__GCC_MACHINE__" ); 57 if ( value != NULL ) { 58 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along 59 #ifdef __DEBUG_H__ 60 cerr << "env arg:\"" << args[nargs] << "\"" << endl; 61 #endif // __DEBUG_H__ 47 char *value; 48 49 value = getenv( "__COMPILER__" ); 50 if ( value != NULL ) { 51 compiler_name = value; 52 #ifdef __DEBUG_H__ 53 cerr << "env arg:\"" << compiler_name << "\"" << endl; 54 #endif // __DEBUG_H__ 55 } // if 56 57 value = getenv( "__GCC_MACHINE__" ); 58 if ( value != NULL ) { 59 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along 60 #ifdef __DEBUG_H__ 61 cerr << "env arg:\"" << args[nargs] << "\"" << endl; 62 #endif // __DEBUG_H__ 63 nargs += 1; 64 } // if 65 66 value = getenv( "__GCC_VERSION__" ); 67 if ( value != NULL ) { 68 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along 69 #ifdef __DEBUG_H__ 70 cerr << "env arg:\"" << args[nargs] << "\"" << endl; 71 #endif // __DEBUG_H__ 72 nargs += 1; 73 } // if 74 } // checkEnv 75 76 77 void rmtmpfile() { 78 if ( unlink( tmpname ) == -1 ) { // remove tmpname 79 perror ( "CFA Translator error: cpp failed" ); 80 exit( EXIT_FAILURE ); 81 } // if 82 tmpfilefd = -1; // mark closed 83 } // rmtmpfile 84 85 86 void sigTermHandler( int signal ) { 87 if ( tmpfilefd != -1 ) { // RACE, file created ? 88 rmtmpfile(); // remove 89 exit( EXIT_FAILURE ); // terminate 90 } // if 91 } // sigTermHandler 92 93 94 void Stage1( const int argc, const char * const argv[] ) { 95 int code; 96 int i; 97 98 string arg; 99 string bprefix; 100 101 const char *cpp_in = NULL; 102 const char *cpp_out = NULL; 103 104 bool CFA_flag = false; 105 bool cpp_flag = false; 106 const char *o_name = NULL; 107 108 const char *args[argc + 100]; // leave space for 100 additional cpp command line values 109 int nargs = 1; // number of arguments in args list; 0 => command name 110 const char *uargs[20]; // leave space for 20 additional cfa-cpp command line values 111 int nuargs = 1; // 0 => command name 112 113 signal( SIGINT, sigTermHandler ); 114 signal( SIGTERM, sigTermHandler ); 115 116 // process all the arguments 117 118 checkEnv( args, nargs ); // arguments passed via environment variables 119 120 for ( i = 1; i < argc; i += 1 ) { 121 #ifdef __DEBUG_H__ 122 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 123 #endif // __DEBUG_H__ 124 arg = argv[i]; 125 #ifdef __DEBUG_H__ 126 cerr << "arg:\"" << arg << "\"" << endl; 127 #endif // __DEBUG_H__ 128 if ( prefix( arg, "-" ) ) { 129 // strip g++ flags that are inappropriate or cause duplicates in subsequent passes 130 131 if ( arg == "-quiet" ) { 132 } else if ( arg == "-imultilib" || arg == "-imultiarch" ) { 133 i += 1; // and the argument 134 } else if ( prefix( arg, "-A" ) ) { 135 } else if ( prefix( arg, "-D__GNU" ) ) { 136 //******** 137 // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT! 138 //******** 139 } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) { 140 i += 1; // and the argument 141 142 // strip flags controlling cpp step 143 144 } else if ( arg == "-D__CPP__" ) { 145 cpp_flag = true; 146 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) { 147 i += 1; // and the argument 148 cpp_flag = true; 149 } else if ( arg == "-D__CFA__" ) { 150 CFA_flag = true; 151 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA__" ) { 152 i += 1; // and the argument 153 CFA_flag = true; 154 } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) { 155 uargs[nuargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str(); 156 nuargs += 1; 157 } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) { 158 uargs[nuargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str(); 159 nuargs += 1; 160 i += 1; // and the argument 161 } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) { 162 bprefix = arg.substr( D__GCC_BPREFIX__.size() ); 163 } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) { 164 bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 ); 165 i += 1; // and the argument 166 167 // all other flags 168 169 } else if ( arg == "-o" ) { 170 i += 1; 171 o_name = argv[i]; 172 } else { 173 args[nargs] = argv[i]; // pass the flag along 174 nargs += 1; 175 // CPP flags with an argument 176 if ( arg == "-D" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" || 177 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" || 178 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) { 179 i += 1; 180 args[nargs] = argv[i]; // pass the argument along 181 nargs += 1; 182 #ifdef __DEBUG_H__ 183 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 184 #endif // __DEBUG_H__ 185 } else if ( arg == "-MD" || arg == "-MMD" ) { 186 args[nargs] = "-MF"; // insert before file 187 nargs += 1; 188 i += 1; 189 args[nargs] = argv[i]; // pass the argument along 190 nargs += 1; 191 #ifdef __DEBUG_H__ 192 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 193 #endif // __DEBUG_H__ 194 } // if 195 } // if 196 } else { // obtain input and possibly output files 197 if ( cpp_in == NULL ) { 198 cpp_in = argv[i]; 199 #ifdef __DEBUG_H__ 200 cerr << "cpp_in:\"" << cpp_in << "\"" << endl; 201 #endif // __DEBUG_H__ 202 } else if ( cpp_out == NULL ) { 203 cpp_out = argv[i]; 204 #ifdef __DEBUG_H__ 205 cerr << "cpp_out:\"" << cpp_out << "\""<< endl; 206 #endif // __DEBUG_H__ 207 } else { 208 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 209 exit( EXIT_FAILURE ); 210 } // if 211 } // if 212 } // for 213 214 #ifdef __DEBUG_H__ 215 cerr << "args:"; 216 for ( i = 1; i < nargs; i += 1 ) { 217 cerr << " " << args[i]; 218 } // for 219 if ( cpp_in != NULL ) cerr << " " << cpp_in; 220 if ( cpp_out != NULL ) cerr << " " << cpp_out; 221 cerr << endl; 222 #endif // __DEBUG_H__ 223 224 if ( cpp_in == NULL ) { 225 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 226 exit( EXIT_FAILURE ); 227 } // if 228 229 if ( cpp_flag ) { 230 // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard 231 // output or -o. The call to cfa has a -E so it does not have to be added to the argument list. 232 233 args[0] = compiler_name.c_str(); 234 args[nargs] = cpp_in; 235 nargs += 1; 236 if ( o_name != NULL ) { // location for output 237 args[nargs] = "-o"; 238 nargs += 1; 239 args[nargs] = o_name; 240 nargs += 1; 241 } // if 242 args[nargs] = NULL; // terminate argument list 243 244 #ifdef __DEBUG_H__ 245 cerr << "nargs: " << nargs << endl; 246 for ( i = 0; args[i] != NULL; i += 1 ) { 247 cerr << args[i] << " "; 248 } // for 249 cerr << endl; 250 #endif // __DEBUG_H__ 251 252 execvp( args[0], (char *const *)args ); // should not return 253 perror( "CFA Translator error: cpp level, execvp" ); 254 exit( EXIT_FAILURE ); 255 } // if 256 257 // Create a temporary file to store output of the C preprocessor. 258 259 tmpfilefd = mkstemp( tmpname ); 260 if ( tmpfilefd == -1 ) { 261 perror( "CFA Translator error: cpp level, mkstemp" ); 262 exit( EXIT_FAILURE ); 263 } // if 264 265 #ifdef __DEBUG_H__ 266 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl; 267 #endif // __DEBUG_H__ 268 269 // Run the C preprocessor and save the output in tmpfile. 270 271 if ( fork() == 0 ) { // child process ? 272 // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects 273 // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error, 274 // when cpp writes to stdout. Hence, stdout is redirected into the temporary file. 275 if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname 276 perror( "CFA Translator error: cpp level, freopen" ); 277 exit( EXIT_FAILURE ); 278 } // if 279 280 args[0] = compiler_name.c_str(); 281 args[nargs] = cpp_in; // input to cpp 282 nargs += 1; 283 args[nargs] = NULL; // terminate argument list 284 285 #ifdef __DEBUG_H__ 286 cerr << "cpp nargs: " << nargs << endl; 287 for ( i = 0; args[i] != NULL; i += 1 ) { 288 cerr << args[i] << " "; 289 } // for 290 cerr << endl; 291 #endif // __DEBUG_H__ 292 293 execvp( args[0], (char *const *)args ); // should not return 294 perror( "CFA Translator error: cpp level, execvp" ); 295 exit( EXIT_FAILURE ); 296 } // if 297 298 wait( &code ); // wait for child to finish 299 300 #ifdef __DEBUG_H__ 301 cerr << "return code from cpp:" << WEXITSTATUS(code) << endl; 302 #endif // __DEBUG_H__ 303 304 if ( WIFSIGNALED(code) != 0 ) { // child failed ? 305 rmtmpfile(); // remove tmpname 306 cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl; 307 exit( EXIT_FAILURE ); 308 } // if 309 310 if ( WEXITSTATUS(code) != 0 ) { // child error ? 311 rmtmpfile(); // remove tmpname 312 exit( WEXITSTATUS( code ) ); // do not continue 313 } // if 314 315 // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard 316 // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file. 317 318 if ( fork() == 0 ) { // child runs CFA 319 uargs[0] = ( *new string( bprefix + "/cfa-cpp" ) ).c_str(); 320 321 uargs[nuargs] = "-p"; 322 nuargs += 1; 323 324 uargs[nuargs] = tmpname; 325 nuargs += 1; 326 if ( o_name != NULL ) { 327 uargs[nuargs] = o_name; 328 nuargs += 1; 329 } else if ( ! CFA_flag ) { // run cfa-cpp ? 330 uargs[nuargs] = cpp_out; 331 nuargs += 1; 332 } // if 333 uargs[nuargs] = NULL; // terminate argument list 334 335 #ifdef __DEBUG_H__ 336 cerr << "cfa-cpp nuargs: " << o_name << " " << CFA_flag << " " << nuargs << endl; 337 for ( i = 0; uargs[i] != NULL; i += 1 ) { 338 cerr << uargs[i] << " "; 339 } // for 340 cerr << endl; 341 #endif // __DEBUG_H__ 342 343 execvp( uargs[0], (char * const *)uargs ); // should not return 344 perror( "CFA Translator error: cpp level, execvp" ); 345 exit( EXIT_FAILURE ); 346 } // if 347 348 wait( &code ); // wait for child to finish 349 350 #ifdef __DEBUG_H__ 351 cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl; 352 #endif // __DEBUG_H__ 353 354 // Must unlink here because file must exist across execvp. 355 rmtmpfile(); // remove tmpname 356 357 if ( WIFSIGNALED(code) ) { // child failed ? 358 cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl; 359 exit( EXIT_FAILURE ); 360 } // if 361 362 exit( WEXITSTATUS(code) ); 363 } // Stage1 364 365 366 void Stage2( const int argc, const char * const * argv ) { 367 int i; 368 369 string arg; 370 371 const char *cpp_in = NULL; 372 373 const char *args[argc + 100]; // leave space for 100 additional cfa command line values 374 int nargs = 1; // number of arguments in args list; 0 => command name 375 376 // process all the arguments 377 378 checkEnv( args, nargs ); // arguments passed via environment variables 379 380 for ( i = 1; i < argc; i += 1 ) { 381 #ifdef __DEBUG_H__ 382 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 383 #endif // __DEBUG_H__ 384 arg = argv[i]; 385 #ifdef __DEBUG_H__ 386 cerr << "arg:\"" << arg << "\"" << endl; 387 #endif // __DEBUG_H__ 388 if ( prefix( arg, "-" ) ) { 389 // strip inappropriate flags 390 391 if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" || 392 // Currently CFA does not suppose precompiled .h files. 393 prefix( arg, "--output-pch" ) ) { 394 395 // strip inappropriate flags with an argument 396 397 } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) { 398 i += 1; 399 #ifdef __DEBUG_H__ 400 cerr << "arg:\"" << argv[i] << "\"" << endl; 401 #endif // __DEBUG_H__ 402 403 // all other flags 404 405 } else { 406 args[nargs] = argv[i]; // pass the flag along 407 nargs += 1; 408 if ( arg == "-o" ) { 409 i += 1; 410 args[nargs] = argv[i]; // pass the argument along 411 nargs += 1; 412 #ifdef __DEBUG_H__ 413 cerr << "arg:\"" << argv[i] << "\"" << endl; 414 #endif // __DEBUG_H__ 415 } // if 416 } // if 417 } else { // obtain input and possibly output files 418 if ( cpp_in == NULL ) { 419 cpp_in = argv[i]; 420 #ifdef __DEBUG_H__ 421 cerr << "cpp_in:\"" << cpp_in << "\"" << endl; 422 #endif // __DEBUG_H__ 423 } else { 424 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 425 exit( EXIT_FAILURE ); 426 } // if 427 } // if 428 } // for 429 430 #ifdef __DEBUG_H__ 431 cerr << "args:"; 432 for ( i = 1; i < nargs; i += 1 ) { 433 cerr << " " << args[i]; 434 } // for 435 cerr << endl; 436 if ( cpp_in != NULL ) cerr << " " << cpp_in; 437 #endif // __DEBUG_H__ 438 439 args[0] = compiler_name.c_str(); 440 args[nargs] = "-S"; // only compile and put assembler output in specified file 62 441 nargs += 1; 63 } // if64 65 value = getenv( "__GCC_VERSION__" );66 if ( value != NULL ) {67 args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along68 #ifdef __DEBUG_H__69 cerr << "env arg:\"" << args[nargs] << "\"" << endl;70 #endif // __DEBUG_H__71 nargs += 1;72 } // if73 } // checkEnv74 75 76 void rmtmpfile() {77 if ( unlink( tmpname ) == -1 ) { // remove tmpname78 perror ( "CFA Translator error: cpp failed" );79 exit( EXIT_FAILURE );80 } // if81 tmpfilefd = -1; // mark closed82 } // rmtmpfile83 84 85 void sigTermHandler( int signal ) {86 if ( tmpfilefd != -1 ) { // RACE, file created ?87 rmtmpfile(); // remove88 exit( EXIT_FAILURE ); // terminate89 } // if90 } // sigTermHandler91 92 93 void Stage1( const int argc, const char * const argv[] ) {94 int code;95 int i;96 97 string arg;98 string bprefix;99 100 const char *cpp_in = NULL;101 const char *cpp_out = NULL;102 103 bool CFA_flag = false;104 bool cpp_flag = false;105 const char *o_name = NULL;106 107 const char *args[argc + 100]; // leave space for 100 additional cpp command line values108 int nargs = 1; // number of arguments in args list; 0 => command name109 const char *uargs[20]; // leave space for 20 additional cfa-cpp command line values110 int nuargs = 1; // 0 => command name111 112 signal( SIGINT, sigTermHandler );113 signal( SIGTERM, sigTermHandler );114 115 // process all the arguments116 117 checkEnv( args, nargs ); // arguments passed via environment variables118 119 for ( i = 1; i < argc; i += 1 ) {120 #ifdef __DEBUG_H__121 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;122 #endif // __DEBUG_H__123 arg = argv[i];124 #ifdef __DEBUG_H__125 cerr << "arg:\"" << arg << "\"" << endl;126 #endif // __DEBUG_H__127 if ( prefix( arg, "-" ) ) {128 // strip g++ flags that are inappropriate or cause duplicates in subsequent passes129 130 if ( arg == "-quiet" ) {131 } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {132 i += 1; // and the argument133 } else if ( prefix( arg, "-A" ) ) {134 } else if ( prefix( arg, "-D__GNU" ) ) {135 //********136 // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!137 //********138 } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {139 i += 1; // and the argument140 141 // strip flags controlling cpp step142 143 } else if ( arg == "-D__CPP__" ) {144 cpp_flag = true;145 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {146 i += 1; // and the argument147 cpp_flag = true;148 } else if ( arg == "-D__CFA__" ) {149 CFA_flag = true;150 } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA__" ) {151 i += 1; // and the argument152 CFA_flag = true;153 } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {154 uargs[nuargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();155 nuargs += 1;156 } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {157 uargs[nuargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();158 nuargs += 1;159 i += 1; // and the argument160 } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {161 bprefix = arg.substr( D__GCC_BPREFIX__.size() );162 } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) {163 bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 );164 i += 1; // and the argument165 166 // all other flags167 168 } else if ( arg == "-o" ) {169 i += 1;170 o_name = argv[i];171 } else {172 args[nargs] = argv[i]; // pass the flag along173 nargs += 1;174 // CPP flags with an argument175 if ( arg == "-D" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||176 arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||177 arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {178 i += 1;179 args[nargs] = argv[i]; // pass the argument along180 nargs += 1;181 #ifdef __DEBUG_H__182 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;183 #endif // __DEBUG_H__184 } else if ( arg == "-MD" || arg == "-MMD" ) {185 args[nargs] = "-MF"; // insert before file186 nargs += 1;187 i += 1;188 args[nargs] = argv[i]; // pass the argument along189 nargs += 1;190 #ifdef __DEBUG_H__191 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;192 #endif // __DEBUG_H__193 } // if194 } // if195 } else { // obtain input and possibly output files196 if ( cpp_in == NULL ) {197 cpp_in = argv[i];198 #ifdef __DEBUG_H__199 cerr << "cpp_in:\"" << cpp_in << "\"" << endl;200 #endif // __DEBUG_H__201 } else if ( cpp_out == NULL ) {202 cpp_out = argv[i];203 #ifdef __DEBUG_H__204 cerr << "cpp_out:\"" << cpp_out << "\""<< endl;205 #endif // __DEBUG_H__206 } else {207 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;208 exit( EXIT_FAILURE );209 } // if210 } // if211 } // for212 213 #ifdef __DEBUG_H__214 cerr << "args:";215 for ( i = 1; i < nargs; i += 1 ) {216 cerr << " " << args[i];217 } // for218 if ( cpp_in != NULL ) cerr << " " << cpp_in;219 if ( cpp_out != NULL ) cerr << " " << cpp_out;220 cerr << endl;221 #endif // __DEBUG_H__222 223 if ( cpp_in == NULL ) {224 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;225 exit( EXIT_FAILURE );226 } // if227 228 if ( cpp_flag ) {229 // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard230 // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.231 232 args[0] = compiler_name.c_str();233 442 args[nargs] = cpp_in; 234 443 nargs += 1; 235 if ( o_name != NULL ) { // location for output 236 args[nargs] = "-o"; 237 nargs += 1; 238 args[nargs] = o_name; 239 nargs += 1; 240 } // if 241 args[nargs] = NULL; // terminate argument list 242 243 #ifdef __DEBUG_H__ 244 cerr << "nargs: " << nargs << endl; 444 args[nargs] = NULL; // terminate argument list 445 446 #ifdef __DEBUG_H__ 447 cerr << "stage2 nargs: " << nargs << endl; 245 448 for ( i = 0; args[i] != NULL; i += 1 ) { 246 449 cerr << args[i] << " "; 247 450 } // for 248 451 cerr << endl; 249 452 #endif // __DEBUG_H__ 250 453 251 execvp( args[0], (char * const *)args );// should not return454 execvp( args[0], (char * const *)args ); // should not return 252 455 perror( "CFA Translator error: cpp level, execvp" ); 253 exit( EXIT_FAILURE ); 254 } // if 255 256 // Create a temporary file to store output of the C preprocessor. 257 258 tmpfilefd = mkstemp( tmpname ); 259 if ( tmpfilefd == -1 ) { 260 perror( "CFA Translator error: cpp level, mkstemp" ); 261 exit( EXIT_FAILURE ); 262 } // if 263 264 #ifdef __DEBUG_H__ 265 cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl; 266 #endif // __DEBUG_H__ 267 268 // Run the C preprocessor and save the output in tmpfile. 269 270 if ( fork() == 0 ) { // child process ? 271 // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects 272 // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error, 273 // when cpp writes to stdout. Hence, stdout is redirected into the temporary file. 274 if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname 275 perror( "CFA Translator error: cpp level, freopen" ); 276 exit( EXIT_FAILURE ); 277 } // if 278 279 args[0] = compiler_name.c_str(); 280 args[nargs] = cpp_in; // input to cpp 281 nargs += 1; 282 args[nargs] = NULL; // terminate argument list 283 284 #ifdef __DEBUG_H__ 285 cerr << "cpp nargs: " << nargs << endl; 286 for ( i = 0; args[i] != NULL; i += 1 ) { 287 cerr << args[i] << " "; 288 } // for 289 cerr << endl; 290 #endif // __DEBUG_H__ 291 292 execvp( args[0], (char *const *)args ); // should not return 293 perror( "CFA Translator error: cpp level, execvp" ); 294 exit( EXIT_FAILURE ); 295 } // if 296 297 wait( &code ); // wait for child to finish 298 299 #ifdef __DEBUG_H__ 300 cerr << "return code from cpp:" << WEXITSTATUS(code) << endl; 301 #endif // __DEBUG_H__ 302 303 if ( WIFSIGNALED(code) != 0 ) { // child failed ? 304 rmtmpfile(); // remove tmpname 305 cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl; 306 exit( EXIT_FAILURE ); 307 } // if 308 309 if ( WEXITSTATUS(code) != 0 ) { // child error ? 310 rmtmpfile(); // remove tmpname 311 exit( WEXITSTATUS( code ) ); // do not continue 312 } // if 313 314 // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard 315 // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file. 316 317 if ( fork() == 0 ) { // child runs CFA 318 uargs[0] = ( *new string( bprefix + "/cfa-cpp" ) ).c_str(); 319 320 uargs[nuargs] = "-p"; 321 nuargs += 1; 322 323 uargs[nuargs] = tmpname; 324 nuargs += 1; 325 if ( o_name != NULL ) { 326 uargs[nuargs] = o_name; 327 nuargs += 1; 328 } else if ( ! CFA_flag ) { // run cfa-cpp ? 329 uargs[nuargs] = cpp_out; 330 nuargs += 1; 331 } // if 332 uargs[nuargs] = NULL; // terminate argument list 333 334 #ifdef __DEBUG_H__ 335 cerr << "cfa-cpp nuargs: " << o_name << " " << CFA_flag << " " << nuargs << endl; 336 for ( i = 0; uargs[i] != NULL; i += 1 ) { 337 cerr << uargs[i] << " "; 338 } // for 339 cerr << endl; 340 #endif // __DEBUG_H__ 341 342 execvp( uargs[0], (char * const *)uargs ); // should not return 343 perror( "CFA Translator error: cpp level, execvp" ); 344 exit( EXIT_FAILURE ); 345 } // if 346 347 wait( &code ); // wait for child to finish 348 349 #ifdef __DEBUG_H__ 350 cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl; 351 #endif // __DEBUG_H__ 352 353 // Must unlink here because file must exist across execvp. 354 rmtmpfile(); // remove tmpname 355 356 if ( WIFSIGNALED(code) ) { // child failed ? 357 cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl; 358 exit( EXIT_FAILURE ); 359 } // if 360 361 exit( WEXITSTATUS(code) ); 362 } // Stage1 363 364 365 void Stage2( const int argc, const char * const * argv ) { 366 int i; 367 368 string arg; 369 370 const char *cpp_in = NULL; 371 372 const char *args[argc + 100]; // leave space for 100 additional cfa command line values 373 int nargs = 1; // number of arguments in args list; 0 => command name 374 375 // process all the arguments 376 377 checkEnv( args, nargs ); // arguments passed via environment variables 378 379 for ( i = 1; i < argc; i += 1 ) { 380 #ifdef __DEBUG_H__ 381 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 382 #endif // __DEBUG_H__ 383 arg = argv[i]; 384 #ifdef __DEBUG_H__ 385 cerr << "arg:\"" << arg << "\"" << endl; 386 #endif // __DEBUG_H__ 387 if ( prefix( arg, "-" ) ) { 388 // strip inappropriate flags 389 390 if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" || 391 // Currently CFA does not suppose precompiled .h files. 392 prefix( arg, "--output-pch" ) ) { 393 394 // strip inappropriate flags with an argument 395 396 } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) { 397 i += 1; 398 #ifdef __DEBUG_H__ 399 cerr << "arg:\"" << argv[i] << "\"" << endl; 400 #endif // __DEBUG_H__ 401 402 // all other flags 403 404 } else { 405 args[nargs] = argv[i]; // pass the flag along 406 nargs += 1; 407 if ( arg == "-o" ) { 408 i += 1; 409 args[nargs] = argv[i]; // pass the argument along 410 nargs += 1; 411 #ifdef __DEBUG_H__ 412 cerr << "arg:\"" << argv[i] << "\"" << endl; 413 #endif // __DEBUG_H__ 414 } // if 415 } // if 416 } else { // obtain input and possibly output files 417 if ( cpp_in == NULL ) { 418 cpp_in = argv[i]; 419 #ifdef __DEBUG_H__ 420 cerr << "cpp_in:\"" << cpp_in << "\"" << endl; 421 #endif // __DEBUG_H__ 422 } else { 456 exit( EXIT_FAILURE ); // tell gcc not to go any further 457 } // Stage2 458 459 460 int main( const int argc, const char * const argv[], const char * const env[] ) { 461 #ifdef __DEBUG_H__ 462 for ( int i = 0; env[i] != NULL; i += 1 ) { 463 cerr << env[i] << endl; 464 } // for 465 #endif // __DEBUG_H__ 466 467 string arg = argv[1]; 468 469 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed. 470 471 if ( arg == "-E" ) { 472 #ifdef __DEBUG_H__ 473 cerr << "Stage1" << endl; 474 #endif // __DEBUG_H__ 475 Stage1( argc, argv ); 476 } else if ( arg == "-fpreprocessed" ) { 477 #ifdef __DEBUG_H__ 478 cerr << "Stage2" << endl; 479 #endif // __DEBUG_H__ 480 Stage2( argc, argv ); 481 } else { 423 482 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 424 483 exit( EXIT_FAILURE ); 425 } // if 426 } // if 427 } // for 428 429 #ifdef __DEBUG_H__ 430 cerr << "args:"; 431 for ( i = 1; i < nargs; i += 1 ) { 432 cerr << " " << args[i]; 433 } // for 434 cerr << endl; 435 if ( cpp_in != NULL ) cerr << " " << cpp_in; 436 #endif // __DEBUG_H__ 437 438 args[0] = compiler_name.c_str(); 439 args[nargs] = "-S"; // only compile and put assembler output in specified file 440 nargs += 1; 441 args[nargs] = cpp_in; 442 nargs += 1; 443 args[nargs] = NULL; // terminate argument list 444 445 #ifdef __DEBUG_H__ 446 cerr << "stage2 nargs: " << nargs << endl; 447 for ( i = 0; args[i] != NULL; i += 1 ) { 448 cerr << args[i] << " "; 449 } // for 450 cerr << endl; 451 #endif // __DEBUG_H__ 452 453 execvp( args[0], (char * const *)args ); // should not return 454 perror( "CFA Translator error: cpp level, execvp" ); 455 exit( EXIT_FAILURE ); // tell gcc not to go any further 456 } // Stage2 457 458 459 int main( const int argc, const char * const argv[], const char * const env[] ) { 460 #ifdef __DEBUG_H__ 461 for ( int i = 0; env[i] != NULL; i += 1 ) { 462 cerr << env[i] << endl; 463 } // for 464 #endif // __DEBUG_H__ 465 466 string arg = argv[1]; 467 468 // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed. 469 470 if ( arg == "-E" ) { 471 #ifdef __DEBUG_H__ 472 cerr << "Stage1" << endl; 473 #endif // __DEBUG_H__ 474 Stage1( argc, argv ); 475 } else if ( arg == "-fpreprocessed" ) { 476 #ifdef __DEBUG_H__ 477 cerr << "Stage2" << endl; 478 #endif // __DEBUG_H__ 479 Stage2( argc, argv ); 480 } else { 481 cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl; 482 exit( EXIT_FAILURE ); 483 } // if 484 } // if 484 485 } // main 485 486 486 487 487 // Local Variables: // 488 // tab-width: 4 // 489 // mode: c++ // 488 490 // compile-command: "make install" // 489 491 // End: // -
driver/cfa.cc
rb8508a2 rb87a5ed 1 // -*- Mode: C++ -*- 2 // 3 // CForall Version 1.0, Copyright (C) Peter A. Buhr 2002 4 // 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 // 5 7 // cfa.cc -- 6 // 8 // 7 9 // Author : Peter A. Buhr 8 10 // Created On : Tue Aug 20 13:44:49 2002 9 11 // Last Modified By : Peter A. Buhr 10 // Last Modified On : Fri May 15 15:01:26201511 // Update Count : 1 0812 // 12 // Last Modified On : Sat May 16 07:47:05 2015 13 // Update Count : 111 14 // 13 15 14 16 #include <iostream> 15 #include <cstdio> // perror16 #include <cstdlib> // putenv, exit17 #include <unistd.h> // execvp18 #include <string> // STL version19 20 #include "config.h" // configure info17 #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 21 23 22 24 using std::cerr; … … 29 31 30 32 bool prefix( string arg, string pre ) { 31 33 return arg.substr( 0, pre.size() ) == pre; 32 34 } // prefix 33 35 34 36 35 37 void shuffle( const char *args[], int S, int E, int N ) { 36 37 #ifdef __DEBUG_H__ 38 39 #endif // __DEBUG_H__ 40 41 #ifdef __DEBUG_H__ 42 cerr << "\t" << j << " " << j-N << endl;43 #endif // __DEBUG_H__ 44 args[j] = args[j-N];45 38 // S & E index 1 passed the end so adjust with -1 39 #ifdef __DEBUG_H__ 40 cerr << "shuffle:" << S << " " << E << " " << N << endl; 41 #endif // __DEBUG_H__ 42 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) { 43 #ifdef __DEBUG_H__ 44 cerr << "\t" << j << " " << j-N << endl; 45 #endif // __DEBUG_H__ 46 args[j] = args[j-N]; 47 } // for 46 48 } // shuffle 47 49 48 50 49 51 int main( int argc, char *argv[] ) { 50 string Version( VERSION ); // current version number from CONFIG 51 string Major( "0" ), Minor( "0" ), Patch( "0" ); // default version numbers 52 int posn1 = Version.find( "." ); // find the divider between major and minor version numbers 53 if ( posn1 == -1 ) { // not there ? 54 Major = Version; 55 } else { 56 Major = Version.substr( 0, posn1 ); 57 int posn2 = Version.find( ".", posn1 + 1 ); // find the divider between minor and patch numbers 58 if ( posn2 == -1 ) { // not there ? 59 Minor = Version.substr( posn1 ); 52 string Version( VERSION ); // current version number from CONFIG 53 string Major( "0" ), Minor( "0" ), Patch( "0" ); // default version numbers 54 int posn1 = Version.find( "." ); // find the divider between major and minor version numbers 55 if ( posn1 == -1 ) { // not there ? 56 Major = Version; 60 57 } else { 61 Minor = Version.substr( posn1 + 1, posn2 - posn1 - 1 ); 62 Patch = Version.substr( posn2 + 1 ); 63 } // if 64 } // if 65 66 string installincdir( CFA_INCDIR ); // fixed location of cc1 and cfa-cpp commands 67 string installlibdir( CFA_LIBDIR ); // fixed location of include files 68 69 string heading; // banner printed at start of cfa compilation 70 string arg; // current command-line argument during command-line parsing 71 string Bprefix; // path where gcc looks for compiler command steps 72 string langstd; // language standard 73 74 string compiler_path( GCC_PATH ); // path/name of C compiler 75 string compiler_name; // name of C compiler 76 77 bool nonoptarg = false; // indicates non-option argument specified 78 bool link = true; // linking as well as compiling 79 bool verbose = false; // -v flag 80 bool quiet = false; // -quiet flag 81 bool debug = true; // -debug flag 82 bool help = false; // -help flag 83 bool CFA_flag = false; // -CFA flag 84 bool cpp_flag = false; // -E or -M flag, preprocessor only 85 bool debugging = false; // -g flag 86 87 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags 88 int sargs = 1; // starting location for arguments in args list 89 int nargs = sargs; // number of arguments in args list; 0 => command name 90 91 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags 92 int nlibs = 0; 93 94 #ifdef __DEBUG_H__ 95 cerr << "CFA:" << endl; 96 #endif // __DEBUG_H__ 97 98 // process command-line arguments 99 100 for ( int i = 1; i < argc; i += 1 ) { 101 #ifdef __DEBUG_H__ 102 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 103 #endif // __DEBUG_H__ 104 arg = argv[i]; // convert to string value 105 #ifdef __DEBUG_H__ 106 cerr << "arg:\"" << arg << "\"" << endl; 107 #endif // __DEBUG_H__ 108 if ( prefix( arg, "-" ) ) { 109 // pass through arguments 110 111 if ( arg == "-Xlinker" || arg == "-o" ) { 112 args[nargs] = argv[i]; // pass the argument along 113 nargs += 1; 114 i += 1; 115 if ( i == argc ) continue; // next argument available ? 116 args[nargs] = argv[i]; // pass the argument along 117 nargs += 1; 118 } else if ( arg == "-XCFA" ) { // CFA pass through 119 i += 1; 120 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str(); 121 nargs += 1; 122 123 // CFA specific arguments 124 125 } else if ( arg == "-CFA" ) { 126 CFA_flag = true; // strip the -CFA flag 127 link = false; 128 args[nargs] = "-E"; // replace the argument with -E 129 nargs += 1; 130 } else if ( arg == "-debug" ) { 131 debug = true; // strip the debug flag 132 } else if ( arg == "-nodebug" ) { 133 debug = false; // strip the nodebug flag 134 } else if ( arg == "-quiet" ) { 135 quiet = true; // strip the quiet flag 136 } else if ( arg == "-noquiet" ) { 137 quiet = false; // strip the noquiet flag 138 } else if ( arg == "-help" ) { 139 help = true; // strip the help flag 140 } else if ( arg == "-nohelp" ) { 141 help = false; // strip the nohelp flag 142 } else if ( arg == "-compiler" ) { 143 // use the user specified compiler 144 i += 1; 145 if ( i == argc ) continue; // next argument available ? 146 compiler_path = argv[i]; 147 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) { 148 cerr << argv[0] << " error, cannot set environment variable." << endl; 149 exit( EXIT_FAILURE ); 58 Major = Version.substr( 0, posn1 ); 59 int posn2 = Version.find( ".", posn1 + 1 ); // find the divider between minor and patch numbers 60 if ( posn2 == -1 ) { // not there ? 61 Minor = Version.substr( posn1 ); 62 } else { 63 Minor = Version.substr( posn1 + 1, posn2 - posn1 - 1 ); 64 Patch = Version.substr( posn2 + 1 ); 150 65 } // if 151 152 // C++ specific arguments 153 154 } else if ( arg == "-v" ) { 155 verbose = true; // verbosity required 156 args[nargs] = argv[i]; // pass the argument along 157 nargs += 1; 158 } else if ( arg == "-g" ) { 159 debugging = true; // symbolic debugging required 160 args[nargs] = argv[i]; // pass the argument along 161 nargs += 1; 162 } else if ( prefix( arg, "-B" ) ) { 163 Bprefix = arg.substr(2); // strip the -B flag 66 } // if 67 68 string installincdir( CFA_INCDIR ); // fixed location of cc1 and cfa-cpp commands 69 string installlibdir( CFA_LIBDIR ); // fixed location of include files 70 71 string heading; // banner printed at start of cfa compilation 72 string arg; // current command-line argument during command-line parsing 73 string Bprefix; // path where gcc looks for compiler command steps 74 string langstd; // language standard 75 76 string compiler_path( GCC_PATH ); // path/name of C compiler 77 string compiler_name; // name of C compiler 78 79 bool nonoptarg = false; // indicates non-option argument specified 80 bool link = true; // linking as well as compiling 81 bool verbose = false; // -v flag 82 bool quiet = false; // -quiet flag 83 bool debug = true; // -debug flag 84 bool help = false; // -help flag 85 bool CFA_flag = false; // -CFA flag 86 bool cpp_flag = false; // -E or -M flag, preprocessor only 87 bool debugging = false; // -g flag 88 89 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags 90 int sargs = 1; // starting location for arguments in args list 91 int nargs = sargs; // number of arguments in args list; 0 => command name 92 93 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags 94 int nlibs = 0; 95 96 #ifdef __DEBUG_H__ 97 cerr << "CFA:" << endl; 98 #endif // __DEBUG_H__ 99 100 // process command-line arguments 101 102 for ( int i = 1; i < argc; i += 1 ) { 103 #ifdef __DEBUG_H__ 104 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl; 105 #endif // __DEBUG_H__ 106 arg = argv[i]; // convert to string value 107 #ifdef __DEBUG_H__ 108 cerr << "arg:\"" << arg << "\"" << endl; 109 #endif // __DEBUG_H__ 110 if ( prefix( arg, "-" ) ) { 111 // pass through arguments 112 113 if ( arg == "-Xlinker" || arg == "-o" ) { 114 args[nargs] = argv[i]; // pass the argument along 115 nargs += 1; 116 i += 1; 117 if ( i == argc ) continue; // next argument available ? 118 args[nargs] = argv[i]; // pass the argument along 119 nargs += 1; 120 } else if ( arg == "-XCFA" ) { // CFA pass through 121 i += 1; 122 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str(); 123 nargs += 1; 124 125 // CFA specific arguments 126 127 } else if ( arg == "-CFA" ) { 128 CFA_flag = true; // strip the -CFA flag 129 link = false; 130 args[nargs] = "-E"; // replace the argument with -E 131 nargs += 1; 132 } else if ( arg == "-debug" ) { 133 debug = true; // strip the debug flag 134 } else if ( arg == "-nodebug" ) { 135 debug = false; // strip the nodebug flag 136 } else if ( arg == "-quiet" ) { 137 quiet = true; // strip the quiet flag 138 } else if ( arg == "-noquiet" ) { 139 quiet = false; // strip the noquiet flag 140 } else if ( arg == "-help" ) { 141 help = true; // strip the help flag 142 } else if ( arg == "-nohelp" ) { 143 help = false; // strip the nohelp flag 144 } else if ( arg == "-compiler" ) { 145 // use the user specified compiler 146 i += 1; 147 if ( i == argc ) continue; // next argument available ? 148 compiler_path = argv[i]; 149 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) { 150 cerr << argv[0] << " error, cannot set environment variable." << endl; 151 exit( EXIT_FAILURE ); 152 } // if 153 154 // C++ specific arguments 155 156 } else if ( arg == "-v" ) { 157 verbose = true; // verbosity required 158 args[nargs] = argv[i]; // pass the argument along 159 nargs += 1; 160 } else if ( arg == "-g" ) { 161 debugging = true; // symbolic debugging required 162 args[nargs] = argv[i]; // pass the argument along 163 nargs += 1; 164 } else if ( prefix( arg, "-B" ) ) { 165 Bprefix = arg.substr(2); // strip the -B flag 166 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); 167 nargs += 1; 168 } else if ( prefix( arg, "-b" ) ) { 169 if ( arg.length() == 2 ) { // separate argument ? 170 i += 1; 171 if ( i == argc ) continue; // next argument available ? 172 arg += argv[i]; // concatenate argument 173 } // if 174 // later versions of gcc require the -b option to appear at the start of the command line 175 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list 176 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along 177 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) { 178 cerr << argv[0] << " error, cannot set environment variable." << endl; 179 exit( EXIT_FAILURE ); 180 } // if 181 sargs += 1; 182 nargs += 1; 183 } else if ( prefix( arg, "-V" ) ) { 184 if ( arg.length() == 2 ) { // separate argument ? 185 i += 1; 186 if ( i == argc ) continue; // next argument available ? 187 arg += argv[i]; // concatenate argument 188 } // if 189 // later versions of gcc require the -V option to appear at the start of the command line 190 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list 191 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along 192 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) { 193 cerr << argv[0] << " error, cannot set environment variable." << endl; 194 exit( EXIT_FAILURE ); 195 } // if 196 sargs += 1; 197 nargs += 1; 198 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) { 199 args[nargs] = argv[i]; // pass the argument along 200 nargs += 1; 201 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) { 202 cpp_flag = true; // cpp only 203 } // if 204 link = false; // no linkage required 205 } else if ( arg[1] == 'l' ) { 206 // if the user specifies a library, load it after user code 207 libs[nlibs] = argv[i]; 208 nlibs += 1; 209 } else { 210 // concatenate any other arguments 211 args[nargs] = argv[i]; 212 nargs += 1; 213 } // if 214 } else { 215 // concatenate other arguments 216 args[nargs] = argv[i]; 217 nargs += 1; 218 nonoptarg = true; 219 } // if 220 } // for 221 222 #ifdef __DEBUG_H__ 223 cerr << "args:"; 224 for ( int i = 1; i < nargs; i += 1 ) { 225 cerr << " " << args[i]; 226 } // for 227 cerr << endl; 228 #endif // __DEBUG_H__ 229 230 if ( cpp_flag && CFA_flag ) { 231 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl; 232 exit( EXIT_FAILURE ); 233 } // if 234 235 string d; 236 if ( debug ) { 237 d = "-d"; 238 } // if 239 240 args[nargs] = "-I" CFA_INCDIR; 241 nargs += 1; 242 243 if ( link ) { 244 // include the cfa library in case it's needed 245 args[nargs] = "-L" CFA_LIBDIR; 246 nargs += 1; 247 args[nargs] = "-lcfa"; 248 nargs += 1; 249 } // if 250 251 // add the correct set of flags based on the type of compile this is 252 253 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str(); 254 nargs += 1; 255 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str(); 256 nargs += 1; 257 258 if ( cpp_flag ) { 259 args[nargs] = "-D__CPP__"; 260 nargs += 1; 261 } // if 262 263 if ( CFA_flag ) { 264 args[nargs] = "-D__CFA__"; 265 nargs += 1; 266 } // if 267 268 if ( debug ) { 269 heading += " (debug)"; 270 args[nargs] = "-D__CFA_DEBUG__"; 271 nargs += 1; 272 } else { 273 heading += " (no debug)"; 274 } // if 275 276 if ( Bprefix.length() == 0 ) { 277 Bprefix = installlibdir; 164 278 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str(); 165 279 nargs += 1; 166 } else if ( prefix( arg, "-b" ) ) { 167 if ( arg.length() == 2 ) { // separate argument ? 168 i += 1; 169 if ( i == argc ) continue; // next argument available ? 170 arg += argv[i]; // concatenate argument 280 } // if 281 282 // execute the compilation command 283 284 args[0] = compiler_path.c_str(); // set compiler command for exec 285 // find actual name of the compiler independent of the path to it 286 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/' 287 if ( p == -1 ) { 288 compiler_name = compiler_path; 289 } else { 290 compiler_name = *new string( compiler_path.substr( p + 1 ) ); 291 } // if 292 293 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name 294 args[nargs] = "-no-integrated-cpp"; 295 nargs += 1; 296 args[nargs] = "-Wno-deprecated"; 297 nargs += 1; 298 args[nargs] = "-std=c99"; 299 nargs += 1; 300 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str(); 301 nargs += 1; 302 } else { 303 cerr << argv[0] << " error, compiler " << compiler_name << " not supported." << endl; 304 exit( EXIT_FAILURE ); 305 } // if 306 307 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries 308 args[nargs] = libs[i]; 309 nargs += 1; 310 } // for 311 312 args[nargs] = NULL; // terminate with NULL 313 314 #ifdef __DEBUG_H__ 315 cerr << "nargs: " << nargs << endl; 316 cerr << "args:" << endl; 317 for ( int i = 0; args[i] != NULL; i += 1 ) { 318 cerr << " \"" << args[i] << "\"" << endl; 319 } // for 320 #endif // __DEBUG_H__ 321 322 if ( ! quiet ) { 323 cerr << "CFA " << "Version " << Version << heading << endl; 324 325 if ( help ) { 326 cerr << 327 "-debug\t\t\t: use cfa runtime with debug checking" << endl << 328 "-help\t\t\t: print this help message" << endl << 329 "-quiet\t\t\t: print no messages from the cfa command" << endl << 330 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl << 331 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl << 332 "...\t\t\t: any other " << compiler_name << " flags" << endl; 171 333 } // if 172 // later versions of gcc require the -b option to appear at the start of the command line 173 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list 174 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along 175 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) { 176 cerr << argv[0] << " error, cannot set environment variable." << endl; 177 exit( EXIT_FAILURE ); 178 } // if 179 sargs += 1; 180 nargs += 1; 181 } else if ( prefix( arg, "-V" ) ) { 182 if ( arg.length() == 2 ) { // separate argument ? 183 i += 1; 184 if ( i == argc ) continue; // next argument available ? 185 arg += argv[i]; // concatenate argument 186 } // if 187 // later versions of gcc require the -V option to appear at the start of the command line 188 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list 189 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along 190 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) { 191 cerr << argv[0] << " error, cannot set environment variable." << endl; 192 exit( EXIT_FAILURE ); 193 } // if 194 sargs += 1; 195 nargs += 1; 196 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) { 197 args[nargs] = argv[i]; // pass the argument along 198 nargs += 1; 199 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) { 200 cpp_flag = true; // cpp only 201 } // if 202 link = false; // no linkage required 203 } else if ( arg[1] == 'l' ) { 204 // if the user specifies a library, load it after user code 205 libs[nlibs] = argv[i]; 206 nlibs += 1; 207 } else { 208 // concatenate any other arguments 209 args[nargs] = argv[i]; 210 nargs += 1; 211 } // if 212 } else { 213 // concatenate other arguments 214 args[nargs] = argv[i]; 215 nargs += 1; 216 nonoptarg = true; 217 } // if 218 } // for 219 220 #ifdef __DEBUG_H__ 221 cerr << "args:"; 222 for ( int i = 1; i < nargs; i += 1 ) { 223 cerr << " " << args[i]; 224 } // for 225 cerr << endl; 226 #endif // __DEBUG_H__ 227 228 if ( cpp_flag && CFA_flag ) { 229 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl; 334 } // if 335 336 if ( verbose ) { 337 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc 338 339 for ( int i = 0; args[i] != NULL; i += 1 ) { 340 cerr << args[i] << " "; 341 } // for 342 cerr << endl; 343 } // if 344 345 if ( ! nonoptarg ) { 346 cerr << argv[0] << " error, no input files" << endl; 347 exit( EXIT_FAILURE ); 348 } // if 349 350 // execute the command and return the result 351 352 execvp( args[0], (char *const *)args ); // should not return 353 perror( "CFA Translator error: cfa level, execvp" ); 230 354 exit( EXIT_FAILURE ); 231 } // if232 233 string d;234 if ( debug ) {235 d = "-d";236 } // if237 238 args[nargs] = "-I" CFA_INCDIR;239 nargs += 1;240 241 if ( link ) {242 // include the cfa library in case it's needed243 args[nargs] = "-L" CFA_LIBDIR;244 nargs += 1;245 args[nargs] = "-lcfa";246 nargs += 1;247 } // if248 249 // add the correct set of flags based on the type of compile this is250 251 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();252 nargs += 1;253 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();254 nargs += 1;255 256 if ( cpp_flag ) {257 args[nargs] = "-D__CPP__";258 nargs += 1;259 } // if260 261 if ( CFA_flag ) {262 args[nargs] = "-D__CFA__";263 nargs += 1;264 } // if265 266 if ( debug ) {267 heading += " (debug)";268 args[nargs] = "-D__CFA_DEBUG__";269 nargs += 1;270 } else {271 heading += " (no debug)";272 } // if273 274 if ( Bprefix.length() == 0 ) {275 Bprefix = installlibdir;276 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();277 nargs += 1;278 } // if279 280 // execute the compilation command281 282 args[0] = compiler_path.c_str(); // set compiler command for exec283 // find actual name of the compiler independent of the path to it284 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'285 if ( p == -1 ) {286 compiler_name = compiler_path;287 } else {288 compiler_name = *new string( compiler_path.substr( p + 1 ) );289 } // if290 291 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name292 args[nargs] = "-no-integrated-cpp";293 nargs += 1;294 args[nargs] = "-Wno-deprecated";295 nargs += 1;296 args[nargs] = "-std=c99";297 nargs += 1;298 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();299 nargs += 1;300 } else {301 cerr << argv[0] << " error, compiler " << compiler_name << " not supported." << endl;302 exit( EXIT_FAILURE );303 } // if304 305 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries306 args[nargs] = libs[i];307 nargs += 1;308 } // for309 310 args[nargs] = NULL; // terminate with NULL311 312 #ifdef __DEBUG_H__313 cerr << "nargs: " << nargs << endl;314 cerr << "args:" << endl;315 for ( int i = 0; args[i] != NULL; i += 1 ) {316 cerr << " \"" << args[i] << "\"" << endl;317 } // for318 #endif // __DEBUG_H__319 320 if ( ! quiet ) {321 cerr << "CFA " << "Version " << Version << heading << endl;322 323 if ( help ) {324 cerr <<325 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<326 "-help\t\t\t: print this help message" << endl <<327 "-quiet\t\t\t: print no messages from the cfa command" << endl <<328 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<329 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<330 "...\t\t\t: any other " << compiler_name << " flags" << endl;331 } // if332 } // if333 334 if ( verbose ) {335 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc336 337 for ( int i = 0; args[i] != NULL; i += 1 ) {338 cerr << args[i] << " ";339 } // for340 cerr << endl;341 } // if342 343 if ( ! nonoptarg ) {344 cerr << argv[0] << " error, no input files" << endl;345 exit( EXIT_FAILURE );346 } // if347 348 // execute the command and return the result349 350 execvp( args[0], (char *const *)args ); // should not return351 perror( "CFA Translator error: cfa level, execvp" );352 exit( EXIT_FAILURE );353 355 } // main 354 356 355 357 // Local Variables: // 358 // tab-width: 4 // 359 // mode: c++ // 356 360 // compile-command: "make install" // 357 361 // End: //
Note:
See TracChangeset
for help on using the changeset viewer.