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