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