source: driver/cfa.cc@ 37fe352

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 no_list persistent-indexer pthread-emulation qualifiedEnum
Last change on this file since 37fe352 was 37fe352, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Added proper multi-lib handling, tests still do not work and arm support is broken

  • Property mode set to 100644
File size: 17.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 : Fri Jul 13 17:40:12 2018
13// Update Count : 258
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 <sys/types.h>
24#include <sys/stat.h>
25
26#include "Common/SemanticError.h"
27#include "config.h" // configure info
28
29using std::cerr;
30using std::endl;
31using std::string;
32using std::to_string;
33
34
35//#define __DEBUG_H__
36
37
38bool prefix( string arg, string pre ) {
39 return arg.substr( 0, pre.size() ) == pre;
40} // prefix
41
42enum { NumSuffixes = 2 };
43const string suffixes[NumSuffixes] = { "cfa", "hfa", };
44
45bool suffix( string arg ) {
46 //std::cerr << arg << std::endl;
47 size_t dot = arg.find_last_of( "." );
48 //std::cerr << dot << " " << (dot != string::npos ? arg.substr( dot + 1 ) : "fred" ) << std::endl;
49 if ( dot == string::npos ) return false;
50 string sx = arg.substr( dot + 1 );
51 for ( int i = 0; i < NumSuffixes; i += 1 ) {
52 if ( sx == suffixes[i] ) return true;
53 } // for
54 return false;
55} // suffix
56
57
58void shuffle( const char *args[], int S, int E, int N ) {
59 // S & E index 1 passed the end so adjust with -1
60 #ifdef __DEBUG_H__
61 cerr << "shuffle:" << S << " " << E << " " << N << endl;
62 #endif // __DEBUG_H__
63 for ( int j = E-1 + N; j > S-1 + N; j -=1 ) {
64 #ifdef __DEBUG_H__
65 cerr << "\t" << j << " " << j-N << endl;
66 #endif // __DEBUG_H__
67 args[j] = args[j-N];
68 } // for
69} // shuffle
70
71static inline bool dirExists(const string & path) {
72 struct stat info;
73 if(stat( path.c_str(), &info ) != 0)
74 return false;
75 else if(info.st_mode & S_IFDIR)
76 return true;
77 else
78 return false;
79} //dirExists
80
81
82#define str(s) #s
83
84int main( int argc, char *argv[] ) {
85 string Version( CFA_VERSION_LONG ); // current version number from CONFIG
86 string Major( str( CFA_VERSION_MAJOR ) ), Minor( str( CFA_VERSION_MINOR ) ), Patch( str( CFA_VERSION_PATCH ) );
87
88 string installincdir( CFA_INCDIR ); // fixed location of include files
89 string installlibdir( CFA_LIBDIR ); // fixed location of cc1 and cfa-cpp commands
90
91 string heading; // banner printed at start of cfa compilation
92 string arg; // current command-line argument during command-line parsing
93 string Bprefix; // path where gcc looks for compiler command steps
94 string langstd; // language standard
95
96 string compiler_path( CFA_BACKEND_CC ); // path/name of C compiler
97 string compiler_name; // name of C compiler
98
99 bool nonoptarg = false; // indicates non-option argument specified
100 bool link = true; // linking as well as compiling
101 bool verbose = false; // -v flag
102 bool quiet = false; // -quiet flag
103 bool debug = true; // -debug flag
104 bool help = false; // -help flag
105 bool CFA_flag = false; // -CFA flag
106 bool cpp_flag = false; // -E or -M flag, preprocessor only
107 bool std_flag = false; // -std= flag
108 bool noincstd_flag = false; // -no-include-stdhdr= flag
109 bool xflag = false; // user supplied -x flag
110 bool debugging __attribute(( unused )) = false; // -g flag
111 bool m32 = false; // -m32 flag
112 bool m64 = false; // -m64 flag
113 bool intree = false;
114
115 const char *args[argc + 100]; // cfa command line values, plus some space for additional flags
116 int sargs = 1; // starting location for arguments in args list
117 int nargs = sargs; // number of arguments in args list; 0 => command name
118
119 const char *libs[argc + 20]; // non-user libraries must come separately, plus some added libraries and flags
120 int nlibs = 0;
121
122 #ifdef __DEBUG_H__
123 cerr << "CFA:" << endl;
124 #endif // __DEBUG_H__
125
126 // process command-line arguments
127
128 for ( int i = 1; i < argc; i += 1 ) {
129 #ifdef __DEBUG_H__
130 cerr << "argv[" << i << "]:\"" << argv[i] << "\"" << endl;
131 #endif // __DEBUG_H__
132 arg = argv[i]; // convert to string value
133 #ifdef __DEBUG_H__
134 cerr << "arg:\"" << arg << "\"" << endl;
135 #endif // __DEBUG_H__
136 if ( prefix( arg, "-" ) ) {
137 // pass through arguments
138
139 if ( arg == "-Xlinker" || arg == "-o" ) {
140 args[nargs] = argv[i]; // pass the argument along
141 nargs += 1;
142 i += 1;
143 if ( i == argc ) continue; // next argument available ?
144 args[nargs] = argv[i]; // pass the argument along
145 nargs += 1;
146 } else if ( arg == "-XCFA" ) { // CFA pass through
147 i += 1;
148 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + argv[i] ) ).c_str();
149 nargs += 1;
150
151 // CFA specific arguments
152
153 } else if ( arg == "-CFA" ) {
154 CFA_flag = true; // strip the -CFA flag
155 link = false;
156 args[nargs] = "-E"; // replace the argument with -E
157 nargs += 1;
158 } else if ( arg == "-debug" ) {
159 debug = true; // strip the debug flag
160 } else if ( arg == "-nodebug" ) {
161 debug = false; // strip the nodebug flag
162 } else if ( arg == "-quiet" ) {
163 quiet = true; // strip the quiet flag
164 } else if ( arg == "-noquiet" ) {
165 quiet = false; // strip the noquiet flag
166 } else if ( arg == "-help" ) {
167 help = true; // strip the help flag
168 } else if ( arg == "-nohelp" ) {
169 help = false; // strip the nohelp flag
170 } else if ( arg == "-no-include-stdhdr" ) {
171 noincstd_flag = true; // strip the no-include-stdhdr flag
172 } else if ( arg == "-in-tree" ) {
173 noincstd_flag = true;
174 intree = true;
175 } else if ( arg == "-compiler" ) {
176 // use the user specified compiler
177 i += 1;
178 if ( i == argc ) continue; // next argument available ?
179 compiler_path = argv[i];
180 if ( putenv( (char *)( *new string( string( "__U_COMPILER__=" ) + argv[i]) ).c_str() ) != 0 ) {
181 cerr << argv[0] << " error, cannot set environment variable." << endl;
182 exit( EXIT_FAILURE );
183 } // if
184
185 // C specific arguments
186
187 } else if ( arg == "-v" ) {
188 verbose = true; // verbosity required
189 args[nargs] = argv[i]; // pass the argument along
190 nargs += 1;
191 } else if ( arg == "-g" ) {
192 debugging = true; // symbolic debugging required
193 args[nargs] = argv[i]; // pass the argument along
194 nargs += 1;
195 } else if ( prefix( arg, "-std=" ) || prefix( arg, "--std=" ) ) {
196 std_flag = true; // -std=XX provided
197 args[nargs] = argv[i]; // pass the argument along
198 nargs += 1;
199 } else if ( arg == "-x" ) {
200 xflag = true;
201 args[nargs] = argv[i]; // pass the argument along
202 nargs += 1;
203 i += 1; // advance to argument
204 args[nargs] = argv[i]; // pass the argument along
205 nargs += 1;
206 // args[nargs] = ( *new string( string("-D__GCC_X__=") + argv[i] ) ).c_str(); // add the argument for -x
207 // nargs += 1;
208 } else if ( prefix( arg, "-x" ) ) {
209 xflag = true;
210 args[nargs] = argv[i]; // pass the argument along
211 nargs += 1;
212 // args[nargs] = ( *new string( string("-D__GCC_X__=") + arg.substr(2) ) ).c_str(); // add the argument for -x
213 // nargs += 1;
214 } else if ( arg == "-w" ) {
215 args[nargs] = argv[i]; // pass the argument along
216 nargs += 1;
217 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
218 nargs += 1;
219 } else if ( prefix( arg, "-W" ) ) { // check before next tests
220 if ( arg == "-Werror" || arg == "-Wall" ) {
221 args[nargs] = argv[i]; // pass the argument along
222 nargs += 1;
223 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str(); // add the argument for cfa-cpp
224 nargs += 1;
225 } else {
226 unsigned int adv = prefix( arg, "-Wno-" ) ? 5 : 2;
227 args[nargs] = argv[i]; // conditionally pass the argument along
228 const char * warning = argv[i] + adv; // extract warning
229 if ( SemanticWarning_Exist( warning ) ) { // replace the argument for cfa-cpp
230 args[nargs] = ( *new string( string("-D__CFA_FLAG__=") + arg ) ).c_str();
231 } // if
232 nargs += 1;
233 } // if
234 } else if ( prefix( arg, "-B" ) ) {
235 Bprefix = arg.substr(2); // strip the -B flag
236 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
237 nargs += 1;
238 } else if ( prefix( arg, "-b" ) ) {
239 if ( arg.length() == 2 ) { // separate argument ?
240 i += 1;
241 if ( i == argc ) continue; // next argument available ?
242 arg += argv[i]; // concatenate argument
243 } // if
244 // later versions of gcc require the -b option to appear at the start of the command line
245 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
246 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
247 if ( putenv( (char *)( *new string( string( "__GCC_MACHINE__=" ) + arg ) ).c_str() ) != 0 ) {
248 cerr << argv[0] << " error, cannot set environment variable." << endl;
249 exit( EXIT_FAILURE );
250 } // if
251 sargs += 1;
252 nargs += 1;
253 } else if ( prefix( arg, "-V" ) ) {
254 if ( arg.length() == 2 ) { // separate argument ?
255 i += 1;
256 if ( i == argc ) continue; // next argument available ?
257 arg += argv[i]; // concatenate argument
258 } // if
259 // later versions of gcc require the -V option to appear at the start of the command line
260 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
261 args[sargs] = ( *new string( arg ) ).c_str(); // pass the argument along
262 if ( putenv( (char *)( *new string( string( "__GCC_VERSION__=" ) + arg ) ).c_str() ) != 0 ) {
263 cerr << argv[0] << " error, cannot set environment variable." << endl;
264 exit( EXIT_FAILURE );
265 } // if
266 sargs += 1;
267 nargs += 1;
268 } else if ( arg == "-c" || arg == "-S" || arg == "-E" || arg == "-M" || arg == "-MM" ) {
269 args[nargs] = argv[i]; // pass the argument along
270 nargs += 1;
271 if ( arg == "-E" || arg == "-M" || arg == "-MM" ) {
272 cpp_flag = true; // cpp only
273 } // if
274 link = false; // no linkage required
275 } else if ( arg[1] == 'l' ) {
276 // if the user specifies a library, load it after user code
277 libs[nlibs] = argv[i];
278 nlibs += 1;
279 } else if ( arg == "-m32" ) {
280 m32 = true;
281 m64 = false;
282 args[nargs] = argv[i];
283 nargs += 1;
284 } else if ( arg == "-m64" ) {
285 m64 = true;
286 m32 = false;
287 args[nargs] = argv[i];
288 nargs += 1;
289 } else {
290 // concatenate any other arguments
291 args[nargs] = argv[i];
292 nargs += 1;
293 } // if
294 } else {
295 bool opt = false;
296 if ( ! xflag && suffix( arg ) ) {
297 args[nargs] = "-x";
298 nargs += 1;
299 args[nargs] = "c";
300 nargs += 1;
301 // args[nargs] = ( *new string( string("-D__GCC_X__=c") ) ).c_str(); // add the argument for -x
302 // nargs += 1;
303 opt = true;
304 } // if
305 // concatenate other arguments
306 args[nargs] = argv[i];
307 nargs += 1;
308 if ( opt ) {
309 args[nargs] = "-x";
310 nargs += 1;
311 args[nargs] = "none";
312 nargs += 1;
313 // args[nargs] = ( *new string( string("-D__GCC_X__=none") ) ).c_str(); // add the argument for -x
314 // nargs += 1;
315 } // if
316 nonoptarg = true;
317 xflag = false;
318 } // if
319 } // for
320
321 #ifdef __x86_64__
322 args[nargs] = "-mcx16"; // allow double-wide CAA
323 nargs += 1;
324 #endif // __x86_64__
325
326 #ifdef __DEBUG_H__
327 cerr << "args:";
328 for ( int i = 1; i < nargs; i += 1 ) {
329 cerr << " " << args[i];
330 } // for
331 cerr << endl;
332 #endif // __DEBUG_H__
333
334 if ( cpp_flag && CFA_flag ) {
335 cerr << argv[0] << " error, cannot use -E and -CFA flags together." << endl;
336 exit( EXIT_FAILURE );
337 } // if
338
339 // add the CFA include-library paths, which allow direct access to header files without directory qualification
340 args[nargs] = "-I" CFA_INCDIR;
341 nargs += 1;
342 if ( ! noincstd_flag ) { // do not use during build
343 args[nargs] = "-I" CFA_INCDIR "/stdhdr";
344 nargs += 1;
345 } // if
346 args[nargs] = "-I" CFA_INCDIR "/concurrency";
347 nargs += 1;
348 args[nargs] = "-I" CFA_INCDIR "/containers";
349 nargs += 1;
350
351 string libdir;
352 if( !intree ) {
353 const char * const arch = m32 ? CFA_32_CPU : (m64 ? CFA_64_CPU : CFA_DEFAULT_CPU);
354 const char * config = debug ? "debug": "nodebug";
355 libdir = string(CFA_LIBDIR) + arch + config;
356 if( !dirExists(libdir) ) {
357 cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
358 cerr << "Was looking for " << libdir << endl;
359 libdir = string(CFA_LIBDIR) + arch + "nolib";
360 }
361
362 if( !dirExists(libdir) ) {
363 cerr << argv[0] << " internal error, cannot find prelude directory." << endl;
364 cerr << "Was looking for " << libdir << endl;
365 exit( EXIT_FAILURE );
366 }
367
368 args[nargs] = ( *new string( string("-D__CFA_FLAG__=--prelude-dir=" ) + libdir) ).c_str();
369 nargs += 1;
370 } else {
371 args[nargs] = "-D__CFA_FLAG__=-t";
372 nargs += 1;
373 }
374
375 if ( link ) {
376 args[nargs] = "-Xlinker";
377 nargs += 1;
378 args[nargs] = "--undefined=__cfaabi_dbg_bits_write";
379 nargs += 1;
380 args[nargs] = "-Xlinker";
381 nargs += 1;
382 args[nargs] = "--undefined=__cfaabi_interpose_startup";
383 nargs += 1;
384 args[nargs] = "-Xlinker";
385 nargs += 1;
386 args[nargs] = "--undefined=__cfaabi_appready_startup";
387 nargs += 1;
388
389 // include the cfa library in case it's needed
390 if( !intree ) {
391 args[nargs] = ( *new string( string("-L" ) + libdir) ).c_str();
392 nargs += 1;
393 }
394 args[nargs] = "-lcfa";
395 nargs += 1;
396 args[nargs] = "-lpthread";
397 nargs += 1;
398 args[nargs] = "-ldl";
399 nargs += 1;
400 args[nargs] = "-lrt";
401 nargs += 1;
402 } // if
403
404 // Add exception flags (unconditionally)
405 args[nargs] = "-fexceptions";
406 nargs += 1;
407
408 // add the correct set of flags based on the type of compile this is
409
410 args[nargs] = ( *new string( string("-D__CFA_MAJOR__=") + Major ) ).c_str();
411 nargs += 1;
412 args[nargs] = ( *new string( string("-D__CFA_MINOR__=") + Minor ) ).c_str();
413 nargs += 1;
414 args[nargs] = ( *new string( string("-D__CFA_PATCH__=") + Patch ) ).c_str();
415 nargs += 1;
416 args[nargs] = "-D__CFA__";
417 nargs += 1;
418 args[nargs] = "-D__CFORALL__";
419 nargs += 1;
420 args[nargs] = "-D__cforall";
421 nargs += 1;
422
423 if ( cpp_flag ) {
424 args[nargs] = "-D__CPP__";
425 nargs += 1;
426 } // if
427
428 shuffle( args, sargs, nargs, 1 ); // make room at front of argument list
429 nargs += 1;
430 if ( CFA_flag ) {
431 args[sargs] = "-D__CFA_FLAG__=-N";
432 args[nargs] = "-D__CFA_PREPROCESS_";
433 nargs += 1;
434 } else {
435 args[sargs] = "-D__CFA_FLAG__=-L";
436 } // if
437 sargs += 1;
438
439 if ( debug ) {
440 heading += " (debug)";
441 args[nargs] = "-D__CFA_DEBUG__";
442 nargs += 1;
443 } else {
444 heading += " (no debug)";
445 } // if
446
447 if ( Bprefix.length() == 0 ) {
448 Bprefix = installlibdir;
449 args[nargs] = ( *new string( string("-D__GCC_BPREFIX__=") + Bprefix ) ).c_str();
450 nargs += 1;
451 } // if
452
453 args[nargs] = "-Xlinker"; // used by backtrace
454 nargs += 1;
455 args[nargs] = "-export-dynamic";
456 nargs += 1;
457
458 // execute the compilation command
459
460 args[0] = compiler_path.c_str(); // set compiler command for exec
461 // find actual name of the compiler independent of the path to it
462 int p = compiler_path.find_last_of( '/' ); // scan r -> l for first '/'
463 if ( p == -1 ) {
464 compiler_name = compiler_path;
465 } else {
466 compiler_name = *new string( compiler_path.substr( p + 1 ) );
467 } // if
468
469 if ( prefix( compiler_name, "gcc" ) ) { // allow suffix on gcc name
470 args[nargs] = "-no-integrated-cpp";
471 nargs += 1;
472 args[nargs] = "-Wno-deprecated";
473 nargs += 1;
474 if ( ! std_flag ) { // default c11, if none specified
475 args[nargs] = "-std=gnu11";
476 nargs += 1;
477 } // if
478 args[nargs] = "-fgnu89-inline";
479 nargs += 1;
480 args[nargs] = "-D__int8_t_defined"; // prevent gcc type-size attributes
481 nargs += 1;
482 args[nargs] = ( *new string( string("-B") + Bprefix + "/" ) ).c_str();
483 nargs += 1;
484 args[nargs] = "-lm";
485 nargs += 1;
486 } else {
487 cerr << argv[0] << " error, compiler \"" << compiler_name << "\" unsupported." << endl;
488 exit( EXIT_FAILURE );
489 } // if
490
491 for ( int i = 0; i < nlibs; i += 1 ) { // copy non-user libraries after all user libraries
492 args[nargs] = libs[i];
493 nargs += 1;
494 } // for
495
496 args[nargs] = NULL; // terminate with NULL
497
498 #ifdef __DEBUG_H__
499 cerr << "nargs: " << nargs << endl;
500 cerr << "args:" << endl;
501 for ( int i = 0; args[i] != NULL; i += 1 ) {
502 cerr << " \"" << args[i] << "\"" << endl;
503 } // for
504 #endif // __DEBUG_H__
505
506 if ( ! quiet ) {
507 cerr << "CFA " << "Version " << Version << heading << endl;
508
509 if ( help ) {
510 cerr <<
511 "-debug\t\t\t: use cfa runtime with debug checking" << endl <<
512 "-help\t\t\t: print this help message" << endl <<
513 "-quiet\t\t\t: print no messages from the cfa command" << endl <<
514 "-CFA\t\t\t: run the cpp preprocessor and the cfa-cpp translator" << endl <<
515 "-XCFA -cfa-cpp-flag\t: pass next flag as-is to the cfa-cpp translator" << endl <<
516 "...\t\t\t: any other " << compiler_name << " flags" << endl;
517 } // if
518 } // if
519
520 if ( verbose ) {
521 if ( argc == 2 ) exit( EXIT_SUCCESS ); // if only the -v flag is specified, do not invoke gcc
522
523 for ( int i = 0; args[i] != NULL; i += 1 ) {
524 cerr << args[i] << " ";
525 } // for
526 cerr << endl;
527 } // if
528
529 if ( ! nonoptarg ) {
530 cerr << argv[0] << " error, no input files" << endl;
531 exit( EXIT_FAILURE );
532 } // if
533
534 // execute the command and return the result
535
536 execvp( args[0], (char *const *)args ); // should not return
537 perror( "CFA Translator error: cfa level, execvp" );
538 exit( EXIT_FAILURE );
539} // main
540
541// Local Variables: //
542// tab-width: 4 //
543// mode: c++ //
544// compile-command: "make install" //
545// End: //
Note: See TracBrowser for help on using the repository browser.