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