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