source: src/driver/cfa.cc@ 9d32bc8

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 9d32bc8 was 44bca7f, checked in by Peter A. Buhr <pabuhr@…>, 7 years ago

first attempt at warning control

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