source: src/driver/cfa.cc@ 46fa473

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 46fa473 was 157d094, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

change from -std=gnu99 to -std=gnu11 for gcc

  • Property mode set to 100644
File size: 15.1 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
[157d094]12// Last Modified On : Mon May 14 14:16:33 2018
13// Update Count : 244
[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;
[e3215c5]160 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
[53ba273]161 std_flag = true; // -std=XX provided
[de62360d]162 args[nargs] = argv[i]; // pass the argument along
163 nargs += 1;
[e3215c5]164 } else if ( arg == "-x" ) { // lost so force along
165 args[nargs] = argv[i]; // pass the argument along
166 nargs += 1;
167 i += 1; // advance to argument
168 args[nargs] = argv[i]; // pass the argument along
169 nargs += 1;
170 args[nargs] = ( *new string( string("-D__GCC_X__=") + argv[i] ) ).c_str(); // add the argument for -x
171 nargs += 1;
172 } else if ( prefix( arg, "-x" ) ) { // lost so force along
173 args[nargs] = argv[i]; // pass the argument along
174 nargs += 1;
175 args[nargs] = ( *new string( string("-D__GCC_X__=") + arg.substr(2) ) ).c_str(); // add the argument for -x
176 nargs += 1;
[44bca7f]177 } else if ( arg == "-w" ) {
178 args[nargs] = argv[i]; // pass the argument along
179 nargs += 1;
180 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
181 nargs += 1;
182 } else if ( prefix( arg, "-W" ) ) { // check before next tests
183 if ( arg == "-Werror" || arg == "-Wall" ) {
184 args[nargs] = argv[i]; // pass the argument along
185 nargs += 1;
186 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
187 nargs += 1;
188 } else {
189 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
190 args[nargs] = argv[i]; // conditionally pass the argument along
[af39199d]191 const char * warning = argv[i] + adv; // extract warning
192 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
193 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
194 } // if
[44bca7f]195 nargs += 1;
196 } // if
[b87a5ed]197 } else if ( prefix( arg, "-B" ) ) {
198 Bprefix = arg.substr(2); // strip the -B flag
199 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
200 nargs += 1;
201 } else if ( prefix( arg, "-b" ) ) {
202 if ( arg.length() == 2 ) { // separate argument ?
203 i += 1;
204 if ( i == argc ) continue; // next argument available ?
205 arg += argv[i]; // concatenate argument
206 } // if
207 // later versions of gcc require the -b option to appear at the start of the command line
208 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
209 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
210 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
211 cerr << argv[0] << " error, cannot set environment variable." << endl;
212 exit( EXIT_FAILURE );
213 } // if
214 sargs += 1;
215 nargs += 1;
216 } else if ( prefix( arg, "-V" ) ) {
217 if ( arg.length() == 2 ) { // separate argument ?
218 i += 1;
219 if ( i == argc ) continue; // next argument available ?
220 arg += argv[i]; // concatenate argument
221 } // if
222 // later versions of gcc require the -V option to appear at the start of the command line
223 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
224 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
225 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
226 cerr << argv[0] << " error, cannot set environment variable." << endl;
227 exit( EXIT_FAILURE );
228 } // if
229 sargs += 1;
230 nargs += 1;
231 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
232 args[nargs] = argv[i]; // pass the argument along
233 nargs += 1;
234 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
235 cpp_flag = true; // cpp only
236 } // if
237 link = false; // no linkage required
238 } else if ( arg[1] == 'l' ) {
239 // if the user specifies a library, load it after user code
240 libs[nlibs] = argv[i];
241 nlibs += 1;
242 } else {
243 // concatenate any other arguments
244 args[nargs] = argv[i];
245 nargs += 1;
246 } // if
247 } else {
248 // concatenate other arguments
249 args[nargs] = argv[i];
250 nargs += 1;
251 nonoptarg = true;
252 } // if
253 } // for
[51b73452]254
[a83d08b]255#ifdef __x86_64__
256 args[nargs] = "-mcx16"; // allow double-wide CAA
257 nargs += 1;
258#endif // __x86_64__
[e3215c5]259
[b87a5ed]260#ifdef __DEBUG_H__
261 cerr << "args:";
262 for ( int i = 1; i < nargs; i += 1 ) {
263 cerr << " " << args[i];
264 } // for
265 cerr << endl;
266#endif // __DEBUG_H__
[51b73452]267
[b87a5ed]268 if ( cpp_flag && CFA_flag ) {
269 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
270 exit( EXIT_FAILURE );
271 } // if
[51b73452]272
[6e4b913]273 // add the CFA include-library paths, which allow direct access to header files without directory qualification
[b87a5ed]274 args[nargs] = "-I" CFA_INCDIR;
275 nargs += 1;
[d746bc8]276 if ( ! noincstd_flag ) { // do not use during build
277 args[nargs] = "-I" CFA_INCDIR "/stdhdr";
278 nargs += 1;
279 } // if
[0e76cf4f]280 args[nargs] = "-I" CFA_INCDIR "/concurrency";
281 nargs += 1;
[76c7f65e]282 args[nargs] = "-I" CFA_INCDIR "/containers";
283 nargs += 1;
284
[0edebf8]285#ifdef HAVE_LIBCFA
[b87a5ed]286 if ( link ) {
[0edebf8]287 #if ! defined(HAVE_LIBCFA_RELEASE)
[e3215c5]288 if ( ! debug ) {
[44bca7f]289 cerr << "error: Option -nodebug is unavailable, libcfa was not installed." << endl;
[0edebf8]290 exit( EXIT_FAILURE );
[e3215c5]291 } // if
[0edebf8]292 #endif
293 #if ! defined(HAVE_LIBCFA_DEBUG)
[e3215c5]294 if ( debug ) {
[44bca7f]295 cerr << "error: Option -debug is unavailable, libcfa-d was not installed." << endl;
[0edebf8]296 exit( EXIT_FAILURE );
[e3215c5]297 } // if
[0edebf8]298 #endif
299
[6bfe5cc]300 args[nargs] = "-Xlinker";
301 nargs += 1;
302 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
303 nargs += 1;
304 args[nargs] = "-Xlinker";
305 nargs += 1;
306 args[nargs] = "--undefined=__cfaabi_interpose_startup";
307 nargs += 1;
308
[b87a5ed]309 // include the cfa library in case it's needed
310 args[nargs] = "-L" CFA_LIBDIR;
[51b73452]311 nargs += 1;
[e3215c5]312 if ( debug ) {
[0edebf8]313 args[nargs] = "-lcfa-d";
314 } else {
315 args[nargs] = "-lcfa";
[e3215c5]316 } // if
[51b73452]317 nargs += 1;
[63f78f0]318 args[nargs] = "-lpthread";
319 nargs += 1;
[9d944b2]320 args[nargs] = "-ldl";
321 nargs += 1;
[c5ac6d5]322 args[nargs] = "-lrt";
323 nargs += 1;
[51b73452]324 } // if
[6bfe5cc]325#endif // HAVE_LIBCFA
[51b73452]326
[e9145a3]327 // Add exception flags (unconditionally)
328 args[nargs] = "-fexceptions";
329 nargs += 1;
330
[b87a5ed]331 // add the correct set of flags based on the type of compile this is
[8c17ab0]332
[ec129c4]333 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
[51b73452]334 nargs += 1;
[b87a5ed]335 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
[51b73452]336 nargs += 1;
[ec129c4]337 args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
[1db21619]338 nargs += 1;
[4c82a3c]339 args[nargs] = "-D__CFA__";
340 nargs += 1;
341 args[nargs] = "-D__CFORALL__";
[02e5ab6]342 nargs += 1;
[6acb935]343 args[nargs] = "-D__cforall";
344 nargs += 1;
[51b73452]345
[b87a5ed]346 if ( cpp_flag ) {
347 args[nargs] = "-D__CPP__";
348 nargs += 1;
349 } // if
[51b73452]350
[fa477f7]351 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
352 nargs += 1;
[b87a5ed]353 if ( CFA_flag ) {
[fa477f7]354 args[sargs] = "-D__CFA_FLAG__=-N";
[4c82a3c]355 args[nargs] = "-D__CFA_PREPROCESS_";
[b87a5ed]356 nargs += 1;
[fa477f7]357 } else {
358 args[sargs] = "-D__CFA_FLAG__=-L";
[b87a5ed]359 } // if
[fa477f7]360 sargs += 1;
[51b73452]361
[b87a5ed]362 if ( debug ) {
363 heading += " (debug)";
364 args[nargs] = "-D__CFA_DEBUG__";
365 nargs += 1;
366 } else {
367 heading += " (no debug)";
368 } // if
[51b73452]369
[b87a5ed]370 if ( Bprefix.length() == 0 ) {
371 Bprefix = installlibdir;
372 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
373 nargs += 1;
374 } // if
[51b73452]375
[6bfe5cc]376 args[nargs] = "-Xlinker"; // used by backtrace
377 nargs += 1;
378 args[nargs] = "-export-dynamic";
379 nargs += 1;
380
[b87a5ed]381 // execute the compilation command
[51b73452]382
[b87a5ed]383 args[0] = compiler_path.c_str(); // set compiler command for exec
384 // find actual name of the compiler independent of the path to it
385 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
386 if ( p == -1 ) {
387 compiler_name = compiler_path;
388 } else {
389 compiler_name = *new string( compiler_path.substr( p + 1 ) );
390 } // if
[51b73452]391
[b87a5ed]392 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
393 args[nargs] = "-no-integrated-cpp";
394 nargs += 1;
[76c7f65e]395 args[nargs] = "-Wno-deprecated";
[b87a5ed]396 nargs += 1;
[157d094]397 if ( ! std_flag ) { // default c11, if none specified
398 args[nargs] = "-std=gnu11";
[de62360d]399 nargs += 1;
400 } // if
[76c7f65e]401 args[nargs] = "-fgnu89-inline";
[6e991d6]402 nargs += 1;
[201aeb9]403 args[nargs] = "-D__int8_t_defined"; // prevent gcc type-size attributes
404 nargs += 1;
[b87a5ed]405 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
406 nargs += 1;
[d3b7937]407 args[nargs] = "-lm";
408 nargs += 1;
[b87a5ed]409 } else {
[e24f13a]410 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
[b87a5ed]411 exit( EXIT_FAILURE );
412 } // if
[51b73452]413
[b87a5ed]414 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
415 args[nargs] = libs[i];
416 nargs += 1;
417 } // for
[51b73452]418
[b87a5ed]419 args[nargs] = NULL; // terminate with NULL
[51b73452]420
421#ifdef __DEBUG_H__
[b87a5ed]422 cerr << "nargs: " << nargs << endl;
423 cerr << "args:" << endl;
424 for ( int i = 0; args[i] != NULL; i += 1 ) {
425 cerr << " \"" << args[i] << "\"" << endl;
426 } // for
[51b73452]427#endif // __DEBUG_H__
428
[b87a5ed]429 if ( ! quiet ) {
430 cerr << "CFA " << "Version " << Version << heading << endl;
431
432 if ( help ) {
433 cerr <<
434 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
435 "-help\t\t\t: print this help message" << endl <<
436 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
437 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
438 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
439 "...\t\t\t: any other " << compiler_name << " flags" << endl;
440 } // if
[51b73452]441 } // if
442
[b87a5ed]443 if ( verbose ) {
444 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
[51b73452]445
[b87a5ed]446 for ( int i = 0; args[i] != NULL; i += 1 ) {
447 cerr << args[i] << " ";
448 } // for
449 cerr << endl;
450 } // if
[51b73452]451
[b87a5ed]452 if ( ! nonoptarg ) {
453 cerr << argv[0] << " error, no input files" << endl;
454 exit( EXIT_FAILURE );
455 } // if
[51b73452]456
[b87a5ed]457 // execute the command and return the result
[51b73452]458
[b87a5ed]459 execvp( args[0], (char *const *)args ); // should not return
460 perror( "CFA Translator error: cfa level, execvp" );
461 exit( EXIT_FAILURE );
[51b73452]462} // main
463
464// Local Variables: //
[b87a5ed]465// tab-width: 4 //
466// mode: c++ //
[51b73452]467// compile-command: "make install" //
468// End: //
Note: See TracBrowser for help on using the repository browser.