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