source: src/driver/cfa.cc@ afd550c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since afd550c was af39199d, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

add and use search routine

  • Property mode set to 100644
File size: 14.4 KB
RevLine 
[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//
[76c7f65e]7// cfa.cc --
[b87a5ed]8//
[51b73452]9// Author : Peter A. Buhr
10// Created On : Tue Aug 20 13:44:49 2002
[201aeb9]11// Last Modified By : Peter A. Buhr
[af39199d]12// Last Modified On : Wed May 2 17:57:43 2018
13// Update Count : 224
[b87a5ed]14//
[51b73452]15
16#include <iostream>
[b87a5ed]17#include <cstdio> // perror
18#include <cstdlib> // putenv, exit
19#include <unistd.h> // execvp
20#include <string> // STL version
[44bca7f]21#include <string.h> // strcmp
[51b73452]22
[44bca7f]23#include "Common/SemanticError.h"
[b87a5ed]24#include "config.h" // configure info
[51b73452]25
26using std::cerr;
27using std::endl;
28using std::string;
[47a8d17]29using std::to_string;
[51b73452]30
31
32//#define __DEBUG_H__
33
34
35bool prefix( string arg, string pre ) {
[b87a5ed]36 return arg.substr( 0, pre.size() ) == pre;
[51b73452]37} // prefix
38
39
40void shuffle( const char *args[], int S, int E, int N ) {
[b87a5ed]41 // S & E index 1 passed the end so adjust with -1
[51b73452]42#ifdef __DEBUG_H__
[b87a5ed]43 cerr << "shuffle:" << S << " " << E << " " << N << endl;
[51b73452]44#endif // __DEBUG_H__
[b87a5ed]45 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
[51b73452]46#ifdef __DEBUG_H__
[b87a5ed]47 cerr << "\t" << j << " " << j-N << endl;
[51b73452]48#endif // __DEBUG_H__
[b87a5ed]49 args[j] = args[j-N];
50 } // for
[51b73452]51} // shuffle
52
53
[4b1afb6]54#define str(s) #s
55
[51b73452]56int main( int argc, char *argv[] ) {
[4b1afb6]57 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
58 string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
[51b73452]59
[00cc023]60 string installincdir( CFA_INCDIR ); // fixed location of include files
61 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands
[51b73452]62
[b87a5ed]63 string heading; // banner printed at start of cfa compilation
64 string arg; // current command-line argument during command-line parsing
65 string Bprefix; // path where gcc looks for compiler command steps
66 string langstd; // language standard
[51b73452]67
[e24f13a]68 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
[b87a5ed]69 string compiler_name; // name of C compiler
[51b73452]70
[b87a5ed]71 bool nonoptarg = false; // indicates non-option argument specified
72 bool link = true; // linking as well as compiling
73 bool verbose = false; // -v flag
74 bool quiet = false; // -quiet flag
75 bool debug = true; // -debug flag
76 bool help = false; // -help flag
77 bool CFA_flag = false; // -CFA flag
78 bool cpp_flag = false; // -E or -M flag, preprocessor only
[de62360d]79 bool std_flag = false; // -std= flag
[d746bc8]80 bool noincstd_flag = false; // -no-include-stdhdr= flag
[6e4b913]81 bool debugging __attribute(( unused )) = false; // -g flag
[51b73452]82
[b87a5ed]83 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
84 int sargs = 1; // starting location for arguments in args list
85 int nargs = sargs; // number of arguments in args list; 0 => command name
[51b73452]86
[b87a5ed]87 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
88 int nlibs = 0;
[51b73452]89
90#ifdef __DEBUG_H__
[b87a5ed]91 cerr << "CFA:" << endl;
[51b73452]92#endif // __DEBUG_H__
93
[b87a5ed]94 // process command-line arguments
[51b73452]95
[b87a5ed]96 for ( int i = 1; i < argc; i += 1 ) {
[51b73452]97#ifdef __DEBUG_H__
[b87a5ed]98 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
[51b73452]99#endif // __DEBUG_H__
[b87a5ed]100 arg = argv[i]; // convert to string value
[51b73452]101#ifdef __DEBUG_H__
[b87a5ed]102 cerr << "arg:\"" << arg << "\"" << endl;
[51b73452]103#endif // __DEBUG_H__
[b87a5ed]104 if ( prefix( arg, "-" ) ) {
105 // pass through arguments
106
107 if ( arg == "-Xlinker" || arg == "-o" ) {
108 args[nargs] = argv[i]; // pass the argument along
109 nargs += 1;
110 i += 1;
111 if ( i == argc ) continue; // next argument available ?
112 args[nargs] = argv[i]; // pass the argument along
113 nargs += 1;
114 } else if ( arg == "-XCFA" ) { // CFA pass through
115 i += 1;
116 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
117 nargs += 1;
118
119 // CFA specific arguments
120
121 } else if ( arg == "-CFA" ) {
122 CFA_flag = true; // strip the -CFA flag
123 link = false;
124 args[nargs] = "-E"; // replace the argument with -E
125 nargs += 1;
126 } else if ( arg == "-debug" ) {
127 debug = true; // strip the debug flag
128 } else if ( arg == "-nodebug" ) {
129 debug = false; // strip the nodebug flag
130 } else if ( arg == "-quiet" ) {
131 quiet = true; // strip the quiet flag
132 } else if ( arg == "-noquiet" ) {
133 quiet = false; // strip the noquiet flag
134 } else if ( arg == "-help" ) {
135 help = true; // strip the help flag
136 } else if ( arg == "-nohelp" ) {
137 help = false; // strip the nohelp flag
[d746bc8]138 } else if ( arg == "-no-include-stdhdr" ) {
139 noincstd_flag = true; // strip the no-include-stdhdr flag
[b87a5ed]140 } else if ( arg == "-compiler" ) {
141 // use the user specified compiler
142 i += 1;
143 if ( i == argc ) continue; // next argument available ?
144 compiler_path = argv[i];
145 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
146 cerr << argv[0] << " error, cannot set environment variable." << endl;
147 exit( EXIT_FAILURE );
148 } // if
149
[de62360d]150 // C specific arguments
[b87a5ed]151
152 } else if ( arg == "-v" ) {
153 verbose = true; // verbosity required
154 args[nargs] = argv[i]; // pass the argument along
155 nargs += 1;
156 } else if ( arg == "-g" ) {
157 debugging = true; // symbolic debugging required
158 args[nargs] = argv[i]; // pass the argument along
159 nargs += 1;
[de62360d]160 } else if ( prefix( arg, "-std=" ) ) {
[53ba273]161 std_flag = true; // -std=XX provided
[de62360d]162 args[nargs] = argv[i]; // pass the argument along
163 nargs += 1;
[44bca7f]164 } else if ( arg == "-w" ) {
165 args[nargs] = argv[i]; // pass the argument along
166 nargs += 1;
167 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
168 nargs += 1;
169 } else if ( prefix( arg, "-W" ) ) { // check before next tests
170 if ( arg == "-Werror" || arg == "-Wall" ) {
171 args[nargs] = argv[i]; // pass the argument along
172 nargs += 1;
173 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
174 nargs += 1;
175 } else {
176 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
177 args[nargs] = argv[i]; // conditionally pass the argument along
[af39199d]178 const char * warning = argv[i] + adv; // extract warning
179 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
180 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
181 } // if
[44bca7f]182 nargs += 1;
183 } // if
[b87a5ed]184 } else if ( prefix( arg, "-B" ) ) {
185 Bprefix = arg.substr(2); // strip the -B flag
186 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
187 nargs += 1;
188 } else if ( prefix( arg, "-b" ) ) {
189 if ( arg.length() == 2 ) { // separate argument ?
190 i += 1;
191 if ( i == argc ) continue; // next argument available ?
192 arg += argv[i]; // concatenate argument
193 } // if
194 // later versions of gcc require the -b option to appear at the start of the command line
195 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
196 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
197 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
198 cerr << argv[0] << " error, cannot set environment variable." << endl;
199 exit( EXIT_FAILURE );
200 } // if
201 sargs += 1;
202 nargs += 1;
203 } else if ( prefix( arg, "-V" ) ) {
204 if ( arg.length() == 2 ) { // separate argument ?
205 i += 1;
206 if ( i == argc ) continue; // next argument available ?
207 arg += argv[i]; // concatenate argument
208 } // if
209 // later versions of gcc require the -V option to appear at the start of the command line
210 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
211 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
212 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
213 cerr << argv[0] << " error, cannot set environment variable." << endl;
214 exit( EXIT_FAILURE );
215 } // if
216 sargs += 1;
217 nargs += 1;
218 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
219 args[nargs] = argv[i]; // pass the argument along
220 nargs += 1;
221 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
222 cpp_flag = true; // cpp only
223 } // if
224 link = false; // no linkage required
225 } else if ( arg[1] == 'l' ) {
226 // if the user specifies a library, load it after user code
227 libs[nlibs] = argv[i];
228 nlibs += 1;
229 } else {
230 // concatenate any other arguments
231 args[nargs] = argv[i];
232 nargs += 1;
233 } // if
234 } else {
235 // concatenate other arguments
236 args[nargs] = argv[i];
237 nargs += 1;
238 nonoptarg = true;
239 } // if
240 } // for
[51b73452]241
[b87a5ed]242#ifdef __DEBUG_H__
243 cerr << "args:";
244 for ( int i = 1; i < nargs; i += 1 ) {
245 cerr << " " << args[i];
246 } // for
247 cerr << endl;
248#endif // __DEBUG_H__
[51b73452]249
[b87a5ed]250 if ( cpp_flag && CFA_flag ) {
251 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
252 exit( EXIT_FAILURE );
253 } // if
[51b73452]254
[6e4b913]255 // add the CFA include-library paths, which allow direct access to header files without directory qualification
[b87a5ed]256 args[nargs] = "-I" CFA_INCDIR;
257 nargs += 1;
[d746bc8]258 if ( ! noincstd_flag ) { // do not use during build
259 args[nargs] = "-I" CFA_INCDIR "/stdhdr";
260 nargs += 1;
261 } // if
[0e76cf4f]262 args[nargs] = "-I" CFA_INCDIR "/concurrency";
263 nargs += 1;
[76c7f65e]264 args[nargs] = "-I" CFA_INCDIR "/containers";
265 nargs += 1;
266
[0edebf8]267#ifdef HAVE_LIBCFA
[b87a5ed]268 if ( link ) {
[0edebf8]269 #if ! defined(HAVE_LIBCFA_RELEASE)
[ff2d7341]270 if( !debug ) {
[44bca7f]271 cerr << "error: Option -nodebug is unavailable, libcfa was not installed." << endl;
[0edebf8]272 exit( EXIT_FAILURE );
273 }
274 #endif
275 #if ! defined(HAVE_LIBCFA_DEBUG)
[ff2d7341]276 if( debug ) {
[44bca7f]277 cerr << "error: Option -debug is unavailable, libcfa-d was not installed." << endl;
[0edebf8]278 exit( EXIT_FAILURE );
279 }
280 #endif
281
[6bfe5cc]282 args[nargs] = "-Xlinker";
283 nargs += 1;
284 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
285 nargs += 1;
286 args[nargs] = "-Xlinker";
287 nargs += 1;
288 args[nargs] = "--undefined=__cfaabi_interpose_startup";
289 nargs += 1;
290
[b87a5ed]291 // include the cfa library in case it's needed
292 args[nargs] = "-L" CFA_LIBDIR;
[51b73452]293 nargs += 1;
[0edebf8]294 if( debug ) {
295 args[nargs] = "-lcfa-d";
296 } else {
297 args[nargs] = "-lcfa";
298 }
[51b73452]299 nargs += 1;
[63f78f0]300 args[nargs] = "-lpthread";
301 nargs += 1;
[9d944b2]302 args[nargs] = "-ldl";
303 nargs += 1;
[c5ac6d5]304 args[nargs] = "-lrt";
305 nargs += 1;
[51b73452]306 } // if
[6bfe5cc]307#endif // HAVE_LIBCFA
[51b73452]308
[e9145a3]309 // Add exception flags (unconditionally)
310 args[nargs] = "-fexceptions";
311 nargs += 1;
312
[b87a5ed]313 // add the correct set of flags based on the type of compile this is
[8c17ab0]314
[ec129c4]315 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
[51b73452]316 nargs += 1;
[b87a5ed]317 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
[51b73452]318 nargs += 1;
[ec129c4]319 args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
[1db21619]320 nargs += 1;
[4c82a3c]321 args[nargs] = "-D__CFA__";
322 nargs += 1;
323 args[nargs] = "-D__CFORALL__";
[02e5ab6]324 nargs += 1;
[6acb935]325 args[nargs] = "-D__cforall";
326 nargs += 1;
[51b73452]327
[b87a5ed]328 if ( cpp_flag ) {
329 args[nargs] = "-D__CPP__";
330 nargs += 1;
331 } // if
[51b73452]332
[fa477f7]333 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
334 nargs += 1;
[b87a5ed]335 if ( CFA_flag ) {
[fa477f7]336 args[sargs] = "-D__CFA_FLAG__=-N";
[4c82a3c]337 args[nargs] = "-D__CFA_PREPROCESS_";
[b87a5ed]338 nargs += 1;
[fa477f7]339 } else {
340 args[sargs] = "-D__CFA_FLAG__=-L";
[b87a5ed]341 } // if
[fa477f7]342 sargs += 1;
[51b73452]343
[b87a5ed]344 if ( debug ) {
345 heading += " (debug)";
346 args[nargs] = "-D__CFA_DEBUG__";
347 nargs += 1;
348 } else {
349 heading += " (no debug)";
350 } // if
[51b73452]351
[b87a5ed]352 if ( Bprefix.length() == 0 ) {
353 Bprefix = installlibdir;
354 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
355 nargs += 1;
356 } // if
[51b73452]357
[6bfe5cc]358 args[nargs] = "-Xlinker"; // used by backtrace
359 nargs += 1;
360 args[nargs] = "-export-dynamic";
361 nargs += 1;
362
[b87a5ed]363 // execute the compilation command
[51b73452]364
[b87a5ed]365 args[0] = compiler_path.c_str(); // set compiler command for exec
366 // find actual name of the compiler independent of the path to it
367 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
368 if ( p == -1 ) {
369 compiler_name = compiler_path;
370 } else {
371 compiler_name = *new string( compiler_path.substr( p + 1 ) );
372 } // if
[51b73452]373
[b87a5ed]374 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
375 args[nargs] = "-no-integrated-cpp";
376 nargs += 1;
[76c7f65e]377 args[nargs] = "-Wno-deprecated";
[b87a5ed]378 nargs += 1;
[de62360d]379 if ( ! std_flag ) { // default c99, if none specified
[53ba273]380 args[nargs] = "-std=gnu99";
[de62360d]381 nargs += 1;
382 } // if
[76c7f65e]383 args[nargs] = "-fgnu89-inline";
[6e991d6]384 nargs += 1;
[201aeb9]385 args[nargs] = "-D__int8_t_defined"; // prevent gcc type-size attributes
386 nargs += 1;
[b87a5ed]387 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
388 nargs += 1;
[d3b7937]389 args[nargs] = "-lm";
390 nargs += 1;
[b87a5ed]391 } else {
[e24f13a]392 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
[b87a5ed]393 exit( EXIT_FAILURE );
394 } // if
[51b73452]395
[b87a5ed]396 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
397 args[nargs] = libs[i];
398 nargs += 1;
399 } // for
[51b73452]400
[b87a5ed]401 args[nargs] = NULL; // terminate with NULL
[51b73452]402
403#ifdef __DEBUG_H__
[b87a5ed]404 cerr << "nargs: " << nargs << endl;
405 cerr << "args:" << endl;
406 for ( int i = 0; args[i] != NULL; i += 1 ) {
407 cerr << " \"" << args[i] << "\"" << endl;
408 } // for
[51b73452]409#endif // __DEBUG_H__
410
[b87a5ed]411 if ( ! quiet ) {
412 cerr << "CFA " << "Version " << Version << heading << endl;
413
414 if ( help ) {
415 cerr <<
416 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
417 "-help\t\t\t: print this help message" << endl <<
418 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
419 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
420 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
421 "...\t\t\t: any other " << compiler_name << " flags" << endl;
422 } // if
[51b73452]423 } // if
424
[b87a5ed]425 if ( verbose ) {
426 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
[51b73452]427
[b87a5ed]428 for ( int i = 0; args[i] != NULL; i += 1 ) {
429 cerr << args[i] << " ";
430 } // for
431 cerr << endl;
432 } // if
[51b73452]433
[b87a5ed]434 if ( ! nonoptarg ) {
435 cerr << argv[0] << " error, no input files" << endl;
436 exit( EXIT_FAILURE );
437 } // if
[51b73452]438
[b87a5ed]439 // execute the command and return the result
[51b73452]440
[b87a5ed]441 execvp( args[0], (char *const *)args ); // should not return
442 perror( "CFA Translator error: cfa level, execvp" );
443 exit( EXIT_FAILURE );
[51b73452]444} // main
445
446// Local Variables: //
[b87a5ed]447// tab-width: 4 //
448// mode: c++ //
[51b73452]449// compile-command: "make install" //
450// End: //
Note: See TracBrowser for help on using the repository browser.