[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 | //
|
---|
[51b73452] | 7 | // cc1.cc --
|
---|
[b87a5ed] | 8 | //
|
---|
| 9 | // Author : Peter A. Buhr
|
---|
[51b73452] | 10 | // Created On : Fri Aug 26 14:23:51 2005
|
---|
| 11 | // Last Modified By : Peter A. Buhr
|
---|
[68bceeb] | 12 | // Last Modified On : Thu Jul 19 10:46:11 2018
|
---|
| 13 | // Update Count : 111
|
---|
[51b73452] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #include <iostream>
|
---|
| 17 | using std::cerr;
|
---|
| 18 | using std::endl;
|
---|
| 19 | #include <string>
|
---|
| 20 | using std::string;
|
---|
[b87a5ed] | 21 | #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
|
---|
[51b73452] | 25 |
|
---|
[b87a5ed] | 26 | #include "config.h" // configure info
|
---|
[51b73452] | 27 |
|
---|
| 28 |
|
---|
| 29 | //#define __DEBUG_H__
|
---|
| 30 |
|
---|
| 31 |
|
---|
[e24f13a] | 32 | string compiler_name( CFA_BACKEND_CC ); // path/name of C compiler
|
---|
[51b73452] | 33 |
|
---|
[e3215c5] | 34 | string D__GCC_X__( "-D__GCC_X__=" );
|
---|
[51b73452] | 35 | string D__GCC_BPREFIX__( "-D__GCC_BPREFIX__=" );
|
---|
[d9a0e76] | 36 | string D__CFA_FLAGPREFIX__( "-D__CFA_FLAG__=" );
|
---|
[51b73452] | 37 |
|
---|
[bdd516a] | 38 | char tmpname[] = P_tmpdir "/CFAXXXXXX";
|
---|
| 39 | int tmpfilefd = -1;
|
---|
| 40 |
|
---|
[51b73452] | 41 |
|
---|
| 42 | bool prefix( string arg, string pre ) {
|
---|
[b87a5ed] | 43 | return arg.substr( 0, pre.size() ) == pre;
|
---|
[51b73452] | 44 | } // prefix
|
---|
| 45 |
|
---|
[dffaeac] | 46 | enum { NumSuffixes = 2 };
|
---|
| 47 | const string suffixes[NumSuffixes] = { "cfa", "hfa", };
|
---|
| 48 |
|
---|
| 49 | bool suffix( string arg ) {
|
---|
| 50 | //std::cerr << arg << std::endl;
|
---|
| 51 | size_t dot = arg.find_last_of( "." );
|
---|
| 52 | //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
|
---|
| 53 | if ( dot == string::npos ) return false;
|
---|
| 54 | string sx = arg.substr( dot + 1 );
|
---|
| 55 | for ( int i = 0; i < NumSuffixes; i += 1 ) {
|
---|
| 56 | if ( sx == suffixes[i] ) return true;
|
---|
| 57 | } // for
|
---|
| 58 | return false;
|
---|
| 59 | } // suffix
|
---|
| 60 |
|
---|
[51b73452] | 61 |
|
---|
| 62 | void checkEnv( const char *args[], int &nargs ) {
|
---|
[b87a5ed] | 63 | char *value;
|
---|
[51b73452] | 64 |
|
---|
[b87a5ed] | 65 | value = getenv( "__COMPILER__" );
|
---|
| 66 | if ( value != NULL ) {
|
---|
| 67 | compiler_name = value;
|
---|
[dffaeac] | 68 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 69 | cerr << "env arg:\"" << compiler_name << "\"" << endl;
|
---|
[dffaeac] | 70 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 71 | } // if
|
---|
[51b73452] | 72 |
|
---|
[b87a5ed] | 73 | value = getenv( "__GCC_MACHINE__" );
|
---|
| 74 | if ( value != NULL ) {
|
---|
| 75 | args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
|
---|
[dffaeac] | 76 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 77 | cerr << "env arg:\"" << args[nargs] << "\"" << endl;
|
---|
[dffaeac] | 78 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 79 | nargs += 1;
|
---|
| 80 | } // if
|
---|
[51b73452] | 81 |
|
---|
[b87a5ed] | 82 | value = getenv( "__GCC_VERSION__" );
|
---|
| 83 | if ( value != NULL ) {
|
---|
| 84 | args[nargs] = ( *new string( value ) ).c_str(); // pass the argument along
|
---|
[dffaeac] | 85 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 86 | cerr << "env arg:\"" << args[nargs] << "\"" << endl;
|
---|
[dffaeac] | 87 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 88 | nargs += 1;
|
---|
| 89 | } // if
|
---|
[51b73452] | 90 | } // checkEnv
|
---|
| 91 |
|
---|
| 92 |
|
---|
[bdd516a] | 93 | void rmtmpfile() {
|
---|
[b87a5ed] | 94 | if ( unlink( tmpname ) == -1 ) { // remove tmpname
|
---|
| 95 | perror ( "CFA Translator error: cpp failed" );
|
---|
| 96 | exit( EXIT_FAILURE );
|
---|
| 97 | } // if
|
---|
| 98 | tmpfilefd = -1; // mark closed
|
---|
[bdd516a] | 99 | } // rmtmpfile
|
---|
| 100 |
|
---|
| 101 |
|
---|
[b3c36f4] | 102 | void sigTermHandler( __attribute__((unused)) int signal ) {
|
---|
[b87a5ed] | 103 | if ( tmpfilefd != -1 ) { // RACE, file created ?
|
---|
| 104 | rmtmpfile(); // remove
|
---|
| 105 | exit( EXIT_FAILURE ); // terminate
|
---|
| 106 | } // if
|
---|
[bdd516a] | 107 | } // sigTermHandler
|
---|
| 108 |
|
---|
| 109 |
|
---|
[51b73452] | 110 | void Stage1( const int argc, const char * const argv[] ) {
|
---|
[b87a5ed] | 111 | int code;
|
---|
| 112 | int i;
|
---|
[51b73452] | 113 |
|
---|
[b87a5ed] | 114 | string arg;
|
---|
| 115 | string bprefix;
|
---|
[51b73452] | 116 |
|
---|
[b87a5ed] | 117 | const char *cpp_in = NULL;
|
---|
| 118 | const char *cpp_out = NULL;
|
---|
[51b73452] | 119 |
|
---|
[b87a5ed] | 120 | bool CFA_flag = false;
|
---|
| 121 | bool cpp_flag = false;
|
---|
| 122 | const char *o_name = NULL;
|
---|
[51b73452] | 123 |
|
---|
[b87a5ed] | 124 | const char *args[argc + 100]; // leave space for 100 additional cpp command line values
|
---|
| 125 | int nargs = 1; // number of arguments in args list; 0 => command name
|
---|
[7b937575] | 126 | const char *cargs[20]; // leave space for 20 additional cfa-cpp command line values
|
---|
| 127 | int ncargs = 1; // 0 => command name
|
---|
[51b73452] | 128 |
|
---|
[b87a5ed] | 129 | signal( SIGINT, sigTermHandler );
|
---|
| 130 | signal( SIGTERM, sigTermHandler );
|
---|
[bdd516a] | 131 |
|
---|
[dffaeac] | 132 | #ifdef __DEBUG_H__
|
---|
[7b937575] | 133 | cerr << "Stage1" << endl;
|
---|
[dffaeac] | 134 | #endif // __DEBUG_H__
|
---|
[7b937575] | 135 |
|
---|
[b87a5ed] | 136 | // process all the arguments
|
---|
[51b73452] | 137 |
|
---|
[b87a5ed] | 138 | checkEnv( args, nargs ); // arguments passed via environment variables
|
---|
[51b73452] | 139 |
|
---|
[b87a5ed] | 140 | for ( i = 1; i < argc; i += 1 ) {
|
---|
[dffaeac] | 141 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 142 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
|
---|
[dffaeac] | 143 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 144 | arg = argv[i];
|
---|
[dffaeac] | 145 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 146 | cerr << "arg:\"" << arg << "\"" << endl;
|
---|
[dffaeac] | 147 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 148 | if ( prefix( arg, "-" ) ) {
|
---|
| 149 | // strip g++ flags that are inappropriate or cause duplicates in subsequent passes
|
---|
| 150 |
|
---|
| 151 | if ( arg == "-quiet" ) {
|
---|
| 152 | } else if ( arg == "-imultilib" || arg == "-imultiarch" ) {
|
---|
| 153 | i += 1; // and the argument
|
---|
| 154 | } else if ( prefix( arg, "-A" ) ) {
|
---|
| 155 | } else if ( prefix( arg, "-D__GNU" ) ) {
|
---|
| 156 | //********
|
---|
| 157 | // GCC 5.6.0 SEPARATED THE -D FROM THE ARGUMENT!
|
---|
| 158 | //********
|
---|
| 159 | } else if ( arg == "-D" && prefix( argv[i + 1], "__GNU" ) ) {
|
---|
| 160 | i += 1; // and the argument
|
---|
| 161 |
|
---|
| 162 | // strip flags controlling cpp step
|
---|
| 163 |
|
---|
| 164 | } else if ( arg == "-D__CPP__" ) {
|
---|
| 165 | cpp_flag = true;
|
---|
| 166 | } else if ( arg == "-D" && string( argv[i + 1] ) == "__CPP__" ) {
|
---|
| 167 | i += 1; // and the argument
|
---|
| 168 | cpp_flag = true;
|
---|
[4c82a3c] | 169 | } else if ( arg == "-D__CFA_PREPROCESS__" ) {
|
---|
[b87a5ed] | 170 | CFA_flag = true;
|
---|
[4c82a3c] | 171 | } else if ( arg == "-D" && string( argv[i + 1] ) == "__CFA_PREPROCESS__" ) {
|
---|
[b87a5ed] | 172 | i += 1; // and the argument
|
---|
| 173 | CFA_flag = true;
|
---|
| 174 | } else if ( prefix( arg, D__CFA_FLAGPREFIX__ ) ) {
|
---|
[7b937575] | 175 | cargs[ncargs] = ( *new string( arg.substr( D__CFA_FLAGPREFIX__.size() ) ) ).c_str();
|
---|
| 176 | ncargs += 1;
|
---|
[b87a5ed] | 177 | } else if ( arg == "-D" && prefix( argv[i + 1], D__CFA_FLAGPREFIX__.substr(2) ) ) {
|
---|
[7b937575] | 178 | cargs[ncargs] = ( *new string( string( argv[i + 1] ).substr( D__CFA_FLAGPREFIX__.size() - 2 ) ) ).c_str();
|
---|
| 179 | ncargs += 1;
|
---|
[b87a5ed] | 180 | i += 1; // and the argument
|
---|
[dffaeac] | 181 | // } else if ( prefix( arg, D__GCC_X__ ) ) {
|
---|
| 182 | // args[nargs] = "-x";
|
---|
| 183 | // nargs += 1;
|
---|
| 184 | // args[nargs] = ( *new string( arg.substr( D__GCC_X__.size() ) ) ).c_str(); // pass the flag along
|
---|
| 185 | // nargs += 1;
|
---|
| 186 | // } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_X__.substr(2) ) ) {
|
---|
| 187 | // args[nargs] = "-x";
|
---|
| 188 | // nargs += 1;
|
---|
| 189 | // args[nargs] = ( *new string( string( argv[i + 1] ).substr( D__GCC_X__.size() - 2 ) ) ).c_str(); // pass the flag along
|
---|
| 190 | // nargs += 1;
|
---|
| 191 | // i += 1; // and the argument
|
---|
[b87a5ed] | 192 | } else if ( prefix( arg, D__GCC_BPREFIX__ ) ) {
|
---|
| 193 | bprefix = arg.substr( D__GCC_BPREFIX__.size() );
|
---|
| 194 | } else if ( arg == "-D" && prefix( argv[i + 1], D__GCC_BPREFIX__.substr(2) ) ) {
|
---|
| 195 | bprefix = string( argv[i + 1] ).substr( D__GCC_BPREFIX__.size() - 2 );
|
---|
| 196 | i += 1; // and the argument
|
---|
| 197 |
|
---|
[f8b6d921] | 198 | // all other flags
|
---|
[b87a5ed] | 199 |
|
---|
| 200 | } else if ( arg == "-o" ) {
|
---|
| 201 | i += 1;
|
---|
| 202 | o_name = argv[i];
|
---|
| 203 | } else {
|
---|
| 204 | args[nargs] = argv[i]; // pass the flag along
|
---|
| 205 | nargs += 1;
|
---|
| 206 | // CPP flags with an argument
|
---|
[f8b6d921] | 207 | if ( arg == "-D" || arg == "-U" || arg == "-I" || arg == "-MF" || arg == "-MT" || arg == "-MQ" ||
|
---|
[b87a5ed] | 208 | arg == "-include" || arg == "-imacros" || arg == "-idirafter" || arg == "-iprefix" ||
|
---|
| 209 | arg == "-iwithprefix" || arg == "-iwithprefixbefore" || arg == "-isystem" || arg == "-isysroot" ) {
|
---|
| 210 | i += 1;
|
---|
| 211 | args[nargs] = argv[i]; // pass the argument along
|
---|
| 212 | nargs += 1;
|
---|
[dffaeac] | 213 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 214 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
|
---|
[dffaeac] | 215 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 216 | } else if ( arg == "-MD" || arg == "-MMD" ) {
|
---|
| 217 | args[nargs] = "-MF"; // insert before file
|
---|
| 218 | nargs += 1;
|
---|
| 219 | i += 1;
|
---|
| 220 | args[nargs] = argv[i]; // pass the argument along
|
---|
| 221 | nargs += 1;
|
---|
[dffaeac] | 222 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 223 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
|
---|
[dffaeac] | 224 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 225 | } // if
|
---|
| 226 | } // if
|
---|
| 227 | } else { // obtain input and possibly output files
|
---|
| 228 | if ( cpp_in == NULL ) {
|
---|
| 229 | cpp_in = argv[i];
|
---|
[dffaeac] | 230 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 231 | cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
|
---|
[dffaeac] | 232 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 233 | } else if ( cpp_out == NULL ) {
|
---|
| 234 | cpp_out = argv[i];
|
---|
[dffaeac] | 235 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 236 | cerr << "cpp_out:\"" << cpp_out << "\""<< endl;
|
---|
[dffaeac] | 237 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 238 | } else {
|
---|
| 239 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
|
---|
| 240 | exit( EXIT_FAILURE );
|
---|
| 241 | } // if
|
---|
| 242 | } // if
|
---|
| 243 | } // for
|
---|
| 244 |
|
---|
[dffaeac] | 245 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 246 | cerr << "args:";
|
---|
| 247 | for ( i = 1; i < nargs; i += 1 ) {
|
---|
| 248 | cerr << " " << args[i];
|
---|
| 249 | } // for
|
---|
| 250 | if ( cpp_in != NULL ) cerr << " " << cpp_in;
|
---|
| 251 | if ( cpp_out != NULL ) cerr << " " << cpp_out;
|
---|
| 252 | cerr << endl;
|
---|
[dffaeac] | 253 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 254 |
|
---|
| 255 | if ( cpp_in == NULL ) {
|
---|
[51b73452] | 256 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
|
---|
| 257 | exit( EXIT_FAILURE );
|
---|
| 258 | } // if
|
---|
[b87a5ed] | 259 |
|
---|
| 260 | if ( cpp_flag ) {
|
---|
| 261 | // The -E flag is specified on the cfa command so only run the preprocessor and output is written to standard
|
---|
| 262 | // output or -o. The call to cfa has a -E so it does not have to be added to the argument list.
|
---|
| 263 |
|
---|
| 264 | args[0] = compiler_name.c_str();
|
---|
| 265 | args[nargs] = cpp_in;
|
---|
| 266 | nargs += 1;
|
---|
| 267 | if ( o_name != NULL ) { // location for output
|
---|
| 268 | args[nargs] = "-o";
|
---|
| 269 | nargs += 1;
|
---|
| 270 | args[nargs] = o_name;
|
---|
| 271 | nargs += 1;
|
---|
| 272 | } // if
|
---|
| 273 | args[nargs] = NULL; // terminate argument list
|
---|
[51b73452] | 274 |
|
---|
[dffaeac] | 275 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 276 | cerr << "nargs: " << nargs << endl;
|
---|
| 277 | for ( i = 0; args[i] != NULL; i += 1 ) {
|
---|
| 278 | cerr << args[i] << " ";
|
---|
| 279 | } // for
|
---|
| 280 | cerr << endl;
|
---|
[dffaeac] | 281 | #endif // __DEBUG_H__
|
---|
[51b73452] | 282 |
|
---|
[b87a5ed] | 283 | execvp( args[0], (char *const *)args ); // should not return
|
---|
| 284 | perror( "CFA Translator error: cpp level, execvp" );
|
---|
| 285 | exit( EXIT_FAILURE );
|
---|
| 286 | } // if
|
---|
[51b73452] | 287 |
|
---|
[b87a5ed] | 288 | // Create a temporary file to store output of the C preprocessor.
|
---|
[51b73452] | 289 |
|
---|
[b87a5ed] | 290 | tmpfilefd = mkstemp( tmpname );
|
---|
| 291 | if ( tmpfilefd == -1 ) {
|
---|
| 292 | perror( "CFA Translator error: cpp level, mkstemp" );
|
---|
| 293 | exit( EXIT_FAILURE );
|
---|
[51b73452] | 294 | } // if
|
---|
| 295 |
|
---|
[dffaeac] | 296 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 297 | cerr << "tmpname:" << tmpname << " tmpfilefd:" << tmpfilefd << endl;
|
---|
[dffaeac] | 298 | #endif // __DEBUG_H__
|
---|
[51b73452] | 299 |
|
---|
[b87a5ed] | 300 | // Run the C preprocessor and save the output in tmpfile.
|
---|
[51b73452] | 301 |
|
---|
[b87a5ed] | 302 | if ( fork() == 0 ) { // child process ?
|
---|
| 303 | // -o xxx.ii cannot be used to write the output file from cpp because no output file is created if cpp detects
|
---|
| 304 | // an error (e.g., cannot find include file). Whereas, output is always generated, even when there is an error,
|
---|
| 305 | // when cpp writes to stdout. Hence, stdout is redirected into the temporary file.
|
---|
| 306 | if ( freopen( tmpname, "w", stdout ) == NULL ) { // redirect stdout to tmpname
|
---|
| 307 | perror( "CFA Translator error: cpp level, freopen" );
|
---|
| 308 | exit( EXIT_FAILURE );
|
---|
| 309 | } // if
|
---|
[51b73452] | 310 |
|
---|
[b87a5ed] | 311 | args[0] = compiler_name.c_str();
|
---|
[dffaeac] | 312 | if ( suffix( cpp_in ) ) {
|
---|
| 313 | args[nargs] = "-x";
|
---|
| 314 | nargs += 1;
|
---|
| 315 | args[nargs] = "c";
|
---|
| 316 | nargs += 1;
|
---|
| 317 | } // if
|
---|
[b87a5ed] | 318 | args[nargs] = cpp_in; // input to cpp
|
---|
| 319 | nargs += 1;
|
---|
| 320 | args[nargs] = NULL; // terminate argument list
|
---|
[51b73452] | 321 |
|
---|
[dffaeac] | 322 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 323 | cerr << "cpp nargs: " << nargs << endl;
|
---|
| 324 | for ( i = 0; args[i] != NULL; i += 1 ) {
|
---|
| 325 | cerr << args[i] << " ";
|
---|
| 326 | } // for
|
---|
| 327 | cerr << endl;
|
---|
[dffaeac] | 328 | #endif // __DEBUG_H__
|
---|
[51b73452] | 329 |
|
---|
[b87a5ed] | 330 | execvp( args[0], (char *const *)args ); // should not return
|
---|
| 331 | perror( "CFA Translator error: cpp level, execvp" );
|
---|
| 332 | exit( EXIT_FAILURE );
|
---|
[51b73452] | 333 | } // if
|
---|
| 334 |
|
---|
[b87a5ed] | 335 | wait( &code ); // wait for child to finish
|
---|
[51b73452] | 336 |
|
---|
[dffaeac] | 337 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 338 | cerr << "return code from cpp:" << WEXITSTATUS(code) << endl;
|
---|
[dffaeac] | 339 | #endif // __DEBUG_H__
|
---|
[51b73452] | 340 |
|
---|
[b87a5ed] | 341 | if ( WIFSIGNALED(code) != 0 ) { // child failed ?
|
---|
| 342 | rmtmpfile(); // remove tmpname
|
---|
| 343 | cerr << "CFA Translator error: cpp failed with signal " << WTERMSIG(code) << endl;
|
---|
| 344 | exit( EXIT_FAILURE );
|
---|
| 345 | } // if
|
---|
[51b73452] | 346 |
|
---|
[b87a5ed] | 347 | if ( WEXITSTATUS(code) != 0 ) { // child error ?
|
---|
| 348 | rmtmpfile(); // remove tmpname
|
---|
| 349 | exit( WEXITSTATUS( code ) ); // do not continue
|
---|
| 350 | } // if
|
---|
[51b73452] | 351 |
|
---|
[b87a5ed] | 352 | // If -CFA flag specified, run the cfa-cpp preprocessor on the temporary file, and output is written to standard
|
---|
| 353 | // output. Otherwise, run the cfa-cpp preprocessor on the temporary file and save the result into the output file.
|
---|
[51b73452] | 354 |
|
---|
[b87a5ed] | 355 | if ( fork() == 0 ) { // child runs CFA
|
---|
[7b937575] | 356 | cargs[0] = ( *new string( bprefix + "/cfa-cpp" ) ).c_str();
|
---|
[b87a5ed] | 357 |
|
---|
[4acc87f] | 358 | // Source file-name used to generate routine names containing global initializations for TU.
|
---|
[7b937575] | 359 | cargs[ncargs] = ( *new string( "-F" ) ).c_str();
|
---|
| 360 | ncargs += 1;
|
---|
[4acc87f] | 361 | cargs[ncargs] = ( *new string( string( cpp_in ) ) ).c_str();
|
---|
[7b937575] | 362 | ncargs += 1;
|
---|
| 363 |
|
---|
| 364 | cargs[ncargs] = tmpname;
|
---|
| 365 | ncargs += 1;
|
---|
[b87a5ed] | 366 | if ( o_name != NULL ) {
|
---|
[7b937575] | 367 | cargs[ncargs] = o_name;
|
---|
| 368 | ncargs += 1;
|
---|
[b87a5ed] | 369 | } else if ( ! CFA_flag ) { // run cfa-cpp ?
|
---|
[7b937575] | 370 | cargs[ncargs] = cpp_out;
|
---|
| 371 | ncargs += 1;
|
---|
[b87a5ed] | 372 | } // if
|
---|
[7b937575] | 373 | cargs[ncargs] = NULL; // terminate argument list
|
---|
[51b73452] | 374 |
|
---|
[dffaeac] | 375 | #ifdef __DEBUG_H__
|
---|
[7b937575] | 376 | cerr << "cfa-cpp ncargs: " << o_name << " " << CFA_flag << " " << ncargs << endl;
|
---|
| 377 | for ( i = 0; cargs[i] != NULL; i += 1 ) {
|
---|
| 378 | cerr << cargs[i] << " ";
|
---|
[b87a5ed] | 379 | } // for
|
---|
| 380 | cerr << endl;
|
---|
[dffaeac] | 381 | #endif // __DEBUG_H__
|
---|
[51b73452] | 382 |
|
---|
[7b937575] | 383 | execvp( cargs[0], (char * const *)cargs ); // should not return
|
---|
[b87a5ed] | 384 | perror( "CFA Translator error: cpp level, execvp" );
|
---|
| 385 | exit( EXIT_FAILURE );
|
---|
| 386 | } // if
|
---|
[51b73452] | 387 |
|
---|
[b87a5ed] | 388 | wait( &code ); // wait for child to finish
|
---|
[51b73452] | 389 |
|
---|
[dffaeac] | 390 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 391 | cerr << "return code from cfa-cpp:" << WEXITSTATUS(code) << endl;
|
---|
[dffaeac] | 392 | #endif // __DEBUG_H__
|
---|
[51b73452] | 393 |
|
---|
[b87a5ed] | 394 | // Must unlink here because file must exist across execvp.
|
---|
| 395 | rmtmpfile(); // remove tmpname
|
---|
[51b73452] | 396 |
|
---|
[b87a5ed] | 397 | if ( WIFSIGNALED(code) ) { // child failed ?
|
---|
| 398 | cerr << "CFA Translator error: cfa-cpp failed with signal " << WTERMSIG(code) << endl;
|
---|
| 399 | exit( EXIT_FAILURE );
|
---|
| 400 | } // if
|
---|
[51b73452] | 401 |
|
---|
[b87a5ed] | 402 | exit( WEXITSTATUS(code) );
|
---|
[51b73452] | 403 | } // Stage1
|
---|
| 404 |
|
---|
| 405 |
|
---|
| 406 | void Stage2( const int argc, const char * const * argv ) {
|
---|
[b87a5ed] | 407 | int i;
|
---|
[51b73452] | 408 |
|
---|
[b87a5ed] | 409 | string arg;
|
---|
[51b73452] | 410 |
|
---|
[b87a5ed] | 411 | const char *cpp_in = NULL;
|
---|
[51b73452] | 412 |
|
---|
[b87a5ed] | 413 | const char *args[argc + 100]; // leave space for 100 additional cfa command line values
|
---|
| 414 | int nargs = 1; // number of arguments in args list; 0 => command name
|
---|
[51b73452] | 415 |
|
---|
[dffaeac] | 416 | #ifdef __DEBUG_H__
|
---|
[7b937575] | 417 | cerr << "Stage2" << endl;
|
---|
[dffaeac] | 418 | #endif // __DEBUG_H__
|
---|
[7b937575] | 419 |
|
---|
[b87a5ed] | 420 | // process all the arguments
|
---|
[51b73452] | 421 |
|
---|
[b87a5ed] | 422 | checkEnv( args, nargs ); // arguments passed via environment variables
|
---|
[51b73452] | 423 |
|
---|
[b87a5ed] | 424 | for ( i = 1; i < argc; i += 1 ) {
|
---|
[dffaeac] | 425 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 426 | cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
|
---|
[dffaeac] | 427 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 428 | arg = argv[i];
|
---|
[dffaeac] | 429 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 430 | cerr << "arg:\"" << arg << "\"" << endl;
|
---|
[dffaeac] | 431 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 432 | if ( prefix( arg, "-" ) ) {
|
---|
| 433 | // strip inappropriate flags
|
---|
[51b73452] | 434 |
|
---|
[b87a5ed] | 435 | if ( arg == "-quiet" || arg == "-version" || arg == "-fpreprocessed" ||
|
---|
[dffaeac] | 436 | // Currently CFA does not suppose precompiled .h files.
|
---|
| 437 | prefix( arg, "--output-pch" ) ) {
|
---|
[51b73452] | 438 |
|
---|
[b87a5ed] | 439 | // strip inappropriate flags with an argument
|
---|
[51b73452] | 440 |
|
---|
[b87a5ed] | 441 | } else if ( arg == "-auxbase" || arg == "-auxbase-strip" || arg == "-dumpbase" ) {
|
---|
| 442 | i += 1;
|
---|
[dffaeac] | 443 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 444 | cerr << "arg:\"" << argv[i] << "\"" << endl;
|
---|
[dffaeac] | 445 | #endif // __DEBUG_H__
|
---|
[51b73452] | 446 |
|
---|
[b87a5ed] | 447 | // all other flags
|
---|
[51b73452] | 448 |
|
---|
[b87a5ed] | 449 | } else {
|
---|
| 450 | args[nargs] = argv[i]; // pass the flag along
|
---|
| 451 | nargs += 1;
|
---|
| 452 | if ( arg == "-o" ) {
|
---|
| 453 | i += 1;
|
---|
| 454 | args[nargs] = argv[i]; // pass the argument along
|
---|
| 455 | nargs += 1;
|
---|
[68bceeb] | 456 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 457 | cerr << "arg:\"" << argv[i] << "\"" << endl;
|
---|
[68bceeb] | 458 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 459 | } // if
|
---|
| 460 | } // if
|
---|
| 461 | } else { // obtain input and possibly output files
|
---|
| 462 | if ( cpp_in == NULL ) {
|
---|
| 463 | cpp_in = argv[i];
|
---|
[dffaeac] | 464 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 465 | cerr << "cpp_in:\"" << cpp_in << "\"" << endl;
|
---|
[dffaeac] | 466 | #endif // __DEBUG_H__
|
---|
[b87a5ed] | 467 | } else {
|
---|
| 468 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
|
---|
| 469 | exit( EXIT_FAILURE );
|
---|
| 470 | } // if
|
---|
| 471 | } // if
|
---|
| 472 | } // for
|
---|
[51b73452] | 473 |
|
---|
[dffaeac] | 474 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 475 | cerr << "args:";
|
---|
| 476 | for ( i = 1; i < nargs; i += 1 ) {
|
---|
| 477 | cerr << " " << args[i];
|
---|
| 478 | } // for
|
---|
| 479 | cerr << endl;
|
---|
| 480 | if ( cpp_in != NULL ) cerr << " " << cpp_in;
|
---|
[dffaeac] | 481 | #endif // __DEBUG_H__
|
---|
[51b73452] | 482 |
|
---|
[b87a5ed] | 483 | args[0] = compiler_name.c_str();
|
---|
| 484 | args[nargs] = "-S"; // only compile and put assembler output in specified file
|
---|
| 485 | nargs += 1;
|
---|
| 486 | args[nargs] = cpp_in;
|
---|
| 487 | nargs += 1;
|
---|
| 488 | args[nargs] = NULL; // terminate argument list
|
---|
[51b73452] | 489 |
|
---|
[dffaeac] | 490 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 491 | cerr << "stage2 nargs: " << nargs << endl;
|
---|
| 492 | for ( i = 0; args[i] != NULL; i += 1 ) {
|
---|
| 493 | cerr << args[i] << " ";
|
---|
| 494 | } // for
|
---|
| 495 | cerr << endl;
|
---|
[dffaeac] | 496 | #endif // __DEBUG_H__
|
---|
[51b73452] | 497 |
|
---|
[b87a5ed] | 498 | execvp( args[0], (char * const *)args ); // should not return
|
---|
| 499 | perror( "CFA Translator error: cpp level, execvp" );
|
---|
| 500 | exit( EXIT_FAILURE ); // tell gcc not to go any further
|
---|
[51b73452] | 501 | } // Stage2
|
---|
| 502 |
|
---|
| 503 |
|
---|
[b3c36f4] | 504 | int main( const int argc, const char * const argv[], __attribute__((unused)) const char * const env[] ) {
|
---|
[dffaeac] | 505 | #ifdef __DEBUG_H__
|
---|
[b87a5ed] | 506 | for ( int i = 0; env[i] != NULL; i += 1 ) {
|
---|
| 507 | cerr << env[i] << endl;
|
---|
| 508 | } // for
|
---|
[dffaeac] | 509 | #endif // __DEBUG_H__
|
---|
[51b73452] | 510 |
|
---|
[b87a5ed] | 511 | string arg = argv[1];
|
---|
[51b73452] | 512 |
|
---|
[b87a5ed] | 513 | // Currently, stage 1 starts with flag -E and stage 2 with flag -fpreprocessed.
|
---|
[51b73452] | 514 |
|
---|
[b87a5ed] | 515 | if ( arg == "-E" ) {
|
---|
| 516 | Stage1( argc, argv );
|
---|
| 517 | } else if ( arg == "-fpreprocessed" ) {
|
---|
| 518 | Stage2( argc, argv );
|
---|
| 519 | } else {
|
---|
| 520 | cerr << "Usage: " << argv[0] << " input-file [output-file] [options]" << endl;
|
---|
| 521 | exit( EXIT_FAILURE );
|
---|
| 522 | } // if
|
---|
[51b73452] | 523 | } // main
|
---|
| 524 |
|
---|
| 525 | // Local Variables: //
|
---|
[b87a5ed] | 526 | // tab-width: 4 //
|
---|
| 527 | // mode: c++ //
|
---|
[51b73452] | 528 | // compile-command: "make install" //
|
---|
| 529 | // End: //
|
---|