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