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